#include<stdio.h>
#include<stdlib.h>
typedef struct LINK
{
int num;
struct LINK *next;
}LINK,*pLINK;
int getNum()
{
int num;
printf("请输入数字:");
scanf("%d",&num);
return num;
}
pLINK headInsertData(pLINK head,pLINK *tail)
{
if(head==NULL)
{
head=(pLINK)malloc(sizeof(LINK));
head->num=getNum();
head->next=NULL;
*tail=head;
return head;
}
pLINK p=(pLINK)malloc(sizeof(LINK));
p->num=getNum();
p->next=head;
head=p;
return head;
}
pLINK tailInsertData(pLINK head,pLINK *tail)//尾插数据
{
if(head==NULL)
{
head=(pLINK)malloc(sizeof(LINK));
head->num=getNum();
head->next=NULL;
*tail=head;
return head;
}
pLINK temp;
for(temp=head;temp->next!=NULL;temp=temp->next);
pLINK p=(pLINK)malloc(sizeof(LINK));
p->num=getNum();
(*tail)->next=p;
p->next=NULL;
*tail=p;
return head;
}
pLINK headDeleteData(pLINK head,pLINK *tail)//头删数据
{
if(head==NULL)
{
printf("无信息可删\n");
return NULL;
}
if(head->next==NULL)
{
free(head);
head=NULL;
*tail=NULL;
return head;
}
pLINK p=head;
head=head->next;
free(p);
p=NULL;
return head;
}
pLINK tailDeleteData(pLINK head,pLINK *tail)//尾删数据
{
if(head==NULL)
{
printf("无信息可删\n");
return NULL;
}
if(head->next==NULL)
{
free(head);
head=NULL;
*tail=NULL;
return head;
}
pLINK temp;
for(temp=head;temp->next!=*tail;temp=temp->next);
free(*tail);
*tail=temp;
temp->next=NULL;
return head;
}
void printData(pLINK head)//打印信息
{
if(head==NULL)
{
printf("无信息可打印\n");
return;
}
pLINK temp;
for(temp=head;temp!=NULL;temp=temp->next)
{
printf("[%d]-->",temp->num);
}
printf("NULL\n");
}
int main()
{
pLINK head=NULL;
pLINK tail=NULL;
int select;
while (1)
{
printf("========\n");
printf("1.头插文件\n");
printf("2.尾插文件\n");
printf("3.头删文件\n");
printf("4.尾删文件\n");
printf("5.打印数据\n");
printf("6.退出\n");
printf("========\n");
scanf("%d",&select);
switch(select)
{
case 1:
head=headInsertData(head,&tail);
break;
case 2:
head=tailInsertData(head,&tail);
break;
case 3:
head=headDeleteData(head,&tail);
break;
case 4:
head=tailDeleteData(head,&tail);
break;
case 5:
printData(head);
break;
case 6:
return 0;
default:
break;
}
}
}
网友评论