当前位置:网站首页>Sequential storage structure, chain storage structure and implementation of stack
Sequential storage structure, chain storage structure and implementation of stack
2022-07-25 18:01:00 【Advanced Xiaoxin】
【 Sequential storage structure 】
Stack empty condition :s->top == -1;
Stack full condition :s->top == MAX_SIZE-1;
Into the stack : First set the stack top pointer top++, And then I'll put the elements e Place it at the top of the stack where the pointer points ;
Out of the stack : First assign the top element of the stack to e, let top--.
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
typedef struct stack {
int data[MAX_SIZE];
int top;
}STACK, *PSTACK;
// Initialization stack
void init(PSTACK &s) {
s = (STACK*)malloc(sizeof(STACK));
s->top = -1;
}
// Destroy the stack
void destroyStack(PSTACK s) {
free(s);
}
// Judge whether the stack is empty
bool isEmpty(PSTACK s) {
return s->top==-1;
}
bool isFull(PSTACK s) {
return s->top == MAX_SIZE-1; //c++ Set another member variable in object-oriented size, Because when there is a parameter structure size Possible and MAX_SIZE Dissimilarity
}
// Into the stack
//top Pointer first ++, Re pressing elements
bool push(PSTACK s, int e) {
if(isFull(s))
return false;
s->top++;
s->data[s->top] = e;
return true;
}
// Out of stack and return out of stack elements
// Save the out of stack elements first ,top Pointer again --
bool pop(PSTACK s, int &e) {
if(isEmpty(s))
return false;
e = s->data[s->top];
s->top--;
return true;
}
// Take the top element of the stack
bool getTop(PSTACK s, int &e) {
if(isEmpty(s))
return false;
e = s->data[s->top];
return true;
}
// Traverse the stack from the bottom
bool stackPrint(PSTACK s) {
if(isEmpty(s))
return false;
for(int i=0;i<=s->top;i++)
printf("%d ",s->data[i]); // You can write another function to output
printf("\n");
return true;
}
int main() {
int e;
PSTACK s;
init(s);
if(isEmpty(s)) printf(" The stack is empty. !\n");
for(int i=0;i<=10;i+=2)
if(!isFull(s))
push(s,i);
if(isFull(s)) printf(" Stack full !\n");
for(int i=0;i<2;i++)
if(pop(s,e)) { // Just consider whether the stack is successful , Is the stack empty Put it in pop Function
printf(" The stack element this time is %d\n",e);
}
if(!stackPrint(s)) printf(" The stack is empty. !\n"); // When the function is called, the function will be executed , It will also return to the status , So you don't have to write else
return 0;
}【 Chain storage structure 】
Stack empty condition :s->next == NULL;
Don't consider the stack full condition for chain stacks
Into the stack : The new node is inserted after the head node
Out of the stack : Take out the value of the first node and release the first node
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *next;
}STACK, *PSTACK;
// Initialization stack
void init(PSTACK &s) {
s = (STACK*)malloc(sizeof(STACK));
s->next = NULL;
}
// Destroy the stack ( Destroy the header node together )
void destroy(PSTACK s) {
PSTACK q = s, p = s->next;
while(p) {
free(q);
q = p;
p = p->next;
}
free(q);
}
// Judge whether the stack is empty
bool isEmpty(PSTACK s) {
return s->next == NULL;
}
// Push
void push(PSTACK s, int e) {
PSTACK p = (STACK*)malloc(sizeof(STACK));
p->data = e;
p->next = s->next;
s->next = p;
}
// Out of the stack
bool pop(PSTACK s, int &e) {
if(isEmpty(s))
return false;
PSTACK p = s->next;
e = p->data;
s->next = p->next;
free(p);
return true;
}
// Get stack top element
bool getTop(PSTACK s, int &e) {
if(isEmpty(s))
return false;
e = s->next->data;
return true;
}
// Traverse the stack from the top of the stack
bool print(PSTACK s) {
if(isEmpty(s))
return false;
PSTACK p = s->next;
while(p) {
printf("%d ",p->data); // You can write another function to output
p = p->next;
}
printf("\n");
return true;
}
int main() {
int e;
PSTACK s;
init(s);
if(isEmpty(s)) printf(" The stack is empty. !\n");
for(int i=1;i<=10;i+=2)
push(s,i);
for(int i=0;i<2;i++)
if(pop(s,e)) {
printf(" The stack element this time is %d\n",e);
}
if(!print(s)) printf(" The stack is empty. ");
destroy(s);
return 0;
} Some thoughts :
You can add some details when designing functions , For example, when destroying the stack or judging whether the stack is empty, first judge whether the stack is created , But generally at the beginning and end of the program is to create and destroy the stack , So this code does not make this judgment .
边栏推荐
- NPDP多少分通过?如何高分通过?
- 云VR:虚拟现实专业化的下一步
- Kendryte K210 在freertos上的lcd屏幕的使用
- srec_cat 常用参数的使用
- Auditing相关注解
- Automated test Po design model
- Idea essential plug-ins
- UnitTest框架应用
- Tme2022 campus recruitment background development / operation development / business operation and maintenance / application development written examination (I) a little self analysis of programming q
- What scenarios have rust, which is becoming more and more mature, applied?
猜你喜欢

Highlights

云VR:虚拟现实专业化的下一步

CH582 BLE 5.0 使用 LE Coded 广播和连接

2022/7/23

Redistemplate solves the problem of oversold inventory in the seckill system with high speed - redis transaction + optimistic lock mechanism

Auditing related notes

nodejs 简单例子程序之express

PageHelper还能结合Lambda表达式实现简洁的分页封装

What scenarios have rust, which is becoming more and more mature, applied?

自动化测试 PO设计模型
随机推荐
软件测试基础知识(思维导图)
nodejs 简单例子程序之express
SVN客户端(TortoiseSVN)安装及使用说明
Redis source code and design analysis -- 16. AOF persistence mechanism
简述聚簇索引、二级索引、索引下推
How many points did NPDP pass? How to pass with high scores?
STM8S003F3 uart的使用
自动化测试 PO设计模型
Several implementations of PHP to solve concurrency problems
图的相关操作
Update 3dcat real time cloud rendering V2.1.2 release
Idea integrates common functions of SVN code management
C语言 cJSON库的使用
How to judge the performance of static code quality analysis tools? These five factors must be considered
关于云XR介绍,以及5G时代云化XR的发展机遇
RedisTemplate解决高并发下秒杀系统库存超卖方案 — Redis事务+乐观锁机制
Cloud XR面临的问题以及Cloud XR主要应用场景
Notes on Flickr's dataset
ROS learning notes (IV) ROS cannot solve rosdep init or update
Joseph Ring problem