2.4 队列--链队列
1 .main.c
//
// main.c
// 10_LinkQueue
//
// Created by ls on 2022/5/23.
// Copyright © 2022 LSC001. All rights reserved.
//
#include "LinkQueue.h"
int main() {
LQueue myqueue;
InitQueue(&myqueue);
int select=1;
ElemType value=0;
while(select)
{
printf("********************************************\n");
printf("* [1] push_back [2] pop_head *\n");
printf("* [3] show_Queue [4] isEmpty *\n");
printf("* [5] get_top [6] length *\n");
printf("* [7] clear [8] destroy *\n");
printf("********************************************\n");
printf("请选择:> ");
scanf("%d",&select);
if(0 == select)
break;
switch (select)
{
case 1:
printf("Please input a number(-1 end): ");
while(scanf("%d",&value),value!= -1)
{
push_back(&myqueue,value);
}
show_Queue(&myqueue);
break;
case 2:
pop_head(&myqueue,&value);
show_Queue(&myqueue);
break;
case 3:
show_Queue(&myqueue);
break;
case 4:
printf("Empty: %ld\n",isEmpty(myqueue));
break;
case 5:
get_top(myqueue);
break;
case 6:
printf("length: %ld\n",length(myqueue));
break;
case 7:
clear0(&myqueue);
break;
case 8:
destroy(&myqueue);
break;
default:
printf("输入错误!");
break;
}
}
destroy(&myqueue);
printf("Hello, World!\n");
return 0;
}
2.LinkQueue.h
//
// LinkQueue.h
// 10_LinkQueue
//
// Created by ls on 2022/5/23.
// Copyright © 2022 LSC001. All rights reserved.
//
#ifndef LinkQueue_h
#define LinkQueue_h
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
// 1 定义宏
#define ElemType int
// 2 定义节点、单链表
typedef struct LQnode
{
ElemType data;
struct LQnode *next;
}LQnode,*PLQnode;
typedef struct LQueue
{
PLQnode front,rear;
int size;
}LQueue;
// 3 函数声明
void InitQueue(LQueue *Q);
void push_back(LQueue *Q,ElemType num); // 1
void pop_head(LQueue *Q,ElemType *num); // 2
void show_Queue(LQueue *Q); // 3
void get_top(LQueue Q); // 4
long isEmpty(LQueue Q); // 5
long length(LQueue Q); // 6
void clear0(LQueue *Q); // 7
void destroy(LQueue *Q); // 8
#endif /* LinkQueue_h */
3.LinkQueue.c
//
// LinkQueue.c
// 10_LinkQueue
//
// Created by ls on 2022/5/23.
// Copyright © 2022 LSC001. All rights reserved.
//
#include "LinkQueue.h"
void InitQueue(LQueue *Q)
{
Q->front = Q->rear = (PLQnode)malloc(sizeof(PLQnode));
assert(Q->front != NULL);
Q->front->next = NULL;
Q->size = 0;
}
PLQnode new_(ElemType num)
{
PLQnode p = (PLQnode)malloc(sizeof(PLQnode));
p->data = num;
p->next = NULL;
return p;
}
void push_back(LQueue *Q,ElemType num) // 1
{
if(!Q->front) return;
PLQnode p = new_(num);
Q->rear->next = p;
Q->rear = p;
Q->size++;
}
void pop_head(LQueue *Q,ElemType *num) // 2
{
if(!Q->front || Q->front == Q->rear) return;
PLQnode p = Q->front->next;
*num = p->data;
Q->front->next = p->next;
if(p == Q->rear) Q->rear = Q->front;
free(p);
p=NULL;
Q->size--;
}
void show_Queue(LQueue *Q) // 3
{
if(!Q->front || Q->front == Q->rear) return;
PLQnode p = Q->front->next;
while (p) {
printf("%d,",p->data);
p = p->next;
}
printf("\n");
}
void get_top(LQueue Q) // 4
{
if(!Q.front || Q.front == Q.rear) return;
printf("%d\n",Q.front->next->data);
}
long isEmpty(LQueue Q) // 5
{
if(!Q.front) return -1000000;
if(Q.front == Q.rear)return 1;
else return 0;
}
long length(LQueue Q) // 6
{
if(!Q.front) return -1000000;
return Q.size;
}
void clear0(LQueue *Q) // 7
{
if(!Q->front) return;
PLQnode p = Q->front->next;
while(p)
{
Q->front->next=p->next;
free(p);
p=Q->front->next;
Q->size--;
}
}
void destroy(LQueue *Q) // 8
{
clear0(Q);
free(Q->front);
Q->front = Q->rear = NULL;
}