#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct student
{
long id;
char name[32];
char sex[8];
int age;
float math;
struct student *next;//指向下一个节点的指针
}stu,*pstu;
stu *head;//全局变量在main函数执行之前初始化,指针默认初始化为NULL
//函数声明
stu *frontadd(stu *head);
stu *rearadd(stu *head);
stu *squadd(stu *head);
stu *delete(stu *head);
void print_list(stu *head);
void free_list(stu *head);
int main()
{
int sel=0;
//先给头节点申请内存空间,但不存值
head=(stu *)malloc(sizeof(stu));
if(NULL==head)
{
printf("head malloc failed\n");
return -1;
}
head->next=NULL;//next指针必须赋空
while(1)
{
printf("\t1:前插法添加节点\n\t2:后插法添加节点\n\t3:顺序添加节点\n\t4:删除节点\n\t5:查看节点\n\t6:退出\n");
printf("请输入选项:");
scanf("%d",&sel);
getchar();//取出缓冲区中的'\n'
switch(sel)
{
case 1:
head=frontadd(head);
break;
case 2:
head=rearadd(head);
break;
case 3:
head=squadd(head);
break;
case 4:
head=delete(head);
break;
case 5:
print_list(head);
break;
case 6:
//释放链表内存
free_list(head);
exit(0);
default:
printf("reinput 1-6\n");
}
}
return 0;
}
stu *frontadd(stu *head)
{
if(NULL==head)
{
printf("head null\n");
return NULL;
}
while(1)
{
stu *s=NULL;
s=(stu *)malloc(sizeof(stu));
if(NULL==s)
{
printf("s malloc failed\n");
return NULL;
}
s->next=NULL;
printf("请输入学号:\n");
scanf("%ld",&s->id);
printf("请输入姓名:\n");
scanf("%s",s->name);
printf("请输入性别:\n");
scanf("%s",s->sex);
printf("请输入年龄:\n");
scanf("%d",&s->age);
printf("请输入数学成绩:\n");
scanf("%f",&s->math);
s->next=head->next;
head->next=s;
char judge;
while(getchar()!='\n');//清空缓冲区
printf("是否继续添加(y/n):");
judge=getchar();
if(judge=='y')
{
continue;
}
else
{
break;
}
}
return head;
}
stu *rearadd(stu *head)
{
if(NULL==head)
{
printf("head null\n");
return NULL;
}
while(1)
{
stu *s=NULL;
s=(stu *)malloc(sizeof(stu));
if(NULL==s)
{
printf("s malloc failed\n");
return NULL;
}
s->next=NULL;
printf("请输入学号:\n");
scanf("%ld",&s->id);
printf("请输入姓名:\n");
scanf("%s",s->name);
printf("请输入性别:\n");
scanf("%s",s->sex);
printf("请输入年龄:\n");
scanf("%d",&s->age);
printf("请输入数学成绩:\n");
scanf("%f",&s->math);
//添加节点
stu *p=NULL;
stu *q=NULL;
q=p=head;
while(p!=NULL)
{
q=p;
p=p->next;
}
q->next=s;
char judge;
while(getchar()!='\n');//清空缓冲区
printf("是否继续添加(y/n):");
judge=getchar();
if(judge=='y')
{
continue;
}
else
{
break;
}
}
return head;
}
stu *squadd(stu *head)
{
if(NULL==head)
{
printf("head null\n");
return NULL;
}
while(1)
{
stu *s=NULL;
s=(stu *)malloc(sizeof(stu));
if(NULL==s)
{
printf("s malloc failed\n");
return NULL;
}
s->next=NULL;
printf("请输入学号(大于0):\n");
scanf("%ld",&s->id);
printf("请输入姓名:\n");
scanf("%s",s->name);
printf("请输入性别:\n");
scanf("%s",s->sex);
printf("请输入年龄:\n");
scanf("%d",&s->age);
printf("请输入数学成绩:\n");
scanf("%f",&s->math);
//添加节点
stu *p=NULL;
stu *q=NULL;
q=p=head;
head->id=0;
while((p!=NULL)&&(s->id>p->id))
{
q=p;
p=p->next;
}
q->next=s;
s->next=p;
char judge;
while(getchar()!='\n');//清空缓冲区
printf("是否继续添加(y/n):");
judge=getchar();
if(judge=='y')
{
continue;
}
else
{
break;
}
}
return head;
}
stu *delete(stu *head)
{
if(NULL==head)
{
printf("delete head null\n");
return NULL;
}
while(1)
{
if(NULL==head->next)
{
printf("valid list empyt\n");
return head;
}
long x=0;
printf("请输入删除的数据:\n");
scanf("%ld",&x);
stu *p=NULL;
stu *q=NULL;
q=p=head;
//q=p=head->next;
while((p!=NULL)&&(p->id!=x))
{
q=p;
p=p->next;
}
if(NULL==p)
{
printf("this %ld not in list\n",x);
return head;
}
else
{
q->next=p->next;
free(p);
p=NULL;
}
char judge;
printf("是否继续删除(y/n):");
while(getchar()!='\n');
judge=getchar();
if(judge=='y')
{
continue;
}
else
{
break;
}
}
return head;
}
void print_list(stu *head)
{
if(NULL==head)
{
printf("head null\n");
return;
}
if(NULL==head->next)
{
printf("valid list empty\n");
return;
}
stu *p=NULL;
p=head->next;
printf("学号\t姓名\t性别\t年龄\t数学\n");
while(p!=NULL)
{
printf("%ld\t%s\t%s\t%d\t%.2f\n",p->id,p->name,p->sex,p->age,p->math);
p=p->next;
}
return;
}
void free_list(stu *head)
{
if(NULL==head)
{
printf("head null\n");
return;
}
stu *p=NULL;
stu *q=NULL;
p=head;
while(p!=NULL)
{
q=p;
p=p->next;
free(q);
q=NULL;
}
return;
}
#include <string.h>
#include <stdlib.h>
typedef struct student
{
long id;
char name[32];
char sex[8];
int age;
float math;
struct student *next;//指向下一个节点的指针
}stu,*pstu;
stu *head;//全局变量在main函数执行之前初始化,指针默认初始化为NULL
//函数声明
stu *frontadd(stu *head);
stu *rearadd(stu *head);
stu *squadd(stu *head);
stu *delete(stu *head);
void print_list(stu *head);
void free_list(stu *head);
int main()
{
int sel=0;
//先给头节点申请内存空间,但不存值
head=(stu *)malloc(sizeof(stu));
if(NULL==head)
{
printf("head malloc failed\n");
return -1;
}
head->next=NULL;//next指针必须赋空
while(1)
{
printf("\t1:前插法添加节点\n\t2:后插法添加节点\n\t3:顺序添加节点\n\t4:删除节点\n\t5:查看节点\n\t6:退出\n");
printf("请输入选项:");
scanf("%d",&sel);
getchar();//取出缓冲区中的'\n'
switch(sel)
{
case 1:
head=frontadd(head);
break;
case 2:
head=rearadd(head);
break;
case 3:
head=squadd(head);
break;
case 4:
head=delete(head);
break;
case 5:
print_list(head);
break;
case 6:
//释放链表内存
free_list(head);
exit(0);
default:
printf("reinput 1-6\n");
}
}
return 0;
}
stu *frontadd(stu *head)
{
if(NULL==head)
{
printf("head null\n");
return NULL;
}
while(1)
{
stu *s=NULL;
s=(stu *)malloc(sizeof(stu));
if(NULL==s)
{
printf("s malloc failed\n");
return NULL;
}
s->next=NULL;
printf("请输入学号:\n");
scanf("%ld",&s->id);
printf("请输入姓名:\n");
scanf("%s",s->name);
printf("请输入性别:\n");
scanf("%s",s->sex);
printf("请输入年龄:\n");
scanf("%d",&s->age);
printf("请输入数学成绩:\n");
scanf("%f",&s->math);
s->next=head->next;
head->next=s;
char judge;
while(getchar()!='\n');//清空缓冲区
printf("是否继续添加(y/n):");
judge=getchar();
if(judge=='y')
{
continue;
}
else
{
break;
}
}
return head;
}
stu *rearadd(stu *head)
{
if(NULL==head)
{
printf("head null\n");
return NULL;
}
while(1)
{
stu *s=NULL;
s=(stu *)malloc(sizeof(stu));
if(NULL==s)
{
printf("s malloc failed\n");
return NULL;
}
s->next=NULL;
printf("请输入学号:\n");
scanf("%ld",&s->id);
printf("请输入姓名:\n");
scanf("%s",s->name);
printf("请输入性别:\n");
scanf("%s",s->sex);
printf("请输入年龄:\n");
scanf("%d",&s->age);
printf("请输入数学成绩:\n");
scanf("%f",&s->math);
//添加节点
stu *p=NULL;
stu *q=NULL;
q=p=head;
while(p!=NULL)
{
q=p;
p=p->next;
}
q->next=s;
char judge;
while(getchar()!='\n');//清空缓冲区
printf("是否继续添加(y/n):");
judge=getchar();
if(judge=='y')
{
continue;
}
else
{
break;
}
}
return head;
}
stu *squadd(stu *head)
{
if(NULL==head)
{
printf("head null\n");
return NULL;
}
while(1)
{
stu *s=NULL;
s=(stu *)malloc(sizeof(stu));
if(NULL==s)
{
printf("s malloc failed\n");
return NULL;
}
s->next=NULL;
printf("请输入学号(大于0):\n");
scanf("%ld",&s->id);
printf("请输入姓名:\n");
scanf("%s",s->name);
printf("请输入性别:\n");
scanf("%s",s->sex);
printf("请输入年龄:\n");
scanf("%d",&s->age);
printf("请输入数学成绩:\n");
scanf("%f",&s->math);
//添加节点
stu *p=NULL;
stu *q=NULL;
q=p=head;
head->id=0;
while((p!=NULL)&&(s->id>p->id))
{
q=p;
p=p->next;
}
q->next=s;
s->next=p;
char judge;
while(getchar()!='\n');//清空缓冲区
printf("是否继续添加(y/n):");
judge=getchar();
if(judge=='y')
{
continue;
}
else
{
break;
}
}
return head;
}
stu *delete(stu *head)
{
if(NULL==head)
{
printf("delete head null\n");
return NULL;
}
while(1)
{
if(NULL==head->next)
{
printf("valid list empyt\n");
return head;
}
long x=0;
printf("请输入删除的数据:\n");
scanf("%ld",&x);
stu *p=NULL;
stu *q=NULL;
q=p=head;
//q=p=head->next;
while((p!=NULL)&&(p->id!=x))
{
q=p;
p=p->next;
}
if(NULL==p)
{
printf("this %ld not in list\n",x);
return head;
}
else
{
q->next=p->next;
free(p);
p=NULL;
}
char judge;
printf("是否继续删除(y/n):");
while(getchar()!='\n');
judge=getchar();
if(judge=='y')
{
continue;
}
else
{
break;
}
}
return head;
}
void print_list(stu *head)
{
if(NULL==head)
{
printf("head null\n");
return;
}
if(NULL==head->next)
{
printf("valid list empty\n");
return;
}
stu *p=NULL;
p=head->next;
printf("学号\t姓名\t性别\t年龄\t数学\n");
while(p!=NULL)
{
printf("%ld\t%s\t%s\t%d\t%.2f\n",p->id,p->name,p->sex,p->age,p->math);
p=p->next;
}
return;
}
void free_list(stu *head)
{
if(NULL==head)
{
printf("head null\n");
return;
}
stu *p=NULL;
stu *q=NULL;
p=head;
while(p!=NULL)
{
q=p;
p=p->next;
free(q);
q=NULL;
}
return;
}