栈的创建及POP,PUSH操作

#include<stdlib.h>
#include<iostream>

using namespace std;

typedef struct Node{
  char ch;
  Node * next;
  Node * prior;
}Node,* Bottom,* Top;

//创建栈
Node * createStack()
{
  char str;//int len=0;
  Bottom bottom=(Node *)malloc(sizeof(Node));
  Top top=bottom;
  cin>>str;
  bottom->ch=NULL;
  while(str!='n')
  {
     Node *s=(Node*)malloc(sizeof(Node));
     s->ch=str;
     top->next=s;
     s->prior=top;
     top=top->next;
     cin>>str;
    //len++;
  }
  top->next=NULL;
  return top;
}

//对POP出栈顶元素,并返回栈顶指针
Top pop(Top top)
{
  //ch=top->ch;
  //Node * p=top;
  top=top->prior;
  //free(p);
  return top;
}

//将元素压入栈
void push(Top top,char ch)
{
  Node *s=(Node *)malloc(sizeof(Node));
  s->ch=ch;
  top->next=s;
  s->prior=top;
  top=top->next;
}

//根据后进先出原则读取栈的所有元素
void procOutput(Top top)
{
  while(top->ch!=NULL)
  {
    cout<<top->ch<<" ";
    top=top->prior;
  }
  cout<<"\n";
}

int main()
{
  char _char;
  Top top=createStack();
  top=pop(top);
  procOutput(top);
  cin>>_char;
  push(top,_char);
  procOutput(top);
  return 0;
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值