用链表的方式编程实现:在键盘上一次输入5个整数,然后将它们反序输出
本帖最后由 VaeSummerTime 于 2021-12-16 20:04 编辑用链表的方式实现:在键盘上一次输入5个整数,然后将它们反序输出
效果如下:
--------------------------------------------------------------------------------------------------
请输入5个整数:11 22 33 44 55
反向输出:55 44 33 22 11
刚开始接触链表,有些不熟悉,可以看下源码吗{:10_324:}
大佬们别复制粘贴网上的代码可以吗,运行一堆报错的{:10_245:} 本帖最后由 jhq999 于 2021-12-16 18:56 编辑
typedef struct LinkList
{
int data;
LinkList *next;
}LLST,*pLLST;
int main()
{
pLLST head=NULL,tmp=NULL;
int count=0,i=0;
scanf("%d",&count);
for(i=0;i<count;i++)
{
tmp=head;
head=new LLST;
head->next=tmp;
scanf("%d",&head->data);
}
while(head)
{
printf("%d ",head->data);
tmp=head;
head=head->next;
delete tmp;
}
return 0;
} #include <iostream>
using namespace std;
struct Node
{
int val;
Node* next;
Node(int val = 0, Node* next = nullptr): val(val), next(next) {};
};
int main(void) {
Node* first = new Node();
cout << "enter five num: ";
for (int i = 0; i < 5; i++) {
int a; cin >> a;
Node* second = new Node(a);
Node* t;
t = first;
first = second;
first->next = t;
}
while (first->next) {
cout << first->val << ' ';
first = first->next;
}
} main.c
#include "list.h"
#include <stdio.h>
void output(const list_t *list, size_t index) {
if(index >= list_size(list)) return;
output(list, index + 1);
int temp; list_get(list, index, &temp, sizeof(temp));
printf("%d ", temp);
}
int main(void) {
list_t *list = list_init();
for(size_t i = 0; i < 5; ++i) {
int temp; scanf("%d", &temp);
list_append(list, &temp, sizeof(temp));
}
output(list, 0); puts("");
list_deinit(list);
return 0;
}
list.h
#ifndef _LIST_H_
#define _LIST_H_
#include <stddef.h>
#include <stdbool.h>
struct list_node_tag {
void *data; size_t size;
struct list_node_tag *next;
};
typedef struct {
struct list_node_tag *head;
size_t size;
} list_t;
list_t *list_init(void);
void list_deinit(list_t *list);
bool list_clean(list_t *list);
bool list_insert(list_t *list, size_t index, const void *data, size_t size);
bool list_delete(list_t *list, size_t index);
bool list_get(const list_t *list, size_t index, void *data, size_t size);
bool list_set(list_t *list, size_t index, const void *data, size_t size);
bool list_append(list_t *list, const void *data, size_t size);
size_t list_size(const list_t *list);
bool list_empty(const list_t *list);
#endif
list.c
#include "list.h"
#include <stdlib.h>
#include <memory.h>
list_t *list_init(void) {
list_t *list = malloc(sizeof(*list));
if(!list) return NULL;
list->head = NULL;
list->size = 0;
return list;
}
void list_deinit(list_t *list) {
if(!list) return;
list_clean(list);
free(list);
}
bool list_clean(list_t *list) {
if(!list) return false;
while(!list_empty(list)) list_delete(list, 0);
return true;
}
bool list_insert(list_t *list, size_t index, const void *data, size_t size) {
if(!list) return false;
if(list_size(list) < index) return false;
if(!data) return false;
struct list_node_tag **current = &list->head;
while(index--) current = &(*current)->next;
struct list_node_tag *node = malloc(sizeof(*node));
if(!node) return false;
node->data = malloc(size);
if(!node->data) {free(node); return false;}
memcpy(node->data, data, size);
node->size = size;
node->next = *current;
*current = node;
++list->size;
return true;
}
bool list_delete(list_t *list, size_t index) {
if(!list) return false;
if(list_size(list) <= index) return false;
struct list_node_tag **current = &list->head;
while(index--) current = &(*current)->next;
struct list_node_tag *temp = *current;
*current = temp->next;
free(temp->data); free(temp);
--list->size;
return true;
}
bool list_get(const list_t *list, size_t index, void *data, size_t size) {
if(!list) return false;
if(list_size(list) <= index) return false;
if(!data) return false;
struct list_node_tag *const *current = &list->head;
while(index--) current = &(*current)->next;
struct list_node_tag *temp = *current;
if(temp->size > size) return false;
memcpy(data, temp->data, temp->size);
return true;
}
bool list_set(list_t *list, size_t index, const void *data, size_t size) {
bool res = list_delete(list, index);
return res ? list_insert(list, index, data, size) : res;
}
bool list_append(list_t *list, const void *data, size_t size) {
if(!list) return false;
return list_insert(list, list_size(list), data, size);
}
size_t list_size(const list_t *list) {
if(!list) return 0;
return list->size;
}
bool list_empty(const list_t *list) {
if(!list) return true;
return list_size(list) == 0;
}
$ gcc-debug -o main main.c list.c
$ ./main
11 22 33 44 55
55 44 33 22 11
$ main.c
#include "stack.h"
#include <stdio.h>
int main(void) {
stack_t *stack = stack_init();
for(size_t i = 0; i < 5; ++i) {
int temp; scanf("%d", &temp);
stack_push(stack, &temp, sizeof(temp));
}
while(!stack_empty(stack)) {
int temp; stack_top(stack, &temp, sizeof(temp));
stack_pop(stack);
printf("%d ", temp);
}
puts("");
stack_deinit(stack);
return 0;
}
stack.h
#ifndef _STACK_H_
#define _STACK_H_
#include <stddef.h>
#include <stdbool.h>
struct stack_node_tag {
void *data;
size_t size;
struct stack_node_tag *next;
};
typedef struct {
struct stack_node_tag *head;
size_t size;
} stack_t;
stack_t *stack_init(void);
void stack_deinit(stack_t *stack);
bool stack_clean(stack_t *stack);
bool stack_push(stack_t *stack, const void *data, size_t size);
bool stack_pop(stack_t *stack);
bool stack_top(const stack_t *stack, void *data, size_t size);
size_t stack_size(const stack_t *stack);
bool stack_empty(const stack_t *stack);
#endif
stack.c
#include "stack.h"
#include <stdlib.h>
#include <memory.h>
stack_t *stack_init(void) {
stack_t *stack = malloc(sizeof(*stack));
if(!stack) return NULL;
stack->head = NULL;
stack->size = 0;
return stack;
}
void stack_deinit(stack_t *stack) {
if(!stack) return;
stack_clean(stack);
free(stack);
}
bool stack_clean(stack_t *stack) {
if(!stack) return false;
while(!stack_empty(stack)) stack_pop(stack);
return true;
}
bool stack_push(stack_t *stack, const void *data, size_t size) {
if(!stack) return false;
if(!data) return false;
struct stack_node_tag *node = malloc(sizeof(*node));
if(!node) return false;
node->data = malloc(size);
if(!node->data) {free(node); return false;}
memcpy(node->data, data, size);
node->size = size;
node->next = stack->head;
stack->head = node;
++stack->size;
return true;
}
bool stack_pop(stack_t *stack) {
if(!stack) return false;
if(stack_empty(stack)) return false;
struct stack_node_tag *temp = stack->head;
stack->head = temp->next;
free(temp->data); free(temp);
--stack->size;
return true;
}
bool stack_top(const stack_t *stack, void *data, size_t size) {
if(!stack) return false;
if(!data) return false;
if(stack_empty(stack)) return false;
struct stack_node_tag *temp = stack->head;
if(temp->size > size) return false;
memcpy(data, temp->data, temp->size);
return true;
}
size_t stack_size(const stack_t *stack) {
if(!stack) return 0;
return stack->size;
}
bool stack_empty(const stack_t *stack) {
if(!stack) return true;
return stack_size(stack) == 0;
}
$ gcc-debug -o main main.c stack.c
$ ./main
11 22 33 44 55
55 44 33 22 11
$ C 代码:#include <stdio.h>
#include <stdlib.h>
struct Node{
int data;
struct Node* next;
};
int main(){
int N;
struct Node *head = NULL, *tail = NULL;
scanf("%d", &N);
while(N--){
tail = head;
head = (struct Node*)malloc(sizeof(struct Node));
scanf("%d", &head->data);
head->next = tail;
}
for(; head; head = head->next) printf("%d ", head->data);
return 0;
}
页:
[1]