130 likes | 318 Views
ADTs - Stacks. By Adam Thomas, Miekah Broomes , Sankara Daly. Objectives. Define a stack. List and explain the operations of a stack. Discuss practical needs of implementing a stack. What is a stack?.
E N D
ADTs - Stacks By Adam Thomas, MiekahBroomes, Sankara Daly
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
‘Push’ and ‘Pop’ Operation • The push operation is used to add data into a stack. • The pop operation is used to remove data from a stack. • N.B. Data is always added to/subtracted from the top of the stack.
Animation of Push and Pop Operations Click to begin animation End of animation. Push Pop 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
Postponing Data Usage • This is deferring data usage till some later point within the program. E.g manual transformation
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.