当前位置:网站首页>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;
}
}边栏推荐
- Solution to the program design of the sequence structure of one book (Chapter 1)
- 海量数据肖枫:共建共治openGauss根社区,共享欣欣向荣新生态
- 说透缓存一致性与内存屏障
- Background image related applications - full, adaptive
- Connection failed during installation of ros2 [ip: 91.189.91.39 80]
- Sliding conflict of view
- Management of product pictures
- Make a game by yourself with pyGame 01
- 无法获取下列许可SOLIDWORKS Standard,无法找到使用许可文件。(-1,359,2)。
- Map structure
猜你喜欢

4276. 擅长C

Include error in vs Code (new header file)

UVM Introduction Experiment 1

What are the differences or similarities between "demand fulfillment to settlement" and "purchase to payment"?

All in one 1251 - Fairy Island for medicine (breadth first search)

微信安装包从0.5M暴涨到260M,为什么我们的程序越来越大?

Flink1.15 source code reading Flink clients client execution process (reading is boring)

众昂矿业:新能源行业快速发展,氟化工产品势头强劲
![ROS2安装时出现Connection failed [IP: 91.189.91.39 80]](/img/7f/92b7d44cddc03c58364d8d3f19198a.png)
ROS2安装时出现Connection failed [IP: 91.189.91.39 80]

I drew a Gu ailing with characters!
随机推荐
4274. 后缀表达式
Creation and simple application of QPushButton button button
OPPO 自研大规模知识图谱及其在数智工程中的应用
regular expression
All in one 1251 - Fairy Island for medicine (breadth first search)
开怀一笑
Luogu Taotao picks apples
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
Massive data Xiao Feng: jointly build and govern opengauss root community and share a thriving new ecosystem
第2章 前台数据展现
4275. Dijkstra sequence
Flask login implementation
Node installation and debugging
Minio installation and use
3311. 最长算术
Forced login, seven cattle cloud upload pictures
MCDF top level verification scheme
Minio 安装与使用
Background coupon management
海关总署:这类产品暂停进口