Lec3 Linked List Applications and Operations 24032023 093738am

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 39

DATA STRUCTURES AND ALGORITHMS

Week 3: Linked List Applications &


Non-Member Functions
Qazi Haseeb Yousaf
Department of CS
Bahria University, Islamabad

1
Applications
• Maintaining records
• Students, books, employees etc.
• Polynomial arithmetic
• 4x4 + 5x3 – 3x + 1
• String Functions
• H-e-l-l-o-ø
• W-o-r-l-d-ø
• H-e-l-l-o-W-o-r-l-d-ø
• Implementation of other ADTs
• Stacks
• Queues

11/18/2023 2
Maintaining Records
using Linked List

3
Examples-Student List
class Student{ //create student node
public: Node *p=new Node;
string name; p->data->name=“xyz”;
string enroll; p->data->enroll=“01-234123-009”;
}; p->marks=34;
p->next=NULL;
//find student node by enrollment
class Node{ Node *t=head;
public: While((t->data->enroll!=“01-233123-001”)&&(t!=NULL))
Student data; {
t=t->next;
int marks; }
Node *next; if(t!=NULL)
}; {
cout<<t->data->name;
cout<<t->marks;
}
class List{
public: Name Name Name
Node *head; Enroll Enroll Enroll
//list operations marks marks marks
};

11/18/2023 4
Examples-Student List
Name Name Name
Enroll Enroll Enroll
marks marks marks

class List{
public:
Node *head;
//member functions
list();
~List();
bool isEmpty();
void insert_student(student, marks);
void delete_student(string);
Student find_student(string);
void display_list();
};

11/18/2023 5
Examples-Student List
Name Name Name
Enroll Enroll Enroll
marks marks marks

void main(){
list dsa, oop;
Student s;
int x, m;
cout<< “students in dsa class =“;
cin>>x;
for(int i=1;i<=x; i++)
{
cout<< “Enter student name”<<endl;
cin>>s->n;
cout<< “Enter student enroll”<<endl;
cin>>s->e;
cout<< “Enter student marks”<<endl;
cin>>m;
dsa.insert_student(s,m);
}
dsa.display_list();
};

11/18/2023 6
Non-Member (stand-alone) Functions

7
Examples-Non-Member Functions
int size(Node *h);
Node* union(Node *h1, Node *h2);
Node* inter(Node *h1, Node *h2);
Node* reverse(Node *h);
void sort(Node *h);
void main(){
list l1, l2, result;
cout<<“Size of list is=“<<size(l1.head);
cout<<“Union of both lists is=“<<endl;
result.head = union(l1.head, l2.head);
result.display_list();
cout<“Common elements in both lists are=“<<endl;
result.head = inter(l1.head, l2.head);
result.display_list();
cout<“Reversed list=”<<endl;
result.head = reverse(l1.head);
result.display_list();
cout<“Sorted list=“<<endl;
sort(l1.head);
};

11/18/2023 8
Utility Functions
Utility Function to check the presence of an element in a list:

bool isPresent (Node *h, int val)


{
Node *t = h;
while (t != NULL)
{
if (t->data == val)
return true;
t = t->next;
}
return false;
}

9
Union of two lists
Node* union(Node *h1, Node *h2)
{
List res;
Node *t1 = h1, *t2 = h2;
// Insert all elements of list1 in result
while (t1 != NULL){
res.insert_end(t1->data);
t1 = t1->next;}
// Insert those elements of list2 which are not
// present in result list
while (t2 != NULL)
{
if (!isPresent(res.head, t2->data))
res.insert_end(t2->data);
t2 = t2->next;
}
return res.head;
}

10
Intersection of two lists
Node* inter(Node *h1, Node *h2)
{
List res;
Node *t1 = h1;

// 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(h2, t1->data))
res.insert_end(t1->data);
t1 = t1->next;
}

return res.head;
}

11
Operations
That modify original Linked List

12
Concatenating two lists

