# Structural characteristics and basic operation of data structure stack

### Structure characteristics and operation of stack

Stack is a linear table that can only be inserted and deleted at one end of the table. In the table, the end that allows insertion and deletion is called "top of the stack", and the end that does not allow insertion and deletion is called "bottom of the stack". The elements in and out of the stack are carried out in the order of "first in first out, last in first out". Similar to linear table, stack has two storage methods: sequential stack and chain stack.

### Sequential stack

Sequential stack refers to the stack realized by sequential storage allocation. At the same time, the top pointer is attached to indicate the position of the top element of the stack in the sequential stack. A one-dimensional array is used to describe the storage area of data elements in the sequential stack, and a maximum storage space is preset. For the top pointer, since the index of the first element of the array is 0, initialize top=-1; Indicates an empty stack. When outputting data elements in the stack, the cyclic variable I < = s.top can be used; To output. Because the insertion and deletion of the sequential stack can only be carried out at the top of the stack, some related operations of the sequential stack are much simpler than the sequential table. The specific C language implementation code is as follows:

```#include<stdio.h>

#define TRUE 1
#define FALSE 0
/***************************************************************************************************
Sequential storage representation of stack
****************************************************************************************************/
const int STACK_INIT_SIZE = 100;   //The default initial allocated maximum space of the sequential stack
const int STACKINCREMENT = 10;     //Default amount of supplemental space
typedef struct {
int *elem;             //An array that stores data
int top;              //Stack top pointer
int stacksize;         //Maximum capacity currently allocated
int incrementsize;     //Agreed additional space
}SqStack;

/*****************************************************************************************************
Build table
******************************************************************************************************/
void InitStack_Sq(SqStack &S, int maxsize = STACK_INIT_SIZE, int incresize = STACKINCREMENT)
{
//Construct an empty stack S, the maximum space allocated for initialization is maxsize, and the preset amount of supplementary space is increase
S.elem = new int[maxsize];        //Allocate array space with max size for the sequential stack
S.top = -1;                       //The current number of elements in the sequence stack is zero
S.stacksize = maxsize;            //The sequential stack can hold maxsize data elements
S.incrementsize = incresize;      //Expand the space of incresize elements each time when necessary
}

/***************************************************************************************************
Return stack top element
****************************************************************************************************/
bool GetTop_Sq(SqStack S, int &e)
{
//If the stack is not empty, use e to return the top element of the stack and return TRUE; otherwise, return FALSE
if (S.top == -1)
return FALSE;
e = S.elem[S.top];
return TRUE;
}

/*****************************************************************************************************
insert
******************************************************************************************************/
void incrementStacksize(SqStack &S)
{
int *a;
a = new int[S.stacksize + S.incrementsize];
for (int i = 0; i <=S.top; i++)
a[i] = S.elem[i];
delete[]S.elem;
S.elem = a;
S.stacksize = S.stacksize + S.incrementsize;
}

void Push_Sq(SqStack &S, int e)
{
//Insert element e as the new stack top element
if (S.top == S.stacksize - 1)
incrementStacksize(S);       //If the space is insufficient, the capacity will be expanded and the space will be reallocated, which is similar to the sequence table
S.elem[++S.top] = e;
}

/*****************************************************************************************************
delete
******************************************************************************************************/
bool Pop_Sq(SqStack &S, int &e)
{
//If the stack is not empty, delete the top element of S, return its value with e, and return TRUE; otherwise, return FALSE
if (S.top == -1)
return FALSE;
e = S.elem[S.top--];
return TRUE;
}

/******************************************************************************************************
Main function
*******************************************************************************************************/
void main()
{
int i;
int e1;       //Used to receive stack top elements
int e2 = 11;  //Used to insert a new stack top element
int e3;       //Used to receive deleted stack top elements
SqStack S;
InitStack_Sq(S);
for (i = 0; i < 10; i++)
{
S.elem[i] = i + 1;
S.top++;
}
printf("The data elements in the sequence stack are:");
for (i = 0; i <= S.top; i++)
printf("%d ", S.elem[i]);
printf("\n");
if (GetTop_Sq(S, e1))
printf("At this time, the stack top element is:%d\n", e1);
else
printf("This is an empty stack.\n");
Push_Sq(S, e2);
printf("After inserting a new stack top element, the data elements of the new stack are:");
for (i = 0; i <= S.top; i++)
printf("%d ", S.elem[i]);
printf("\n");
if (Pop_Sq(S, e3))
{
printf("The deleted stack top element is%d,The data elements in the stack after deletion are:",e3);
for (i = 0; i <= S.top; i++)
printf("%d ", S.elem[i]);
}
else
printf("This is an empty stack.");
}
```

