栈也是一种线性表,只不过是受限制的线性表
只能从一端读取或者删除元素。先进后出特性(FILO)
栈也分为顺序栈和链表栈
顺序栈不需要额外的空间来指明下个节点的位置,空间利用率高
但是不能动态扩容,而链表栈刚好与之相对。下面是两种栈的代码
顺序栈
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #define OK 1 #define ERROR 0 #define MAX 100 #define DELAY 1 typedef int ElemType; typedef int Status; typedef struct{ ElemType * base; ElemType * top; } Sqstack; Status initStack(Sqstack * stack){ stack->base = (ElemType *)malloc(MAX * sizeof(ElemType)); stack->top = stack->base; return OK; } Status stackEmpty(Sqstack stack){ if(stack.top == stack.base)return OK; else return ERROR; } int stackLength(Sqstack stack){ int count = stack.top - stack.base; return count; } Status getTopValue(Sqstack stack,ElemType * e){ *e = *(stack.top-1); return OK; } Status Push(Sqstack *stack,ElemType e){ if((stack->top - stack->base) >= MAX)return ERROR; *(stack->top) = e; stack->top++; return OK; } Status Pop(Sqstack *stack,ElemType * e){ if(stack->top == stack->base)return ERROR; *e = *(stack->top-1); stack->top--; return OK; } Status printStack(Sqstack stack){ if( stackEmpty(stack) ){ printf("empty "); return OK; } printf("stack len is %d ",stackLength(stack)); while(stack.base < stack.top){ printf("[%d] ",*(stack.base)); stack.base++; } printf(" "); return OK; } int main(){ Sqstack stack; ElemType value; initStack(&stack); while(1){ system("clear"); printf("p = push P = pop "); printf("g = get Value e = exit "); printStack(stack); printf("enter a your choose:"); char sel = getchar(); if( sel == 'p'){ printf("enter push value:"); scanf("%d",&value); Push(&stack,value); sleep(DELAY); } else if(sel == 'P'){ Pop(&stack,&value); printf("value:%d is poped ",value); sleep(DELAY); } else if(sel == 'g'){ getTopValue(stack,&value); printf("value is %d ",value); sleep(DELAY); } else if(sel == 'e'){ exit(0); } } }
链表栈
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #define OK 1 #define ERROR 0 typedef int ElemType; typedef int Status; typedef struct Node{ ElemType data; struct Node * next; } Node; Node * initStack(){ Node * s = (Node*)malloc(sizeof(Node)); s->next = NULL; return s; } Status Push(Node *s,ElemType e){ Node *new = (Node*)malloc(sizeof(Node)); new->data = e; new->next = s->next; s->next = new; return OK; } Status Pop(Node *s,ElemType *e){ Node *top = s->next; *e = top->data; s->next = top->next; free(top); return OK; } ElemType getTopvalue(Node *s){ Node *top = s->next; return top->data; } int getLength(Node *s){ int len=0; while(s->next){ s=s->next; len++; } return len; } Status stackEmpty(Node *s){ if(s->next)return ERROR; else return OK; } Status printStack(Node *s){ if(stackEmpty(s)){printf("stack is empty ");return ERROR;} else printf("stack len is %d ",getLength(s)); while(s=s->next)printf("[%d]",s->data); printf(" "); return OK; } int main(){ char sel; ElemType val; Node * s = initStack(); while(1){ system("clear"); printStack(s); printf("enter code:"); sel = getchar(); if(sel == 'p'){ printf("enter value:"); scanf("%d",&val); Push(s,val); } if(sel == 'P'){ Pop(s,&val); printf("%d is poped ",val); sleep(1); } if(sel == 'g'){ printf("top value is %d ",getTopvalue(s)); sleep(1); } if(sel == 'e')exit(0); } }