当前位置:网站首页>链队实现(C语言)
链队实现(C语言)
2022-07-06 09:23:00 【安若兮~】
我们之前已经了解过栈了,我们知道,栈是先进后出,后进先出
那么队则是先进先出,后进后出
就像我们去排队买饭一样,排在前面的人就先买到,天经地义对吧>-<
那么我们知道了队的原理,链队实现起来就相对容易多了。
在这里我们要注意:我们这里的头节点不能和之前的头节点一样了,因为我们这是链式队列,需要的操作是先进先出,后进后出,即尾插头删,所以这里我们的头节点要有两个指针域,分别指向待插入位置和待删除位置。
我们先在头文件中进行函数声明
typedef int ELEM_TYPE;
typedef struct Node {
ELEM_TYPE data;
struct Node* next;
}Node,*PNode;
typedef struct Head {
struct Node* front;
struct Node* rear;
}Head,*Plist;
void Init(Plist plq);
bool Push(Plist plq,ELEM_TYPE val);
bool Pop(Plist plq);
bool Top(Plist plq,ELEM_TYPE* rtval);
int Getlength(Plist plq);
bool IsEmpty(Plist plq);
void Clear(Plist plq);
void Destory(Plist plq);
void Show(Plist plq);
头文件中函数定义
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include"list_queue.h"
void Init(Plist plq) {
assert(plq!=NULL);
plq->front = NULL;
plq->rear = NULL;
}
bool Push(Plist plq, ELEM_TYPE val) {
assert(plq != NULL);
struct Node* pnewnode = (struct Node*)malloc(sizeof(struct Node));
assert(pnewnode!=NULL);
pnewnode->data = val;
if (IsEmpty(plq)) {
pnewnode->next = NULL;
plq->front = plq->rear = pnewnode;
return true;
}
pnewnode->next = plq->rear->next;
plq->rear->next = pnewnode;
plq->rear = pnewnode;
return true;
}
bool Pop(Plist plq) {
assert(plq!=NULL);
if (IsEmpty(plq)) {
return false;
}
if (plq->front->next==NULL){//plq->front==plq->rear
free(plq->front);
plq->front = plq->rear = NULL;
return true;
}
struct Node* p = plq->front;
plq->front = p->next;
free(p);
p = NULL;
return true;
}
bool Top(Plist plq, ELEM_TYPE* rtval) {
assert(plq != NULL);
if (IsEmpty(plq)) {
return false;
}
*rtval = plq->front->data;
return true;
}
int Getlength(Plist plq) {
assert(plq!=NULL);
int count = 0;
for (struct Node* p = plq->front; p!= NULL;p=p->next) {
count++;
}
return count;
}
bool IsEmpty(Plist plq) {
assert(plq!=NULL);
return plq->front == NULL;
}
void Clear(Plist plq) {
assert(plq!=NULL);
Destory(plq);
}
void Destory(Plist plq) {
assert(plq != NULL);
/*while (plq->front!=NULL) {
struct Node* p = plq->front;
plq->front = p->next;
free(p);
}
plq->rear =plq->front= NULL;*/
struct Node* p = plq->front;
struct Node* q;
plq->front = plq->rear = NULL;
while (p!=NULL) {
q = p->next;
free(p);
p = q;
}
}
void Show(Plist plq) {
assert(plq != NULL);
for (struct Node* p = plq->front; p != NULL; p = p->next) {
printf("%5d",p->data);
}
printf("\n");
}
测试文件
测试用例
#include<stdio.h>
#include"list_queue.h"
int main() {
struct Head hd;
Init(&hd);
for (int i = 0; i < 20; i++) {
Push(&hd,i+1);
}
Show(&hd);
Pop(&hd);
Pop(&hd);
Pop(&hd);
Show(&hd);
printf("%d\n",Getlength(&hd));
ELEM_TYPE temp;
Top(&hd,&temp);
printf("%d\n",temp);
Clear(&hd);
Show(&hd);
Destory(&hd);
return 0;
}
边栏推荐
- 强化學習基礎記錄
- 7-14 error ticket (PTA program design)
- Wei Shen of Peking University revealed the current situation: his class is not very good, and there are only 5 or 6 middle-term students left after leaving class
- Low income from doing we media? 90% of people make mistakes in these three points
- Strengthen basic learning records
- 7-9 make house number 3.0 (PTA program design)
- 7-15 h0161. Find the greatest common divisor and the least common multiple (PTA program design)
- 简单理解ES6的Promise
- 2022 Teddy cup data mining challenge question C idea and post game summary
- Strengthen basic learning records
猜你喜欢
Poker game program - man machine confrontation
Using spacedesk to realize any device in the LAN as a computer expansion screen
附加简化版示例数据库到SqlServer数据库实例中
强化學習基礎記錄
2. First knowledge of C language (2)
实验六 继承和多态
How to turn wechat applet into uniapp
sqqyw(淡然点图标系统)漏洞复现和74cms漏洞复现
Network layer - simple ARP disconnection
xray与burp联动 挖掘
随机推荐
Intensive literature reading series (I): Courier routing and assignment for food delivery service using reinforcement learning
Difference and understanding between detected and non detected anomalies
QT meta object qmetaobject indexofslot and other functions to obtain class methods attention
JS several ways to judge whether an object is an array
网络基础之路由详解
【头歌educoder数据表中数据的插入、修改和删除】
Which is more advantageous in short-term or long-term spot gold investment?
Using qcommonstyle to draw custom form parts
Attack and defense world misc practice area (simplerar, base64stego, no matter how high your Kung Fu is, you are afraid of kitchen knives)
7-9 make house number 3.0 (PTA program design)
Hackmyvm target series (6) -videoclub
小程序web抓包-fiddler
Low income from doing we media? 90% of people make mistakes in these three points
7-11 mechanic mustadio (PTA program design)
Interpretation of iterator related "itertools" module usage
【educoder数据库实验 索引】
HackMyvm靶机系列(7)-Tron
Record a penetration of the cat shed from outside to inside. Library operation extraction flag
Strengthen basic learning records
Relationship between hashcode() and equals()