当前位置:网站首页>堆栈的实现之顺序存储,链式存储
堆栈的实现之顺序存储,链式存储
2022-07-17 22:49:00 【疯疯癫癫才自由】
栈的顺序存储结构由一个一维数组和一个记录栈顶元素位置的变量组成,
另外还可以用一个变量来存储堆栈的最大容量Maxsize
/**
栈的顺序存储结构由一个一维数组和一个记录栈顶元素位置的变量组成,
另外还可以用一个变量来存储堆栈的最大容量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("堆栈满\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("堆栈空\n");
return error;
}
else
{
return (S->Data[(S->Top)--]);
}
}
*/堆栈的链式存储实现:
/**
堆栈的链式存储实现:
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
#define error NULL
typedef int ElementType;
const ElementType inf=1e9;
typedef struct SNode * PtrToLNode;
// 这里的位置就是数组的整型下标,从0开始,前面提到的位序是指第几个,从1开始
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 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("堆栈空\n");
return inf;
}
else
{
FirstCell=S->next;
TopElem=FirstCell->data;
S->next=FirstCell->next;
free(FirstCell);
return TopElem;
}
}边栏推荐
- A - trees on the level
- 2021.07.13【B站】是这样崩的
- 揭开服务网格~Istio Service Mesh神秘的面纱
- Distributed transaction summary
- High performance pxie data preprocessing board based on kinex ultrascale series FPGA (ku060 +fmc sub card interface)
- TDesign compositionapi refactoring path
- kube-proxy & Service & Endpoint
- Read the paper: temporary graph networks for deep learning on dynamic graphs
- Domestic fpga/dsp/zynq Chip & board scheme
- GYM103660E.Disjoint Path On Tree 树上计数
猜你喜欢

原始套接字

【微服务】 微服务学习笔记三:利用Feign替换RestTemplate完成远程调用
![[XSS range 10-14] insert when you see parameters: find hidden parameters and various attributes](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[XSS range 10-14] insert when you see parameters: find hidden parameters and various attributes

B树

Leetcode 1275. 找出井字棋的获胜者

Wechat applet 7 cloud storage

Li Hongyi machine learning -- return to July 13, 2022

长安链学习研究-存储分析wal机制

High performance pxie data preprocessing board based on kinex ultrascale series FPGA (ku060 +fmc sub card interface)

Characteristics of DMA mode
随机推荐
UVA340 Master-Mind Hints
Leetcode 1275. Find out the winner of tic tac toe
MySQL installation
BigScience 开源 Bloom 的自然语言处理模型
Istio XDS配置生成实现
kube-proxy & Service & Endpoint
GYM103660E.Disjoint Path On Tree 树上计数
傅里叶变换的再理解
07_服务注册与发现总结
Cilium & Hubble
Leetcode 1296. 划分数组为连续数字的集合(提供一种思路)
2. MySQL introduction
Mongodb partition cluster construction
人脸技术:不清楚人照片修复成高质量高清晰图像框架(附源代码下载)
UVA - 12096 The SetStack Computer
Unveil the mystery of service grid istio service mesh
Knapsack problem
GYM103660E. Disjoint path on tree count
FPGA (VGA Protocol Implementation)
[code attached] how to realize handwritten digit recognition with hog+svm