当前位置:网站首页>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;
}
边栏推荐
- Middleware vulnerability recurrence Apache
- 7-7 7003 组合锁(PTA程序设计)
- 图书管理系统
- [MySQL table structure and integrity constraint modification (Alter)]
- Hackmyvm target series (5) -warez
- 力扣152题乘数最大子数组
- 强化学习基础记录
- 记一次api接口SQL注入实战
- Intranet information collection of Intranet penetration (I)
- The most popular colloquial system explains the base of numbers
猜你喜欢

Attach the simplified sample database to the SQLSERVER database instance

Windows platform mongodb database installation

Detailed explanation of network foundation

《英特尔 oneAPI—打开异构新纪元》

HackMyvm靶机系列(3)-visions

网络基础详解

HackMyvm靶機系列(3)-visions

Interpretation of iterator related "itertools" module usage

强化学习基础记录

Internet Management (Information Collection)
随机推荐
xray與burp聯動 挖掘
Detailed explanation of network foundation routing
7-1 输出2到n之间的全部素数(PTA程序设计)
SQL injection
Callback function ----------- callback
Applet Web Capture -fiddler
Experiment 7 use of common classes (correction post)
Experiment 8 exception handling
[insert, modify and delete data in the headsong educator data table]
7-14 error ticket (PTA program design)
Hackmyvm target series (2) -warrior
msf生成payload大全
7-1 output all primes between 2 and n (PTA programming)
附加简化版示例数据库到SqlServer数据库实例中
SRC mining ideas and methods
Matlab opens M file garbled solution
Windows platform mongodb database installation
HackMyvm靶机系列(3)-visions
Feature extraction and detection 14 plane object recognition
强化学习基础记录