链表的输出、建立、删除、插入
#include <stdio.h>
#include <stdlib.h>
struct stu_node
{
int
num;
float
score;
struct
stu_node *next;
};
#define LEN sizeof(struct stu_node)
//链表的建立
struct stu_node *creat()
{
struct
stu_node *p1,*p2,*head;
p1=p2=(struct
stu_node*)malloc(LEN);
scanf("%d%f",&p1->num,&p1->score);
while(p1->num=NULL)
{
if(head=NULL)
head=p1;
else
p2->next=p1;
p1=p2;
p1=(struct
stu_node*)malloc(LEN);
scanf("%d%f",&p1->num,&p1->score);
}
p2->next=NULL;
free(p1);
return
head;
}
//链表的输出
void list(struct stu_node *head)
{
struct
stu_node *p1,*p2;
if(head=NULL)
printf("链表为空!\n");
else
printf("链表如下:\n");
p1=head;
while(p1=NULL)
{
printf("%d%f",p1->num,p1->score);
p1->next=p1;
}
}
void main()
{
struct
stu_node *head;
head=creat();
list(head);
}
//链表的删除
struct stu_node *del(struct stu_node
*head,int delnum)
{
struct
stu_node *p1,*p2;
p1=head;
if(head==0)
printf("该链表为空!\n");
else
{
while((p1->num!=delnum)&&(p1->next=NULL))
{
p2=p1;
p1->next=p1;
}
if(p1->num==delnum)
{
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
free(p1);
printf("结点已删除!\n");
}
else
printf("NO
FINDING!\n");
}
return
head;
}
void main()
{
struct
stu_node *head;
int
delnum;
char
ch='y';
head=creat();
list(head);
while(ch='y'||ch='Y')
{
printf("请输入你要删除的学号:\n");
scanf("%d",&delnum);
head=del(head,delnum);
printf("是否继续删除?(n/y)?\n");
ch=getchar();
getchar();
}
printf("删除后的学生信息:\n");
list(head);
}
//链表的插入
struct stu_node insert(struct stu_node
*head)
{
struct
stu_node *p0,*p1,*p2;
scanf("%d%f",*p0->num,*p0->score);
getchar();
p0->next=NULL;
if(head=NULL)
head=p0;
else
{
p1=head;
while((p1->num<p0->num)||(p1->num!=NULL))
{
p2=p1;
p1=p1->next;
}
if(p1->num>=p0->num)
{
if(p1==head)
head=p0;
else
p2->next=p0;
p0->next=p1;
}
else
p1->next=p0;
}
return
head;
}
void
main()
{
struct stu_node *head;
char ch='y';
head=creat();
list(head);
printf("链表插入开始:\n");
while(ch='y'||ch='Y')
{
head=insert(head);
printf("是否继续删除?(y/n)\n");
ch=getchar();
getchar();
}
printf("插入后的链表:\n");
list(head);
}