📄 list_again.c
字号:
//用结构体和指针实现动态链表程序
#include <stdio.h>
#include <malloc.h>
#define NULL 0
#define LEN sizeof(struct list)
struct list
{int num;
struct list *next;
};
int n;
#ifdef MIAN
int main()
{
int test=0;
struct list *creat(void);
int print(struct list *head);
struct list *insert(struct list *head);
struct list *del(struct list *head);
int max_min(struct list *head);
int back_list();
int list_process();
test=list_process();
if(test)
printf("success!\n");
return(1);
}
#endif
struct list *creat(void) //创建链表
{
struct list *head,*p1,*p2;
printf("Please input new record:\n");
n=0;
p1=p2=(struct list *)malloc(LEN); //开辟新的空间
scanf("%d",&p1->num);
head=NULL;
while(p1->num!=0)
{
n++;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct list *)malloc(LEN); //开辟新的空间
scanf("%d",&p1->num);
}
p2->next=NULL;
return(head);
}
/*
**打印链表(3)
*/
int print(struct list *head) //输出链表
{
struct list *p;
p=head;
if(head==NULL)
printf("The list is null.\n");
else
printf("There are %d records.\n",n);
do{
printf("num is %d\n",p->num);
p=p->next;
}while(p!=NULL);
return(1);
}
/*
**插入一个结点(1)
*/
struct list *insert(struct list *head) //插入
{
struct list *p0,*p1,*p2;
p1=head;
printf("input new list:\n");
p0=(struct list *)malloc(sizeof(struct list));
scanf("%d",&p0->num);
if(head==NULL)
{head=p0; p1->next=NULL;}
else
{
while((p0->num>p1->num)&&(p1->next!=NULL))
{p2=p1; p1=p1->next;}
if(p0->num<=p1->num)
{if(head==p1)
head=p0;
else
p2->next=p0;
p0->next=p1;
}
else
{p1->next=p0; p0->next=NULL;}
}
n++;
return(head);
}
/*
**删除一个结点(2)
*/
struct list *del(struct list *head) //有问题,但找不出来
{
struct list *p1,*p2;
int num;
printf("Please input del num:\n");
scanf("%d",&num);
p1=head;
if(head==NULL)
printf("The list is null.\n");
while(num!=p1->num&&p1->next!=NULL)
{p2=p1; p1=p1->next;}
if(num==p1->num)
{if(p1==head)
head=p1->next;
else
p2->next=p1->next;
printf("delete:%d\n",num);
n=n-1;
}
else
printf("The num is not found.\n");
return(head);
}
/*
**返回链表中的最大与最小值(4)
*/
int max_min(struct list *head)
{
struct list *p1,*p2;
int max=0,min=0;
p2=head;
p1=head->next;
while(max<p1->num&&p1->num!=NULL)
{
max=p1->num;
p2=p1;
p1=p1->next;
}
printf("max is %d:\n",max);
p2=head;
p1=head->next;
while(min>p1->num&&p1->num!=NULL)
{
min=p1->num;
p2=p1;
p1=p1->next;
}
printf("min is %d:\n",min);
return(1);
}
/*
**返回上层菜单(5)
*/
int back_list()
{
return(1);
}
/*
**退出(0)
*/
int list_process()
{
struct list *head,*t;
int c,i,j;
head=creat();
do{
i=j=0;
printf("1.input a node\n");
printf("2.delete a node\n");
printf("3.print the list\n");
printf("4.return the max&min\n");
printf("5.back_list\n");
printf("0.exit\n");
printf("please choose:\n");
scanf("%d",&c);
switch(c)
{
case 1: i=(int)insert(head); break;
case 2: i=(int)del(head); break;
case 3: i=print(head); break;
case 4: i=max_min(head); break;
case 5: j=back_list();break;
case 0: break;
default:printf("you input a wrong number!\n");list_process();
}
}while(i);
if(j)
return(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -