1 #include "head.h" | 1 #ifndef ww | 1 #include "head.h"
2 //创建双向链表 | 2 #define ww | 2 int main(int argc, const char *argv[])
3 p create_double() | 3 #include <stdio.h> | 3 {
4 { | 4 #include <string.h> | 4 p h=create_double();
5 p h=(p)malloc(sizeof(stu)); | 5 #include <stdlib.h> | 5 insert_head(h,10);
6 if(h==0) | 6 typedef struct stu | 6 insert_head(h,20);
7 { | 7 { | 7 insert_head(h,30);
8 printf("空间申请失败\n"); | 8 int data; | 8 show(h);
9 return 0; | 9 struct stu*pri; | 9
10 } | 10 struct stu*next; | 10 return 0;
11 h->data=0; | 11 }stu,*p; | 11 }
12 h->next=0; | 12 |~
13 h->pri=0; | 13 //创建双向链表 |~
14 } | 14 p create_double(); |~
15 //创建双向链表节点 | 15 //创建双向链表节点 |~
16 p create_node(int n) | 16 p create_node(int n); |~
17 { | 17 //头插 |~
18 p new=(p)malloc(sizeof(stu)); | 18 void insert_head(p h,int n); |~
19 if(new==0) | 19 //输出 |~
20 { | 20 void show(p h); |~
21 printf("空间申请失败\n"); | 21 |~
22 return 0; | 22 |~
23 } | 23 |~
24 new->data=n; | 24 |~
25 return new; | 25 |~
26 } | 26 |~
27 //判空 | 27 |~
28 int empty(p h) | 28 #endif |~
29 { |~ |~
30 if(h==0) |~ |~
31 { |~ |~
32 printf("入参为空\n"); |~ |~
33 return -1; |~ |~
34 } |~ |~
35 return h->next==0?1:0; |~ |~
36 } |~ |~
37 //头插 |~ |~
38 void insert_head(p h,int n) |~ |~
39 { |~ |~
40 if(h==0) |~ |~
41 { |~ |~
42 printf("入参为空\n"); |~ |~
43 return; |~ |~
44 } |~ |~
45 //定义新指针接收新建节点首地址 |~ |~
46 p new=create_node(n); |~ |~
47 new->next=h->next; |~ |~
48 if(h->next!=0)//可能只有头 |~ |~
49 { |~ |~
50 h->next->pri=new; |~ |~
51 } |~ |~
52 new->pri=h; |~ |~
53 h->next=new; |~ |~
54 } |~ |~
55 //输出 |~ |~
56 void show(p h) |~ |~
57 { |~ |~
58 if(h==0) |~ |~
59 { |~ |~
60 printf("入参为空\n"); |~ |~
61 return; |~ |~
62 } |~ |~
63 if(empty(h)) |~ |~
64 { |~ |~
65 printf("链表为空\n"); |~ |~
66 return; |~ |~
67 } |~ |~
68 p p1=h->next; |~ |~
69 while(p1!=0) |~ |~
70 { |~ |~
71 printf("%d->",p1->data); |~ |~
72 p1=p1->next; |~ |~
73 } |~ |~
74 |~ |~
75 }
还没写完