The basic operation of the link stack (into the stack, the stack) to be optimized

#include<stdio.h>
#include<malloc.h>
#define ElemType int


typedef struct LinkNode {// stack node type defined
 elemType Data;
 struct * Next LinkNode;
} LinkNode;


typedef struct Stack {// definition of the stack structure
 LinkNode * top; // stack pointer
 int stacksize; // stack size
} LinkStack;
LinkStack Init_stack (LinkStack & S) {
 // LinkStack S;
 S.top = NULL; // stack initialization
 S.stacksize = 0;
 return S;
}


LinkStack Push_stack (LinkStack & S) {
 LinkNode * P;
 elemType A;
 P = (LinkNode *) the malloc (the sizeof (LinkNode));
 IF (P == NULL) {
  the printf ( "overflow");
 } the else {
  the printf ( " input element stack exit n -1 input \ ");
  Scanf ("% D ", & A);
  ! the while (A = -. 1) {
   p-> Data = A;
   p-> Next S.top =;
   S P = .top;
   S.stacksize ++;
   Scanf ( "% D", & A);
  }
 }
 return S;
}


void Pop_stack(LinkStack &S){
 LinkNode *p;
 int a;
 if(S.top==NULL){
  printf("下溢");
 }
 else{
  p=S.top;
  a=S.top->data;
  S.top=S.top->next;
  printf("栈顶元素出栈:%d\n",a);
  free(p);
  printf("%d",S.top->data);
  S.stacksize--;
 }
}


Get_top void (LinkStack & S) {
 elemType A;
 IF (S.top == NULL) {
  the printf ( "empty stack");
 }
 the printf ( "\ n-take top element:");
 the printf ( "% D \ n-" , S.top-> Data);
}


int main(){
 LinkStack S;
 Init_stack(S);
 Push_stack(S);
 //Get_top(S);
 Pop_stack(S);
// Pop_stack(S);
 //Get_top(S);
 return 0;
}

Guess you like

Origin www.cnblogs.com/jiafeng1996/p/11300793.html