Dynamic stack sequentially stored a basic operation (initialization, the stack, access to the top element, the stack)

#include <stdio.h>
#include <malloc.h>
#define 20 is the MAXSIZE
#define elemType int
typedef struct {// SQtack dynamic sequential storage structure, a storage space with the stack size variations
 ElemType * top, * bottom; // define bottom of the stack and the stack
 int stacksize; // stack of storage space
} SQtack;


void Init_stack (SQtack & S) { // Initialization stack
 S.bottom = (elemType *) the malloc (the MAXSIZE * the sizeof (elemType));
 IF the printf ( "not applied to the memory \ n-") (S.bottom!);
 S .top = S.bottom; // initialize the stack, the stack and the bottom stack so that points to the same location
 S.stacksize the MAXSIZE =;
}


Push_stack void (SQtack & S) {
 elemType E;
 the printf ( "Enter stack elements: \ n-");
 Scanf ( "% D", & E);
 the while (E = -. 1!) {
  IF (S.top-S. ==. 1 S.stacksize-bottom) {
   the printf ( "full stack"); // used here-bottom = STACKSIZE-Top. 1
   // determines whether the stack is full, a waste of stack space,
  } the else {
  // here by way of the first discharge element and then jerk
   * = E S.top;
   S.top ++;
   Scanf ( "% D", & E);
     }
 }
}


void Get_top (SQtack & S) { // Stack Access elements
 elemType top_e;
 IF (S.bottom = S.top!) {
  top_e * = (. 1-S.top);
  the printf ( "% D: stack \ n" , top_e);
 }
}


Pop_stack void (SQtack & S) {
 elemType A;
 IF (S.top == S.bottom) {
  the printf ( "empty stack");
 } the else {
  S.top -; // Save Save first, and then the stack
  a = S.top *;
  the printf ( "% D: the stack \ n-", A);
 }
}


main int () {
 SQtack S;
 Init_stack (S); // initialize stack
 Push_stack (S); // stack
 Get_top (S); // access to the top element, the stack also 
 Pop_stack (S); // the top element from the stack
 Get_top (S);
 return 0;
}

Guess you like

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