13
CONCATENATING TWO
LISTS

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


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

if (head2==NULL)
return p;

while(p->next!=NULL)
p=p->next;

p->next=q;
return p;
}

14
CONCATENATING TWO
LISTS

int main ()
{
list l1, l2; //inserted some values in both
l1.display();
l2.display();
l1.head=concatenate(l1.head, l2.head);
l1.display();
return 0;
}

15
Function to swap two nodes

• Write a non-member function to swap the nodes containing values


‘x’ and ‘y’

void swapNodes(int x, int y, Node **h)


swapNodes(4, 5, &(l.head))

void swapNodes(int x, int y, Node *h)

swapNodes(4, 5, l.head)
16
void swapNodes(int x, int y, Node **h)
{
// Nothing to do if x and y are same
if (x == y) return;

// Search for x , keep track of prevX and CurrX


Node *prevX = NULL;
Node *currX = *h;

while (currX!=0 && currX->data != x)


{
prevX = currX;
currX = currX->next;
}
// Search for y , keep track of prevY and CurrY
Node *prevY = NULL;
Node *currY = *h;
while (currY!=0 && currY->data != y)
{
prevY = currY;
currY = currY->next;
}

17
// If either x or y is not present, nothing to do
if (currX == NULL || currY == NULL)
{return;}

// If x is not head of linked list


if (prevX != NULL)
{prevX->next = currY;}

else // Else make y as new head


{*h = currY;}
// If y is not head of linked list
if (prevY != NULL)
{prevY->next = currX;}

else // Else make x as new head


{*h = currX;}
// Swap next pointers
Node *temp = currY->next;
currY->next = currX->next;
currX->next = temp;
}
18
Function to reverse the list
void reverselist(Node **h)
{
Node *pred = NULL, *curr = *h, *succ;
while (curr!=0)
{
succ = curr->next;
curr->next = pred;
pred = curr;
curr = succ;
}
*h = pred;
}

19
String Operations using Linked List

20
Comparing two strings represented as linked lists

// Linked list Node structure


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

21
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

22
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

23
Comparing strings (linked lists)
int compare(Node *h1, Node *h2)
{
Node *list1=h1, *list2=h2;
// 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;
}
24
Polynomial Arithmetic
using Linked List

25
Examples-Polynomial List

11/18/2023 26
Examples-Polynomial List

27
Examples-Polynomial List

28
Stack Implementation
using Linked List

29
Dynamic Stack

• Stack is a data structure that is used to reverse the order of the


elements.
• Elements are added to and removed from the beginning of the stack
(we also call it the “top” of the stack).

30
Insertion & deletion on stack

31
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

32
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.

33
The stack ADT Class

Class Stack{
*Same node class will be used as list
public:
Node *top; Class Node{
Stack(); public:
bool isEmpty(); int data;
void push(int); Node *next;
};
int pop();
void
displayStack();
int Top_val();
};

34
Stack implementation using linked list

Stack::Stack()
{
top=NULL;
}

bool Stack::isEmpty()
{
if(top==NULL)
return true;
else
return false;
}

35
Stack implementation using linked list

void Stack::push(int val)


{
Node *p=new Node;
p->data=val;
p->next=NULL;
if(isEmpty())
{top=p;}
else{
p->next=top;
top=p;}
}

36
Stack implementation using linked list
int Stack::pop()
{
int t;
Node *p=top;
if(isEmpty())
{cout<<“Stack underflow”;
exit(0);}
else{
t=top->data;
top=top->next;
delete p;}
return t;
}

37
Stack implementation using linked list
int Stack::Top_val()
{
int t;
if(isEmpty())
{cout<<“Stack is empty”;
exit(0);}
else{
return top->data;
}
}

38
Stack implementation using linked list
void Stack::displayStack()
{
Node *t=top;
if(isEmpty())
{cout<<“Stack is empty”;
exit(0);}
else{
while(t!=NULL)
{ cout<<t->data;}
}
}

39

You might also like