头删,尾插
#include<stdio.h>
#include<stdlib.h>
typedef struct Queue
{
int num;
struct Queue *next;
}Queue;
typedef struct pQueue
{
Queue *first;
Queue *tail;
} pQueue;
int getNum()
{
int num;
printf("请输入数字:");
scanf("%d",&num);
return num;
}
pQueue* creatQueue(pQueue *queueHead) //创建链表
{
if (queueHead==NULL)
{
queueHead=(pQueue *)malloc(sizeof(pQueue));
queueHead->first=queueHead->tail=NULL;
return queueHead;
}
return queueHead;
}
void tailInsertData(pQueue *queueHead) //尾插
{
if(queueHead==NULL)
{
printf("没有创建成功\n");
return ;
}
Queue *p=(Queue *)malloc(sizeof (Queue));
p->num=getNum();
if(queueHead->first==NULL)
{
queueHead->first=p;
queueHead->tail=p;
p->next=NULL;
return;
}
p->next=NULL;
queueHead->tail->next=p;
queueHead->tail=p;
}
void headDeleteDta(pQueue *queueHead) //头删
{
if(queueHead==NULL||queueHead->tail==NULL)
{
printf("无信息可删!\n");
return ;
}
if(queueHead->first->next==NULL)
{
free(queueHead->first);
queueHead->first=queueHead->tail=NULL;
return;
}
Queue *p=queueHead->first;
queueHead->first=p->next;
free(p);
p=NULL;
}
void printData(pQueue *queueHead) //打印信息
{
if(queueHead==NULL||queueHead->tail==NULL)
{
printf("无信息可打印\n");
return;
}
Queue *temp;
printf("Head-->");
for(temp=queueHead->first; temp!=NULL; temp=temp->next)
{
printf("[%d]-->",temp->num);
}
printf("NULL\n");
}
int main()
{
pQueue *queueHead=NULL;
int select;
while(1)
{
printf("=======\n");
printf("1.创建数据\n");
printf("2.删除数据\n");
printf("3.插入数据\n");
printf("4.打印队列\n");
printf("5.退出\n");
printf("=======\n");
scanf("%d",&select);
switch(select)
{
case 1:
queueHead=creatQueue(queueHead);
break;
case 2:
headDeleteDta(queueHead);
break;
case 3:
tailInsertData(queueHead);
break;
case 4:
printData(queueHead);
break;
case 5:
return 0;
default:
break;
}
}
return 0;
}