一个简朴的C语言制作的单链表

打印 上一主题 下一主题

主题 1770|帖子 1770|积分 5310

C_LINK_LIST



  • 这是单链表和双链表的根本效果
点击查看代码
  1. //single_link_list
  2. struct NODE
  3.     int value;
  4.     struct NODE * next;
  5. }
  6. ```c
  7. //double linked list
  8. struct NODE{
  9.     int value;
  10.     struct NODE * previous;
  11.     struct NODE * next;
  12. }
  13. 这是自己制作的一个C语言单链表,他的功能包括创建一个单链表,查找删除特定的值,并且实现倒序单链表。
  14. 她的不足之处在于他无法在主函数中特定停止,等待用户进行输入。
  15. This is my code.
  16. ```c
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include "singly_linked_list.h"
  20. //struct  NODE * sll_reverse(struct NODE * first);
  21. int sll_remove(Node ** rootp, int value);
  22. int dll_insert(Node ** rootp, int value);
  23. Node * sll_reverse(Node *first);
  24. int main(){
  25.     int num;
  26.     Node *first = (Node *)malloc(sizeof(Node));
  27.     if(first == NULL){
  28.         perror("NO Buffle: ");
  29.         return 1;
  30.     }
  31.     first->value = 3;
  32.     first->link = NULL;
  33.     Node *rootp = first;
  34.     while (scanf("%d" , &num) != EOF){
  35.         dll_insert(&rootp, num);
  36.     }
  37.     Node * ptr = rootp;
  38.     printf("\n");
  39.     while(ptr != NULL){
  40.         printf(" %d", ptr->value);
  41.         ptr = ptr->link;
  42.     }
  43.    /* getchar();getchar();
  44.     printf("Input a single number you want to remove: ");
  45.     getchar();
  46.     while(scanf("%d", &num) != EOF){
  47.         sll_remove(&rootp, num);
  48.     }
  49.     ptr = rootp;
  50.     while(ptr != NULL){
  51.         printf(" %d", ptr->value);
  52.         ptr = ptr->link;
  53.     }*/
  54.     rootp = sll_reverse(rootp);
  55.     printf("\n");
  56.     ptr = rootp;
  57.     while(ptr != NULL){
  58.         printf(" %d", ptr->value);
  59.         ptr = ptr->link;
  60.     }
  61.     ptr = rootp;
  62.     while(ptr != NULL){
  63.         Node * temp=ptr;
  64.         ptr = ptr->link;
  65.         free(temp);
  66.     }
  67.     return 0;
  68. }
  69. int dll_insert(Node ** rootp, int value){
  70.     Node *previous = NULL;
  71.     Node *current = *rootp;
  72.     while(current != NULL && current->value < value){
  73.         previous = current;
  74.         current = current->link;
  75.     }
  76.     if (current != NULL && current-> value == value)
  77.         return 0;
  78.     Node *new = (Node *)malloc(sizeof(Node));
  79.     if (new == NULL){
  80.         perror("NO buffle : ");
  81.         exit(EXIT_FAILURE);
  82.     }
  83.     new->value = value;
  84.     new->link = current;
  85.     if(previous != NULL){
  86.         previous->link  = new;
  87.     }
  88.     else{
  89.         *rootp = new;
  90.     }
  91.     return 0;
  92. }
  93. int sll_remove(Node ** rootp,int value){
  94.     Node *previous = NULL;
  95.     Node *current = *rootp;
  96.    
  97.     while(current != NULL && current->value != value){
  98.         previous = current;
  99.         current = current->link;
  100.     }
  101.     if(current != NULL){
  102.         Node * temp = current;
  103.         current = current->link;
  104.         free(temp);
  105.         if(previous != NULL){
  106.             return 0;
  107.         }
  108.         else {
  109.             *rootp = current;
  110.             return 0;
  111.         }
  112.     }
  113.     else {
  114.         printf("No this module. ");
  115.         return 1;
  116.     }
  117. }
  118.         
  119. Node * sll_reverse(Node * first){
  120.     int i = 0;
  121.     Node *temp = first;
  122.     while(temp != NULL){
  123.         temp = temp->link;
  124.         i++;
  125.     }
  126.     if( i == 0){
  127.         printf("None Node.");
  128.         return first;
  129.     }
  130.     Node *array[i];
  131.     temp = first;
  132.     for(int j = 0; j< i;j++){
  133.         ar#include <stdlib.h>
  134. #include <stdio.h>
  135. #include "singly_linked_list.h"
  136. //struct  NODE * sll_reverse(struct NODE * first);
  137. int sll_remove(Node ** rootp, int value);
  138. int dll_insert(Node ** rootp, int value);
  139. Node * sll_reverse(Node *first);
  140. int main(){
  141.     int num;
  142.     Node *first = (Node *)malloc(sizeof(Node));
  143.     if(first == NULL){
  144.         perror("NO Buffle: ");
  145.         return 1;
  146.     }
  147.     first->value = 3;
  148.     first->link = NULL;
  149.     Node *rootp = first;
  150.     while (scanf("%d" , &num) != EOF){
  151.         dll_insert(&rootp, num);
  152.     }
  153.     Node * ptr = rootp;
  154.     printf("\n");
  155.     while(ptr != NULL){
  156.         printf(" %d", ptr->value);
  157.         ptr = ptr->link;
  158.     }
  159.    /* getchar();getchar();
  160.     printf("Input a single number you want to remove: ");
  161.     getchar();
  162.     while(scanf("%d", &num) != EOF){
  163.         sll_remove(&rootp, num);
  164.     }
  165.     ptr = rootp;
  166.     while(ptr != NULL){
  167.         printf(" %d", ptr->value);
  168.         ptr = ptr->link;
  169.     }*/
  170.     rootp = sll_reverse(rootp);
  171.     printf("\n");
  172.     ptr = rootp;
  173.     while(ptr != NULL){
  174.         printf(" %d", ptr->value);
  175.         ptr = ptr->link;
  176.     }
  177.     ptr = rootp;
  178.     while(ptr != NULL){
  179.         Node * temp=ptr;
  180.         ptr = ptr->link;
  181.         free(temp);
  182.     }
  183.     return 0;
  184. }
  185. int dll_insert(Node ** rootp, int value){
  186.     Node *previous = NULL;
  187.     Node *current = *rootp;
  188.     while(current != NULL && current->value < value){
  189.         previous = current;
  190.         current = current->link;
  191.     }
  192.     if (current != NULL && current-> value == value)
  193.         return 0;
  194.     Node *new = (Node *)malloc(sizeof(Node));
  195.     if (new == NULL){
  196.         perror("NO buffle : ");
  197.         exit(EXIT_FAILURE);
  198.     }
  199.     new->value = value;
  200.     new->link = current;
  201.     if(previous != NULL){
  202.         previous->link  = new;
  203.     }
  204.     else{
  205.         *rootp = new;
  206.     }
  207.     return 0;
  208. }
  209. int sll_remove(Node ** rootp,int value){
  210.     Node *previous = NULL;
  211.     Node *current = *rootp;
  212.    
  213.     while(current != NULL && current->value != value){
  214.         previous = current;
  215.         current = current->link;
  216.     }
  217.     if(current != NULL){
  218.         Node * temp = current;
  219.         current = current->link;
  220.         free(temp);
  221.         if(previous != NULL){
  222.             return 0;
  223.         }
  224.         else {
  225.             *rootp = current;
  226.             return 0;
  227.         }
  228.     }
  229.     else {
  230.         printf("No this module. ");
  231.         return 1;
  232.     }
  233. }
  234.         
  235. Node * sll_reverse(Node * first){
  236.     int i = 0;
  237.     Node *temp = first;
  238.     while(temp != NULL){
  239.         temp = temp->link;
  240.         i++;
  241.     }
  242.     if( i == 0){
  243.         printf("None Node.");
  244.         return first;
  245.     }
  246.     Node *array[i];
  247.     temp = first;
  248.     for(int j = 0; j< i;j++){
  249.         array[j] = temp;
  250.         temp = temp->link;
  251.     }
  252.     Node *new =  array[i-1];
  253.     for(int k = i-1;k >= 0;k--){
  254.         temp = array[k];
  255.         if(k != 0){
  256.             temp->link = array[k-1];
  257.         }
  258.         else{
  259.             temp->link = NULL;
  260.         }
  261.     }
  262.     return new;
  263. }
  264. ray[j] = temp;
  265.         temp = temp->link;
  266.     }
  267.     Node *new =  array[i-1];
  268.     for(int k = i-1;k >= 0;k--){
  269.         temp = array[k];
  270.         if(k != 0){
  271.             temp->link = array[k-1];
  272.         }
  273.         else{
  274.             temp->link = NULL;
  275.         }
  276.     }
  277.     return new;
  278. }
