zoukankan      html  css  js  c++  java
  • 《数据结构》 栈代码操作集合

    栈的基本操作代码,来自《数据结构-用C语言描述》(第二版)高教社

    栈的数据结构相当于受限制(只能从栈顶取元素,先进后出LIFO)的顺序表或单链表,可以参考之前的博客。

    /*以下为顺序栈*/
    #define Stack_Size 50   /*设栈中元素为50*/
    typedef struct {
        StackElemType elem[Stack_Size];
        int top;    //用来存放栈顶元素的下标
    } SeqStack;
    /*初始化*/
    void InitStack(SeqStck) {
        S->top = -1;
    }
    /*进栈:将x置入新栈顶*/
    int Push(SeqStack *S, StackElemType x) {
        if(S->top == Stack_Size -1) {
            return(FALSE);
        }
        S->top++;
        S->elem[S->top] = x;
        return(TRUE);
    }
    /*出栈*/
    int Pop(SeqStack *S, StackElemType *x) {
        if(S->top = -1) {
            return(FALSE);
        }
        else {
            *x = S->elem[top];
            top--;      //修改栈顶指针 *x = S->elem[top--]
            return(TRUE);
        }
    }
    /*读栈顶*/
    int GetTop(SeqStack *S, StackElemType *x) {
        if(top = -1) {
            return(FALSE);
        }
        else {
            *x = S->elem[S-top];
            return(TRUE);
        }
    }
    /*以下为链栈*/
    typedef struct node {
        StackElemType data;
        struct node *next;
    } LinkStackNode, *LinkStack;
    /*初始化;即单链表的初始化*/
    InitLink(LinkStack *top) {
        *top =(LinkStack)malloc(sizeof(Node));
        (*top)->next = NULL;
    }
    /*进栈*/
    int Push(LinkStack top, StackElemType x) {
        LinkStackNode *temp;
        temp = (LinkStackNode *)malloc(sizeof(LinkStackNode));
        if(temp == NULL) {
            return(FALSE);
        }
        temp->data = x;
        temp->next = top->next;
        top->next = temp;
        return (TRUE);
    }
    int Pop(LinkStack top, StackElemType *x) {
        LinkStackNode *temp;
        temp = top->next;
        if(top == NULL){
            return (FALSE);
        }
        top->next = temp->next;
        *x = temp->data;
        free(temp);
        return(TRUE);
    }
  • 相关阅读:
    python实例26[查询修改文件的属性]
    [SCM]源码管理 VisualSVN Server+TortoiseSVN
    持续集成之“依赖管理”
    Spoon:在“云”上运行桌面应用程序
    数字签名的验证
    判断Linux/Unix为32位或64位
    持续集成理论和实践的新进展
    [SCM]源码管理 SVN Server
    [BuildRelease Management]Parabuild
    为VM增加磁盘空间
  • 原文地址:https://www.cnblogs.com/wanghongze95/p/13842661.html
Copyright © 2011-2022 走看看