栈的实现代码

Posted Dëm0n

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了栈的实现代码相关的知识,希望对你有一定的参考价值。

栈也是一种线性表,只不过是受限制的线性表

他只能从一端读取或者删除元素。栈也分为顺序栈和链表栈

顺序栈不需要额外的空间来指明下个节点的位置,空间利用率高

但是不能动态扩容,而链表栈刚好与之相对。

#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\n");
    return OK;
  }

  printf("stack len is %d \n",stackLength(stack));

  while(stack.base < stack.top){
     printf("[%d] ",*(stack.base));
     stack.base++;
  }

  printf("\n");
  return OK;
}

int main(){
  Sqstack stack;
  ElemType value;
  initStack(&stack);

  while(1){
    system("clear");
    printf("p = push\nP = pop\n");
    printf("g = get\nValue  e = exit\n");

    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\n",value);
       sleep(DELAY);
    }
  
    else if(sel == g){
       getTopValue(stack,&value);
       printf("value is %d\n",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\n");return ERROR;}
  else printf("stack len is %d\n",getLength(s));  

  while(s=s->next)printf("[%d]",s->data);
  printf("\n");
  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\n",val);
      sleep(1);
    }
 
    if(sel == g){
      printf("top value is %d\n",getTopvalue(s));
      sleep(1);
    }

    if(sel == e)exit(0);
  }
}

 

以上是关于栈的实现代码的主要内容,如果未能解决你的问题,请参考以下文章

数据结构复习代码——栈的顺序实现以及一些基本操作

栈的实现代码

数据结构基础学习——栈的概念及代码实现

栈的实现原理

java数据结构-栈的底层实现:数组实现压入,弹出,判断空栈测试代码

UEFI中USB协议栈的实现简介