Stacks Data Structure
[Note: Visit cPlusPlus.psexam.com for examples and source code of C++ implementation of Stacks Data Structure. Click Here]
Today its turn of Stacks. In our previous posts we’ve dealt a bit about Data Types, Data Structures and Abstract Data Types, A list of different data structures in use in programming world, List data structure, Linked list data structure and compared lists with arrays. If you have missed, its good to go back and check the previous posts for them.
Following Computer Officer syllabus of Public Service Commission, we are at the end on topic 2.2
Let’s collect the bits now!
What is a Stack?
A stack is a special type of list, where only the element at one end can be accessed. Items can be “pushed” onto one end of the stack structure. New items are inserted before the others, as each old element moves down one position. The first element is referred to as the “top” item, and is the only item that may be accessed at any time. In order to access items that are further down the stack, they must be moved to the top by “popping” the appropriate number of items. Popping refers to removing the top element of a stack. This is referred to as a LIFO structure, “Last In, First Out”.
These rules make stacks very restricted in use, however they are very efficient and much easier to implement than lists. The uses of stacks vary from programming a simple card game, to maintaining the order of operations in a complex program. For example, a stack is useful in a management program where the newest tasks must be executed first. The node of a stack is usually presented with the following structure, which is very similar to that of a list node.
Source ThinkQuest [visit the page for example codes]
A stack is a last in, first out (LIFO) abstract data type and data structure. A stack can have any abstract data type as an element, but is characterized by only two fundamental operations: push and pop. The push operation adds to the top of the list, hiding any items already on the stack, or initializing the stack if it is empty. The pop operation removes an item from the top of the list, and returns this value to the caller. A pop either reveals previously concealed items, or results in an empty list.
A stack is a restricted data structure, because only a small number of operations are performed on it. The nature of the pop and push operations also means that stack elements have a natural order. Elements are removed from the stack in the reverse order to the order of their addition: therefore, the lower elements are typically those that have been in the list the longest.
Source Wikipedia
Stack Operation
Besides the push and pop operations on a stack, it is desirable to have at least two more: an initialize operation, which prepares the stack for use and sets it to an empty state, and an empty operation which is simply a test to see whether the stack is empty. The empty operation is useful to guard against an attempt to pop an empty stack, which is an error.
Push: Add an item to the top of the stack
pop: remove an item from the top of the stack
empty: test whether the stack is empty
full: test whether the stack is full
initialize: set up the stack in an empty condition
Source: Data Strucutres and Algorithms by Shi-Kuo Chang
Where Stacks can prove be useful
- Conversion of arithmetic expressions (infix, prefix, postfix)
- Evaluation of arithmetic expressions. Convert the expression in postfix notation and implement stack to evaluate.
- Stacks are useful for recursive solutions like finding factorial.
- Classic computer science problem 8-queens problem can be solved by using stacks.
- Stacks are very helpful to deciding whether some pair of expression delimiters are properly paired, even if they are embedded multiple pairs deep.
- Stacks can be extremely useful to reverse the list items.
- One useful application of stack is to sort a number of data using quick sort algorithm.
Sources:
C++ Data Structures: A Laboratory Course
by Stefan Brandle, Jonathan Geisler, james Roberge
Classic Data Structures by D. Samanta
Stacks Representation
Stacks can be implemented as:
- Array representation
- Linked List representation
We’ll be digging more on stacks in coming posts. Till then, happy learning!
[Note: Visit cPlusPlus.psexam.com for examples and source code of C++ implementation of Stacks Data Structure. Click Here]
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.
