mirror of
https://github.com/by777/dataStructureForC.git
synced 2026-02-03 01:53:15 +08:00
l链队基本操作
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
#include "io.h"
|
||||
|
||||
#define MAXSIZE 20
|
||||
#define OVERFLOW -1
|
||||
#define OK 1
|
||||
#define ERROR 0
|
||||
#define TRUE 1
|
||||
@@ -33,13 +34,13 @@ Status InitQueue(LinkQueue *Q)
|
||||
Q->front = Q->rear = (QueuePtr)malloc(sizeof(QNode));
|
||||
if (!Q->front)
|
||||
{
|
||||
exit(-1);
|
||||
exit(OVERFLOW);
|
||||
}
|
||||
Q->front->next = NULL;
|
||||
return OK;
|
||||
}
|
||||
|
||||
Status Destroy(LinkQueue *Q)
|
||||
Status DestroyQueue(LinkQueue *Q)
|
||||
{
|
||||
while (Q->front)
|
||||
{
|
||||
@@ -101,6 +102,77 @@ Status GetHead(LinkQueue Q, ElemType *e)
|
||||
return OK;
|
||||
}
|
||||
|
||||
Status EnQueue(LinkQueue *Q,ElemType e){
|
||||
Q
|
||||
Status EnQueue(LinkQueue *Q, ElemType e)
|
||||
{
|
||||
QueuePtr p = (QueuePtr)malloc(sizeof(QNode));
|
||||
if (!p)
|
||||
{
|
||||
exit(OVERFLOW);
|
||||
}
|
||||
p->data = e;
|
||||
p->next = NULL;
|
||||
Q->rear->next = p; //添加到链尾
|
||||
Q->rear = p;
|
||||
return OK;
|
||||
}
|
||||
|
||||
Status DeQueue(LinkQueue *Q, ElemType *e)
|
||||
{
|
||||
if (Q->rear == Q->front)
|
||||
{
|
||||
return ERROR;
|
||||
}
|
||||
QueuePtr p = Q->front->next;
|
||||
*e = p->data;
|
||||
Q->front->next = p->next;
|
||||
if (Q->rear == p)
|
||||
{
|
||||
// 若链表除头节点外只剩下一个元素时,则需将rear指向头节点
|
||||
//若队头就是队尾,则删除后将rear指向front
|
||||
Q->rear = Q->front;
|
||||
}
|
||||
free(p);
|
||||
return OK;
|
||||
}
|
||||
|
||||
Status QueueTraverse(LinkQueue Q)
|
||||
{
|
||||
QueuePtr p;
|
||||
p = Q.front->next;
|
||||
while (p)
|
||||
{
|
||||
visit(p->data);
|
||||
p = p->next;
|
||||
}
|
||||
printf("\n");
|
||||
return OK;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ElemType e;
|
||||
LinkQueue q;
|
||||
int i = InitQueue(&q);
|
||||
if (i)
|
||||
{
|
||||
printf("inited! \n");
|
||||
printf("是否空队列?", QueueEmpty(q));
|
||||
}
|
||||
EnQueue(&q, -5);
|
||||
EnQueue(&q, 5);
|
||||
EnQueue(&q, 10);
|
||||
printf("插入3个元素(-5,5,10)后,队列的长度为%d\n", QueueLength(q));
|
||||
QueueTraverse(q);
|
||||
i = GetHead(q, &e);
|
||||
if (i == OK)
|
||||
printf("队头元素是:%d\n", e);
|
||||
DeQueue(&q, &e);
|
||||
printf("删除了队头元素%d\n", e);
|
||||
QueueTraverse(q);
|
||||
ClearQueue(&q);
|
||||
printf("清空队列后,q.front=%u q.rear=%u q.front->next=%u\n",q.front,q.rear,q.front->next);
|
||||
DestroyQueue(&q);
|
||||
printf("销毁队列后,q.front=%u q.rear=%u\n",q.front, q.rear);
|
||||
getchar();
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user