入栈/出栈

栈的push/pop操作

#include<iostream>
#include<vector>
using namespace std;
struct node
{
    int data;
    node *next;
};
struct stack_queue
{
    node *bottom;
    node *top;
};
//入栈
stack_queue *push_stack(stack_queue *ST, int num)
{
    //stack_queue *s=new stack_queue;
    node *p=new node;
    p->data=num;
    p->next=NULL;
    if(ST->bottom==NULL)
    {
        ST->bottom=p;
        ST->top=p;
    }
    else
    {
        ST->top->next=p;
        ST->top=p;
    }
    return ST;
}
//出栈
stack_queue *pop_stack(stack_queue *ST)
{
    node *p=new node;
    node *p2=ST->bottom;
    if(ST->bottom==NULL)
    {
        cout<<"overflow"<<endl;
    }
    else
    {
        p=ST->top;
        if(ST->bottom==ST->top)
        {
            ST->bottom=NULL;
            ST->top=NULL;
            delete p;
        }
        else
        {
            while(p2->next!=ST->top) p2=p2->next;
            p2->next=NULL;
            ST->top=p2;
            delete p;
        }
    }
    return ST;
}
//print
void print_stack(stack_queue *s)
{
    node *p=s->bottom;
    vector<int> temp;
    while(p!=NULL)
    {
        temp.push_back(p->data);
        p=p->next;
    }
    cout<<"输出栈"<<endl;
    for(int i=temp.size()-1;i>=0;i--)
        cout<<temp[i]<<endl;
}
int main()
{
    stack_queue *p=new stack_queue;
    stack_queue *p1;
    p->bottom=NULL;
    p->top=NULL;
    int x;
    char c;
    cout<<"输入要入栈的元素:"<<endl;
    while(cin>>x)
    {
        p1=push_stack(p,x);
        cin.get(c);
        if(c==‘\n‘)
            break;
    }
    //print_stack(p1);
    stack_queue *p2=pop_stack(p1);
    print_stack(p2);
    return 0;
}
时间: 2024-08-06 19:46:01

入栈/出栈的相关文章

BZOJ 3786 星系探索 Splay维护树的入栈出栈序

题目大意:给出一棵树,要求有以下这些操作:1.求出一个节点到根的点权和.2.将一个节点的父亲改变.3.将一个子树中的每一个节点都加上一个权值. 思路:LCT就不用想了,因为有子树操作.然后就是一个很神奇的东西了,就是Splay维护树的入栈出栈序.这个玩应是做了这个题之后才知道的.但是感觉真的很dio. 首先,我们先按照题意,将树建出来.然后从根开始深搜,这样一个点进入DFS函数和出DFS函数的时候就会有两个时间点,就是入栈的时间和出栈的时间.然后利用Splay维护一个序列,就是入栈出栈的顺序.在

顺序栈的初始化入栈出栈以及打印栈的信息

使用的开发工具CLion CLion 2017.2.1 Build #CL-172.3544.40, built on August 2, 2017Licensed to CLion EvaluatorExpiration date: September 15, 2017JRE: 1.8.0_152-release-915-b6 x86_64JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.oMac OS X 10.12.4   1 #include

压栈出栈遍历栈实例代码

#include<stdio.h> #include<stdlib.h> #include<malloc.h> typedef struct Node//定义一个链表结构体 { int data; struct Node* pNext; }NODE,*PNODE; typedef struct Stack//定义一个栈结构体 { PNODE pTop; PNODE pBottom; }STACK,*PSTACK; void initStack(PSTACK); void

栈的链式存储结构和入栈出栈操作

参考<大话数据结构>P98~99——栈的链式存储结构. 进栈: 出栈: 举个简单的例子: 代码和解释如下(VS2012测试通过): 1 #include <iostream> 2 #include <string> 3 using namespace std; 4 5 typedef string status;//用书上推荐的status返回是否成功,C++中的模板类string比字符数组char[]更方便 6 7 //栈的结点 8 //包含data,和指向下一个结点

两栈共享空间的存储结构和入栈出栈操作

参考<大话数据结构>P95~96——两栈共享存储空间. 当两个栈的需求空间有相反关系时,也就是一个栈增长时,另一个栈在缩短,可以采用两栈共享空间结构.这是针对两个具有相同数据类型的栈的一个设计技巧. 举个简单的例子: 代码和解释如下(VS2012测试通过): 1 #include <iostream> 2 #include <string> 3 using namespace std; 4 5 #define MAXSIZE 6 //本例中栈满共6个元素 6 typed

编程实现栈的入栈/出栈操作

完整代码如下,其实队栈都只是链表的一种变化而已 #include <stdio.h> #include <stdlib.h> typedef struct student * PNode; typedef struct stacklink * PStack; typedef struct student { int data; PNode next; }Node; typedef struct stacklink { PNode zhandi; PNode top; }Stack;

23 入栈 出栈

分析下列程序输出:string int main() { StackS; char x,y; Initstack(S); x='n';y='g'; Push(S,x);Push(S,'i');Push(S,y); Pop(S,x);Push(S,'r');Push(S,'t');Push(S,x); Pop(S,x);Push(S,' s'); while(!StackEmpty(S)) { Pop(S,y);printf(y); }; Printf(x); } 分析:看完如下的结果即可.补充一

Python模拟入栈出栈操作

目标: 1.编写菜单,提示用户操作选项(push,pop,view,quit) 2.规则:定义列表,先入栈,后出栈,后入栈,先出栈 1.模拟入栈.出栈操作 >>> list1 = [] >>> list1.append('a') >>> list1 ['a'] >>> list1.append('b') >>> list1 ['a', 'b'] >>> list1.pop() 'b' >>

2.1.1线性表实现栈的建立,入栈,出栈等操作

#include <stdio.h> #include <stdlib.h> #define STACK_INIT_SIZE 100 #define STACK_INCREMENT 10 #define OK 1 #define ERROR 0 #define OVERFLOW -1 typedef int status; typedef int SElemType; typedef struct { SElemType *base;//在构造之前和销毁之后,base的值为NULL