1. 程式人生 > >棧的實現代碼

棧的實現代碼

scan har scanf stack ror lib () 位置 null

棧也是一種線性表,只不過是受限制的線性表

他只能從一端讀取或者刪除元素。棧也分為順序棧和鏈表棧

順序棧不需要額外的空間來指明下個節點的位置,空間利用率高

但是不能動態擴容,而鏈表棧剛好與之相對。

#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);
  }
}

棧的實現代碼