#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct node *head;
struct node
{
int date;
struct node *next;
};
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&//函数声明
void jianli();
struct node *creatlist();
void shanchu();
int del();
struct node *find();
void charu();
void insert();
void printlist();
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&//
void jianli() //建立链表函数
{
head=creatlist();
}
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&//
struct node *creatlist() //建立链表函数
{
struct node *h,*p,*q;
int a;
h=(struct node *)malloc(sizeof(struct node));
p=q=h;
printf("\n %%% 输入数据建立链表,当输入为数据0时停止建立链表 %%% \n");
scanf("%d",&a);
while(a!=0)
{
p=(struct node *)malloc(sizeof(struct node));
p->date=a;
q->next=p;
q=p;
printf("\n %%% 输入数据建立链表 %%% \n");
scanf("%d",&a);
}
p->next=NULL;
return h;
}
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&//
void shanchu() //调用找节点的函数
{ int i;
printf("*** 输入你要删除的数据 ***\n");
scanf("%d",&i);
del(head,i);
}
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&//
int del(struct node *h,int x)
{
struct node *p,*q;
if(h->next==NULL)
{printf("list is null.\n");
return 0;
}
q=find(h,x);
if(q!=NULL)
{
p=q->next;
q->next=p->next;
free(p);
return 1;
}
return 0;
}
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&//
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&//
struct node *find(struct node *h,int x) //查找要删除的节点
{
struct node *p,*q;
p=h->next;
q=h;
while(p!=NULL&&p->date!=x)
{
q=p;
p=p->next;
}
if(p==NULL)
return NULL;
else
return q;
}
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&//
void charu()
{
int i;
printf("***** 输入你要插入的数据 *****\n\n");
scanf("%d",&i);
insert(head,i);
}
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&//插入函数
void insert(struct node *h,int x)
{
struct node *p,*q,*s;
s=(struct node *)malloc(sizeof(struct node));
s->date=x;
q=h;
p=h->next;
while(p!=NULL&&x>p->date)
{
q=p;
p=p->next;
}
s->next=p;
q->next=s;
}
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&// 输出链表
void printlist()
{
struct node *p;
p=head->next;
printf("****** 输出链表 ********\n");
while(p!=NULL)
{
printf(" %d ",p->date);
p=p->next;
}
printf("\n");
}
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&//
int main()
{ int i;
//clrscr();
system("color 3E");
meimu:printf("\n *************************** \n");
printf(" 用结构处理单向链表 \n\n");
printf(" @@@@@@@@@@@@ 主菜单 @@@@@@@@@@@@@@@ \n");
printf(" 1: 建立链表 \n");
printf(" 2: 删除节点 \n");
printf(" 3: 插入节点 \n");
printf(" 4: 输出链表 \n");
printf(" 0: 退出主菜单 \n");
printf(" 输入你要操作的选项数字 \n");
scanf("%d",&i);
switch(i)
{
case 1:jianli();break;
case 2:shanchu();break;
case 3:charu();break;
case 4:printlist();break;
case 0:exit(0);break;
}
goto meimu;
return 0;
}