复制代码
头文件: single_link_list.h
  1. typedef struct NODE {
  2.     int value;
  3.     struct NODE * link;
  4. }Node;
复制代码
这是我制作的双链表
双链表与单链表的区别:单链表只能按某种顺序执行,而双链表在每个节点中都有前一个节点和后一个节点的指针,能够访问前一个节点和后一个节点。
这是我编写的双链表实现方式:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct NODE {
  4.     struct NODE * fwd;
  5.     struct NODE * bwd;
  6.     int value;
  7. } Node;
  8. Node * insert(Node *, int );
  9. int main(){
  10.     int new_value;
  11.     Node * top;
  12.     top = (Node *)malloc(sizeof(Node));
  13.     if(top == NULL){
  14.         perror("No memory: ");
  15.         exit(EXIT_FAILURE);
  16.     }
  17.     top->bwd = NULL;
  18.     top->fwd = NULL;
  19.     printf("Hello linked list.\n");
  20.     printf("Input some integet(-1 to quit). ");
  21.     while((scanf("%d", &new_value) == 1) && new_value != -1){
  22.         top=insert(top, new_value);
  23.     }
  24.     if(top->fwd == NULL){
  25.         printf("NULL in top.\n");
  26.         free(top);
  27.         return 0;
  28.     }
  29.     for(Node * temp=top->fwd; temp != NULL; temp = temp->fwd){
  30.         printf("%d ", temp->value);
  31.     }
  32.     //Node * current = top;
  33.     Node * current = top->fwd;
  34.     while(current != NULL){
  35.         Node * temp = current;
  36.         current = current->fwd;
  37.         free(temp);
  38.     }
  39.     free(top);
  40.     return 0;
  41. }
  42. Node * insert(Node * rootp, int value){
  43.     Node * this;
  44.     Node * next;
  45.     Node * new;
  46.    
  47.     for (this = rootp; (next = this->fwd) != NULL ; this = next ){
  48.         if(next->value == value)
  49.             return rootp;
  50.         if (this->value > value)
  51.             break;
  52.     }
  53.     new = (Node *)malloc(sizeof(Node));
  54.     if (new == NULL){
  55.         perror("No memory: ");
  56.         exit(EXIT_FAILURE);
  57.     }
  58.     new->value = value;
  59.     if (next != NULL){
  60.         new->fwd = next;
  61.         if (this != rootp){
  62.             this->fwd = new;
  63.             new->bwd = this;
  64.         }
  65.         else{
  66.             rootp->fwd = new;
  67.             new->bwd = NULL;
  68.         }
  69.         next->bwd = new;
  70.     }
  71.     else{
  72.         new->fwd = NULL;
  73.         if(this != rootp){
  74.             this->fwd = new;
  75.             new->bwd = this;
  76.         }
  77.         else{
  78.             rootp->fwd = new;
  79.             new->bwd = NULL;
  80.         }
  81.         rootp->bwd = new;
  82.     }
  83.     return rootp;
  84. }
复制代码

2025-04-08 12:37:42 星期二

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

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

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

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

本版积分规则

盛世宏图

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表