现在的位置: 首页 > 综合 > 正文

数据结构——单链表的操作

2012年09月19日 ⁄ 综合 ⁄ 共 2724字 ⁄ 字号 评论关闭

建立有三个学生信息的单向链表,并实现其插入 ,排序 , 删除 ,输出 等操作

  1 #include <stdio.h>
2 #include <malloc.h>
3 #include <stdlib.h>
4
5 struct stud{
6 char name[15] ;
7 int number ;
8 float score[2];
9 struct stud *next ;
10 };
11 int n ; //定义全局变量n代表链表中结点个数
12
13 struct stud *creat(); //创建链表
14 struct stud *dele(struct stud *head , int num); //删除链表中的一个数据
15 struct stud *inst(struct stud *head , struct stud *stul); //插入一个数据到链表中
16 void print(struct stud *head); //输出链表
17
18
19 int main(void)
20 {
21 struct stud *head , stu ;
22 int del_num ;
23
24 printf("请输入数据:");
25 head = creat();
26 print(head);
27
28 printf("\n 请输入要删除学生的学号:");
29 scanf("%d" , &del_num);
30
31 head = dele(head , del_num);
32
33 printf("\n 请输入要插入学生的信息: ");
34 gets(stu.name);
35 scanf(" %d %f %f", &stu.number , &stu.score[0] , &stu.score[1]);
36 head = inst(head , &stu);
37 printf(head);
38
39 return 0 ;
40 }
41
42 struct stud *creat()
43 {
44 struct stud *head , *p1 , *p2 ;
45 {
46 n = 0 ;
47 p1 = p2 = (struct stud *)malloc(sizeof(struct stud)); //开辟新结点 动态分配内存给p1 , p2
48 gets(p1 -> name);
49 scanf(" %d %f %f" , &p1 -> number , &p1 -> score[0] , p1 -> score[1]);
50 head = NULL ;
51 while(p1 -> number != 0) //第一次循环之后则判断第63行 p1 -> number 值是否为0
52 {
53 n++ ;
54 if(n == 1)
55 head = p1 ; //head 指向新开辟的结点
56 else
57 {
58 p2 -> next = p1 ; // 使p2链入链表中成为第二个结点
59 p2 = p1 ; // p2指向p1所指结点
60 }
61 p1 = (struct stud*)malloc(sizeof(struct stud)); //重新分配内存
62 gets(p1 -> name);
63 scanf(" %d %f %f" , &p1 -> number , &p1 -> score[0] , &p1 -> score[1]);
64 }
65 p2 -> next = NULL ;
66 }
67 return head ;
68 }
69
70 struct stud * dele(struct stud *head , int num)
71 {
72 struct stud *p1 , *p2 ;
73 if(head == NULL)
74 {
75 printf("空链表");
76 //goto end ; 也可以使用goto语句,但不推荐
77 exit(0) ;
78 }
79 p1 = head ; //p1指向头结点
80 while(num != p1 -> number && p1 -> next != NULL) //若p1 -> next == NULL 则为空链表
81 {p2 = p1 ; p1 = p1 -> next ;}
82 //p1指向的不是所要找的结点,后面还有结点, 使p2 , p1向后移动一个结点 ,并继续寻找
83
84 if(num == p1 -> number)
85 {
86 if(p1 = head)
87 {
88 head = p1 -> next ; //找到在第一个结点处 ,删除就是使head 指向 p1 所指向的结点
89 }
90 else if(p1 -> next == NULL)
91 {
92 p2 -> next = NULL ; //找到在尾结点
93 }
94 else
95 {
96 p2 -> next = p1 -> next ; //找到在中间结点 ,用p2 结点代替 p1的位置
97 }
98 printf(" 已删除:%d !" , num);
99
100 free(p1); //释放指针p1所指结点空间
101 n-- ;
102 }
103 else
104 printf(" 没找到:%d !" , num);
105
106 // end :
107 return head ;
108 }
109
110 struct stud *inst(struct stud *head , struct stud *stul) //按由小到大排序插入
111 {
112 struct stud *p0 , *p1 , *p2 ;
113 p1 = head ; //p1指向头结点
114 p0 = stul ; //p0指向要插入的结点
115 if(head == NULL)
116 {
117 head = p0 ; p0 -> next = NULL ;
118 }
119 else
120 {
121 while(p0 -> number > p1 -> number && p1 -> next != NULL)
122 {p2 = p1 ; p1 = p1 -> next;}
123
124 if(p0 -> number < p1 -> number)
125 {
126 if(head = p1)
127 head = p0 ;
128 else
129 {
130 p2 -> next = p0 ;
131 p0 -> next = p1 ;
132 }
133 }
134
135 else
136 {
137 p1 -> next = p0 ;
138 p0 -> next = NULL ;
139 }
140 }
141 n++ ;
142 return head ;
143 }
144
145 void print(struct stud *head)
146 {
147 struct stud *p ;
148 p = head ;
149
150 while(p != NULL)
151 {
152 printf("%s\t" ,p -> name );
153 printf("%d\t" ,p -> number );
154 printf("%6.2f %6.2f \n" , p -> score[0] , p -> score[1]);
155 p = p -> next ; // p 指向下一个结点 ,类似于数组里面的 p++
156 }
157
158 return ; //代表程序结束 , 无实际意义
159 }

翻转单链表

 1 struct *node reverse(struct node *head)
2 {
3 struct node *p , *q , *r ;
4 p = NULL ;
5 q = head ;
6
7 while(q)
8 {
9 r = q -> next ;
10 q -> next = p ;
11 p = q ;
12 q = r ;
13 }
14 return p ;
15 }

抱歉!评论已关闭.