Lecture 6 - Stack
Lecture 6 - Stack
DATA
STRUCTURES
STACK
• Stack is a linear data structure (container)
– follows a particular order (Last-in-first-out - LIFO) in which
the operations (insertion/delation) are performed.
COMPUTER
• Page-visited history in a Web browser
• Undo sequence in a text editor
SCIENCE • Saving local variables when one function
calls another, and this one calls another
Insert (Push) and delete (Pop)
operations take place at the same end
last-in–first-out (LIFO)
BASIC OPERATIONS
• push() − Inserting an element in the stack. The new element is inserted
at the top of the stack.
• pop() − Removing (accessing) an element from the stack. There’s only
one element that we can remove.
• peek() [top ()]− Get the top data element of the stack, without
removing it.
• isEmpty() − Check if the stack is empty or not. It conventionally
returns a boolean value (1 or 0):
• isFull() − opposite of isEmpty().
EXERCISE
• Describe the output of the following series of stack operations
– Push(8)
– Push(3)
– Pop()
– Push(2)
– Push(5) 1
– Pop() 9
– Pop() 8
– Push(9)
– Push(1)
IMPLEMENTATION OF STACK
• There are two ways to implement a stack:
– Static Stack: Using array
• Fixed size stack
– If the stack is full and does not contain enought space for push
operation, the stack is then considered to be in an overflow
state.
• Item, top
struct Stack {
int top;
unsigned capacity;
int* array;
};
ARRAY IMPLEMENTATION OF STACK
Define a one dimensional array of specific size and insert/delete the values
into/from that array by using LIFO principle with the help of a variable
called 'top’.
1. If the stack empty, the top is set to -1.
2. Whenever we want to insert a value into the stack, increment the top
value by one and then insert.
3. Whenever we want to delete a value from the stack, then delete the
top value and decrement the top value by one.
ALGORITHMS
FOR PUSH AND
POP
OPERATIONS
If the stack is full, we
cannot push a new
item according to this
example.
What can we do to
add a new item?
İf the stack (array capacity) is full
instead of throwing an exception, we can replace the array with
a larger one
if (isFull(stack)) {
//Step 1
int *array2 = (int *) malloc(sizeof(int)* stack->capacity*2);
3 //Step 2
2
for(i =0; i<stack->capacity; i++)
array2[i] = stack->array[i];
1
//Step 3
free(stack->array);
stack->array = array2;
stack->capacity *= 2;
}
İf the stack (array capacity) becomes less by popping some elements,
we need to decrease the size of array.
X X X X X X
free(stack->array);
stack->array = array2;
stack->capacity /= 2;
}
SIMULATION FOR ARRAY
IMPLEMENTATION
• https://www.cs.usfca.edu/~galles/visualization/StackArray.html
LINKED LIST IMPLEMENTATION OF
STACK
There are two ends of a linked list: head and tail.
WHY?
LINKED LIST IMPLEMENTATION OF STACK
Removing an element at
the start is constant time
but
removal at the end
required traversing the
list to the node one
before the last
LINKED LIST IMPLEMENTATION OF STACK
The top node in the stack
always contains null in its
address field.
• But in our usual form an arithmetic expression may consist of more than one
operator and two operands
– e.g. (A+B)*C(D/(J+D)).
https://www.youtube.com/watch?v=ndgYfRMIuX8
https://www.youtube.com/watch?v=A3ZUpyrnCbM
MIT Course
https://www.youtube.com/watch?v=CHhwJjR0mZA