Question

Circle Game Sheila is a naughty girl. Yesterday she came up with a boring game and forced other classmates to play with her. The rule is simple. Some classmates will form a circle initially. Each classmate will be identified by a tag (an integer). Unluckily, you are chosen to be the judge of this game and answer Sheilas queries. Sheila will give the following types of orders: 1. Student with tag T is added to the circle. 2. Remove a student from the circle 3. Do the query on current circle. Please use c++ and write a class to implement linked list for this problem and do not use the libraries <list> <vector> etc. Input The input consists of a single test case. The first line contains an integer N (1N<1000) indicating the initial number of students in the circle. Then follows N integers indicating the tags of the N students in clockwise direction. The circle begins with the first number in the sequence. Then there is an integer Q(1Q100) denoting the number of operations. Initially, the reference point points to the first student, then after each operation, the reference point is updated. There will be 3 kinds of operations in the following format:

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note : Completed as per your requirement... Please run and check with your inputs..

Code:

#include <iostream>
#include <unistd.h>

using namespace std;

struct node
{
   int data;                       //struct to store the data...
   node *next;                  //Pointer to point next cell in a list..
};

class linked_list
{
   private:
       node *head, *tail;
   public:
       linked_list()
       {
           head = NULL;
           tail = NULL;
       }

       int add_node(int n)                      //Func to add initial list elements...
       {

            node *tmp = new node;   //Creating new node...

           tmp->data = n;           //Assigning user entered value to it..
           tmp->next = head;       //Since it is circular list assigning head

           if(head == NULL)
           {
               head = tmp;        //New List...First element..
               tail = head;
           }
           else
           {
               node *temp = new node;   //Create temporary node to traverse list
               temp = head;
               while( temp != tail)    //Traverse until end of the list
               {
                   temp = temp->next;   //Point to next cell
               }
               temp->next = tmp;       //Add at the end of the list..
               tail = tmp;               //Assigning temp to tail..
           }
        tail->next = head;      //Since circular list always make tail's next to point head..
       return 0;
       }
      
       int add_nodeAtPosition(int position, int value, int no, int *ref) //Tag 1 func call...
       {
           node *newnode = new node;       //Creating new node...
           newnode->data = value;           //Assigning user entered value to it..
           newnode->next = NULL;          
  
           if(head == NULL)               //Validating list...
           {      
               cout<<"\n\t<--Head null-->\n";
               head = newnode;            //If list is empty assign new node to head..
           }
           else if (position > no) {
               cout<<"\n\t<--Position out of range-->\n"; //Invalid position entered by user..
               return 0;
           }
           else{

               node *temp1 = head;
               node *temp2 = head;

               while(--position) {           //Make it to point the exact poisition entered by user..      
                   temp1 = temp1->next;   //Point to next cell
                   temp2 = temp2->next;
                   (*ref)++;               //Refernce value to update the reference position...
               }  
               newnode -> next = temp2 -> next; //Inserting new node...          
               temp1->next = newnode;              //Please understand this by analysing this..  
               (*ref)++;
           }

       return 0;
       }

       int deleteAtPosition(int position, int no, int *ref)
       {
              
           if(head == NULL)               //Validating list...
           {
               cout<<"\n\t<--List empty-->\n";
               return 0;
           }
           else if (position > no) {       //Invalid position entered by user..
               cout<<"\n\t<--Position out of range-->\n";
               return 0;
           }
           else{
               (*ref) = 1;
               node *temp1 = new node;       //Temporary nodes to traverse list..
               node *temp = new node;
               temp = head;
              
               if (position == 1){           //Delete at begining...
                   temp = head;
                   tail->next = head -> next;   
                   head = head->next;
                   delete temp;
                   return 0;
               }
               else
               {
                   --position;              //Make position value to point exactly where the user entered..  
                   while(--position) {
                       temp = temp->next;
                       (*ref)++;
                   }
                   temp1 = temp-> next;
                   temp -> next = temp -> next -> next;
                   delete temp1;        //Free the memory...
                   (*ref)++;  
               }
           }
       return 0;
       }

