当前位置:网站首页>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;
}
边栏推荐
- Callback function ----------- callback
- Spot gold prices rose amid volatility, and the rise in U.S. prices is likely to become the key to the future
- Attack and defense world misc practice area (GIF lift table ext3)
- Hackmyvm target series (3) -visions
- Experiment 7 use of common classes (correction post)
- 7-4 hash table search (PTA program design)
- Read only error handling
- 7-4 散列表查找(PTA程序设计)
- Record a penetration of the cat shed from outside to inside. Library operation extraction flag
- 内网渗透之内网信息收集(二)
猜你喜欢
"Gold, silver and four" job hopping needs to be cautious. Can an article solve the interview?
Hackmyvm Target Series (3) - vues
Meituan dynamic thread pool practice ideas, open source
强化学习基础记录
[paper reproduction] cyclegan (based on pytorch framework) {unfinished}
Nuxtjs quick start (nuxt2)
Intel oneapi - opening a new era of heterogeneity
Read only error handling
HackMyvm靶機系列(3)-visions
附加简化版示例数据库到SqlServer数据库实例中
随机推荐
Force deduction 152 question multiplier maximum subarray
Hackmyvm target series (7) -tron
Windows platform mongodb database installation
Strengthen basic learning records
Hackmyvm target series (6) -videoclub
captcha-killer验证码识别插件
Experiment 4 array
DVWA (5th week)
实验六 继承和多态
网络基础之路由详解
What language should I learn from zero foundation. Suggestions
How to turn wechat applet into uniapp
实验四 数组
The United States has repeatedly revealed that the yield of interest rate hiked treasury bonds continued to rise
浅谈漏洞发现思路
Network layer - simple ARP disconnection
实验七 常用类的使用(修正帖)
Detailed explanation of three ways of HTTP caching
Detailed explanation of network foundation routing
7-11 机工士姆斯塔迪奥(PTA程序设计)