Why is C programming called Pop


This article will look at stacks and how they work.

What's this?

A stack is a data area in which data can be stored. So far so good, nothing new. The interesting thing is how the data is organized.

You only need 2 statements to organize data in a stack: Push and pop.

With push you put data on the stack, with pop you get it back.

You only ever put data on one end of the stack. The data is also read again from the same end. That is why a stack is also called a stack, or because of the "direction" (from higher addresses to lower) basement stacks.



Think of a stack as a stack of plates. At the beginning the stack is empty, i.e. there is no stack at all. If you then put an element (a plate) on the stack, the stack consists of one element (plate). Now you can take the element from the stack again. After this action the stack is empty again. But you can also put another element on the stack. Then there are 2 elements on the stack. If you then take an element from the stack, you only get the "topmost" one (like a stack of plates, you can only take the topmost plate).

So the last item put on the stack is returned first. The element that was put first on the stack is consequently taken last from the stack.
This principle is called FILO, which means something like "First in, last out" (first in, last out).

Of course you can too LIFO name (“Last in, first out” - “Last in, first out”).


Example implementation

I would also like to give a small example implementation for a separate stack here. Please post questions about the code in the forum.

#include #include typedefstruct {unsignedint * esp; unsignedint size; unsignedint maxSize;} Stack; Stack * CreateStack (unsignedint maxSize); void Push (Stack * stack, unsignedint value); unsignedint Pop (Stack * stack); void DestroyStack (Stack * stack); int isEmpty (Stack * stack); int isFull (Stack * stack) ; int main () {stack * s; int i; s = CreateStack (5); for (i = 0; i <10; i ++) if (! isFull (s)) Push (s, i); for (i = 0; i <10; i ++) if (! isEmpty (s)) printf ("Element% d:% u \ n", i +1, Pop (s)); DestroyStack (s); return0; } Stack * CreateStack (unsignedint maxSize) {if (maxSize <= 0) return0; Stack * stack = (stack *) malloc (sizeof (stack)); stack-> maxSize = maxSize; stack-> size = 0; stack-> esp = (unsignedint *) malloc (maxSize * sizeof (unsignedint)); return stack;} void Push (Stack * stack, unsignedint value) {stack-> esp [stack-> size] = value; stack-> size ++;} unsignedint Pop (Stack * stack) {stack-> size -; return stack-> esp [stack-> size];} void DestroyStack (Stack * stack) {free (stack-> esp); free (stack); stack = NULL;} int isEmpty (Stack * s) {return (s-> size == 0);} int isFull (Stack * s) {return (s-> size == s-> maxSize);}