数据结构——栈
栈(stack)又名堆栈,它是一种运算受限的线性表。限定仅在表尾进行插入和删除操作的线性表。这一端被称为栈顶,相对地,把另一端称为栈底。向一个栈插入新元素又称作进栈、入栈或压栈,它是把新元素放到栈顶元素的上面,使之成为新的栈顶元素;从一个栈删除元素又称作出栈或退栈,它是把栈顶元素删除掉,使其相邻的元素成为新的栈顶元素。 栈的形式(如图)栈顶指针->进栈的最后一个元素
栈底指针是一个空结点,由最先入栈元素指向
如图
完整代码
# include <stdio.h> # include <malloc.h> # include <stdlib.h> typedef struct Node { int data; //数据 struct Node * pNext; //指向下一个结点 }NODE, * PNODE; typedef struct Stack { PNODE pTop; //栈顶指针 PNODE pBottom; //栈底指针 }STACK, * PSTACK; //PSTACK 等价于 struct STACK * void init(PSTACK); //初始化创造空结点 void push(PSTACK, int );//压栈 void traverse(PSTACK);//遍历 bool pop(PSTACK, int *);//出栈 void clear(PSTACK pS);//清栈 int main(void) { STACK S; //STACK 等价于 struct Stack int val; init(&S); //目的是造出一个空栈 for (int i = 0; i < 6; i++) { push(&S, i); //压栈 traverse(&S); //遍历输出 } for (int i = 0; i < 6; i++) { pop(&S, &val);//出栈 traverse(&S); //遍历输出 } push(&S, 100); //压栈 traverse(&S); //遍历输出 clear(&S); return 0; } void init(PSTACK pS) { pS->pTop = (PNODE)malloc(sizeof(NODE));//创造空结点给栈地指针; if (NULL == pS->pTop) { printf("动态内存分配失败!\n"); exit(-1); } else { pS->pBottom = pS->pTop;//栈顶指针=栈底指针 pS->pTop->pNext = NULL; //pS->Bottom->pNext = NULL; } } void push(PSTACK pS, int val) { PNODE pNew = (PNODE)malloc(sizeof(NODE)); pNew->data = val; pNew->pNext = pS->pTop; //新结点指向原栈顶指针指向的结点 pS->pTop = pNew; //栈顶指针为指向新结点 return; } void traverse(PSTACK pS) { PNODE p = pS->pTop; while (p != pS->pBottom) { printf("%d ", p->data); p = p->pNext; } printf("\n"); return; } bool empty(PSTACK pS) { if (pS->pTop == pS->pBottom) return true; else return false; } //把pS所指向的栈出栈一次,并把出栈的元素存入pVal形参所指向的变量中,如果出栈失败,返回false,否则返回true bool pop(PSTACK pS, int * pVal) { if ( empty(pS) ) //pS本身存放的就是S的地址 { return false; } else { PNODE r = pS->pTop; *pVal = r->data; pS->pTop = r->pNext; free(r); r = NULL; return true; } } //clear清空 void clear(PSTACK pS) { if (empty(pS)) { return; } else { PNODE p = pS->pTop; PNODE q = NULL; while (p != pS->pBottom) { q = p->pNext; free(p); p = q; } pS->pTop = pS->pBottom; } }View Code
声明与定义
# include <stdio.h> # include <malloc.h> # include <stdlib.h> typedef struct Node { int data; //数据 struct Node * pNext; //指向下一个结点 }NODE, * PNODE; typedef struct Stack { PNODE pTop; //栈顶指针 PNODE pBottom; //栈底指针 }STACK, * PSTACK; //PSTACK 等价于 struct STACK * void init(PSTACK); //初始化创造空结点 void push(PSTACK, int );//压栈 void traverse(PSTACK);//遍历 bool pop(PSTACK, int *);//出栈 void clear(PSTACK pS);//清栈
初始化
void init(PSTACK pS) { pS->pTop = (PNODE)malloc(sizeof(NODE));//创造空结点给栈地指针; if (NULL == pS->pTop) { printf("动态内存分配失败!\n"); exit(-1); } else { pS->pBottom = pS->pTop;//栈顶指针=栈底指针 pS->pTop->pNext = NULL; //pS->Bottom->pNext = NULL; } }
判断是否为空
bool empty(PSTACK pS) { if (pS->pTop == pS->pBottom) return true; else return false; }
压栈
void push(PSTACK pS, int val) { PNODE pNew = (PNODE)malloc(sizeof(NODE)); pNew->data = val; pNew->pNext = pS->pTop; //新结点指向原栈顶指针指向的结点 pS->pTop = pNew; //栈顶指针为指向新结点 return; }
出栈
//把pS所指向的栈出栈一次,并把出栈的元素存入pVal形参所指向的变量中,如果出栈失败,返回false,否则返回true bool pop(PSTACK pS, int * pVal) { if ( empty(pS) ) //pS本身存放的就是S的地址 { return false; } else { PNODE r = pS->pTop; *pVal = r->data; pS->pTop = r->pNext; free(r); r = NULL; return true; } }
遍历
void traverse(PSTACK pS) { PNODE p = pS->pTop; while (p != pS->pBottom) { printf("%d ", p->data); p = p->pNext; } printf("\n"); return; }
清栈
//clear清空 void clear(PSTACK pS) { if (empty(pS)) { return; } else { PNODE p = pS->pTop; PNODE q = NULL; while (p != pS->pBottom) { q = p->pNext; free(p); p = q; } pS->pTop = pS->pBottom; } }