鱼C论坛

 找回密码
 立即注册
查看: 1156|回复: 5

[已解决]用链表的方式编程实现:在键盘上一次输入5个整数,然后将它们反序输出

[复制链接]
发表于 2021-12-16 18:36:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 VaeSummerTime 于 2021-12-16 20:04 编辑

用链表的方式实现:在键盘上一次输入5个整数,然后将它们反序输出
效果如下:
--------------------------------------------------------------------------------------------------
请输入5个整数:11 22 33 44 55
反向输出:55 44 33 22 11

刚开始接触链表,有些不熟悉,可以看下源码吗
大佬们别复制粘贴网上的代码可以吗,运行一堆报错的
最佳答案
2021-12-17 10:45:18
C 代码:
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct Node{
  4.         int data;
  5.         struct Node* next;
  6. };

  7. int main(){
  8.         int N;
  9.         struct Node *head = NULL, *tail = NULL;
  10.         scanf("%d", &N);
  11.         while(N--){
  12.                 tail = head;
  13.                 head = (struct Node*)malloc(sizeof(struct Node));
  14.                 scanf("%d", &head->data);
  15.                 head->next = tail;
  16.         }
  17.        
  18.         for(; head; head = head->next) printf("%d ", head->data);
  19.         return 0;
  20. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-12-16 18:53:23 | 显示全部楼层
本帖最后由 jhq999 于 2021-12-16 18:56 编辑
  1. typedef struct LinkList
  2. {
  3.    int data;
  4.    LinkList *next;
  5. }LLST,*pLLST;
  6. int main()
  7. {
  8.     pLLST head=NULL,tmp=NULL;
  9.     int count=0,i=0;
  10.     scanf("%d",&count);
  11.     for(i=0;i<count;i++)
  12.     {
  13.         tmp=head;
  14.         head=new LLST;
  15.         head->next=tmp;
  16.         scanf("%d",&head->data);
  17.      }
  18.    
  19.     while(head)
  20.     {
  21.           printf("%d   ",head->data);
  22.           tmp=head;
  23.           head=head->next;
  24.           delete tmp;
  25.      }
  26.     return 0;
  27. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-12-16 19:04:00 | 显示全部楼层
  1. #include <iostream>

  2. using namespace std;

  3. struct Node
  4. {
  5.     int val;
  6.     Node* next;
  7.     Node(int val = 0, Node* next = nullptr): val(val), next(next) {};
  8. };

  9. int main(void) {
  10.     Node* first = new Node();
  11.     cout << "enter five num: ";
  12.     for (int i = 0; i < 5; i++) {
  13.         int a; cin >> a;
  14.         Node* second = new Node(a);
  15.         Node* t;
  16.         
  17.         t = first;
  18.         first = second;
  19.         first->next = t;
  20.     }

  21.     while (first->next) {
  22.         cout << first->val << ' ';
  23.         first = first->next;
  24.     }
  25. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-12-16 19:10:23 | 显示全部楼层
main.c
  1. #include "list.h"
  2. #include <stdio.h>

  3. void output(const list_t *list, size_t index) {
  4.     if(index >= list_size(list)) return;
  5.     output(list, index + 1);
  6.     int temp; list_get(list, index, &temp, sizeof(temp));
  7.     printf("%d ", temp);
  8. }

  9. int main(void) {
  10.     list_t *list = list_init();
  11.     for(size_t i = 0; i < 5; ++i) {
  12.         int temp; scanf("%d", &temp);
  13.         list_append(list, &temp, sizeof(temp));
  14.     }
  15.     output(list, 0); puts("");
  16.     list_deinit(list);
  17.     return 0;
  18. }
复制代码


list.h
  1. #ifndef _LIST_H_
  2. #define _LIST_H_

  3. #include <stddef.h>
  4. #include <stdbool.h>

  5. struct list_node_tag {
  6.     void *data; size_t size;
  7.     struct list_node_tag *next;
  8. };

  9. typedef struct {
  10.     struct list_node_tag *head;
  11.     size_t size;
  12. } list_t;

  13. list_t *list_init(void);
  14. void list_deinit(list_t *list);
  15. bool list_clean(list_t *list);
  16. bool list_insert(list_t *list, size_t index, const void *data, size_t size);
  17. bool list_delete(list_t *list, size_t index);
  18. bool list_get(const list_t *list, size_t index, void *data, size_t size);
  19. bool list_set(list_t *list, size_t index, const void *data, size_t size);
  20. bool list_append(list_t *list, const void *data, size_t size);
  21. size_t list_size(const list_t *list);
  22. bool list_empty(const list_t *list);

  23. #endif
复制代码


list.c
  1. #include "list.h"
  2. #include <stdlib.h>
  3. #include <memory.h>

  4. list_t *list_init(void) {
  5.     list_t *list = malloc(sizeof(*list));
  6.     if(!list) return NULL;
  7.     list->head = NULL;
  8.     list->size = 0;
  9.     return list;
  10. }

  11. void list_deinit(list_t *list) {
  12.     if(!list) return;
  13.     list_clean(list);
  14.     free(list);
  15. }

  16. bool list_clean(list_t *list) {
  17.     if(!list) return false;
  18.     while(!list_empty(list)) list_delete(list, 0);
  19.     return true;
  20. }

  21. bool list_insert(list_t *list, size_t index, const void *data, size_t size) {
  22.     if(!list) return false;
  23.     if(list_size(list) < index) return false;
  24.     if(!data) return false;
  25.     struct list_node_tag **current = &list->head;
  26.     while(index--) current = &(*current)->next;
  27.     struct list_node_tag *node = malloc(sizeof(*node));
  28.     if(!node) return false;
  29.     node->data = malloc(size);
  30.     if(!node->data) {free(node); return false;}
  31.     memcpy(node->data, data, size);
  32.     node->size = size;
  33.     node->next = *current;
  34.     *current = node;
  35.     ++list->size;
  36.     return true;
  37. }

  38. bool list_delete(list_t *list, size_t index) {
  39.     if(!list) return false;
  40.     if(list_size(list) <= index) return false;
  41.     struct list_node_tag **current = &list->head;
  42.     while(index--) current = &(*current)->next;
  43.     struct list_node_tag *temp = *current;
  44.     *current = temp->next;
  45.     free(temp->data); free(temp);
  46.     --list->size;
  47.     return true;
  48. }

  49. bool list_get(const list_t *list, size_t index, void *data, size_t size) {
  50.     if(!list) return false;
  51.     if(list_size(list) <= index) return false;
  52.     if(!data) return false;
  53.     struct list_node_tag *const *current = &list->head;
  54.     while(index--) current = &(*current)->next;
  55.     struct list_node_tag *temp = *current;
  56.     if(temp->size > size) return false;
  57.     memcpy(data, temp->data, temp->size);
  58.     return true;
  59. }

  60. bool list_set(list_t *list, size_t index, const void *data, size_t size) {
  61.     bool res = list_delete(list, index);
  62.     return res ? list_insert(list, index, data, size) : res;
  63. }

  64. bool list_append(list_t *list, const void *data, size_t size) {
  65.     if(!list) return false;
  66.     return list_insert(list, list_size(list), data, size);
  67. }

  68. size_t list_size(const list_t *list) {
  69.     if(!list) return 0;
  70.     return list->size;
  71. }

  72. bool list_empty(const list_t *list) {
  73.     if(!list) return true;
  74.     return list_size(list) == 0;
  75. }
复制代码

  1. $ gcc-debug -o main main.c list.c
  2. $ ./main
  3. 11 22 33 44 55
  4. 55 44 33 22 11
  5. $
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2021-12-16 19:21:39 | 显示全部楼层
main.c
  1. #include "stack.h"
  2. #include <stdio.h>

  3. int main(void) {
  4.     stack_t *stack = stack_init();
  5.     for(size_t i = 0; i < 5; ++i) {
  6.         int temp; scanf("%d", &temp);
  7.         stack_push(stack, &temp, sizeof(temp));
  8.     }
  9.     while(!stack_empty(stack)) {
  10.         int temp; stack_top(stack, &temp, sizeof(temp));
  11.         stack_pop(stack);
  12.         printf("%d ", temp);
  13.     }
  14.     puts("");
  15.     stack_deinit(stack);
  16.     return 0;
  17. }
复制代码


stack.h
  1. #ifndef _STACK_H_
  2. #define _STACK_H_

  3. #include <stddef.h>
  4. #include <stdbool.h>

  5. struct stack_node_tag {
  6.     void *data;
  7.     size_t size;
  8.     struct stack_node_tag *next;
  9. };

  10. typedef struct {
  11.     struct stack_node_tag *head;
  12.     size_t size;
  13. } stack_t;

  14. stack_t *stack_init(void);
  15. void stack_deinit(stack_t *stack);
  16. bool stack_clean(stack_t *stack);
  17. bool stack_push(stack_t *stack, const void *data, size_t size);
  18. bool stack_pop(stack_t *stack);
  19. bool stack_top(const stack_t *stack, void *data, size_t size);
  20. size_t stack_size(const stack_t *stack);
  21. bool stack_empty(const stack_t *stack);

  22. #endif
复制代码


stack.c
  1. #include "stack.h"
  2. #include <stdlib.h>
  3. #include <memory.h>

  4. stack_t *stack_init(void) {
  5.     stack_t *stack = malloc(sizeof(*stack));
  6.     if(!stack) return NULL;
  7.     stack->head = NULL;
  8.     stack->size = 0;
  9.     return stack;
  10. }

  11. void stack_deinit(stack_t *stack) {
  12.     if(!stack) return;
  13.     stack_clean(stack);
  14.     free(stack);
  15. }

  16. bool stack_clean(stack_t *stack) {
  17.     if(!stack) return false;
  18.     while(!stack_empty(stack)) stack_pop(stack);
  19.     return true;
  20. }

  21. bool stack_push(stack_t *stack, const void *data, size_t size) {
  22.     if(!stack) return false;
  23.     if(!data) return false;
  24.     struct stack_node_tag *node = malloc(sizeof(*node));
  25.     if(!node) return false;
  26.     node->data = malloc(size);
  27.     if(!node->data) {free(node); return false;}
  28.     memcpy(node->data, data, size);
  29.     node->size = size;
  30.     node->next = stack->head;
  31.     stack->head = node;
  32.     ++stack->size;
  33.     return true;
  34. }

  35. bool stack_pop(stack_t *stack) {
  36.     if(!stack) return false;
  37.     if(stack_empty(stack)) return false;
  38.     struct stack_node_tag *temp = stack->head;
  39.     stack->head = temp->next;
  40.     free(temp->data); free(temp);
  41.     --stack->size;
  42.     return true;
  43. }

  44. bool stack_top(const stack_t *stack, void *data, size_t size) {
  45.     if(!stack) return false;
  46.     if(!data) return false;
  47.     if(stack_empty(stack)) return false;
  48.     struct stack_node_tag *temp = stack->head;
  49.     if(temp->size > size) return false;
  50.     memcpy(data, temp->data, temp->size);
  51.     return true;
  52. }

  53. size_t stack_size(const stack_t *stack) {
  54.     if(!stack) return 0;
  55.     return stack->size;
  56. }

  57. bool stack_empty(const stack_t *stack) {
  58.     if(!stack) return true;
  59.     return stack_size(stack) == 0;
  60. }
复制代码

  1. $ gcc-debug -o main main.c stack.c
  2. $ ./main
  3. 11 22 33 44 55
  4. 55 44 33 22 11
  5. $
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-12-17 10:45:18 From FishC Mobile | 显示全部楼层    本楼为最佳答案   
C 代码:
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct Node{
  4.         int data;
  5.         struct Node* next;
  6. };

  7. int main(){
  8.         int N;
  9.         struct Node *head = NULL, *tail = NULL;
  10.         scanf("%d", &N);
  11.         while(N--){
  12.                 tail = head;
  13.                 head = (struct Node*)malloc(sizeof(struct Node));
  14.                 scanf("%d", &head->data);
  15.                 head->next = tail;
  16.         }
  17.        
  18.         for(; head; head = head->next) printf("%d ", head->data);
  19.         return 0;
  20. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-25 02:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表