zoukankan      html  css  js  c++  java
  • 链栈

    #include<stdio.h>
    #include<stdlib.h>
    typedef int type;
    typedef struct Node
    {
        type info;
        struct Node*next;
    }node;
    //返回一个栈的头指针
    node*init()
    {
        node*head=(node*)malloc(sizeof(node));
        head->next=NULL;
        return head;
    }
    //将值为x的节点压栈
    void push(node*head,type x)
    {
        node*p=(node*)malloc(sizeof(node));
        p->info=x;
        p->next=head->next;
        head->next=p;
    }
    //打印栈
    void display(node*head)
    {
        node*p=head->next;
        while(p){
            printf("%5d",p->info);
            p=p->next;
        }
        printf("
    ");
    }
    //销毁栈
    void destory(node*head)
    {
        node*p=head,*q;
        while(p){
            q=p->next;
            free(p);
            p=q;
        }
    }
    //判断栈是否为空
    int empty(node*head)
    {
        return head?0:1;
    }
    //出栈
    void pop(node*head)
    {
        if(!empty(head)){
                node*p=head->next;
                head->next=p->next;
                free(p);
            }
        else printf("error
    ");
    }
    //返回栈顶元素
    type top(node*head)
    {
        if(!empty(head))
            return head->next->info;
        else {
            printf("error
    ");
            return -1;
        }
    }
    

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    Daily Scrum
    Daily Scrum
    Daily Scrum
    Daily Scrum
    Daily Scrum
    bing背单词交互流程
    立会2015-11-23
    每日例会11.16
    每日立会2015-11-11
    单词挑战设计0.1
  • 原文地址:https://www.cnblogs.com/Thereisnospon/p/4768523.html
Copyright © 2011-2022 走看看