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

Extra Credit 2 Implement the unsorted single linked list as we did in the class

ID: 3779471 • Letter: E

Question

Extra Credit 2 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. 2. DeletesecondLastDuplicat0: For any element in the linked list, if there are multiple copies 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 7 Insert 5 g) Print out the list h) Delete the last duplicate of 5 Print out the list j) Delete the last duplicate of 11 k) Print out the list Insert 11 m) nsert 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;

/*

* Node Declaration

*/

struct node

{

    int info;

    struct node *next;

}*start;

/*

* Class Declaration

*/

class single_llist

{

    public:

        node* create_node(int);

      

        void insert(int);

        void deletelastduplicate(int);

       void deletesecondlastduplicate(int);

        void search();

       void display();

        single_llist()
       {

            start = NULL;

        }

};

/*

* Main :contains menu

*/

main()

{

    int choice, nodes, element, position, i;

    single_llist sl;

    start = NULL;
   struct node *temp=start;

       sl.insert(5);
       sl.insert(7);
       sl.insert(11);
       sl.insert(5);
       sl.insert(7);
       sl.insert(5);
       sl.display();
       sl.deletelastduplicate(5);
       sl.display();
       sl.deletelastduplicate(11);
       sl.display();
        sl.insert(11);
        sl.insert(7);
        sl.display();
        sl.deletesecondlastduplicate(5);
        sl.display();
        sl.deletesecondlastduplicate(7);
        sl.display();

}

/*

* Creating Node

*/

node *single_llist::create_node(int value)

{

    struct node *temp, *s;

    temp = new(struct node);

    if (temp == NULL)

    {

        cout<<"Memory not allocated "<<endl;

        return 0;

    }

    else

    {

        temp->info = value;

        temp->next = NULL;   

        return temp;

    }

}

void single_llist::insert(int value)

{

    struct node *temp, *s;

    temp = create_node(value);

    s = start;
    if(s==NULL){start=temp;return;
    }

    while (s->next != NULL)

    {       

        s = s->next;      

    }

    temp->next = NULL;

    s->next = temp;

  

}

void single_llist::deletelastduplicate(int d)//which deletes last duplicate

{
   struct node *t=start,*temp=NULL,*prev=NULL,*p=NULL,*pp=NULL;
  
   while(t!=NULL)
   {
       if(t->info==d){prev=p;pp=temp;
       temp=t;//finding last duplicate
      
       }
           p=t;
           t=t->next;
   }
   if(temp!=NULL && pp !=NULL)
   {
           prev->next=temp->next;//deleting last duplicate
   }
}

void single_llist::deletesecondlastduplicate(int d)//which deletes second last duplicate

{
   struct node *t=start,*temp=NULL,*prev=NULL,*p=NULL;
  
   while(t!=NULL)
   {
       if(t->info==d){prev=temp;//finding second last duplicate
       temp=t;
      
       }
          
           t=t->next;
   }
   t=start;
   while(t!=NULL)
   {
       if(t==prev){break;
      
       }
           p=t;
           t=t->next;
   }
  
   if(prev!=NULL && temp!=NULL)//deleting second last duplicate
   {
           if(prev==start){
               start=start->next;
               return;
           }
          
           p->next=prev->next;
   }
}


void single_llist::search()

{

    int value, pos = 0;

    bool flag = false;

    if (start == NULL)

    {

        cout<<"List is empty"<<endl;

        return;

    }

    cout<<"Enter the value to be searched: ";

    cin>>value;

    struct node *s;

    s = start;

    while (s != NULL)

    {

        pos++;

        if (s->info == value)

        {

            flag = true;

            cout<<"Element "<<value<<" is found at position "<<pos<<endl;

        }

        s = s->next;

    }

    if (!flag)

        cout<<"Element "<<value<<" not found in the list"<<endl;

}

/*

* Display Elements of a link list

*/

void single_llist::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->info<<"->";

        temp = temp->next;

    }

    cout<<"NULL"<<endl;

}

ouput:-

Elements of list are:
5->7->11->5->7->5->NULL
Elements of list are:
5->7->11->5->7->NULL
Elements of list are:
5->7->11->5->7->NULL
Elements of list are:
5->7->11->5->7->11->7->NULL
Elements of list are:
7->11->5->7->11->7->NULL
Elements of list are:
7->11->5->11->7->NULL


Process exited normally.
Press any key to continue . . .

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote