• 栈基本操作


    PS1:

    顺序栈:

     1 //ADT Stack
     2 //line
     3 #define MaxStackSize 100
     4 typedef int ElemType;
     5 typedef struct
     6 {
     7  ElemType stack[MaxStackSize];
     8  int top;
     9 }Stack;
    10 void InitStack(Stack *S)
    11 {
    12  if((S=(Stack*)malloc(sizeof(Stack)))==NULL)
    13   exit(OVERFLOW);
    14  S->top=-1;
    15 }
    16 int StackEmpty(Stack S)
    17 {
    18  if(S.top==-1)
    19   return 1;
    20  else return 0;
    21 }
    22 void StackPush(Stack*S,ElemType elem)
    23 {
    24  if(S->top==MaxStackSize-1)
    25  {
    26   pritnf("%d","Stack is full");
    27   exit(0);
    28  }
    29  else
    30  S-top++;
    31  S->top=elem;
    32 }
    33 void StackPop(Stack *S ,ElemType * elem)
    34 {
    35  if(StackEmpty(*S))
    36  {
    37   printf("Stack is empty");
    38   exit(0);
    39  }
    40  else
    41  *elem=S->top--;
    42 }
    43 void Gettop(Stack S,ElemType * elem)
    44 {
    45  if(StackEmpty(*S))
    46  {
    47   printf("Stack is empty");
    48   exit(0);
    49  }
    50  else
    51  *elem=S.top;
    52 }

    PS2:

    链式栈:

     1 //ADT Stack
     2 //Node
     3 typedef int ElemType;
     4 typedef struct
     5 {
     6  ElemType data;
     7  Struct StackNode *next;
     8 }StackNode;
     9 typedef struct
    10 {
    11  StackNode *top;
    12  int length;
    13 }Stack;
    14 void InitStack(Stack *S)
    15 {
    16  S->top=NULL;
    17 }
    18 void Push(Stack *S,ElemType elem)
    19 {
    20  p=(StackNode*)malloc(sizeof(StackNode));
    21  if(!p)
    22  exit(OVERFLOW);
    23  else
    24  {
    25   p->data=elem;
    26   p->next=S->top;
    27   S->top=p;
    28  }
    29 }
    30 void Pop(Stack *S,ElemType &elem)
    31 {
    32  if(StackEmpty(*S))
    33   exit("Stack is empty");
    34  else
    35  {
    36   elem=S->top->data;
    37   p=S->top;
    38   S->top=p->next;
    39   free(p);
    40  }
    41 }
    42 void GetTop(Stack S,ElemType &elem)
    43 {
    44  if(StackEmpty(S))
    45   exit("Stack is empty");
    46  else
    47   elem=S.top->data;
    48 }
    49 int StackEmpty(Stack S)
    50 {
    51  if(S.top==NULL)
    52   return 1;
    53  else
    54   return 0;
    55 }
  • 相关阅读:
    hdu 2019 数列有序!
    hdu 2023 求平均成绩
    HDU 5805 NanoApe Loves Sequence (思维题) BestCoder Round #86 1002
    51nod 1264 线段相交
    Gym 100801A Alex Origami Squares (求正方形边长)
    HDU 5512 Pagodas (gcd)
    HDU 5510 Bazinga (字符串匹配)
    UVALive 7269 Snake Carpet (构造)
    UVALive 7270 Osu! Master (阅读理解题)
    UVALive 7267 Mysterious Antiques in Sackler Museum (判断长方形)
  • 原文地址:https://www.cnblogs.com/pengjunwei/p/3677682.html
Copyright © 2020-2023  润新知