### Chain stack

Chain stack is a stack realized by chain allocation. For the sequential stack, although the space expansion can be reallocated in the full state, it is also a last resort. Therefore, when the maximum capacity that the stack may reach cannot be estimated in advance, it is more appropriate to use the chain stack. The node structure of the chain stack is the same as that of the chain list. It is worth noting that the direction of the pointer in the chain stack is from the top of the stack to the bottom of the stack.

Like the sequential stack, the operation of chain stack is much simpler than that of single chain list. It should be noted that for functions that need to return a value with e, int & e is required for the formal parameter part when defining the function; To show. The reason is that the function is a Boolean function. If you want to change the value of e, you need to pass the address into the function, so use & e. At the same time, for the assignment of the chain stack, you can use the array with the value assigned in advance or the scanf function to assign the value. In essence, it is called continuously   void Push_ L (linkstack & S, int e) insert function.

```#include<stdio.h>
#define TRUE 1
#define FALSE 0

/**********************************************************************
Define structure type
***********************************************************************/
typedef struct LNode {
int data;
struct LNode *next;

/************************************************************************
Build table
************************************************************************/
{
//Create an empty chain stack, that is, set the stack top pointer to null
S = NULL;
}
/*************************************************************************
insert
**************************************************************************/
void Push_L(LinkStack &S, int e)
{
//Insert a new stack top element e at the top of the chain stack
LNode *p = new LNode;     //Assign nodes to new stack top elements
p->data = e;
p->next = S;              //Insert new stack top element
S = p;                    //Modify stack top pointer
}
/***************************************************************************
delete
****************************************************************************/
bool Pop_L(LinkStack &S, int &e)
{
//If the stack is not empty, delete the top element of the stack, return its value with e, and return TRUE; otherwise, return FALSE
if (S)
{
LNode *p;
p = S;
S = S->next;    //Modify stack top pointer
e = p->data;    //Return stack top element
delete p;       //Free node space
return TRUE;
}
else
return FALSE;
}
/******************************************************************************
Main function
*******************************************************************************/
void main()
{
int i;
int e1;      //Used to receive newly inserted data elements
int e2;      //Used to return the deleted stack top element
int a[10] = { 1,2,3,4,5,6,7,8,9,10 };
LNode *p;
InitStack_L(S);
for (i = 0; i < 10; i++)
{
e1 = a[i];
Push_L(S, e1);
}
printf("The data elements of this chain stack are:");
for (p = S; p != NULL; p = p->next)
printf("%d ", p->data);
printf("\n");
if (Pop_L(S, e2))
{
printf("The deleted stack top element is%d,The data elements in the stack after deletion are:", e2);
for (p = S; p != NULL; p = p->next)
printf("%d ", p->data);
}
else
printf("This is an empty stack.");

}```

This note is based on Yan Weimin's data structure and application algorithm course

The pictures cited are from the cloud classroom of central China Normal University

All code works correctly on Visual Studio 2017

If there is any error, please point it out

Tags: data structure stack

Posted on Mon, 04 Oct 2021 21:55:50 -0400 by mona02