链表的创建,插入,删除,查询,更改
链表创建
Node * CreateList ( )
{
Node * head= NULL ;
Node * prev, * pCurrent;
prev= pCurrent= ( Node* ) malloc ( sizeof ( Node) ) ;
if ( prev== NULL ) {
puts ( "fail" ) ;
exit ( - 1 ) ;
}
printf ( "请输入第%d个学生的姓名:" , count+ 1 ) ;
scanf ( "%s" , pCurrent-> stu_name) ;
printf ( "请输入第%d个学生的分数:" , count+ 1 ) ;
scanf ( "%f" , & ( pCurrent-> stu_score) ) ;
while ( strcmp ( pCurrent-> stu_name, "结束" ) )
{
count++ ;
if ( count== 1 ) {
head= pCurrent;
}
else {
prev-> next= pCurrent;
}
prev= pCurrent;
pCurrent= ( Node* ) malloc ( sizeof ( Node) ) ;
printf ( "请输入第%d个学生的姓名:" , count+ 1 ) ;
scanf ( "%s" , pCurrent-> stu_name) ;
printf ( "请输入第%d个学生的分数:" , count+ 1 ) ;
scanf ( "%f" , & ( pCurrent-> stu_score) ) ;
}
prev-> next= NULL ;
printf ( "\nlist Create success\n" ) ;
return head;
}
链表的删除
Node * DeleteList ( Node * head)
{
Node * prev, * pCurrent;
prev= pCurrent= ( Node* ) malloc ( sizeof ( Node) ) ;
char * name;
name= ( char * ) malloc ( 20 * sizeof ( char ) ) ;
printf ( "请输入你要删除的名字:" ) ;
scanf ( "%s" , name) ;
if ( head== NULL )
{
puts ( "This is a NULL list\n" ) ;
return NULL ;
}
else {
pCurrent= head;
while ( strcmp ( pCurrent-> stu_name, name) && pCurrent-> next)
{
prev= pCurrent;
pCurrent= pCurrent-> next;
}
if ( strcmp ( pCurrent-> stu_name, name) == 0 )
{
if ( pCurrent== head) {
head= pCurrent-> next;
free ( pCurrent) ;
}
else {
prev-> next= pCurrent-> next;
}
}
else {
puts ( "NO Found need delete node\n" ) ;
}
printf ( "\nName:%s delete success\n" , name) ;
count= count- 1 ;
}
free ( name) ;
return head;
}
链表的插入
中间插
Node * InserList ( Node * head)
{
Node * target;
Node * prev, * pCurrent;
target= ( Node * ) malloc ( sizeof ( Node) ) ;
pCurrent= head;
printf ( "请输入你要插入的姓名:" ) ;
scanf ( "%s" , target-> stu_name) ;
printf ( "请输入你要插入的分数:" ) ;
scanf ( "%f" , & ( target-> stu_score) ) ;
if ( head== NULL )
{
head= target;
target-> next= NULL ;
}
else {
while ( ( target-> stu_score > pCurrent-> stu_score) && pCurrent-> next)
{
prev= pCurrent;
pCurrent= pCurrent-> next;
}
if ( target-> stu_score < pCurrent-> stu_score)
{
if ( pCurrent== head) {
head= target;
}
else {
prev-> next= target;
}
target-> next= pCurrent;
}
else {
pCurrent-> next= target;
target-> next= NULL ;
}
}
printf ( "\nName:%s inser success\n" , target-> stu_name) ;
count= count+ 1 ;
return head;
}
尾插法
Node * InserList ( Node * head)
{
Node * pCurrent;
Node * target;
target= ( Node * ) malloc ( sizeof ( Node) ) ;
pCurrent= head;
printf ( "请输入你要插入的姓名:" ) ;
scanf ( "%s" , target-> stu_name) ;
printf ( "请输入你要插入的分数:" ) ;
scanf ( "%f" , & ( target-> stu_score) ) ;
if ( head== NULL )
{
head= target;
target-> next= NULL ;
}
else {
while ( pCurrent-> next)
{
pCurrent= pCurrent-> next;
}
pCurrent-> next= target;
target-> next= NULL ;
}
count= count+ 1 ;
return head;
}
头插法
Node * InserList ( Node * head)
{
Node * pCurrent;
Node * target;
target= ( Node * ) malloc ( sizeof ( Node) ) ;
pCurrent= head;
printf ( "请输入你要插入的姓名:" ) ;
scanf ( "%s" , target-> stu_name) ;
printf ( "请输入你要插入的分数:" ) ;
scanf ( "%f" , & ( target-> stu_score) ) ;
if ( head== NULL )
{
head= target;
target-> next= NULL ;
}
else {
head= target;
target-> next= pCurrent;
pCurrent= head;
}
count= count+ 1 ;
return head;
}
更改链表
Node * ChangeList ( Node * head)
{
Node * pCurrent, * target;
target= ( Node * ) malloc ( sizeof ( Node) ) ;
pCurrent= head;
printf ( "请输入你要更改的姓名:" ) ;
scanf ( "%s" , target-> stu_name) ;
if ( head== NULL )
{
puts ( "error\n" ) ;
return NULL ;
}
else {
while ( strcmp ( target-> stu_name, pCurrent-> stu_name) && pCurrent-> next)
{
pCurrent= pCurrent-> next;
}
if ( strcmp ( target-> stu_name, pCurrent-> stu_name) == 0 )
{
printf ( "你要更改的分数是:" ) ;
scanf ( "%f" , & ( pCurrent-> stu_score) ) ;
}
else {
printf ( "查无此人,请重新输入!\n" ) ;
}
}
return head;
}
打印链表
void PrintList ( Node * head)
{
Node * temphead= head;
if ( head)
{
puts ( "=======================================" ) ;
printf ( "一共打印%d个学生的信息\n" , count) ;
while ( temphead)
{
printf ( "%s\t%.1f\n" , temphead-> stu_name, temphead-> stu_score) ;
temphead= temphead-> next;
}
puts ( "=======================================" ) ;
}
else {
puts ( "This is a NULL list\n" ) ;
}
}
释放链表
void FreeLinkList ( Node* head)
{
Node * pCurrent;
pCurrent= head;
while ( pCurrent)
{
head= pCurrent-> next;
free ( pCurrent) ;
pCurrent= head;
}
}
完整代码
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <windows.h>
typedef struct student {
char stu_name[ 20 ] ;
float stu_score;
struct student * next;
} Node;
int count= 0 ;
Node * CreateList ( ) ;
void PrintList ( Node * head) ;
Node * DeleteList ( Node * head) ;
Node * InserList ( Node * head) ;
void FreeLinkList ( Node* head) ;
Node * ChangeList ( Node * head) ;
Node * CreateList ( )
{
Node * head= NULL ;
Node * prev, * pCurrent;
prev= pCurrent= ( Node* ) malloc ( sizeof ( Node) ) ;
if ( prev== NULL ) {
puts ( "fail" ) ;
exit ( - 1 ) ;
}
printf ( "请输入第%d个学生的姓名:" , count+ 1 ) ;
scanf ( "%s" , pCurrent-> stu_name) ;
printf ( "请输入第%d个学生的分数:" , count+ 1 ) ;
scanf ( "%f" , & ( pCurrent-> stu_score) ) ;
while ( strcmp ( pCurrent-> stu_name, "结束" ) )
{
count++ ;
if ( count== 1 ) {
head= pCurrent;
}
else {
prev-> next= pCurrent;
}
prev= pCurrent;
pCurrent= ( Node* ) malloc ( sizeof ( Node) ) ;
printf ( "请输入第%d个学生的姓名:" , count+ 1 ) ;
scanf ( "%s" , pCurrent-> stu_name) ;
printf ( "请输入第%d个学生的分数:" , count+ 1 ) ;
scanf ( "%f" , & ( pCurrent-> stu_score) ) ;
}
prev-> next= NULL ;
printf ( "\nlist Create success\n" ) ;
return head;
}
Node * DeleteList ( Node * head)
{
Node * prev, * pCurrent;
prev= pCurrent= ( Node* ) malloc ( sizeof ( Node) ) ;
char * name;
name= ( char * ) malloc ( 20 * sizeof ( char ) ) ;
printf ( "请输入你要删除的名字:" ) ;
scanf ( "%s" , name) ;
if ( head== NULL )
{
puts ( "This is a NULL list\n" ) ;
return NULL ;
}
else {
pCurrent= head;
while ( strcmp ( pCurrent-> stu_name, name) && pCurrent-> next)
{
prev= pCurrent;
pCurrent= pCurrent-> next;
}
if ( strcmp ( pCurrent-> stu_name, name) == 0 )
{
if ( pCurrent== head) {
head= pCurrent-> next;
free ( pCurrent) ;
}
else {
prev-> next= pCurrent-> next;
}
}
else {
puts ( "NO Found need delete node\n" ) ;
}
printf ( "\nName:%s delete success\n" , name) ;
count= count- 1 ;
}
free ( name) ;
return head;
}
Node * InserList ( Node * head)
{
Node * target;
Node * prev, * pCurrent;
target= ( Node * ) malloc ( sizeof ( Node) ) ;
pCurrent= head;
printf ( "请输入你要插入的姓名:" ) ;
scanf ( "%s" , target-> stu_name) ;
printf ( "请输入你要插入的分数:" ) ;
scanf ( "%f" , & ( target-> stu_score) ) ;
if ( head== NULL )
{
head= target;
target-> next= NULL ;
}
else {
while ( ( target-> stu_score > pCurrent-> stu_score) && pCurrent-> next)
{
prev= pCurrent;
pCurrent= pCurrent-> next;
}
if ( target-> stu_score < pCurrent-> stu_score)
{
if ( pCurrent== head) {
head= target;
}
else {
prev-> next= target;
}
target-> next= pCurrent;
}
else {
pCurrent-> next= target;
target-> next= NULL ;
}
}
printf ( "\nName:%s inser success\n" , target-> stu_name) ;
count= count+ 1 ;
return head;
}
Node * ChangeList ( Node * head)
{
Node * pCurrent, * target;
target= ( Node * ) malloc ( sizeof ( Node) ) ;
pCurrent= head;
printf ( "请输入你要更改的姓名:" ) ;
scanf ( "%s" , target-> stu_name) ;
if ( head== NULL )
{
puts ( "error\n" ) ;
return NULL ;
}
else {
while ( strcmp ( target-> stu_name, pCurrent-> stu_name) && pCurrent-> next)
{
pCurrent= pCurrent-> next;
}
if ( strcmp ( target-> stu_name, pCurrent-> stu_name) == 0 )
{
printf ( "你要更改的分数是:" ) ;
scanf ( "%f" , & ( pCurrent-> stu_score) ) ;
}
else {
printf ( "查无此人,请重新输入!\n" ) ;
}
}
return head;
}
void PrintList ( Node * head)
{
Node * temphead= head;
if ( head)
{
puts ( "=======================================" ) ;
printf ( "一共打印%d个学生的信息\n" , count) ;
while ( temphead)
{
printf ( "%s\t%.1f\n" , temphead-> stu_name, temphead-> stu_score) ;
temphead= temphead-> next;
}
puts ( "=======================================" ) ;
}
else {
puts ( "This is a NULL list\n" ) ;
}
}
void FreeLinkList ( Node* head)
{
Node * pCurrent;
pCurrent= head;
while ( pCurrent)
{
head= pCurrent-> next;
free ( pCurrent) ;
pCurrent= head;
}
}
int main ( )
{
Node * stu;
int num;
do
{
printf ( "1:创建链表\t2:删除节点\n3:插入节点\t4:更改链表\n5:退出\n" ) ;
printf ( "请输入:" ) ;
scanf ( "%d" , & num) ;
switch ( num)
{
case 1 :
stu= CreateList ( ) ;
PrintList ( stu) ;
break ;
case 2 :
stu= DeleteList ( stu) ;
PrintList ( stu) ;
break ;
case 3 :
stu= InserList ( stu) ;
PrintList ( stu) ;
break ;
case 4 :
stu= ChangeList ( stu) ;
PrintList ( stu) ;
break ;
case 5 :
break ;
}
} while ( num!= 5 ) ;
FreeLinkList ( stu) ;
system ( "pause" ) ;
return 0 ;
}