Professional Documents
Culture Documents
Lec3 Linked List Applications and Operations 24032023 093738am
Lec3 Linked List Applications and Operations 24032023 093738am
Lec3 Linked List Applications and Operations 24032023 093738am
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:
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;
return res.head;
}
11
Operations
That modify original Linked List
12
Concatenating two lists
13
CONCATENATING TWO
LISTS
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
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;
17
// If either x or y is not present, nothing to do
if (currX == NULL || currY == NULL)
{return;}
19
String Operations using Linked List
20
Comparing two strings represented as linked lists
21
Comparing strings (linked lists)
22
Comparing strings (linked lists)
• Example:
Output: -1
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 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
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
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
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