Chapter 6: C Language Data Structure and Algorithm Elementary Stack

Series Article Directory



foreword

Stack: A special linear list that only allows insertion and deletion of elements at a fixed end.


1. Stack

insert image description here

The structure of the stack is shown in the figure above: like a bucket, elements are placed vertically.
insert image description here

The stack only allows insertion and deletion of elements at a fixed end. The end of the data insertion and deletion operation is called the top of the stack, and the other end is called the bottom of the stack, that is, elements followlast in first outThe principle, but note that this is relative to the elements in the stack.

Second, the implementation of the stack

insert image description here
We can use the structure of an array or a linked list to implement a stack, but the data structure of the stack does not have a random insertion method, because it can only be inserted at the end.
When we simulate in the form of a linked list, we need to constantly find the tail. The time complexity of this process is O(N). So we are usingarrayThe structure of the stack can be better realized.

typedef struct Stack
{
    
    
	STDataType* a;
	int capacity;
	int top;
}ST;

3. Realization of interface functions

1. Initialization

void StackInit(ST* ps)
{
    
    
	assert(ps);
	ps->capacity = 4;
	ps->a = (STDataType*)malloc(sizeof(STDataType)*(ps->capacity));
	assert(ps->a);
	ps->top = 0;//栈顶元素的下一位置下标 top = 0/ 栈顶元素 top = -1
}

By default, open up four element size spaces, set top to 0, and capacity to 4.

2. Destroy the stack

void StackDestory(ST* ps)
{
    
    
	assert(ps);

	free(ps->a);
	ps->a = NULL;
	ps->top = 0;
	ps->capacity = 0;

}

3. Push and pop

void StackPush(ST* ps, STDataType x)
{
    
    
	assert(ps);

	//扩容
	if (ps->top == ps->capacity)
	{
    
    
		ps->a = (STDataType*)realloc(ps->a, sizeof(STDataType) * ps->capacity * 2);
		ps->capacity *= 2;
		assert(ps->a);
	}

	ps->a[ps->top] = x;
	ps->top++;
}
void StackPop(ST* ps)
{
    
    
	assert(ps);
	assert(ps->top > 0);
	ps->top--;
}

Pushing the stack is to insert elements at the end, so pay attention to the possibility of expansion.
When a stack is empty, it happens to be when top is 0.

4. Empty judgment

bool StackEmpty(ST* ps)
{
    
    
	assert(ps);
	
	return ps->top == 0;
}

When a stack is empty, it happens to be when top is 0, so we can use top to judge whether the stack is empty.

5. Number of elements

int StackSize(ST* ps)
{
    
    
	assert(ps);

	return ps->top;
}

6. Return the top element of the stack

STDataType StackTop(ST* ps)
{
    
    
	assert(ps);
	assert(ps->top > 0);
	return ps->a[ps->top - 1];
}

4. Access to elements in the stack

Stacks cannot continuously traverse elements like sequential lists and linked lists. Because, if you want to traverse the elements, you must take out the top element of the stack, that is, we must delete the element at the top of the stack to access the next element. Therefore, the stack can only be traversed once, and after traversing once, it means that the stack is empty.


Summarize

The characteristic of the stack is last in first out.
There is a solution to any problem, and there is no unthinkable. — Edison

Guess you like

Origin blog.csdn.net/yanyongfu523/article/details/129467259