       int updateReference(int *ref) {        //Func to update the refernce pointer...
          
           int pos = 1;
           node *temp = new node;
           temp = head;
           while(pos != *(ref)) {           //Traverse untill the refernce index and stop traversal..  
               temp = temp->next;
               pos++;
           }
           head = temp;                   //make head to point the refernce index...
          
           while(tail->next != head) {      
               tail = tail->next;           //Make tail to point the index before head..
           }  
           (*ref) = 1;
       return 0;
       }  
      
       int display_node(int position, int *ref) { //Tag 3
          
           node *temp = new node;
           temp = head;
           (*ref) = 1;
           while(--position) {               //Point to the position entered b user...
               temp = temp->next;
               (*ref)++;                   //Increment the index of the refernce position also..
           }  
           cout<<"\n\t"<<temp->data<<"\n"; //Display the value in the index...
       return 0;
       }

   /*   int disp_node() {
           node *temp = new node;
           temp = head;
           cout<<"\t";
           do{
               cout<<temp->data<<"-->";
               temp = temp->next;
           }while(temp != head);
           cout<<endl<<endl;
       return 0;
       }
   */
};

int main()
{
   linked_list a;
   static int ref = 1;
   int no, value;
   int i = 0;
   int tags;
   int nop;
   int pos = 1;
   cin>>no;           //Total no of elements..
  
   for(i = 0; i < no; i++) { //Adding initial elements...
       cin>>value;
       a.add_node(value);
   }
   a.disp_node();
   cin>>nop;
   
   while(nop--)
   {
       cin>>tags>>pos;   //Tags and ith postition..
       if (tags == 1) {
          
           cin>>value;        //Value to be inserted...

           //cout<<"\n\tAfter inserting value : "<<endl;  
           a.add_nodeAtPosition(pos, value, no, &ref);
           //a.disp_node();
           a.updateReference(&ref);
           //cout<<"\n\tAfter refernce position update : "<<endl;  
           //a.disp_node();
       }
       else if(tags == 2) {
                  
           a.deleteAtPosition(pos,no, &ref);
           a.updateReference(&ref);
           //cout<<"\n\tAfter refernce position update : "<<endl;  
           //a.disp_node();
       }
       else if(tags == 3) {

           a.display_node(pos, &ref);
           a.updateReference(&ref);
           //cout<<"\n\tAfter refernce position update : "<<endl;  
           //a.disp_node();
       }
       else
           cout<<"\nInvalid tags.."<<endl;
   }
   return 0;
}

Output:

        > ./a.out 1 2 3 45 6 8 4 1 3 33 After inserting value After refernce position update 3 4 6 After refernce position update 2 5 After refernce position update 3 7 2 After refernce position update

Output 2: (as per you required..)

       

________________________________________________________________________________________

Please comment if required any help...

Add a comment
Know the answer?
Add Answer to:
Circle Game Sheila is a naughty girl. Yesterday she came up with a boring game and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Josefine is currently setting up a new computer network at The University of Algorithms. The netw...

    Algorithm implementation in Java please. Josefine is currently setting up a new computer network at The University of Algorithms. The network consists of N computers numbered from 0 to N -1 that are initially not connected at all She builds the network by adding network cables between pairs of computers one at a time. Two computers A and B are connected if there exists at least one series of cables that leads from computer A to computer B. As other...

  • Topic:Data structures and algorithms programming Compiler:C++ 11, flag: -static -std=c++0x Boring job One day, Rick got...

    Topic:Data structures and algorithms programming Compiler:C++ 11, flag: -static -std=c++0x Boring job One day, Rick got a sequence of N magic numbers. In order to find the latent code in the sequence, he decides to do the following operations with a magic parameter K: Step (1) Take out the first k numbers from current sequences into a temporary sequence ni namn, and pick the largest element Vmax out of these k numbers, then subtract 1 from the remaining K-1 elements....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT