链表的基本操作包括建立链表、链表的插入、删除、输出和查找等。
#include
<
stdlib.h
>
#include
<
stdio.h
>
#include
<
conio.h
>
#include
<
ctype.h
>
#define
NULL 0
#define
LEN sizeof(struct student)
extern
unsigned _floatconvert;
/*
防止floating point formats not linked 错误发生
*/
#pragma extref _floatconvert
typedef
struct
student
{
long
num;
float
score;
struct
student
*
next;
}STU;
int
n;
STU
*
CreateEnd()
{
STU
*
p1,
*
p2,
*
head;
n
=
0
;head
=
NULL;
p1
=
(STU
*
)malloc(LEN);
/*
创建第一个结点
*/
scanf(
"
%ld,%f
"
,
&
p1
->
num,
&
p1
->
score);
p1
->
next
=
NULL;
while
(p1
->
num
!=
0
)
/*
将结点加入链表
*/
{
++
n;
if
(n
==
1
)
/*
是第一个结点,作表头
*/
head
=
p1;
else
/*
不是第一个结点,作表尾
*/
p2
->
next
=
p1;
p2
=
p1;
p1
=
(STU
*
)malloc(LEN);
/*
开辟下一个结点
*/
scanf(
"
%ld,%f
"
,
&
p1
->
num,
&
p1
->
score);
p1
->
next
=
NULL;
}
free(p1);
/*
释放最后一个结点所占的内存
*/
return
(head);
/*
返回链表的头指针
*/
}
STU
*
CreateStart()
{
STU
*
head,
*
p;
n
=
0
;
head
=
NULL;
p
=
(STU
*
)malloc(LEN);
scanf(
"
%ld,%f
"
,
&
p
->
num,
&
p
->
score);
p
->
next
=
head;
while
(p
->
num
!=
0
)
{
++
n;
head
=
p;
p
=
(STU
*
)malloc(LEN);
scanf(
"
%ld,%f
"
,
&
p
->
num,
&
p
->
score);
p
->
next
=
head;
}
free(p);
return
(head);
}
void
print(STU
*
head)
{
STU
*
p;
printf(
"
\nNow,These %d nodes are :\n
"
,n);
p
=
head;
do
{
printf(
"
%ld\t%5.1f\n
"
,p
->
num,p
->
score);
p
=
p
->
next;
}
while
(p
!=
NULL);
}
STU
*
insert(STU
*
head,STU
*
stud)
{
STU
*
p0,
*
p1,
*
p2;
p1
=
head;
/*
p1指向第一个结点
*/
p0
=
stud;
/*
p0指向要插入的结点
*/
if
(head
==
NULL)
/*
原来是空表
*/
{head
=
p0;p0
->
next
=
NULL;}
/*
使p0指向的结点作为链表第一个结点
*/
else
{
while
((p0
->
num
>
p1
->
num)
&&
(p1
!=
NULL))
{p2
=
p1;p1
=
p1
->
next;}
/*
找插入点
*/
if
(head
==
p1)
/*
作为表头
*/
{p0
->
next
=
head;head
=
p0;}
else
/*
插到p2指向的结点之后
*/
{p2
->
next
=
p0;p0
->
next
=
p1;}
}
++
n;
return
head;
}
STU
*
del(STU
*
head,
long
num)
{
STU
*
p1,
*
p2;
if
(head
==
NULL)
{printf(
"
\nList null!\n
"
);
goto
end;}
/*
链表为空
*/
p1
=
head;
/*
从头结点开始查找
*/
while
(num
!=
p1
->
num
&&
p1
!=
NULL)
/*
p1指向的不是所要找的结点,并且没有到表尾
*/
{p2
=
p1;p1
=
p1
->
next;}
/*
后移一个结点
*/
if
(num
==
p1
->
num)
/*
找到需要删除的结点
*/
{
if
(p1
==
head)
/*
p1指向的是头结点
*/
head
=
p1
->
next;
/*
第二个结点成为新的头结点
*/
else
p2
->
next
=
p1
->
next;
/*
后继结点的地址赋给前一结点
*/
printf(
"
delete:%ld\n
"
,num);
free(p1);
n
--
;
}
else
printf(
"
%ld not been found!\n
"
,num);
end:
return
head;
}
STU
*
find(STU
*
head,
long
num)
{
STU
*
p1,
*
p2;
if
(head
==
NULL)
{printf(
"
\nlist null!\n
"
);
goto
end;}
p1
=
head;
while
(num
!=
p1
->
num
&&
p1
!=
NULL)
{p2
=
p1;p1
=
p1
->
next;}
if
(p1
!=
NULL)
printf(
"
find:%ld %5.1f\n
"
,num,p1
->
score);
else
printf(
"
%ld not been found!\n
"
,num);
end:
return
head;
}
void
main()
{
STU
*
head,
*
stu;
long
find_num,del_num;
char
letter;
head
=
CreateEnd();
//
head=CreateStart();
print(head);
do
{
printf(
"
A Insert student information\n
"
);
printf(
"
B Find student information\n
"
);
printf(
"
C Delete student information\n
"
);
printf(
"
Q Quit\n
"
);
printf(
"
Please select: \n
"
);
letter
=
getch();
letter
=
toupper(letter);
switch
(letter)
{
case
'
A
'
:
printf(
"
Please input the information of student:\n
"
);
stu
=
(STU
*
)malloc(LEN);
scanf(
"
%d,%f
"
,
&
stu
->
num,
&
stu
->
score);
head
=
insert(head,stu);
print(head);
break
;
case
'
B
'
:
printf(
"
Please input the find number:\n
"
);
scanf(
"
%ld
"
,
&
find_num);
head
=
find(head,find_num);
print(head);
break
;
case
'
C
'
:
printf(
"
Please input the delete number:\n
"
);
scanf(
"
%ld
"
,
&
del_num);
head
=
del(head,del_num);
print(head);
break
;
}
}
while
(letter
!=
'
Q
'
);
getch();
}