链表基本操作的实现

来源:互联网 发布:matlab gui界面编程 编辑:程序博客网 时间:2024/05/16 07:32
#include<stdio.h>
#include<stdlib.h>
#include <malloc.h>
#define LEN sizeof(struct student)  
typedef struct student{
long num;
char name[128];
struct student *next;
}Stu;       //定义节点
int n=0;    //全局变量,用来记录链表的长度
int item;  //定义菜单选项
int number; //要删除学生的学号
struct student *Create(){
Stu *head,*p1,*p2;
head=(struct student *)malloc(LEN);//开辟一个LEN大小的空间,并让p1,p2指向他
if(head==NULL){
printf("内存申请失败");
}
else{
head->next=NULL;
}
p1=p2=(struct student *)malloc(LEN);
if(p1==NULL){
printf("内存申请失败");
}
else{
printf("请输入学生的学号和姓名:");
scanf("%ld %s",&p1->num,&p1->name);
}


while(p1->num!=0)
{
n=n+1;
if(NULL==head->next)
{
head->next=p1;
}
else
{
p2->next=p1;
}
p2=p1;
p1=(struct student *)malloc(LEN);
printf("请输入学生的学号和姓名:");
scanf("%ld %s",&p1->num,p1->name);
}
//将尾节点的指针置为NULL 
p2->next=NULL;
return head;
}
void Print(struct student *head){
struct student *p;
p=head;
if(p==NULL){
printf("链表为空!");
}
else{
while(p!=NULL)
{
printf("%ld %s\n",p->num,p->name);
//指针指向下一个节点 
p=p->next;
}
}
}
struct student *Delete(struct student * head,int num){
struct student *p1,*p2;
p1=head;
if(p1==NULL){
printf("链表为空!");
}
else{
while(num!=p1->num&&p1->next!=NULL){
p2=p1;
p1=p1->next;
}
if(p1->num==num){
p2->next=p1->next;
free(p1);
}
else
printf("链表中没有要删除的元素\n");
}
return head;
}
void Search(struct student * head,int num){
struct student *p;
p=head;
if(p==NULL){
printf("链表为空!");
}
else{
while(num!=p->num&&p->next!=NULL){
p=p->next;
}
if(p->num==num){
printf("%d,%s",p->num,p->name);
}
else
printf("链表中没有要查找的元素\n");
}
}
struct student  *Insert(struct student *head,struct student *newstu){
struct student *p1,*p2,*p3;
p3=newstu;
p1=head;
if(p1==NULL){
p1->next=p3;
p3->next=NULL;
}
else{
while((p3->num > p1->num)&&(p1->next!=NULL)){
p2=p1;
            p1=p1->next;
}
if(p3->num <= p1->num)
        {
p2->next=p3;
p3->next=p1;
        }
        else
        {
p1->next=p3;
p3->next=NULL;
}
}
return head;
}


void Show(){
printf("             欢迎光临学生管理系统(链表版带头结点):\n");
printf("************************************************************\n");
printf("             显示主菜单:0\n");
printf("             学生查询:1\n");
printf("             学生创建:2\n");
printf("             学生删除:3\n");
printf("             学生信息修改:4\n");
printf("             学生输出:5\n");
printf("             退出:6\n");
printf("             添加学生:7\n");
}
void main(){
Stu *newstu;
Stu *head;
Show();
head=(struct student *)malloc(LEN);
head->next=NULL;
while(1){
printf("请输入菜单项:");
scanf("%d",&item);
switch(item){
case 0:
Show();
break;
case 1:
printf("请输入要查询学生的学号:");
scanf("%d",&number);
Search(head,number);
break;
case 2:
head=Create();
break;
case 3:
printf("请输入要删除学生的学号:");
scanf("%d",&number);
head=Delete(head,number);
Print(head->next);
break;
case 4:
printf("暂无此功能");
break;
case 5:
Print(head->next);
break;
case 6:
exit(0);
break;
case 7:
newstu=(struct student *)malloc(LEN);
printf("请输入要插入的学生的学号和姓名\n");
scanf("%d %s",&newstu->num,newstu->name);
head=Insert(head,newstu);
break;
default:
break;
}
}
}
0 0