CPCS204 09 Stacks Implementation ArraysLinkedList
CPCS204 09 Stacks Implementation ArraysLinkedList
CP
CS
20
4
CP
CS
20
4
STACKS
Implementation
CP Implementation - Introduction
CS • Implementation
20 o Arrays
4 o Linked List
• Accessibility
o Top, LIFO
• Operations
o Push
o Pop
o isEmpty
o isFull
o top
3 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan
Data Structures - I
“No Bonus” marks for this course anymore
CP
CS
20
4 Stack
Implementation
Arrays
CP Array Implementation
CS
• Array • Array as STACK
20 o Push
o Insertion
4 • first, last, Anywhere • Only at Top
o Deletion o Pop
• Only from Top
• first, last, Anywhere
o isEmpty
o Traversal
• Top == -1
o Searching o isFull
o Sorting • Top == maxSize-1
o Merging o top or peek
• Return the value at Top
5 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan
Data Structures - I
“No Bonus” marks for this course anymore
4
public StackArray(int size) { maxSize
maxSize = size; 3 5
S 3 5
stack 2
4 Top
maxSize 1
-1
3 5 0
stack 2 6
Top
1 9
2
0 4 True
4 }
return (top == maxSize-1);
4 1
maxSize
S 3 0 5
stack 2 6
4 Top
maxSize 1 9
4
3 5 0 4
stack 2 6
Top
1 9
2
0 4 True
value = stack[top];
top--;
return value;
}
}
public int pop() {
if (isEmpty())
System.out.println(“Cannot POP; stack is empty.”);
else
return stack[top--];
16 } Jonathan (Yahya) Cazalas
Dr. Dr. Muhammad Umair Ramzan
Data Structures - I
“No Bonus” marks for this course anymore
CP
CS
20
4 Stack
Implementation
Linked List
public StackNode(int i) {
data = i;
next = null;
}
public StackNode(int i, StackNode n) {
data = i;
next = n;
}
}
25 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan
Data Structures - I
“No Bonus” marks for this course anymore
null
Top
}
Top
Value = 13 null
Top
10 null
13 50 8 10 null
Top
Temp = 13
13 50 10 null
Top
Temp = 13
13 50 10 null
CP
CS
20
4