#include "stdafx.h"
#include<stdio.h>
#include<stdlib.h>
typedef struct tagLinkList
{
int data;
struct tagLinkList * pNext;
}LINKLIST,*LPLINKLIST;
void printDuque(LPLINKLIST head)
{
LPLINKLIST ll = head->pNext;
//循环输出队列所有元素
while(ll != NULL)
{
printf("%d\t",ll->data);
ll = ll->pNext;
}
printf("\n");
}
LPLINKLIST addq(LPLINKLIST tail, int data)
{
//添加新节点到队列尾部
LPLINKLIST ll = (LPLINKLIST)malloc(sizeof(LINKLIST));
ll->data = data;
ll->pNext = NULL;
tail->pNext = ll;
//返回新添加的节点返回作为尾节点
return ll;
}
void deleteq(LPLINKLIST head, LPLINKLIST * tail,int * data)
{
//队列头 == 队列尾时队列为空
if(head != * tail)
{
//head 之后的第一个节点为first in 节点
LPLINKLIST curr = head->pNext;
LPLINKLIST next = curr->pNext;
//返回节点数据
*data = curr->data;
//释放节点
free(curr);
head->pNext = next;
//队列为空时,设置队尾指针 = 队首指针
if(next == NULL)
{
*tail = head;
}
printDuque(head);
}
else
{
printf("The duque is null...\n");
}
}
void destoryLink(LPLINKLIST head)
{
LPLINKLIST ll = head->pNext;
while(ll != NULL)
{
LPLINKLIST next = ll->pNext;
free(ll);
ll = next;
}
}
int main(void)
{
LINKLIST head;
LPLINKLIST tail = &head;
int data = 0;
head.pNext = NULL;
tail = addq(tail,1);
tail = addq(tail,2);
tail = addq(tail,3);
tail = addq(tail,4);
tail = addq(tail,5);
printDuque(&head);
deleteq(&head,&tail,&data);
deleteq(&head,&tail,&data);
deleteq(&head,&tail,&data);
deleteq(&head,&tail,&data);
deleteq(&head,&tail,&data);
deleteq(&head,&tail,&data);
destoryLink(&head);
return 0;
}
|