今天学习了栈的C++实现,跟单链表很像:

push相当于单链表在第一个位置插入元素;

pop相当于单链表在第一个位置删除元素;

1、空栈检查

 int stack::isEmpty(Stack *S)
{
return S->next == NULL;
}

2、创建一个空栈

 stack::Stack *stack::createStack()
{
Stack *S;
S = (Stack*)new(Stack);
//栈空间满后,溢出
if (S == NULL)
cout << "Out of space! " << '\n';
S->next = NULL;
makeEmpty(S);
return S;
}

空栈只有头结点,第9行表示若不为空栈则删除除头结点以外的所有结点。

3、清空栈(保留头结点)

 void stack::makeEmpty(Stack *S)
{
if (isEmpty(S))
cout << "Donnot need to makeEmpty!" << '\n';
else
while (!isEmpty(S))
pop(S);
}

4、push操作

 stack::Stack *stack::push(int x, Stack *S)
{
Stack *tem;
tem = (Stack*)new(Stack);
if (tem == NULL)
{
cout << "Out of space! " << '\n';
}
else
{
cout << "please input the data to push: " << '\n'; scanf_s("%d",&x); tem->Data = x;
tem->next = S->next;
S->next = tem;
return S;
}
}

5、top操作

 int stack::top(Stack *S)
{
if (isEmpty(S))
{
cout << "Empty stack! " << '\n';
return -;
}
else
return S->next->Data;
}

6、pop操作(释放第一个结点后,显示该结点的数据元素)

 stack::Stack *stack::pop(Stack *S)
{
Stack *p;
p = NULL;
if (isEmpty(S))
cout << "Empty stack! " << '\n';
else
{
p = S->next;
cout << "the Data be poped is : " << p->Data << endl;
S->next = p->next;
free(p);
return S;
}
}

7、处理栈(删除包括头结点)

 void stack::disposeStack(Stack *S)
{
if (S == NULL)
cout << "Donnot need to disposeStack! " << '\n';
while (!isEmpty(S))
pop(S);
free(S);
}

8、主函数

 int main()
{
cout << '\n' << "***************************************" << '\n' << '\n';
cout << "Welcome to the stack world! " << '\n';
cout << '\n' << "***************************************" << '\n' << '\n'; int i = ;
int j = ;
int topElement = ;
stack *a = new stack;
stack::Stack *S = NULL;
int x = ;
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(x, S);
break;
case :
cout << "pop now begin : ";
S = a->pop(S);
break;
default:
cout << "End the stack. ";
a->disposeStack(S);
i = ;
break;
} }
}

运行结果:

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

  1. 29. 栈的push,pop序列

    题目:给定2个整数序列,其中1个是栈的push顺序,判断另一个有没有可能是对应的pop顺序 解:其实这题主要是判断进栈次数和出栈次数誓不是相等.我是用栈作的,效率不高,每一个元素最多出栈1次,进栈1此 ...

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

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

  3. php实现栈操作(不用push pop 库函数)

    直接上代码 <?php /*php不用库函数实现栈操作 * @author Geyaru 2019-04-20 */ class stack{ private $top = -1; //栈指针初 ...

  4. 汇编 push ,pop指令

    知识点:  PUSH  POP  CALL堆栈平衡  RETN指令 一.PUSH入栈指令 (压栈指令): 格式: PUSH 操作数 //sub esp,4 ;mov [esp],EBP 操作数 ...

  5. 用OC实现一个栈:结合单链表创建动态栈

    一.介绍 栈是一种数据存储结构,存储的数据具有先进后出的特点.栈一般分为动态栈和静态栈. 静态栈比较好理解,例如用数组实现的栈.动态栈可以用链表来实现. 方式:固定base指针,每次更改top指向入栈 ...

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

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

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

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

  8. 【PAT甲级】1051 Pop Sequence (25 分)(栈的模拟)

    题意: 输入三个正整数M,N,K(<=1000),分别代表栈的容量,序列长度和输入序列的组数.接着输入K组出栈序列,输出是否可能以该序列的顺序出栈.数字1~N按照顺序随机入栈(入栈时机随机,未知 ...

  9. 1051 Pop Sequence (25分)栈

    刷题 题意:栈的容量是5,从1~7这7个数字,写5个测试数据 做法:模拟栈 #include<bits/stdc++.h> using namespace std; const int m ...

随机推荐

  1. C语言字符串操作常用库函数

    C语言字符串操作常用库函数 *********************************************************************************** 函数 ...

  2. mysql replace into用法与坑

    需要主键一致 PRIMARY KEY (id) PRIMARY KEY (id, ts) 坑: https://blog.xupeng.me/2013/10/11/mysql-replace-into ...

  3. hdu1078 dp(递推)+搜索

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1078 题意:老鼠从(1.1)点出发,每次最多只能走K步,而且下一步走的位置的值必须必当前值 ...

  4. CSS3-animation,表格表单的格式化

    animation 1.与transition一样,animation在IE9之前都不支持,不仅如此,还需要大量的供应商前缀 2.定义关键帧:@内容中需要大量的前缀 @keyframes  fadeI ...

  5. 去除手机端a标签等按下去背景色

    a,button,input,textarea,label,i,em{/*highlight*/ -webkit-tap-highlight-color: rgba(255,0,0,0); borde ...

  6. scau 8637 阶乘与因子 筛素数

    时间限制:500MS  内存限制:1000K提交次数:189 通过次数:46 题型: 编程题   语言: G++;GCC Description 游戏玩了很久总会厌的,连Lyd的蚂蚁都被放生了.... ...

  7. 1、Delphi 打开目录和txt文件模块

    //1.打开目录和打开txt文件 procedure TMainForm.bbtnOpenLoClick(Sender: TObject); var sLogName: string; begin s ...

  8. 我的c++学习(4) C++输入输出格式的控制

    默认进制:默认状态下,数据按十进制输入输出.如果要求按八进制或十六进制输入输出,在cin或cout中必须指明相应的数据形式,oct为八进制,hex为十六进制,dec为十进制. #include &qu ...

  9. FString的相关文档,另外还有4种LOG的方法

    https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/StringHandling/FString/index ...

  10. Angular JS笔记

    1.引导程序 使用ng-app开始引导一个程序:标记了AngularJS应用的作用域 <!doctype html> <html lang="en" ng-app ...