初学数据结构——单链表

    看了一天的数据结构。突然觉得自己太迟没有看到这本书。如果不是要学习并查集,估计我这辈子可能不会去看数据结构这本书。因为单从书名来看,我就觉得是非常深奥的知识,但是能把深奥的知识学懂一点点,也是很快乐的事。

    今天自己写了单链表,虽然和书上的大有不同,但是实现的意义是相同的。
    (>_<觉得自己好蠢,写了一下午才写出来。笨死了!!)

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4 
  5 typedef int elemtype;
  6 
  7 typedef struct node
  8 {
  9     elemtype data;
 10     struct node *next;
 11 }Node;
 12 
 13 typedef struct node *linklist;
 14 
 15 linklist create_list(linklist head, elemtype n) //创建链表
 16 {
 17     linklist pre;
 18     head = (linklist)malloc(sizeof(Node));
 19     pre  = (linklist)malloc(sizeof(Node));
 20     head->next = pre;
 21     scanf("%d",&pre->data);
 22     for(int i = 1; i < n; i++)
 23     {
 24         pre->next = (linklist)malloc(sizeof(Node));
 25         pre = pre->next;
 26         scanf("%d",&pre->data);
 27     }
 28     pre->next = NULL;
 29     return head;
 30 }
 31 
 32 linklist add_element_tail(linklist head, elemtype data) //从尾部增加元素
 33 {
 34     linklist pre,temp;
 35     pre = head->next;
 36     temp = (linklist)malloc(sizeof(Node));
 37     temp->data = data;
 38     while(pre->next != NULL)
 39     {
 40         pre = pre->next;
 41     }
 42     pre->next = temp;
 43     temp->next = NULL;
 44     return head;
 45 }
 46 
 47 linklist add_element_head(linklist head, elemtype data)  //从头部增加元素
 48 {
 49     linklist pre,temp;
 50     pre = head->next;
 51     temp = (linklist)malloc(sizeof(Node));
 52     temp->data = data;
 53     head->next = temp;
 54     temp->next = pre;
 55     return head;
 56 }
 57 
 58 linklist insert_take_place(linklist head, elemtype data, int place) //根据位置增加元素
 59 {
 60     linklist pre,temp;
 61     pre = head;
 62     temp = (linklist)malloc(sizeof(Node));
 63     temp->data = data;
 64     for(int i = 0; i < place; i++)
 65     {
 66         pre = pre->next;
 67     }
 68     temp->next = pre->next;
 69     pre->next = temp;
 70     return head;
 71 }
 72 
 73 linklist insert_take_value(linklist head, elemtype data, elemtype value) //根据数据值增加元素
 74 {
 75     linklist pre,temp;
 76     pre = head->next;  //如果要在值前面插入 请更换此行语句为pre = head;
 77     temp = (linklist)malloc(sizeof(Node));
 78     temp->data = data;
 79     while(pre->data != value)  //如果要在值前面插入 请更换此行语句为pre->next->data == value
 80     {
 81         pre = pre->next;
 82     }
 83     temp->next = pre->next;
 84     pre->next =temp;
 85     return head;
 86 }
 87 
 88 void delete_list(linklist &head)  //删除链表
 89 {
 90     linklist pre;
 91     while(head != NULL)
 92     {
 93         pre = head->next;
 94         free(head);
 95         head = pre;
 96     }
 97     printf("List was deleted\n");
 98 }
 99 
100 void print_list(linklist head) //打印链表
101 {
102     if(head == NULL)
103         printf("No List\n");
104     else
105     {
106         head = head->next;
107         while(head != NULL)
108         {
109             printf("%d ",head->data);
110             head = head->next;
111         }
112         printf("\n");
113     }
114 }
115 
116 
117 int main()
118 {
119     linklist phead = NULL;
120     phead = create_list(phead,10);
121     phead = add_element_tail(phead,11);
122     phead = add_element_head(phead,12);
123     phead = insert_take_place(phead,13,5);
124     phead = insert_take_value(phead,14,13);
125     print_list(phead);
126     delete_list(phead);
127     print_list(phead);
128     return 0;
129 }

 

posted @ 2013-08-07 20:31  清瞳浅水  阅读(253)  评论(0编辑  收藏  举报