博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
《数据结构》 栈代码操作集合
阅读量:4122 次
发布时间:2019-05-25

本文共 1496 字,大约阅读时间需要 4 分钟。

栈的基本操作代码,来自《数据结构-用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);}

转载地址:http://ubvpi.baihongyu.com/

你可能感兴趣的文章
Java代码检查工具Checkstyle常见输出结果
查看>>
北京十大情人分手圣地
查看>>
Android自动关机代码
查看>>
Android中启动其他Activity并返回结果
查看>>
2009年33所高校被暂停或被限制招生
查看>>
GlassFish 部署及应用入门
查看>>
iWatch报错: Authorization request cancled
查看>>
iWatch报错: Authorizationsession time out
查看>>
如何运行从网上下载的iWatch项目详细步骤.
查看>>
X-code7 beta error: warning: Is a directory
查看>>
Error: An App ID with identifier "*****" is not avaliable. Please enter a different string.
查看>>
X-code beta 开发iWatch项目,运行没有错误,但是某些操作一点就崩,而且找不错误的原因场景一
查看>>
Xcode 报错: Extra argument in call
查看>>
iTunes Connect 上传APP报错: Communication error. please use diagnostic mode to check connectivity.
查看>>
#import <Cocoa/Cocoa.h> 报错 Lexical or Preprocessor Issue 'Cocoa/Cocoa.h' file not found
查看>>
`MQTTClient (~> 0.2.6)` required by `Podfile`
查看>>
X-Code 报错 ld: library not found for -lAFNetworking
查看>>
Bitcode
查看>>
If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
查看>>
3.5 YOLO9000: Better,Faster,Stronger(YOLO9000:更好,更快,更强)
查看>>