实现单链表的方法
/**
*File:Main.c
*Descript:单链表操作
*Author:Red_angelX
*/
#include<stdio.h>
#defineMAXNUM100
/**
*学生结构体链表
**/
typedefstructstudent
{
intnumber;
charname[10];
intscore;
//新增链表指针
structstudent*next;
}student;
/**
*创建链表
**/
student*create()
{
intn,i;
student*head,*preview,*current;/*head表头preview前一个current当前*/
if((head=(student*)(malloc(sizeof(student))))==NULL)
{
printf("无法分配内存");
exit(0);
}
head->next=NULL;/*表头置空初始化*/
preview=head;/*preview指向表头*/
printf("请输入学生数:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
if((current=(student*)(malloc(sizeof(student))))==NULL)
{
printf("无法分配内存");
exit(0);
}
preview->next=current;/*把前一个节点指向当前节点,连接所有链表*/
printf("请输入学生%d的数据: ",i+1);
printf("学号:");
scanf("%d",¤t->number);
printf("姓名:");
scanf("%s",¤t->name);
printf("成绩:");
scanf("%d",¤t->score);
current->next=NULL;
preview=current;
}
return(head);
}
/**
*查找节点
**/
voidfind(student*stu)
{
intx;
student*head;
printf("请输入要查找学生的学号:");
scanf("%d",&x);
head=stu;
while(head&&head->number!=x)
{
head=head->next;/*指向下条记录*/
}
if(head)
{
printf("学号姓名成绩 ");
printf("%6d",head->number);
printf("%s",head->name);
printf("%6d ",head->score);
}
else
{
printf("没找到! ");
}
}
/**
*插入节点
*就当往最后插入了
**/
voidinsert(student*stu)
{
inti,n;
student*head,*current;
printf("请插入学生数:");
scanf("%d",&n);
head=stu;
//把head移动到最后
while(head->next)
head=head->next;
for(i=0;i<n;i++)
{
if((current=(student*)(malloc(sizeof(student))))==NULL)
{
printf("无法分配内存");
exit(0);
}
head->next=current;
printf("请输入学生%d的数据: ",i+1);
printf("学号:");
scanf("%d",¤t->number);
printf("姓名:");
scanf("%s",¤t->name);
printf("成绩:");
scanf("%d",¤t->score);
current->next=NULL;
head=current;
}
}
/**
*删除节点
**/
voiddelete(student*stu)
{
intx;
student*head,*temp;
printf("输入要删除成绩学生的学号:");
scanf("%d",&x);
head=stu;
while(head->next&&head->next->number!=x)
{
head=head->next;/*指向下条记录*/
}
if(head->next)
{
temp=head->next;
head->next=head->next->next;
free(temp);
printf("已删除! ");
}
else
{
printf("无此学生成绩,无法删除! ");
}
}
/**
*主函数,链表操作
*
*Author:Red_angelX
*/
voidmain()
{
student*stu;
intk;
while(1)
{
//clrscr();
printf("****************************** ");
printf("|学生成绩管理| ");
printf("|*******************************| ");
printf("|1登记成绩| ");
printf("|2查询成绩| ");
printf("|3插入成绩| ");
printf("|4删除成绩| ");
printf("|0退出程序| ");
printf("|*******************************| ");
printf("|CodeByRed_angelX| ");
printf("******************************* ");
printf(" ");
printf("请输入选择的功能号:");
scanf("%d",&k);
switch(k)
{
case1:
stu=create();
break;
case2:
find(stu);
break;
case3:
insert(stu);
break;
case4:
delete(stu);
break;
case0:exit(0);
default:printf("选择错误!");
}
}
}
*File:Main.c
*Descript:单链表操作
*Author:Red_angelX
*/
#include<stdio.h>
#defineMAXNUM100
/**
*学生结构体链表
**/
typedefstructstudent
{
intnumber;
charname[10];
intscore;
//新增链表指针
structstudent*next;
}student;
/**
*创建链表
**/
student*create()
{
intn,i;
student*head,*preview,*current;/*head表头preview前一个current当前*/
if((head=(student*)(malloc(sizeof(student))))==NULL)
{
printf("无法分配内存");
exit(0);
}
head->next=NULL;/*表头置空初始化*/
preview=head;/*preview指向表头*/
printf("请输入学生数:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
if((current=(student*)(malloc(sizeof(student))))==NULL)
{
printf("无法分配内存");
exit(0);
}
preview->next=current;/*把前一个节点指向当前节点,连接所有链表*/
printf("请输入学生%d的数据: ",i+1);
printf("学号:");
scanf("%d",¤t->number);
printf("姓名:");
scanf("%s",¤t->name);
printf("成绩:");
scanf("%d",¤t->score);
current->next=NULL;
preview=current;
}
return(head);
}
/**
*查找节点
**/
voidfind(student*stu)
{
intx;
student*head;
printf("请输入要查找学生的学号:");
scanf("%d",&x);
head=stu;
while(head&&head->number!=x)
{
head=head->next;/*指向下条记录*/
}
if(head)
{
printf("学号姓名成绩 ");
printf("%6d",head->number);
printf("%s",head->name);
printf("%6d ",head->score);
}
else
{
printf("没找到! ");
}
}
/**
*插入节点
*就当往最后插入了
**/
voidinsert(student*stu)
{
inti,n;
student*head,*current;
printf("请插入学生数:");
scanf("%d",&n);
head=stu;
//把head移动到最后
while(head->next)
head=head->next;
for(i=0;i<n;i++)
{
if((current=(student*)(malloc(sizeof(student))))==NULL)
{
printf("无法分配内存");
exit(0);
}
head->next=current;
printf("请输入学生%d的数据: ",i+1);
printf("学号:");
scanf("%d",¤t->number);
printf("姓名:");
scanf("%s",¤t->name);
printf("成绩:");
scanf("%d",¤t->score);
current->next=NULL;
head=current;
}
}
/**
*删除节点
**/
voiddelete(student*stu)
{
intx;
student*head,*temp;
printf("输入要删除成绩学生的学号:");
scanf("%d",&x);
head=stu;
while(head->next&&head->next->number!=x)
{
head=head->next;/*指向下条记录*/
}
if(head->next)
{
temp=head->next;
head->next=head->next->next;
free(temp);
printf("已删除! ");
}
else
{
printf("无此学生成绩,无法删除! ");
}
}
/**
*主函数,链表操作
*
*Author:Red_angelX
*/
voidmain()
{
student*stu;
intk;
while(1)
{
//clrscr();
printf("****************************** ");
printf("|学生成绩管理| ");
printf("|*******************************| ");
printf("|1登记成绩| ");
printf("|2查询成绩| ");
printf("|3插入成绩| ");
printf("|4删除成绩| ");
printf("|0退出程序| ");
printf("|*******************************| ");
printf("|CodeByRed_angelX| ");
printf("******************************* ");
printf(" ");
printf("请输入选择的功能号:");
scanf("%d",&k);
switch(k)
{
case1:
stu=create();
break;
case2:
find(stu);
break;
case3:
insert(stu);
break;
case4:
delete(stu);
break;
case0:exit(0);
default:printf("选择错误!");
}
}
}
本文地址:http://www.45fan.com/dnjc/73205.html