线性表之单链表

来源:互联网 发布:网络涉军举报平台上线 编辑:程序博客网 时间:2024/05/21 09:25
[cpp] view plaincopy
  1. /* 
  2.  * LinkedList 
  3.  * linc 
  4.  * 2013.2.26 
  5.  */  
  6. #include <stdio.h>  
  7. #include <stdlib.h>  
  8. #include <time.h>  
  9.   
  10. #define OK 1    
  11. #define ERROR -1    
  12. #define TURE 1    
  13. #define FALSE 0   
  14.   
  15. struct Node  
  16. {  
  17.     int data;  
  18.     struct Node *next;  
  19. };  
  20. typedef struct Node *Head;  
  21. typedef struct Node *LinkedList;  
  22.   
  23. //create the linked list,head insert  
  24. void createList(LinkedList *list,int size,Head *head)  
  25. {  
  26.     /* 
  27.     *head = (Head)malloc(sizeof(struct Node)); 
  28.     printf("head test0"); 
  29.     (*head)->data = size; 
  30.     printf("head test1"); 
  31.     //(*head)->next = (*list); 
  32.     printf("head test2"); 
  33.     */  
  34.     LinkedList tmpList;  
  35.     srand(time(0));  
  36.     *list = (LinkedList)malloc(sizeof(struct Node));  
  37.     (*list)->next = NULL;  
  38.     for(int i = 0; i < size; i++)  
  39.     {  
  40.         tmpList = (LinkedList)malloc(sizeof(struct Node));  
  41.         tmpList->data = rand()%100 + 1;  
  42.         tmpList->next = (*list)->next;  
  43.         (*list)->next = tmpList;  
  44.         printf("list->data %d is %d\n",i,tmpList->data);  
  45.     }  
  46.     //head node  
  47.     tmpList = (LinkedList)malloc(sizeof(struct Node));  
  48.     tmpList->data = size;  
  49.     tmpList->next = (*list)->next;  
  50.     (*list)->next =tmpList;  
  51.   
  52.     /* 
  53.     while((*list) != NULL) 
  54.     { 
  55.         printf("data is %d\n",(*list)->data); 
  56.         (*list)->next; 
  57.     } 
  58.     */  
  59.   
  60. }  
  61. //get element of list  
  62. int getElement(LinkedList list,int index,int *element)  
  63. {  
  64.     printf("getElement\n");  
  65.     LinkedList tmpList = list->next;  
  66.     printf("getElement---test\n");  
  67.     int size = tmpList->data;//the list size  
  68.     printf("the size is %d\n",size);  
  69.     if(index > size)  
  70.     {  
  71.         return ERROR;  
  72.     }  
  73.       
  74.     int count = 0;  
  75.     while(tmpList && count < index+1)//+1 for head node  
  76.     {  
  77.         printf("data is %d\n",tmpList->data);  
  78.         tmpList = tmpList->next;  
  79.         ++count;  
  80.     }  
  81.     if(!tmpList)  
  82.         return ERROR;  
  83.     *element = tmpList->data;  
  84.     return OK;  
  85. }  
  86.   
  87. //clear list  
  88. int clearList(LinkedList *list)  
  89. {  
  90.     //use 2 points free the list  
  91.     LinkedList tmpList1,tmpList2;  
  92.     tmpList1 = (*list)->next;  
  93.     while(tmpList1)  
  94.     {  
  95.         printf("free the data is %d\n",tmpList1->data);  
  96.         tmpList2 = tmpList1->next;  
  97.         free(tmpList1);  
  98.         tmpList1 = tmpList2;  
  99.     }  
  100.     (*list)->next = NULL;  
  101.     return OK;  
  102. }  
  103.   
  104. //insert  
  105. //after index  
  106. int insert(LinkedList *list,int index,int element)  
  107. {  
  108.     printf("insert index is %d,element is %d\n",index,element);  
  109.     LinkedList tmpList = (*list)->next;  
  110.     int size = tmpList->data;  
  111.     if(index > size)  
  112.     {  
  113.         printf("ERROR:the index > size.\n");  
  114.         return ERROR;  
  115.     }  
  116.     int count = 0;  
  117.     while(tmpList && count < index+1)  
  118.     {  
  119.         tmpList = tmpList->next;  
  120.         ++count;  
  121.     }  
  122.     if(!tmpList)  
  123.         return ERROR;  
  124.     LinkedList node = (LinkedList)malloc(sizeof(struct Node));  
  125.     node->data = element;  
  126.     node->next = tmpList->next;  
  127.     tmpList->next = node;  
  128.     return OK;  
  129. }  
  130. //delete  
  131. int delete(LinkedList *list,int index)  
  132. {  
  133.     printf("delete,index is %d\n",index);  
  134.     LinkedList tmpList = (*list)->next;  
  135.     int size = tmpList->data;  
  136.     if(index > size)  
  137.     {  
  138.         printf("ERROR:the index > size.\n");  
  139.         return ERROR;  
  140.     }  
  141.     int count = 0;  
  142.     while(tmpList && count < index+1)  
  143.     {  
  144.         tmpList = tmpList->next;  
  145.         ++count;  
  146.     }  
  147.     if(!tmpList)  
  148.         return ERROR;  
  149.     LinkedList node = tmpList->next;  
  150.     tmpList->next = node->next;  
  151.     free(node);  
  152.     return OK;  
  153. }  
  154.   
  155. int main()  
  156. {  
  157.     LinkedList *list;  
  158.     Head *head;  
  159.     createList(list,10,head);  
  160.     int i = 1;  
  161.     getElement(*list,5,&i);  
  162.     insert(list,5,20);  
  163.     printf("getElement is %d\n",i);  
  164.     getElement(*list,5,&i);  
  165.     printf("getElement is %d\n",i);  
  166.     delete(list,5);  
  167.     clearList(list);  
  168.     return 0;  
  169. }  
