Data structure (C language description) stack

1 The basic concept of the stack

The stack is a special table that only inserts and deletes at the head of the table. Therefore, the table head has a special meaning to the stack, which is called the top of the stack. The end of the table is called the bottom of the stack. A stack that does not contain any elements is called an empty stack.
Assuming that the elements in a stack S are a(n), a(n-1),..., a(1), then a(1) is called the bottom element of the stack, and a(n) is the top element of the stack. The elements in the stack are pushed into the stack in the order of a(1), a(2),..., a(n). At any time, the element that is popped from the stack is the top element of the stack . In other words, the stack modification is carried out on the principle of last in, first out. Therefore, the stack is also called the Last In First Out (Last In First Out) table, or LIFO table for short .
Insert picture description here

The stack is also an abstract data type. The commonly used stack operations are as follows.
(1) StackEmpty(S): Test whether the stack S is empty.
(2) StackFull(S): Test whether the stack S is full.
(3) StackTop(S): Return the top element of stack S.
(4) Push(x, S): Insert element x on the top of stack S, which is referred to as pushing element x onto the stack.
(5) Pop(S): delete and return the top element of stack S, referred to as stack pop.
The application of the stack is very wide, as long as the problem meets the LIFO principle, the stack can be used.

2 Use an array to implement a stack

When an array data is used to store stack elements, the bottom of the stack is fixed at the bottom of the array, that is, data[0] is the earliest element to be put on the stack, and the stack is extended to the top of the array (in the direction of increasing subscript).

2.1 Stack structure implemented with array

Stack is defined as follows.

typedef struct astack *Stack;//栈指针类型
typedef struct astack;//栈结构
	int top,//栈顶
	maxtop;//栈空间上界
	StackItem *data;//存储栈元素的数组
}Sstack;

The stack element is stored in the array data, with top pointing to the current stack top position, the stack top element is stored in data[top], and the stack capacity is maxtop.

2.2 Type definition of stack elements

Define the type of stack element as int.

typedef int StackItem;//栈元素类型int
typedef StackItem *addr;//栈元素指针类型

2.3 Function StackInit(size)

Create an empty stack with a capacity of size.

Stack StackInit(int size)
{
    
    
	Stack S=(Stack)malloc(sizeof *S);
	S->data=(StackItem *)malloc(size*sizeof(StackItem));
	S->maxtop=size;
	S->top=-1;
	return S;
}

2.4 Function StackEmpty(S)

When top=-1, the current stack is empty.

int StackEmpty(Stack S)
{
    
    
	return S->top<0;
}

2.5 Function StackFull(S)

When top=maxtop, the current stack is full.

int StackFull(Stack S)
{
    
    
	return S->top>=S->maxtop;
}

2.6 Function StackTop(S)

The top element of the stack is stored in data[top].

StackItem StackTop(Stack S)
{
    
    //前提栈为非空
	if(StackEmpty(S)) exit(1);
	return S->data[S->top];
}

2.7 Function Push(x,S)

The new top element x should be stored in data[top+1].

void Push(StackItem x,Stack S)
{
    
    //前提栈未满
	if(StackFull(S)) exit(1);
	S->data[++S->top]=x;
}

2.8 Function Pop(S)

After deleting the top element of the stack, the new top element of the stack is in data[top-1].

StackItem Pop(Stack S)
{
    
    //栈为非空
	if(StackEmpty(S)) exit(1);
	return S->data[S->top--];
}

2.9 Function StackFree(S)

Since the array data is dynamically allocated, StackFree should release the space allocated to data at the end of use to avoid memory leaks.

void StackFree(Stack S)
{
    
    
	free(S->data);
	free(S);
}

2.10 Using multiple stacks

When using stacks in some algorithms, it is often necessary to use multiple stacks at the same time. In order to prevent each stack from overflowing during the running of the algorithm, a larger stack space is usually preset for each stack. But it is not easy to do this, because the maximum space actually used by each stack in the algorithm operation process is difficult to estimate . On the other hand, the actual size of each stack is constantly changing during the operation of the algorithm , and it often happens that one stack is full and the other stack is empty.
] Suppose that the two stacks in the program share an array data[0:n]. Using the feature that the bottom position of the stack remains unchanged, the bottom of the two stacks can be set at the two ends of the array data respectively, and then each stretches toward the middle of the array data. The initial values ​​of the tops of the two stacks are 0 and n respectively, and overflow may occur when the tops of the two stacks meet. Since the two stacks can complement each other, the actual maximum space available for each stack is often greater than n/2.
Two stacks sharing the same array space
Insert picture description here

3 Implement the stack with pointers

A pointer is used to implement a stack, which is called a chain stack, as shown in the figure below.
Insert picture description here

3.1 The node type definition of the chain stack

typedef struct snode *slink;//栈结点指针类型
typedef struct snode{
    
    //栈结构
	StackItem element;//栈元素
	slink next;//下一结点指针
}StackNode;

slink NewStackNode()
{
    
    
	return (slink)malloc(sizeof(StackNode));
}

Its data member element stores stack elements, next is a pointer to the next node, and the function NewStackNode() creates a new node.

3.2 Chain stack Stack implemented with pointers

typedef struct lstack *Stack;//栈指针类型
typedef struct lstack{
    
    //栈结构
	slink top;//栈顶指针
}Lstack;

top is a pointer to the top node of the stack.

3.3 Function StackInit(size)

The function StackInit() sets top to a null pointer and creates an empty stack.

Stack StackInit()
{
    
    
	Stack S=(Stack)malloc(sizeof *S);
	S->top=0;
	return S;
}

3.4 Function StackEmpty(S)

Check whether the pointer top to the top of the stack is a null pointer.

int StackEmpty(Stack S)
{
    
    
	return S->top==0;
}

3.5 Function StackTop(S)

Return the element in the top node of the stack S.

StackItem StackTop(Stack S)
{
    
    //前提栈为非空
if(StackEmpty(S)) exit(1);
return S->top->element;
}

3.6 Function Push(x,S)

The function Push(x,S) first creates a new node for element x, and then modifies S's stack node pointer top to make the new node the new stack top node.

void Push(StackItem x,Stack S)
{
    
    
	slink p=NewStackNode();
	p->element=x;
	p->next=S->top;
	S->top=p;
}

3.7 Function Pop(S)

The function Pop(S) first stores the top element of S in x, then modifies the pointer of the top of the stack to point to the next element of the top element of the stack, thereby deleting the top element of the stack, and finally returns x.

StackItem Pop(Stack S)
{
    
    //前提栈非空
	if(StackEmpty(S)) exit(1);
	StackItem x=S->top->element;
	slink p=S->top;
	S->top=p->next;
	free(p);
	return x;
}

Guess you like

Origin blog.csdn.net/qq_45059457/article/details/114778129