Stack implementation (array stack)

catalogue 1, Foreword 2, What is a stack? 3, Implementat...
1. Structure definition of stack elements
2. Stack initialization
2. Insertion of stack elements (stacking)
3. Out of stack
4. Stack destruction
5. Return stack top element
6. Returns the number of stack elements
  7. Determine whether the stack is empty

catalogue

1, Foreword

2, What is a stack?

3, Implementation of each interface of stack

1. Structure definition of stack elements

2. Stack initialization

2. Insertion of stack elements (stacking)

3. Out of stack

4. Stack destruction

5. Return stack top element

6. Returns the number of stack elements

  7. Determine whether the stack is empty

3, Conclusion

1, Foreword

This article involves the knowledge of sequence table. If you have children's shoes that don't know what sequence table is, please read the sequence table of linear table in the previous blog.

2, What is a stack?

Stack: a special linear table that allows insertion and deletion of elements only at a fixed end. One end for data insertion and deletion 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 last in first out LIFO (Last ln First Qut).
Stack pressing: the stack insertion operation is called stack entering / stack pressing / stack entering, and the input data is at the top of the stack.
Stack out: stack deletion is called stack out. The output data is also at the top of the stack.

  Understand what is called the last in first out principle from the figure below.

3, Implementation of each interface of stack

In order to facilitate you to understand and test the code, I will first give the test code of each interface.

void test1() { ST p; StackInit(&p); StackPush(&p, 1); StackPush(&p, 2); printf("%d ", StackTop(&p)); StackPop(&p); StackPush(&p, 3); StackPush(&p, 4); while(!StackEmpty(&p)) { printf("%d ", StackTop(&p)); StackPop(&p); } StackDestroy(&p); } int main() { test1(); return 0; }

1. Structure definition of stack elements

typedef int SDataType; typedef struct stack { SDataType* a; //The stack here uses dynamic arrays int top; //Represents the top of the stack int capacity; //Stack capacity }ST;

2. Stack initialization

Initializing the stack is simple. We only need to make pointer a point to null, and top and capacity equal to 0.

void StackInit(ST* ps) { assert(ps); ps->a = NULL; ps->top = 0; ps->capacity = 0; }

2. Insertion of stack elements (stacking)

We use the head of the array as the top of the stack and the tail as the top of the stack. The stack is last in first out. Therefore, when we insert elements (that is, into the stack), we only need to insert them at the tail of the array; When deleting elements (i.e. out of the stack), you only need to make top-1; This satisfies the definition of the stack.

void StackPush(ST* ps, SDataType x) { assert(ps); if (ps->top == ps->capacity) { int NewCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2; SDataType* temp = realloc(ps->a, sizeof(SDataType) * NewCapacity); if (temp == NULL) { printf("realloc fail.\n"); exit(-1); } ps->a = temp; ps->capacity = NewCapacity; } ps->a[ps->top] = x; ps->top++; }

When top==capacity, it indicates that capacity expansion is required. When capacity is 0, there is no stack element. At this time, we allocate four spaces to pointer A. if capacity= 0, let's make capacity*2 and expand the space pointed to by pointer a twice.

3. Out of stack

When we stack, we need to ensure that there are elements that can be stacked, so we use assert (PS - > top > 0).

void StackPop(ST* ps) { assert(ps); assert(ps->top > 0); ps->top--; }

4. Stack destruction

We need to free the space of pointer a, and then assign top and capacity to 0.

void StackDestroy(ST* ps) { assert(ps); free(ps->a); ps->a = NULL; ps->top = 0;//Not written ps->capacity =0;//Not written }

5. Return stack top element

The subscript of the top element of the stack is top-1, so we only need to return the subscript element; At the same time, we should ensure that the stack has elements.

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

6. Returns the number of stack elements

If the value of top is several, the stack will have many elements, so we only need to return the value of top.

int StackSize(ST* ps) { assert(ps); return ps->top; }

  7. Determine whether the stack is empty

We just need to judge whether top is equal to 0.

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

In this blog, we use array to complete stack operation, which is the extension of sequence table. Therefore, we mainly understand how to implement sequence table. Please see my last blog for the specific implementation of sequence table. Of course, we can also use the linked list to complete the stack operation, but the linked list stack has no advantage over the array stack (I will explain the specific reasons in the blog on the linked list later), so we usually use the array to complete the stack operation.

9 November 2021, 16:39 | Views: 4432

Add new comment

For adding a comment, please log in
or create account

0 comments