Lecture 6
Lecture 6
1
Stack Implementation
using Linked List
2
What is a stack?
It is an ordered group of homogeneous items.
Elements are added to and removed from the top of the stack (the most
recently added items are at the top of the stack).
The last element to be added is the first to be removed (LIFO: Last In, First
Out).
3
Insertion & deletion on stack
4
Applications of stack
“Back” button of Web Browser
◦ History of visited web pages is pushed onto the stack and
popped when “back” button is clicked
Saving local variables when one function calls another, and this one calls
another, and so on.
5
Stack as an ADT
A stack is an ordered collection of data items in which access is
possible only at one end (called the top of the stack).
Basic operations:
1. Construct a stack (usually empty)
2. Check if stack is empty
3. Push: Add an element at the top of the stack
4. Top: Retrieve the top element of the stack
5. Pop: Remove the top element of the stack
6
Related terminology
Top
◦ A pointer that points the top element in the stack.
Stack Underflow
◦ When there is no element in the stack, we cannot pop an
element: stack underflow
Stack Overflow
◦ When the stack contains equal number of elements as per its
capacity and no more elements can be added: stack overflow
7
Implementing the Stack
8
Stack implementation
Implementation can be done in two ways
◦ Static implementation
◦ Dynamic Implementation
Static Implementation
◦ Stacks have fixed size, and are implemented as arrays
◦ It is also inefficient for utilization of memory
Dynamic Implementation
◦ Stack grow in size as needed, and implemented as linked lists
◦ Dynamic Implementation is done through pointers
◦ The memory is efficiently utilized with Dynamic Implementations
9
Stack Operations
10
The stack Class
Class Stack{
private:
Node *myTop;
public:
Stack();
bool isEmpty();
void push(int);
int pop();
void displayStack();
int Top();
};
9. S.Pop(); 12
Dynamic memory allocation
– new/delete
A dynamic variable is created and destroyed while the program
is running
int *p ;
p = new int ;
*p = 25 ;
13
Dynamic memory allocation
– new/delete
delete p ; destroys the dynamic variable pointed by p
After delete p, p becomes an undefined pointer variable: a
dangling pointer.
Beware: before using * again, be sure p points to something and
is not a dangling pointer. Otherwise unpredictable effects.
14
Stack implementation using
linked list
10
Push(20);
top
20 10
Push(30); top
30 20 10
top 15
Stack implementation using
linked list
30 20 10
top
Pop();
20 10
top
16
Stack implementation using linked list
void stack::push(int value) {
Node *ptr;
ptr=new Node;
ptr->data=value;
ptr->next=NULL;
if(top!=NULL)
ptr->next=top;
top=ptr;
}
17
Stack implementation using linked list
int stack::pop() {
Node *temp;
if(top==NULL) {
cout<<"nThe stack is empty!!!";
return -1;
}
temp=top;
top=top->next;
int retValue = temp->data;
delete temp;
return retValue;
}
18
Linked List Operations
19
Concatenating two lists
20
CONCATENATING TWO LISTS
if (head2==NULL)
return head1;
p=head1;
while (p->next!=NULL)
p=p->next;
p->next=head2;
return head1;
}
21
Utility Function
Utility Function to check the presence of an element in a list:
22
Union of two lists
void getUnion(Node *head1, Node *head2)
{
List result;
Node *t1 = head1, *t2 = head2;
// Insert all elements of list1 in result
while (t1 != NULL){
result.insert_end(t1->data);
t1 = t1->next;
}
// Insert those elements of list2 which are not
// present in result list
while (t2 != NULL) {
if (!isPresent(result.head, t2->data))
result.insert_end(t2->data);
t2 = t2->next;
}
result.traverse();
}
23
Intersection of two lists
void getIntersection(Node *head1, Node *head2)
{
List result;
Node *t1 = head1;
result.traverse();
}
24
Comparing two strings represented as linked lists
25
Comparing strings (linked
lists)
Function similar to strcmp()
Returns 0 if both strings are same
Returns 1 if first linked list is lexicographically greater,
Returns -1 if second string is lexicographically greater
26
Comparing strings (linked
lists)
Example:
Input: list1 = g->e->e->k->s->a
list2 = g->e->e->k->s->b
Output: -1
//If none of the above conditions is true, both lists have //reached end
return 0;
}
28