[Data structure] Detailed implementation of the stack (c language)

Table of contents

1. The basic concept of the stack

2. The basic structure of the stack

​edit

3. Implementation of the stack


1. The basic concept of the stack

Stack: A special linear list that only allows insertion and deletion of elements at a fixed end . One end where data insertion and deletion operations are performed
is called the top of the stack, and the other end is called the bottom of the stack. The data elements in the stack follow the principle of LIFO (Last In First Out) .
Push (push) : The insertion operation of the stack is called push/push/push, and the incoming data is at the top of the stack.
Popping the stack (popping the stack) : The deletion operation of the stack is called popping the stack. The output data is also on the top of the stack

2. The basic structure of the stack

 When inserting a piece of data, if the insertion is successful, then the data is now the top element of the stack

The first element to be pushed onto the stack is the bottom element of the stack.

The first element pushed into the stack is pressed at the bottom of the stack. When an element needs to be taken out, of course, only the elements above the bottom of the stack can be taken out before the element at the bottom of the stack can be taken out.

3. Implementation of the stack

The implementation of the stack is divided into two ways, 1. Array implementation 2. Linked list implementation

Both implementations have advantages and disadvantages, but relatively speaking, I think the implementation of arrays is better. Using a linked list requires space to be applied for every time it is pushed into the stack and space needs to be released when it is popped out of the stack, while the array only needs to be filled when the capacity is full. When expanding, the most important thing is that the physical structure of the array is continuous. When calculating the cache, the hit rate is higher and the reading efficiency is higher.

The implementation of the linked list is divided into two methods, one is to insert at the end of the stack, and to delete at the end of the stack. The second is when the head is inserted into the stack and the head is deleted when the stack is removed.

Relatively speaking, the second method is better, because tail insertion and tail deletion need to find the tail every time, and the time complexity is O(n) , while head deletion and head insertion do not need to find the tail, and the time complexity is O(1 )

ok, I won’t elaborate on the implementation of the stack by the linked list. The main content of today’s talk is to implement the stack with an array

The array implements the stack. When it is pushed into the stack , it is like the tail insertion when we implemented the sequence table before , and when it is popped out , it is equivalent to the tail deletion of the sequence table . At this time, their time complexity is O(1)

From the figure below, we can see that the element at the bottom of the stack is always the first element to go in. When an element is pushed into the stack, the top of the stack becomes the position of the element. When an element is popped out of the stack, the stack The top is the previous element of this element

In summary, the top element of the stack is actually the last element of the array

 The implementation of stack by array is almost done in words, now let's start to implement it with code

1. Related interface functions

2. Define a structure

 3. Initialization

Initialize top==0, at this time top indicates the number of elements in the stack , and also indicates the next position of the top element in the stack, that is, [ top-1] indicates the top element in the stack

 4. Push into the stack

When pushing into the stack, it is necessary to judge whether the stack is full, and expand the capacity when it is full

 5. Determine whether the stack is empty

When top is zero, the stack is empty

 6. Pop out

To obtain the top element of the stack in the figure below, you need to assert to prevent access to the data [-1] position when the stack is empty (illegal access)

 

 Declare that popping out of the stack is used in conjunction with obtaining the top element of the stack , as shown in the following figure:

 At this point, the top element of the stack is obtained and then popped out of the stack

7. Get the number of elements in the stack

 

 8. Destruction of the stack

This is the end of the knowledge sharing about the stack. If you find it helpful, please like it. Thank you for your support.

Is there any room for improvement, I hope everyone can point it out

886!

Guess you like

Origin blog.csdn.net/m0_72532428/article/details/129748918