当前位置:网站首页>链队实现(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;
}
边栏推荐
猜你喜欢

Attach the simplified sample database to the SQLSERVER database instance

HackMyvm靶机系列(1)-webmaster

Poker game program - man machine confrontation

强化学习基础记录

扑克牌游戏程序——人机对抗

Hackmyvm target series (1) -webmaster

SRC挖掘思路及方法

Using spacedesk to realize any device in the LAN as a computer expansion screen

Xray and burp linkage mining

Middleware vulnerability recurrence Apache
随机推荐
1143_ SiCp learning notes_ Tree recursion
Experiment five categories and objects
Record once, modify password logic vulnerability actual combat
Using qcommonstyle to draw custom form parts
QT meta object qmetaobject indexofslot and other functions to obtain class methods attention
实验八 异常处理
Package bedding of components
HackMyvm靶机系列(2)-warrior
Tencent map circle
渗透测试学习与实战阶段分析
7-11 机工士姆斯塔迪奥(PTA程序设计)
Implementation of count (*) in MySQL
Get started with typescript
SQL注入
Detailed explanation of network foundation routing
实验六 继承和多态
【MySQL数据库的学习】
Meituan dynamic thread pool practice ideas, open source
Beautified table style
外网打点(信息收集)