0% found this document useful (0 votes)
14 views28 pages

Lecture 6

The document discusses stack implementation using linked lists, explaining the stack's definition, operations, and applications such as browser history and undo functionality. It covers both static and dynamic implementations, with dynamic being more efficient in memory usage. Additionally, it includes linked list operations like concatenation, union, intersection, and string comparison using linked lists.

Uploaded by

SHORTFLIX
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views28 pages

Lecture 6

The document discusses stack implementation using linked lists, explaining the stack's definition, operations, and applications such as browser history and undo functionality. It covers both static and dynamic implementations, with dynamic being more efficient in memory usage. Additionally, it includes linked list operations like concatenation, union, intersection, and string comparison using linked lists.

Uploaded by

SHORTFLIX
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

DATA STRUCTURES AND ALGORITHMS

Linked List Applications & Operations

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

“Undo” functionality of a text editor

Reversing the order of elements in an array

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

Construction: Initializes an empty stack.


Empty operation: Determines if stack contains any values
Push operation: Modifies a stack by adding a value to top of
stack
Top operation: Retrieves the value at the top of the stack
Pop operation: Modifies a stack by removing the top value
of the stack
To help with debugging, add early on:
Output: Displays all the elements stored in the stack.

10
The stack Class
Class Stack{
private:
Node *myTop;
public:
Stack();
bool isEmpty();
void push(int);
int pop();
void displayStack();
int Top();
};

*Same node class will be used as list


11
Self Practice
Give output after following sequence of commands have been
executed:
1. S.Push(‘A’);
2. S.Push(‘B’);
3. S.Push(‘C’);
4. S.Pop();
5. S.Pop();
6. S.Push(‘D’);
7. S.Push(‘E’);
8. S.Pop();

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

Node * concatenate (Node *head1, Node *head2)


{
Node *p;
if (head1==NULL)
return head2;

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:

bool isPresent (Node *head, int data)


{
Node *t = head;
while (t != NULL)
{
if (t->data == data)
return 1;
t = t->next;
}
return 0;
}

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;

// Traverse list1 and search each element of it in


// list2. If the element is present in list 2, then
// insert the element to result
while (t1 != NULL) {
if (isPresent(head2, t1->data))
result.insert_end(t1->data);
t1 = t1->next;
}

result.traverse();
}

24
Comparing two strings represented as linked lists

// Linked list Node structure


class Node
{
public:
char c;
Node *next;
};

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

Input: list1 = g->e->e->k->s->a


list2 = g->e->e->k->s
Output: 1

Input: list1 = g->e->e->k->s


list2 = g->e->e->k->s
Output: 0
27
Comparing strings (linked lists)
int compare(Node *list1, Node *list2)
{
// Traverse both lists. Stop when either end of a
// linked list is reached or current characters don't //match
while (list1!=0 && list2!=0 && list1->data == list2->data)
{
list1 = list1->next;
list2 = list2->next;
}

// If both lists are not empty, compare mismatching characters


if (list1!=0 && list2!=0)
return (list1->data > list2->data)? 1: -1;

// If either of the two lists has reached end


if (list1!=0 && list2==0) return 1;
if (list2!=0 && list1 == 0) return -1;

//If none of the above conditions is true, both lists have //reached end
return 0;
}

28

You might also like