Professional Documents
Culture Documents
Stacks
Stacks
Objectives
Define a stack. List and explain the operations of a stack. Discuss practical needs of implementing a stack.
What is a stack?
A stack is a list of data items that can only be accessed at one end, known as the top of the stack. It is an ADT (Abstract Data Type), which is a data package which contains both the data structure and the operations that can be performed on this data structure.
Stack Operations
Just like any other data structure, there are operations which can be carried out with Stacks. The four basic stack operations are:
Stack: Creates an empty stack. Push: Inserts an element at the top of the stack. Pop: Deletes the top element. Empty: Checks the status of the stack.
Stack Operation
Before we can do anything with a stack, we must create a stack in the first place. The stack operation creates an empty stack in memory. It is denoted in the following format.
Empty stack
stackName
Stack
Application Stacks
There a four basic application stacks: reversing data, pairing data, postponing data usage and backtracking steps.
Reversing Data
Reversing data items requires that a given set of data items be reordered so that the first and last items are exchanged, with all of the positions between the first and last also being relatively exchanged
Pairing Data
Paring data is any logic that breaks data into independent pieces for further processing. E.g to translate a source program to machine language
Backtracking Steps
This is a process when you need to access the most recent data element in a series of elements. Think of a labyrinth or maze - how do you find a way from an entrance to an exit? Once you reach a dead end, you must backtrack. But backtrack to where? to the previous choice point. Therefore, at each choice point you store on a stack all possible choices. Then backtracking simply means popping a next choice from the stack.