1
/*线性表链式存储-单链表的基本操作*/
2
#include "stdafx.h"
3
#include <stdio.h>
4
#include <malloc.h>
5
typedef char ElemType;
6
struct LNode
7
{
8
ElemType data;
9
struct LNode *next;
10
};
11
12
//***********************************************************置空表setnull()
13
void setnull(struct LNode **p)
14
{
15
*p=NULL;
16
}
17
18
//************************************************************求长度length()
19
int length(struct LNode **p)
20
{
21
int n=0;
22
struct LNode *q=*p;
23
while (q!=NULL)
24
{
25
n++;
26
q=q->next;
27
}
28
return(n);
29
}
30
31
//*************************************************************取结点get()
32
ElemType get(struct LNode **p,int i)
33
{
34
int j=1;
35
struct LNode *q=*p;
36
while (j<i && q!=NULL) /*查找第i个结点*/
37
{
38
q=q->next;j++;
39
}
40
if (q!=NULL) /*找到了第i个结点*/
41
return(q->data);
42
else
43
{
44
printf("位置参数不正确!\n");
45
return NULL;
46
}
47
}
48
49
//************************************************************按值查找locate()
50
int locate(struct LNode **p,ElemType x)
51
{
52
int n=0;
53
struct LNode *q=*p;
54
while (q!=NULL && q->data!=x) /*查找data域为x的第一个结点*/
55
{
56
q=q->next;
57
n++;
58
}
59
if (q==NULL) /*未找到data域等于x的结点*/
60
return(-1);
61
else /*找到data域等于x的结点*/
62
return(n+1);
63
}
64
65
//**********************************************************插入结点insert()
66
void insert(struct LNode **p,ElemType x,int i)
67
{
68
int j=1;
69
struct LNode *s,*q;
70
s=(struct LNode *)malloc(sizeof(struct LNode)); /*建立要插入的结点s*/
71
s->data=x;
72
q=*p;
73
if (i==1) /*插入的结点作为头结点*/
74
{
75
s->next=q;
76
*p=s;
77
}
78
else
79
{
80
while (j<i-1 && q->next!=NULL) /*查找第i-1个结点*/
81
{
82
q=q->next;j++;
83
}
84
if (j==i-1) /*找到了第i-1个结点,由q指向它*/
85
{
86
s->next=q->next; /*将结点s插入到q结点之后*/
87
q->next=s;
88
}
89
else
90
printf("位置参数不正确!\n");
91
}
92
}
93
94
//*********************************************************删除结点del()
95
void del(struct LNode **p,int i)
96
{
97
int j=1;
98
struct LNode *q=*p,*t;
99
if (i==1) /*删除链表的头结点*/
100
{
101
t=q;
102
*p=q->next;
103
}
104
else
105
{
106
while (j<i-1 && q->next!=NULL) /*查找第i-1个结点*/
107
{
108
q=q->next;j++;
109
}
110
111
if (q->next!=NULL && j==i-1) /*找到第i-1个结点,由q指向它*/
112
{
113
t=q->next; /*t指向要删除的结点*/
114
q->next=t->next; /*将q之后的结点删除*/
115
}
116
else printf("位置参数不正确!\n");
117
}
118
if (t!=NULL) /*在t不为空时释放该结点*/
119
free(t);
120
}
121
122
//********************************************************显示链表display()
123
void display(struct LNode **p)
124
{
125
struct LNode *q;
126
q=*p;
127
printf("单链表显示:");
128
if (q==NULL) /*链表为空时*/
129
printf("链表为空!");
130
else if (q->next==NULL) /*链表只有一个结点时*/
131
printf("%c\n",q->data);
132
else { /*链表存在一个以上的结点时*/
133
while (q->next!=NULL) /*显示前面的结点*/
134
{
135
printf("%c→",q->data);q=q->next;
136
}
137
138
printf("%c",q->data); /*显示最后一个结点*/
139
}
140
141
printf("\n");
142
}
143
144
void main()
145
{
146
struct LNode *head;
147
setnull(&head);
148
insert(&head,'a',1);
149
insert(&head,'b',2);
150
insert(&head,'a',2);
151
insert(&head,'c',4);
152
insert(&head,'d',3);
153
insert(&head,'e',1);
154
display(&head);
155
printf("单链表长度=%d\n",length(&head));
156
printf("位置:%d 值:%c\n",3,get(&head,3));
157
printf("值:%c 位置:%d\n",'a',locate(&head,'a'));
158
printf("删除第1个结点:");
159
del(&head,1);
160
display(&head);
161
printf("删除第5个结点:");
162
del(&head,5);
163
display(&head);
164
printf("删除开头3个结点:");
165
del(&head,3);
166
del(&head,2);
167
del(&head,1);
168
display(&head);
169
}
170
171
/*
172
运行结果:
173
单链表显示:e→a→a→d→b→c
174
单链表长度=6
175
位置:3 值:a
176
值:a 位置:2
177
删除第1个结点:单链表显示:a→a→d→b→c
178
删除第5个结点:单链表显示:a→a→d→b
179
删除开头3个结点:单链表显示:b
180
*/
/*线性表链式存储-单链表的基本操作*/2
#include "stdafx.h"3
#include <stdio.h>4
#include <malloc.h>5
typedef char ElemType;6
struct LNode7
{8
ElemType data;9
struct LNode *next;10
};11

