当前位置:网站首页>Chain team implementation (C language)
Chain team implementation (C language)
2022-07-06 14:16:00 【An Ruoxi~】
We have known about stack before , We know , Stack is first in first out , Last in, first out
Then the team is first in, first out , last in last out
It's like we go to the queue to buy food , The person in the front row will buy it first , It's natural, right >-<
So we know the principle of team , It is relatively easy to realize the chain team .
Here we should pay attention to : The head node here cannot be the same as the previous head node , Because this is a chain queue , The required operation is first in, first out , last in last out , Tail plug deletion , So here our head node should have two pointer fields , Point to the position to be inserted and the position to be deleted respectively .
Let's declare the function in the header file first
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);
Function definition in header file
#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");
}
The test file
The test case
#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;
}
边栏推荐
- Hackmyvm target series (3) -visions
- HackMyvm靶机系列(1)-webmaster
- Force deduction 152 question multiplier maximum subarray
- 网络基础之路由详解
- Detailed explanation of network foundation
- 7-1 output all primes between 2 and n (PTA programming)
- SQL injection
- HackMyvm靶机系列(7)-Tron
- 攻防世界MISC练习区(SimpleRAR、base64stego、功夫再高也怕菜刀)
- Read only error handling
猜你喜欢
Intensive literature reading series (I): Courier routing and assignment for food delivery service using reinforcement learning
强化学习基础记录
[dark horse morning post] Shanghai Municipal Bureau of supervision responded that Zhong Xue had a high fever and did not melt; Michael admitted that two batches of pure milk were unqualified; Wechat i
captcha-killer验证码识别插件
Ucos-iii learning records (11) - task management
搭建域环境(win)
sqqyw(淡然点图标系统)漏洞复现和74cms漏洞复现
撲克牌遊戲程序——人機對抗
Detailed explanation of network foundation routing
Strengthen basic learning records
随机推荐
HackMyvm靶机系列(6)-videoclub
[paper reproduction] cyclegan (based on pytorch framework) {unfinished}
HackMyvm靶机系列(4)-vulny
Record an edu, SQL injection practice
Hackmyvm target series (2) -warrior
Spot gold prices rose amid volatility, and the rise in U.S. prices is likely to become the key to the future
DVWA (5th week)
小程序web抓包-fiddler
XSS unexpected event
Feature extraction and detection 14 plane object recognition
【Numpy和Pytorch的数据处理】
Get started with typescript
Programme de jeu de cartes - confrontation homme - machine
内网渗透之内网信息收集(五)
循环队列(C语言)
[experiment index of educator database]
Only 40% of the articles are original? Here comes the modification method
HackMyvm靶机系列(7)-Tron
记一次,修改密码逻辑漏洞实战
Canvas foundation 2 - arc - draw arc