链式存储结构的优点在于它在找到目标元素后,删除或者插入都十分方便,只需将指针挪动一次就好;缺点就是不能像顺序存储结构那样迅速的找到目标元素,只能笨笨的一个一个元素的遍历下去。
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 小孩长得太快怎么办 脑出血压着神经不会说话怎么办 四岁宝宝说话有点口吃怎么办 三岁宝宝有点口吃怎么办 3岁宝宝有点口吃怎么办 三岁宝宝说话有点口吃怎么办 六岁说话重复第一个字怎么办 宝贝烧到39.5度怎么办 宝贝39度不退烧怎么办 两岁多小儿突然变得口吃怎么办 百度两周岁宝宝口吃怎么办 2岁宝宝偶尔结巴怎么办 两岁宝宝说话磕巴怎么办 宝宝两岁结巴了怎么办 人多说话就紧张怎么办 小孩拉尿不叫人怎么办 2岁宝宝说话有点结巴怎么办 两岁半的宝宝说话结巴怎么办 2个月宝宝怕洗澡怎么办 2岁宝宝不喜欢喝奶粉怎么办 宝宝断奶不喜欢喝奶粉怎么办 宝宝不喜欢奶粉的味道怎么办 四个月宝宝不喜欢吃奶粉怎么办 四岁宝宝有口臭怎么办 4个月宝宝口臭怎么办 2岁宝宝有口臭是怎么办 两岁宝宝有口气怎么办 2岁宝宝口气重是什么原因怎么办 两岁宝宝口气重怎么办 两岁宝宝有口臭怎么办 两岁身高不达标怎么办 两岁宝宝82厘米怎么办 2岁幼儿说话结巴怎么办 2岁的宝宝结巴怎么办 2岁半宝宝口吃怎么办 2周岁宝宝不说话怎么办 三周岁宝宝不说话怎么办 2周岁宝宝突然说话结巴怎么办 两周岁宝宝突然说话结巴怎么办 三周岁宝宝说话突然结巴怎么办 小孩g和d不分怎么办