12
//***********************************************************置空表setnull()13
void setnull(struct LNode **p)14
{15
*p=NULL;16
}17

18
//************************************************************求长度length()19
int length(struct LNode **p)20
{21
int n=0;22
struct LNode *q=*p;23
while (q!=NULL)24
{25
n++;26
q=q->next;27
}28
return(n);29
}30

31
//*************************************************************取结点get()32
ElemType get(struct LNode **p,int i)33
{34
int j=1;35
struct LNode *q=*p;36
while (j<i && q!=NULL) /*查找第i个结点*/37
{38
q=q->next;j++;39
}40
if (q!=NULL) /*找到了第i个结点*/41
return(q->data);42
else43
{44
printf("位置参数不正确!\n");45
return NULL;46
}47
}48

49
//************************************************************按值查找locate()50
int locate(struct LNode **p,ElemType x)51
{52
int n=0;53
struct LNode *q=*p;54
while (q!=NULL && q->data!=x) /*查找data域为x的第一个结点*/55
{56
q=q->next;57
n++;58
}59
if (q==NULL) /*未找到data域等于x的结点*/60
return(-1);61
else /*找到data域等于x的结点*/62
return(n+1);63
}64

65
//**********************************************************插入结点insert()66
void insert(struct LNode **p,ElemType x,int i)67
{68
int j=1;69
struct LNode *s,*q;70
s=(struct LNode *)malloc(sizeof(struct LNode)); /*建立要插入的结点s*/71
s->data=x;72
q=*p;73
if (i==1) /*插入的结点作为头结点*/74
{75
s->next=q;76
*p=s;77
}78
else79
{80
while (j<i-1 && q->next!=NULL) /*查找第i-1个结点*/81
{82
q=q->next;j++;83
}84
if (j==i-1) /*找到了第i-1个结点,由q指向它*/85
{86
s->next=q->next; /*将结点s插入到q结点之后*/87
q->next=s;88
}89
else 90
printf("位置参数不正确!\n");91
}92
}93

94
//*********************************************************删除结点del()95
void del(struct LNode **p,int i)96
{97
int j=1;98
struct LNode *q=*p,*t;99
if (i==1) /*删除链表的头结点*/100
{101
t=q;102
*p=q->next;103
}104
else105
{106
while (j<i-1 && q->next!=NULL) /*查找第i-1个结点*/107
{108
q=q->next;j++;109
}110

111
if (q->next!=NULL && j==i-1) /*找到第i-1个结点,由q指向它*/112
{113
t=q->next; /*t指向要删除的结点*/114
q->next=t->next; /*将q之后的结点删除*/115
}116
else printf("位置参数不正确!\n");117
}118
if (t!=NULL) /*在t不为空时释放该结点*/119
free(t);120
}121

122
//********************************************************显示链表display()123
void display(struct LNode **p)124
{125
struct LNode *q;126
q=*p;127
printf("单链表显示:");128
if (q==NULL) /*链表为空时*/129
printf("链表为空!");130
else if (q->next==NULL) /*链表只有一个结点时*/131
printf("%c\n",q->data);132
else { /*链表存在一个以上的结点时*/133
while (q->next!=NULL) /*显示前面的结点*/134
{135
printf("%c→",q->data);q=q->next;136
}137

138
printf("%c",q->data); /*显示最后一个结点*/139
}140

141
printf("\n");142
}143

144
void main()145
{146
struct LNode *head;147
setnull(&head);148
insert(&head,'a',1);149
insert(&head,'b',2);150
insert(&head,'a',2);151
insert(&head,'c',4);152
insert(&head,'d',3);153
insert(&head,'e',1);154
display(&head);155
printf("单链表长度=%d\n",length(&head));156
printf("位置:%d 值:%c\n",3,get(&head,3));157
printf("值:%c 位置:%d\n",'a',locate(&head,'a'));158
printf("删除第1个结点:");159
del(&head,1);160
display(&head);161
printf("删除第5个结点:");162
del(&head,5);163
display(&head);164
printf("删除开头3个结点:");165
del(&head,3);166
del(&head,2);167
del(&head,1);168
display(&head);169
}170

171
/*172
运行结果:173
单链表显示:e→a→a→d→b→c174
单链表长度=6175
位置:3 值:a176
值:a 位置:2177
删除第1个结点:单链表显示:a→a→d→b→c178
删除第5个结点:单链表显示:a→a→d→b179
删除开头3个结点:单链表显示:b180
*/


浙公网安备 33010602011771号