当前位置:网站首页>Implementation of stack stack
Implementation of stack stack
2022-06-11 22:12:00 【iEucliwood】
The basic concept and discussion of stack
The stack only allows data to be inserted or deleted at the fixed end of the table , This end is called the top of the stack , The opposite end is the bottom of the stack , The insertion of the top position of the stack is called pushing or pressing , Deletion is called out of stack . Such a data structure has a feature of last in first out LIFO(last in first off), It can also be "first in, second out" , Stack can be implemented by array table or linked list , When implemented with an array, the end of the array is used as the top of the stack , Because the insertion and deletion at the end of the array only need O(1) Time for , Similarly, if it is implemented with a single linked list , Take one end of the head of the linked list as the top of the stack , It's also because O(1) Insert delete operation . There are many examples of stack applications , For example, it can be used by the compiler to determine whether the symbols are balanced , Function call . If the balance symbol traverses the symbols one by one from left to right , The last left parenthesis must match the right parenthesis first , The first left parenthesis must match the last right parenthesis . Generally, the last function called will be released first .
The realization of the stack
Here we use arrays to realize , Because stack is a fast structure , You need to stack in and out frequently , The linked list requires frequent application and free space , Relatively poor performance .
The basic idea
A variable record capacity , A variable records the position of the top of the stack , There is also a pointer to the array space .
Structure declaration and function declaration
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
#define MINSIZE (4)
typedef char DataType;
typedef struct StackRecord StackRecord;
typedef struct StackRecord* Stack;
struct StackRecord
{
DataType* data;
int capacity;
int top;
};
Stack CreateStack(size_t size);
void StackPush(Stack s, DataType x);
void StackPop(Stack s);
DataType StackTop(Stack s);
size_t StackSize(Stack s);
void StackPrint(Stack s);
int IsEmpty(Stack s);
int IsFull(Stack s);initialization :
, There will be top Initialize to -1, Because there was no data at first , And some programmers will top Initialize to 0, Pay attention to this situation top Indicates the next position at the top of the stack .
Stack CreateStack(size_t size)
{
Stack s = (Stack)malloc(sizeof(StackRecord));
if (s == NULL)
{
printf("out of spaces!\n");
exit(-1);
}
s->top = -1;
s->capacity = size > MINSIZE ? size : MINSIZE;
s->data = (DataType*)malloc(sizeof(DataType) * s->capacity);
if (s->data == NULL)
{
printf("out of spaces!\n");
exit(-1);
}
return s;
}In and out of the stack :
void StackPush(Stack s, DataType x)
{
assert(s);
if (IsFull(s))
{
s->capacity *= 2;
DataType* tmp = (DataType*)realloc(s->data, sizeof(DataType) * s->capacity);
if (tmp == NULL)
printf("push failed!,out of spaces!\n");
else
s->data = tmp;
}
s->data[++s->top] = x;
}
void StackPop(Stack s)
{
assert(s);
if (!IsEmpty(s))
s->top--;
}Determine empty and full and return stack size :
int IsEmpty(Stack s)
{
return s->top == -1;
}
int IsFull(Stack s)
{
return s->top + 1 == s->capacity;
}
size_t StackSize(Stack s)
{
assert(s);
return s->top + 1;
}
Returns the top of stack element and prints the top of stack :
DataType StackTop(Stack s)
{
assert(s);
return s->data[s->top];
}
void StackPrint(Stack s)
{
assert(s);
while (!IsEmpty(s))
{
printf("%d", StackTop(s));
StackPop(s);
}
printf("\n");
}In fact, the basic operation required by the stack is only entering the stack and exiting the stack , When writing other operations you add , Follow the basic operation of the stack , Print as above , Users should only be able to access top stack elements , This is what the stack data structure requires , When the user wants to print the stack , You can only access the top element of the stack and exit the stack , Go print one by one . So you have to pay a price to print the stack , You need to stack all the elements .
边栏推荐
- STM32开发笔记112:ADS1258驱动设计——读寄存器
- Summary of common paging methods
- 高考结束,人生才刚刚开始,10年职场老鸟给的建议
- How to view the installation date of the win system
- [Yu Yue education] Yancheng Normal University Advanced Algebra reference
- [niuke.com] DP30 [template] 01 Backpack
- Superscalar processor design yaoyongbin Chapter 2 cache -- Excerpt from subsection 2.4
- 超标量处理器设计 姚永斌 第2章 Cache --2.4 小节摘录
- 360 online enterprise security cloud is open to small, medium and micro enterprises for free
- 剑指offer数组题型总结篇
猜你喜欢

How to view the installation date of the win system

Inner join execution plan changed

Classes and objects (3)

Classes and objects (2)

BUUCTF(5)

The shortcomings of the "big model" and the strengths of the "knowledge map"

高考结束,人生才刚刚开始,10年职场老鸟给的建议

Leetcode - day 2

Huawei equipment configuration h-vpn

Internet of things development practice 18 scenario linkage: how does an intelligent light perceive light? (I) (learning notes)
随机推荐
[Chongqing Guangdong education] college physics of Xiangtan University: mechanical and thermal reference materials
超標量處理器設計 姚永斌 第2章 Cache --2.4 小節摘錄
[niuke.com] dp31 [template] complete Backpack
Optimization of quick sort
[Yu Yue education] basic engineering English of Zhejiang industrial and Commercial University (wuyiping) reference materials
Go IO module
Basic operation and question type summary of linked list
Learning bit segment (1)
Zhanrui IOT chip 8910dm is certified by Deutsche Telekom
6.项目上线
Use the securecrtportable script function to read data from network devices
Unity3d getlaunchintintforpackage getting package returned null
Prefabricated dishes in the trillion market have also begun to roll inside. How can brands stand out in the fierce competition?
Stack栈的实现
Basic operation and question type summary of binary tree
Xshell不小心按到ctrl+s造成页面锁定的解决办法
[Yu Yue education] General English of Shenyang Institute of Engineering (4) reference materials
打印机无法打印测试页是什么原因
360 online enterprise security cloud is open to small, medium and micro enterprises for free
【NodeJs】Electron安装