今天学习了利用数组方式的栈的C++实现,这种方式跟指针实现有很多不一样的地方:

栈的指针实现,栈的创建申请头结点,push需要申请新的结点,pop释放结点,这些结点都放在第一个位置,top时,S->next->data即可。

栈的数组实现,只申请一个结点,该结点的结构体内包含,数组的最大容量、栈顶元素下标、指向整形数组的指针(用于存放和删除新的元素)。

S->topOfStack == -1,空栈;

S->topOfStack == S->capacity - 1,满栈;

1、声明结点

     struct Node
{
int capacity; //数组的最大容量
int topOfStack; //栈顶下标为-1表示空栈,每次添加新的元素,栈顶元素下标加1
int *Array; //指向整形数组的指针
};
typedef struct Node stack;

2、空栈,满栈判断

 int stackArray::isEmpty(stack *S)
{
return S->topOfStack == emptyTOS;
}
int stackArray::isFull(stack *S)
{
return S->topOfStack == S->capacity - ;
}

3、创建栈

 stackArray::stack *stackArray::createStack(int maxElements)
{
if (maxElements < minStackSize)
cout << "the space of stack is too short,please increase value of maxElements!" << endl; stack *S;
S = (stack*)new(stack);
if (S == NULL)
cout << "Out of space! " << '\n'; S->Array = new int[maxElements];
if (S->Array == NULL)
cout << "Out of space! " << '\n'; S->topOfStack = emptyTOS;   //栈顶下标置-1表示空栈
S->capacity = maxElements;   //数组最大容量赋值
makeEmpty(S);
return S;
}

5、push,top,pop

 stackArray::stack *stackArray::push(stack *S)
{
if (isFull(S))
{
cout << "stack is full!" << endl;
return ;
}
int x = ;
cout << "Please input the data to push: " << endl;
scanf_s("%d", &x);
S->Array[++S->topOfStack] = x;
return S;
}
int stackArray::top(stack *S)
{
if (isEmpty(S)) //非空判断
{
cout << "empty stack! " << endl;
return -;
}
else
return S->Array[S->topOfStack];
}
stackArray::stack *stackArray::pop(stack *S)
{
if (isEmpty(S)) //非空判断
{
cout << "empty stack! " << endl;
return ;
}
else
{
S->topOfStack--;
return S;
}
}

6、主函数

 int main(int argc, char * argv[])
{
cout << '\n' << "***************************************" << '\n' << '\n';
cout << "Welcome to the stackArray world! " << '\n';
cout << '\n' << "***************************************" << '\n' << '\n'; int i = ;
int j = ;
int topElement = ;
stackArray *a = new stackArray;
stackArray::stack *S = NULL;
//int x = 0;
while (i)
{
cout << '\n' << "***************************************" << '\n';
cout << " 0 : end the stack " << '\n';
cout << " 1 : creat a stack " << '\n';
cout << " 2 : display the top element of stack " << '\n';
cout << " 3 : push a node in the stack " << '\n';
cout << " 4 : pop a node from the stack " << '\n';
cout << "***************************************" << '\n';
cout << "Please input the function your want with the number above : " << '\n';
scanf_s("%d", &j); switch (j)
{
case :
cout << "CreatStack now begin : ";
S = a->createStack();
break;
case :
topElement = a->top(S);
cout << "The top element of stack is : " << topElement;
break;
case :
cout << "push now begin : ";
S = a->push(S);
break;
case :
cout << "pop now begin : ";
S = a->pop(S);
break;
default:
cout << "End the stack. ";
a->disposeStack(S);
i = ;
break;
} } return ;
}

效果同指针,不再赘述。

