數據結構問題---鏈表運算集合

-------------------------------------
典型例題 24:數據結構問題---鏈表運算集合
-------------------------------------
 1    #include <stdio.h>
 2    #include <stdlib.h>
 3    #include <string.h>
 4   
 5    //節點信息;
 6    typedef struct student
 7    {
 8        int data;
 9        struct student *next;
10    }node;
11   
12    //創建單鏈表,返回頭指針;
13    node *create_list()
14    {
15        node *head,*p,*s;
16        int x,cycle = 1;
17        head = (node*)malloc(sizeof(node));
18        head->next =NULL;
19        p = head;
20        while(cycle)
21        {
22            printf("/nplease input the data:");
23            scanf("%d",&x);
24            if(x!=0)
25            {
26                s = (node*)malloc(sizeof(node));
27                s->data = x;
28                p->next = s;
29                p = s;
30            }else{
31                cycle = 0;
32            }
33        }
34        return (head);
35    }
36   
37    //遍歷單鏈表元素;
38    void display_list(node *head)
39    {
40        node *p;
41        p = head->next;
42        if(head->next!= NULL)
43        {
44            while(p->next != NULL)
45            {
46                printf("%d -->",p->data );
47                p = p->next;
48            }
49            printf("%d;/n",p->data);
50        }
51    }
52    //獲取單鏈表長度
53    int getlen_list(node *head)
54    {
55        int length=0;
56        node *p;
57        p = head;
58        while(p != NULL)
59        {
60            p = p->next;
61            length++;
62        }
63        return (length);
64    }
65   
66    //單鏈表刪除結點
67    //有兩種情況:1、有單獨頭結點情況,這情況不用考慮刪除頭結點;
68    //2、沒有單獨頭結點情況,這情況要考慮刪除頭結點的情況;
69    //現在我的實現有單獨的頭文件,所有本來不用考慮刪除頭結點!
70    //不過考慮到兼容性,我還是實現檢測這種情況;
71   
72    node * delnode_list(node * head,int num)
73    {
74        node *p1,*p2;
75        p1 = head;
76        while(num != p1->data&&p1->next != NULL)
77            {
78                p2 = p1;
79                p1 = p1->next;
80            }
81   
82        if(num == p1 -> data)
83            {
84                if(p1 == head)
85                    {
86                        head = p1->next;
87                    }else{
88                    p2->next = p1 ->next;
89                }
90                free(p1);
91            }else{
92            printf("/n %d could not been found",num);
93        }
94        return head;
95    }
96    //單鏈表插入結點;
97    //跟上面差不多;
98    node *insertnode_list(node *head,int num)
99    {
100        node *p0,*p1,*p2;
101        p1 = head;
102        p0 = (node*)malloc(sizeof(node));
103        p0 ->data = num;
104        while(num != p1->data&&p1->next!=NULL)
105            {
106                p2 = p1;
107                p1 = p1->next;
108                   
109            }
110        if(num <= p1->data)
111            {
112                if(head==p1)
113                    {
114                        p0->next = p1;
115                        head = p0;
116                    }else{
117                    p2->next = p0;
118                    p0->next = p1;
119                }
120            }else{
121            p1->next = p0;
122            p0->next = NULL;
123        }
124        return (head);
125    }
126   
127    //主界面選項;
128    int main()
129    {
130        node *listhead;
131        int length;
132        int delnum;
133        int insertnum;
134    menu:
135        printf("/n-- -- -- -- --single list menu -- -- -- -- -/n");
136        printf("create the list -- -- -- -- --input the number 1/n");
137        printf("display the list -- -- -- -- -input the number 2/n");
138        printf("get length of the list -- -- -input the number 3/n");
139        printf("delete node -- -- -- - -- -- -input the number 4/n");
140        printf("insert node -- -- -- - -- -- -input the number 5/n");
141        printf("quit or exit -- -- --  -- -- -input the number 0/n");
142        int choose;
143        scanf("%d",&choose);
144   
145        switch(choose)
146        {
147            case 1:
148                listhead = create_list();
149                break;
150            case 2:
151                display_list(listhead);
152                break;
153            case 3:
154                length = getlen_list(listhead);
155                printf("/nThe length of the list is :%d(contain the head node) /n",length );
156                break;
157            case 4:
158                printf("/n Please input the number you want to delete:");
159                scanf("%d",&delnum);
160                listhead = delnode_list(listhead,delnum);
161                break;
162            case 5:
163                printf("/n Please input the number you want to insert:");
164                scanf("%d",&insertnum);
165                listhead = insertnode_list(listhead,insertnum);
166                break;
167            default:
168                goto end;
169        }
170        goto menu;
171    end:
172        return 0;
173    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章