Reference books: Data structure (C language Edition) Min Wu Weimin Tsinghua University Press 1. Stack definition
Stacks are linear tables that qualify for inserts or deletions at the end of a table. On the stack, one end of the Allow insert and delete operation is called the top of the stack, and the other end is called the stack Bottom (bottom). A stack that contains no elements is called an empty stack.
In the stack operation, the stack of the insert operation is called into the stack or into the stack, the stack of the removal of operations called the stack or out of the stack.
Brief Description:
As with linear tables, stacks also have two storage representations: 1. Sequential stacks (dynamic array, static array) 2. Chain stack (single linked list, static list)
Analysis: 1.1 order Stack (top is the next position of the topmost element of the stack)
Dynamic Arrays:
#define STACK_INIT_SIZE 100//Initial Allocations
#define Stackincreamence 10//Allocation increment
typedef int SELEMTYPE;
typedef struct sqstack{
Selemtype *base;
Selemtype *top;
int stacksize;//the currently allocated storage space, in element units
}sqstack;
Stack does not exist: base = NULL
Empty stack: top = base
Full stack: top-base >= stacksize
static array:
#define MAXSIZE 100
typedef struct sqstack{
Selemtype Data[maxsize];
int top;//point to top of stack element
}sqstack;
Empty stack: top = 0
Full stack: top = MAXSIZE 1.2 chain stack (single-link table, tops point to stack Element)
typedef struct sqnode{
Selemtype data;
struct Sqnode *next;
}sqnode, *linkstack;
Linkstack top;//Stack Top pointer
Empty stack: Stacknode *top = null 2. Code implementation
This example realizes that the stack's storage structure is a static array of 2.1 basic Definitions
This example implements the sequential stack in a static array, where top is the next position on the stack's topmost element
#include <stdio.h>
#define MAXSIZE
typedef char Selemtype;
typedef struct sqstack{
selemtype data[maxsize];
int top;//point to stack top element
}sqstack;
2.2. Initialize Stack
Initialize empty stack
void Initstack (Sqstack &s) {
s.top = 0;
}
2.3. Stack empty
Stack null
bool IsEmpty (Sqstack s) {
if (s.top = 0) {
printf ("is empty stack \ n");/return
true;
} else{return
false;
}
2.4. Stack full
Stack full
bool Isfull (Sqstack s) {
if (s.top = = MAXSIZE) {return
true;
}
else{return
false;
}
2.5. Take the top element of the stack
Take stack top element
void Gettopelem (Sqstack s, Selemtype &e) {
if (!isempty (s))
e = s.data[s.top-1];
else
printf ("This stack is empty stack, fetch top element failed \ n");
}
2.6. Into the stack
into stack
void push (Sqstack &s, Selemtype e) {
if (!isfull (s)) {
S.data[s.top] = e;
s.top++;
} else
printf ("This stack is full, the stack operation failed \ n");
}
2.7. Out Stack
Stack
void pop (Sqstack &s, Selemtype &e) {
if (!isempty (s)) {
e = s.data[s.top-1];
s.top--;
}
else
printf ("This stack is an empty stack, the stack operation failed \ n");
}
2.8. Create stacks using stack operations
Use the stack operation to create an initial stack
void Createstatck (sqstack &s, int n) {
printf ("Enter the elements in the stack sequentially: \ n");
for (int i = 0; i < n; i++) {
selemtype e;
scanf ("%c", &e);
Push (S, e);
GetChar ();//Inhale carriage return
}
printf ("\ n");
}
2.9. Print output Stack
Print output stack element
void Printstack (Sqstack s) {
int stacklen = s.top;//stack long
printf ("Print stack element:");
for (int i = 0; i < Stacklen i++) {
printf ('%c ', s.data[i]);
}
printf ("\ n");
}
2.10 Demo
void Main () {
sqstack s;
Initstack (s);
CREATESTATCK (S, 5);//Generate a stack from the top of the stack to the bottom of the stack: A,c,e,g,i
printstack (s);
The new element into the stack: J
printf ("Enter a new element into the stack (such as the letter J):");
Selemtype Pushelem;
scanf ("%c", &pushelem);
Push (S, Pushelem);
Printstack (s);
Out stack
selemtype Popelem;
Pop (S, popelem);
printf ("\ n out stack element is%c\n", Popelem);
Printstack (s);
}