栈的C++实现(数组)——创建-push-pop-top-清空栈-处理栈的更多相关文章

  1. 数组方法push() pop() shift() unshift() splice() sort() reverse() contact()浅拷贝 slice()原数组拷贝

    push() pop() shift() unshift() splice() sort() reverse() 参考资料:https://wangdoc.com/javascript/stdlib/ ...

  2. js数组方法push pop shift unshift的返回值

    push() 方法可向数组的末尾添加一个或多个元素,并返回新的长度. pop() 方法用于删除并返回数组的最后一个元素. unshift() 方法可向数组的开头添加一个或更多元素,并返回新的长度. s ...

  3. JQuery操作数组函数 push(),pop(),unshift(),shift()

    1.array.push() :在数组尾部添加新的元素,并返回新的数组长度. 2.array.unshift() :在数组头部添加新的元素,并返回新的数组长度.[听说IE浏览器不支持] 3.array ...

  4. Android For JNI(四)——C的数组,指针长度,堆内存和栈内存,malloc,学生管理系统

    Android For JNI(四)--C的数组,指针长度,堆内存和栈内存,malloc,学生管理系统 好几天每写JNI了,现在任务也越来越重了,工作的强度有点高,还有好几个系列的博客要等着更新,几本 ...

  5. js开发:数组的push()、pop()、shift()和unshift()(转)

    js开发:数组的push().pop().shift()和unshift() 2017-05-18 11:49 1534人阅读 评论(0) 收藏 举报  分类: javascript开发(22)  版 ...

  6. js数组定义、属性及方法(push/pop/unshfit/shfit/reverse/sort/slice/splice/indexOf/lastIndexOf)

    数组 一.定义数组 * 字面量方式  var 数组名称 = [ value,value,... ] * 构造函数方式 var 数组名称 = new Array(value,value,...):  v ...

  7. 数据结构---设计一个栈,push, pop, min 时间复杂度都是 O(1)

    普通的栈,push, pop 操作的复杂度是 O(1), 但是如果要找出其中的最小值,则需要 O(N)的时间. 题目要求 min 复杂度也是 O(1), 做法便是 空间换时间,每一步栈的最小值都用一个 ...

  8. js中常用数组方法concat join push pop slice splice shift

    javascript给我们很多常用的 数组方法,极大方便了我们做程序.下面我们来介绍下常用的集中数组方法. 比如 concat() join() push() pop() unshift() shif ...

  9. Js数组的操作push,pop,shift,unshift等方法详细介绍

    js中针对数组操作的方法还是比较多的,今天突然想到来总结一下,也算是温故而知新吧.不过不会针对每个方法进行讲解,我只是选择其中的一些来讲. 首 先来讲一下push和pop方法,这两个方法只会对数组从尾 ...

  10. js 的数组怎么push一个对象. Js数组的操作push,pop,shift,unshift JavaScrip

    push()函数用于向当前数组的添加一个或多个元素,并返回新的数组长度.新的元素将会依次添加到数组的末尾. 该函数属于Array对象,所有主流浏览器均支持该函数. 语法 array.push( ite ...

随机推荐

  1. cdh环境下,spark streaming与flume的集成问题总结

    文章发自:http://www.cnblogs.com/hark0623/p/4170156.html  转发请注明 如何做集成,其实特别简单,网上其实就是教程. http://blog.csdn.n ...

  2. C# 使用线程池,设置每个线程的执行时间,过了时间强制结束

    改用thread来驱动这个方法先建一个类下面的成员,来存放进程List<Thread> pool = new List<Thread>();在需要启动你的方法的时候建进城Thr ...

  3. 到程序集里取DLL

    C:\Windows\assembly\gac_msil

  4. 用div,ul,input模拟select下拉框

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  5. 《DSP using MATLAB》示例Example4.11

    代码: b = [1, 0]; a = [1, -0.9]; % %% ---------------------------------------------- %% START a determ ...

  6. Chage

    For many times,i've given my own a new lifestyle,such as don't stay up late,have breakfast......whil ...

  7. 使用Spring发送邮件

    http://www.oschina.net/code/snippet_253813_36503

  8. Experimental Educational Round: VolBIT Formulas Blitz

    cf的一次数学场... 递推 C 题意:长度<=n的数只含有7或8的个数 分析:每一位都有2种可能,累加不同长度的方案数就是总方案数 组合 G 题意:将5个苹果和3个梨放进n个不同的盒子里的方案 ...

  9. apache activemq的重连

    1.activemq的重连机制 maxReconnectAttempts -1 | 0 From version 5.6 onwards: -1 is default and means retry ...

  10. Colorado Potato Beetle(CF的某道) & 鬼畜宽搜

    题意: 一个人在一张大图上走,给你路径与起点,求他走出的矩形面积并.(大概这个意思自行百度标题... SOL: 与其说这是一道图论题不如说是一道生动活泼的STL-vector教学.... 离散化宽搜, ...