In this tutorial, you will
1. Learn the concept of stacks.
2. Be given practical examples of LIFO
3. Learn what the types of implementation of stack are?
4. learn the array based implementation of stacks.
What is the concept of stacks?
The concept of stacks is very simple. It is that if you insert an element in a stack, it always occupies top position and when you remove element, the element at top is always removed. i.e. the element inserted last is removed first. Another name given to such insertion is LIFO i.e. last in first out. In stacks, elements can only be inserted and removed from one end. Thus, if in a linked list, if elements are added and removed from end or start but not both(only end or only start ) it will act as a stack . Implementation of stacks will be discussed later.
What are some practical examples of LIFO?
There are many examples of LIFO (Last in First out) in practical life which will help you understand stacks better .
1. Putting dishes on each other. When you put dish for washing or any other purpose, basically it is like stacks. You always put the new dish on top and while removing, you remove the top most dish.
2. For all the people with accounting background, you would know that the concept of LIFO is also used in accounting. When items are added in an inventory, the item added last is sold first.
What are different implementations of stacks ?
There are two types of implementations of stacks
1. Array based implementation.
2. Pointer (linked list) based implementation.
What is array based implementation of stacks?
In array based implementation, we use arrays to store data and we add new data in arrays and delete data from array. Actually a counter is used to have the information of number of elements filled in array and while adding data, counter is incremented and while removing data, counter’s value is decreased. It is mostly used when you know the maximum amount of data. C++ code and step by step implementation of code is given.
- class Stack
- char arr;
- int top;
- void push(char value)
- int get_top()
- return -1;
- return arr[top-1];
- bool Empty()
- return 1;
- return 0;
- int pop ()
- return arr[top];
Note: Linked list based implementation will be discussed in next tutorial.
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.