Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement the unsorted single linked list as we did in the class and implement t

ID: 3924155 • Letter: I

Question

Implement the unsorted single linked list as we did in the class and implement the following operations: 1.DeletelastDuplicat(): For any element in the linked list, if there are multiple copies (> = 2 copies), delete the last copy. DeleteSecondlastDuplicat(): For any element in the linked list, if there are multiple copies (> = 2 copies), delete the second last copy Test your program with the following operations: a)insert 5 b)Insert 7 c)Insert 11 d)Insert 5 e)Insert f) Insert 5 g) Print out the list h) Delete the last duplicate of 5 i)Print out the list i) Delete the last duplicate of 11 k) Print out the list I) Insert 11 m) Insert 7 n) Print out the list O) Delete the second last duplicate of 5 p) Print out the list q) Delete the second last duplicate of 7 r) Print out the list

Explanation / Answer

#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
struct node
{
int data;
struct node *next;
}*start;
class LinkedList
{
public:
node* create_node(int);
void insert_begin();
void insert_pos();
void insert_last();
void delete_pos(int);
void delete_lastdup();
void delete_seclast();
int search(int);
void display();
int get_count(int);
LinkedList()
{
start = NULL;
}
};
int main()
{
int choice, pos, value, res=0;
LinkedList sl;
start = NULL;
while (1)
{
cout<<endl<<"Operations on singly linked list"<<endl;
cout<<"1.Insert Node at beginning"<<endl;
cout<<"2.Insert node at last"<<endl;
cout<<"3.Insert node at position"<<endl;
cout<<"4.Delete a Particular Node"<<endl;
cout<<"5.Search Element"<<endl;
cout<<"6.Display Linked List"<<endl;
cout<<"7.Delete last duplicate"<<endl;
cout<<"8.Delete second last duplicate"<<endl;
cout<<"9.Exit "<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Inserting Node at Beginning: "<<endl;
sl.insert_begin();
cout<<endl;
break;
case 2:
cout<<"Inserting Node at Last: "<<endl;
sl.insert_last();
cout<<endl;
break;
case 3:
cout<<"Inserting Node at a given position:"<<endl;
sl.insert_pos();
cout<<endl;
break;
case 4:
cout<<"Delete a particular node: "<<endl;
cout<<"Enter the position of value to be deleted: ";
cin>>pos;
sl.delete_pos(pos);
break;
case 5:
cout<<"Search element in Link List: "<<endl;
cout<<"Enter the value to be searched: ";
cin>>value;
res=sl.search(value);
cout<<"Element "<<value<<" is found at position "<<res<<endl;
cout<<endl;
break;
case 6:
cout<<"Display elements of link list"<<endl;
sl.display();
cout<<endl;
break;
case 7:
sl.delete_lastdup();
cout<<endl;
break;
case 8:
sl.delete_seclast();
cout<<endl;
break;
case 9:
exit(1);
break;
default:
cout<<"Enter valid choice..!!"<<endl;
}
}
return 0;
}
//function to create node
node *LinkedList::create_node(int value)
{
struct node *temp;
temp = new(struct node);
if (temp == NULL)
{
return 0;
}
else
{
temp->data = value;
temp->next = NULL;   
return temp;
}
}

//function to insert element at beginning
void LinkedList::insert_begin()
{
int value;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *temp, *p;
temp = create_node(value);
if (start == NULL)
{
start = temp;
start->next = NULL;
}
else
{
p = start;
start = temp;
start->next = p;
}
cout<<"Element Inserted at beginning"<<endl;
}

int LinkedList::get_count(int value)
{
int count=0;
struct node *s;
s = start;
while (s->next != NULL)
{   
if(s->data == value)
count++;
s = s->next;
}
return count;
}

//function to insert node at last
void LinkedList::insert_last()
{
int value;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *temp, *s;
temp = create_node(value);
s = start;
while (s->next != NULL)
{   
s = s->next;
}
temp->next = NULL;
s->next = temp;
cout<<"Element Inserted at last"<<endl;
}
//function to insert node at position
void LinkedList::insert_pos()
{
int value, pos, counter = 0;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *temp, *s, *ptr;
temp = create_node(value);
cout<<"Enter the postion at which node to be inserted: ";
cin>>pos;
int i;
s = start;
while (s != NULL)
{
s = s->next;
counter++;
}
if (pos == 1)
{
if (start == NULL)
{
start = temp;
start->next = NULL;
}
else
{
ptr = start;
start = temp;
start->next = ptr;
}
}
else if (pos > 1 && pos <= counter)
{
s = start;
for (i = 1; i < pos; i++)
{
ptr = s;
s = s->next;
}
ptr->next = temp;
temp->next = s;
}
else
{
cout<<"Positon out of range"<<endl;
}
}

//delete last duplicate element
void LinkedList::delete_lastdup()
{
int value,c=0,pos=0;
cout<<"Enter element to be deleted:";
cin>>value;
c=get_count(value);
if(c<2)
{
cout<<"duplicate does not exist"<<endl;
return;
}
else
{
pos=search(value);
delete_pos(pos);
}
}

//delete second last duplicate element
void LinkedList::delete_seclast()
{
int value,c=0,pos=0,res=0,f=0,x;
cout<<"Enter element to be deleted:";
cin>>value;
c=get_count(value);
x=c-1;
if(c<2)
{
cout<<"duplicate does not exist"<<endl;
return;
}
else
{
struct node *s;
s = start;
while (s != NULL)
{
pos++;
if (s->data == value)
{
f++;
res=pos;
  
if(f==x)
break;
  
}
s = s->next;
}
delete_pos(res);
}
}

//delete element at position
void LinkedList::delete_pos(int pos)
{
int i, counter = 0;
if (start == NULL)
{
cout<<"List is empty"<<endl;
return;
}
struct node *s, *ptr;
s = start;
if (pos == 1)
{
start = s->next;
}
else
{
while (s != NULL)
{
s = s->next;
counter++;
}
if (pos > 0 && pos <= counter)
{
s = start;
for (i = 1;i < pos;i++)
{
ptr = s;
s = s->next;
}
ptr->next = s->next;
}
else
{
cout<<"Position out of range"<<endl;
}
free(s);
cout<<"Element Deleted"<<endl;
}
}

//function to search element
int LinkedList::search(int value)
{
int pos = 0,res=0;
bool flag = false;
if (start == NULL)
{
cout<<"Empty list..!!"<<endl;
return pos;
}

struct node *s;
s = start;
while (s != NULL)
{
pos++;
if (s->data == value)
{
flag = true;
res=pos;
}
s = s->next;
}
if (!flag)
cout<<"Element "<<value<<" not found in the list"<<endl;
return res;
}

//function to display element
void LinkedList::display()
{
struct node *temp;
if (start == NULL)
{
cout<<"The List is Empty"<<endl;
return;
}
temp = start;
cout<<"Elements of list are: "<<endl;
while (temp != NULL)
{
cout<<temp->data<<"->";
temp = temp->next;
}
cout<<"NULL"<<endl;
}