当前位置:网站首页>Sequential storage and chain storage of stack implementation
Sequential storage and chain storage of stack implementation
2022-07-27 08:41:00 【Madness makes freedom】
The sequential storage structure of the stack consists of a one-dimensional array and a variable that records the position of the elements at the top of the stack ,
In addition, a variable can be used to store the maximum capacity of the stack Maxsize
/**
The sequential storage structure of the stack consists of a one-dimensional array and a variable that records the position of the elements at the top of the stack ,
In addition, a variable can be used to store the maximum capacity of the stack Maxsize
*/
/**
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
#define error -1
const int Maxsize = 100;
typedef int ElementType;
typedef int Position;
typedef struct SNode * PtrToSNode;
struct SNode
{
ElementType *Data;
Position Top;
int Maxsize;
};
typedef PtrToSNode Stack;
Stack CreateStack(int Maxsize);
bool IsFull(Stack S);
bool Push(Stack S,ElementType X);
bool IsEmpty(Stack S);
ElementType Pop(Stack S);
int main()
{
Stack S=CreateStack(Maxsize);
for(int i=1;i<50000000;i+=i)
Push(S,i);
for(int i=0,T=S->Top;i<=T+5;++i)
{
auto tem=Pop(S);
if(tem!=error)
printf("%d ",tem);
}
return 0;
}
Stack CreateStack(int Maxsize)
{
Stack S=(Stack) malloc(sizeof(SNode));
S->Data=(ElementType *) malloc(Maxsize * sizeof(ElementType));
S->Top=-1;
S->Maxsize=Maxsize;
return S;
}
bool IsFull(Stack S)
{
return (S->Top==S->Maxsize-1);
}
bool Push(Stack S,ElementType X)
{
if(IsFull(S))
{
printf(" The stack is full \n");
return false;
}
else
{
S->Data[++(S->Top)]=X;
return true;
}
}
bool IsEmpty(Stack S)
{
return (S->Top==-1);
}
ElementType Pop(Stack S)
{
if(IsEmpty(S))
{
printf(" Stack empty \n");
return error;
}
else
{
return (S->Data[(S->Top)--]);
}
}
*/The chain storage implementation of stack :
/**
The chain storage implementation of stack :
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
#define error NULL
typedef int ElementType;
const ElementType inf=1e9;
typedef struct SNode * PtrToLNode;
// The position here is the integer subscript of the array , from 0 Start , The bit order mentioned above refers to the number , from 1 Start
struct SNode
{
ElementType data;
PtrToLNode next;
};
typedef PtrToLNode Position;
typedef PtrToLNode Stack;
Stack CreateStack();
bool IsFull(Stack S);
bool Push(Stack S,ElementType X);
bool IsEmpty(Stack S);
ElementType Pop(Stack S);
int main()
{
Stack S=CreateStack();
for(int i=1;i<50000000;i+=i)
Push(S,i);
while(!IsEmpty(S))
{
auto tem=Pop(S);
if(tem!=inf)
printf("%d ",tem);
}
return 0;
}
// Stack of leading nodes
Stack CreateStack()
{
Stack S;
S=(Stack) malloc(sizeof(SNode));
S->next=NULL;
return S;
}
bool Push(Stack S,ElementType X)
{
Position Temcell;
Temcell=(PtrToLNode) malloc(sizeof(SNode));
Temcell->data=X;
Temcell->next=S->next;
S->next=Temcell;
return true;
}
bool IsEmpty(Stack S)
{
return (S->next==NULL);
}
ElementType Pop(Stack S)
{
Position FirstCell;
ElementType TopElem;
if(IsEmpty(S))
{
printf(" Stack empty \n");
return inf;
}
else
{
FirstCell=S->next;
TopElem=FirstCell->data;
S->next=FirstCell->next;
free(FirstCell);
return TopElem;
}
}边栏推荐
- Cookie addition, deletion, modification and exception
- Installation and use of Supervisor
- 3311. 最长算术
- The shelf life you filled in has been less than 10 days until now, and it is not allowed to publish. If the actual shelf life is more than 10 days, please truthfully fill in the production date and pu
- 4278. Summit
- Alibaba cloud international receipt message introduction and configuration process
- Connection failed during installation of ros2 [ip: 91.189.91.39 80]
- 网络IO总结文
- Realize SPU management in the background
- 如何卸载--奇安信安全终端管理系统
猜你喜欢

One book 1201 Fibonacci sequence

面试官:什么是脚手架?为什么需要脚手架?常用的脚手架有哪些?

redis 网络IO

Initial summary of flask framework creation project

Realization of backstage brand management function

Eval and assert execute one sentence Trojan horse

Day4 --- flask blueprint and rest ful

On Valentine's day, I drew an object with characters!

After downloading URL loader and specifying the size of the image with limit, the image will not be displayed

Node installation and debugging
随机推荐
MySQL Express
Flutter 渲染机制——GPU线程渲染
好吃难吃饱七分为宜;好喝难喝醉三分为佳
All in one 1251 - Fairy Island for medicine (breadth first search)
VS Code中#include报错(新建的头文件)
How to permanently set source
NIO总结文——一篇读懂NIO整个流程
All in one 1329 cells (breadth first search)
Block, there is a gap between the block elements in the row
开怀一笑
Realization of background channel group management function
面试官:什么是脚手架?为什么需要脚手架?常用的脚手架有哪些?
Day5 - Flame restful request response and Sqlalchemy Foundation
View 的滑动冲突
List delete collection elements
How to view instances of software objects in QSIM?
The following license SolidWorks Standard cannot be obtained, and the use license file cannot be found. (-1,359,2)。
4278. Summit
[uni app advanced practice] take you hand-in-hand to learn the development of a purely practical complex project 1/100
带宽 与 货币