链式栈和链式队列--简单易懂

#include<iostream>
#include<cstdlib>
using namespace std;

typedef int datatype;

typedef struct link_node{
    datatype info;
    struct link_node*next;
}node;  

node *init()
{
    return NULL;
}

int empty(node *top)
{
    return top?0:1;
}

datatype read(node *top)
{
    if(!top)
    {
        cout << "栈空" << endl;
        exit(1);
    }
    
    return top->info;
}

void display(node *top)
{
    node *p;
    p = top;
    if(!p)  cout << "链式栈为空" << endl;
    while(p)
    {
        cout << p->info;
        p = p->next;
    }
}

node* push(node *top,datatype x)
{
    node *p;
    p = new struct link_node();
    p->info = x;
    p->next = top;
    top = p;
    return top;
}

node *pop(node *top)
{
    node* p;
    if(!p)
    {
        cout << "链式栈为空" << endl;
        return NULL;
    }
    p = top;
    top = top->next;
    delete p;
    return top;
}

void recycle(node *top)  //用来删除动态申请内存
{
    node *p = top;
    if(!p)
       exit(1);
    while(top)
    {
        p = top;
        top = top->next;
        delete p;
    }    
}

int main()
{
    node *top ;
    top = init();
    top = push(top,1);
    top = push(top,2);
    top = push(top,3);
    top = push(top,4);
    display(top);
    cout << "栈顶为:" << read(top) << endl;
    recycle(top);
    return 0;
}

 

 

#include<iostream>
#include<cstdlib>
using namespace std;

typedef int datatype;

typedef struct link_node{
    datatype info;
    struct link_node*next;
}node;

typedef struct
{
    node*front,*rear;  //队列的队首和队尾指针,指向第一个和最后一个队列成员
}queue;

queue*init()
{
   queue *qu;
   qu = new queue();
   qu->front = qu->rear = NULL;
   return qu;    
}

int empty(queue qu)
{
   return qu.front?0:1;    
}

void display(queue *qu)
{
   node *p = qu->front;
   if(!p)  cout << "队空" << endl;
   while(p)
   {
           cout << p->info << endl;
           p = p->next;
   }
}

queue*insert(queue*qu,datatype x)
{
    node*p;
    p = new node();
    p->info = x;
    p->next = NULL;
    if(!qu->front)
       qu->front = qu->rear = p;
    else
    {
        qu->rear->next = p;
        qu->rear = p;
    }
    return qu;
}

datatype read(queue *qu)
{
    if(!qu->front){ cout << "队空" << endl;exit(1); }  
    return qu->front->info;
}

queue *del(queue *qu)
{
    node *p;
    if(!qu->front) { cout<<"无法删除"<<endl;return qu;}
    p = qu->front;  //指向队首
    qu->front = p->next;   //队首指针指向下一个结点
    delete p;//用free来没报错,奇怪
    if(!qu->front) qu->rear = NULL;   //队列中唯一元素被删除后,队列为空
    return qu;
}

void recycle(queue *qu)  //用来删除动态申请内存
{
    node *p,*q;
    q = p = qu->front;    
    delete qu; //qu结点一定存在,故先删除
    if(!p)
      exit(1);
    while(q)
    {
        p = q;  //保存一个临时结点,以便用来删除
        q = q->next;
        delete(p);
    }
}

int main()
{
    queue *qu;
    qu = init();
    insert(qu,1);
    insert(qu,2);
    insert(qu,3);
    insert(qu,4);
    display(qu);
    cout << "队首为:" << read(qu) << endl;
    recycle(qu);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值