Question

Implement a singly linked list having all unique elements with the following operations.I 0 x –...

Implement a singly linked list having all unique elements with the following operations.I 0 x – Inserts element x at the end.
I 1 y x – If the element y exists, then insert element x after the element y, else insert element y before the existing element x. Assuming either the element x or the element y exists.
I 2 z y x – Inserts element x in the middle of the elements z and y. The element z appears before the element y.
U x p – Links the next pointer of the element x to the node lying at the pth position from the element x while traversing towards right. In case of insufficient number of nodes, the counting continues by updating the existing linked list to its circular version.
Input:
Line 1 contains an integer N indicating the total number of operations.
Each of the following N lines contains an operation to be performed in the format as is described above.
Output:
Line 1 has 1 if the linked list gets updated to its circular version, else 0.
Line 2 has a count of the number of nodes whose addresses are contained in the next pointer of multiple nodes. If output at Line 2 is zero then output Line 4 will not be printed.
Line 3 has space separated contents of all the nodes which are counted to get the output at Line 2 in increasing order. If output at Line 2 is zero then output Line 3 will have space separated contents of the generated linked list.
Line 4 has space separated respective frequencies of each output value, say x, at Line 3. 'Frequency of each output value x' means the count of multiple nodes whose next pointers have address of a node with this value x.
Constraints
All integers range in between 1 and 1000.
Sample Input I:
3
I 0 1
I 1 1 7
I 2 1 7 3
Sample Output I:
0
0
1 3 7
EXPLANATION I:
Linked list after execution of each of the three operations given in the input is shown below.

1

1 - > 7

1 - > 3 - > 7

Sample Input II:
10
I 0 1
I 0 7
I 1 6 7
I 1 1 2
I 2 1 7 3
I 2 3 6 5
I 2 1 7 4
U 2 3
U 2 2
U 1 6
Sample Output II:
1
1
6
3

Homework Answers

Answer #1

CODE:

#include <iostream>

#include<map>

#include<vector>

#include<algorithm>

#include<cmath>

using namespace std;

class Node

{

public:

int val;

Node *next;

Node(int x)

{

this->next=NULL;this->val=x;

}

};

int searchPos(Node *head,int x)//search position of x

{

int c=0;

Node *temp=head;

while(temp!=NULL)

{

c++;

if(temp->val==x)

break;

temp=temp->next;

  

}

return c;

}

void insert(Node *head,int x,int pos,map<int,vector<Node*>> &m)//inserts node at pos x n updates map too

{

int l=pos;

l-=2;

Node *temp=head;

while(l--)

temp=temp->next;

Node *p=temp->next;

temp->next=new Node(x);

x->next=p;

m[x].push_back(temp);

}

int main()

{

int n;

cin>>n;

Node *head=NULL,*tail=head;

map<int,vector<Node*>>m;//map to store current node value n prev node addresses,traversal becomes easy

while(n--)

{ string s;

int circular=0;//circular list or not

getline(cin,s);

vector<string> v;

boost::split(v, s, boost::is_any_of(" ")); //splitting string

int len=v.size();

if(v[0]=='I')

{

if(len==3)

{

if(head==NULL)//empty list

{ head=new Node(v[2]);tail=head;m[v[2]].push_back(NULL);}

else//not empty list

{

tail->next=new Node(v[2]);

m[v[2]].push_back(tail);//push to map

tail=tail->next;

  

}

}

else if(len==4)

{

if(m.find(v[2])!=m.end())//adding next to y

{

Node *temp=head;

while(temp!=NULL && temp->val!=v[2])

temp=temp->next;

Node *x=new Node(v[3]);

x->next=temp->next;

temp->next=x;

m[v[3]].push_back(temp);

}

else//adding before x

{

Node *p=m[v[3]];

Node *cur=p->next;

Node *x=new Node(v[2]);

if(p==NULL)

{

head=x;

x->next=cur;

m[x].push_back(NULL);

}

else

{

x->next=p->next;

p->next=x;

m[x].push_back(p);

}

}

}

else

{

int a,b,c;

a=searchPos(v[2]);//position of y

b=searchPos(v[3]);//position of z

c=ceil((a+b)/2);//calcute mid position for inserting x between y and z

insert(head,v[4],c,m);

}

}

else

{

int a,b,c=0;

a=v[1];

b=v[2];

Node *temp=head;

while(temp)//searching for x

{

if(temp->val==a)

break;

temp=temp->next;

}

Node *x=temp;

while(b--)//traversing p nodes from x

{

if(temp->next==NULL)//making it circular

{temp->next=head;circular=1;}

else

temp=temp->next;

  

}

x->next=temp;

m[temp->val].push_back(x);

}

}

int count=0;

vector<pair<int,int>>s;

for(auto x:m)

{

if(x->second.size()>1)

{ count+=1;

s.push_back(x->first,x->second.size());

}

}

sort(s.begin(),s.end())//sorting the nodes having multiple prev nodes

if(circular!=0)

cout<<"1"<<"\n";

else

cout<<"0\n";

cout<<count<<"\n";//count of nodes with multiple pre nodes

int p=s.size();

  

if(p!=0)

{

for(auto x:s)

cout<<x.first<<" ";

cout<<"\n";

for(auto x:s)

cout<<x.second<<" ";

cout<<"\n";

}

else//if there is no node with multiple pre nodes,print original linked list

{

Node *temp=head;

while(temp!=NULL)

{

cout<<temp->val<<" ";temp=temp->next;

}

cout<<"\n";

}

return 0;

}

Know the answer?
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
Similar Questions
1. Build a double linked list with 5 in the first node following the instructions: Insert...
1. Build a double linked list with 5 in the first node following the instructions: Insert a node with 3 at the top of the list Insert a node with 10 at the bottom of the list Insert a node with 7 between nodes with 5 and 10 2. Start deleting nodes from the list in the following order; Delete the node with 7 Delete the node with 3 Delete the node with 10 3. Print the resulting list forward...
Using C++ / provide code comments so I can understand. Create a simple linked list program...
Using C++ / provide code comments so I can understand. Create a simple linked list program to create a class list containing class node { void *info; node *next; public: node (void *v) {info = v; next = 0; } void put_next (node *n) {next = n;} node *get_next ( ) {return next;} void *get_info ( ) {return info;} }; Be able to initially fill the list. Provide functions to insert/append nodes and remove nodes from the linked list. Be...
8.19 LAB: Grocery shopping list (linked list: inserting at the end of a list) PLEASE ANSWER...
8.19 LAB: Grocery shopping list (linked list: inserting at the end of a list) PLEASE ANSWER IN C++ Given main(), define an InsertAtEnd() member function in the ItemNode class that adds an element to the end of a linked list. DO NOT print the dummy head node. Ex. if the input is: 4 Kale Lettuce Carrots Peanuts where 4 is the number of items to be inserted; Kale, Lettuce, Carrots, Peanuts are the names of the items to be added...
The language is Java. Using a singly-linked list, implement the four queue methods enqueue(), dequeue(), peek(),...
The language is Java. Using a singly-linked list, implement the four queue methods enqueue(), dequeue(), peek(), and isEmpty(). For this assignment, enqueue() will be implemented in an unusual manner. That is, in the version of enqueue() we will use, if the element being processed is already in the queue then the element will not be enqueued and the equivalent element already in the queue will be placed at the end of the queue. Additionally, you must implement a circular queue....
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings....
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings. You must base your code on the doubly linked list implementation given in my Week 8 slides. Change the code so that instead of an ‘int’ each node stores a string (choose a suitable size). Each node should also have a next node pointer, and previous node pointer. Then write functions to implement the following linked list operations: • A printList function that prints...
What will be the final linked-list after executing the following method on the given input singly...
What will be the final linked-list after executing the following method on the given input singly linked-list? Consider that the singly linked-list does not have a tail reference. Input: 1->2->3->4->5->6->7->8->null                                                                                                  void method(list){ if(list.head == null) return; Node slow_ref = list.head; Node fast_ref = list.head; Node prevS = null; Node prevF = null; while(fast_ref != null && fast_ref.next != null){ prevS = slow_ref; slow_ref = slow_ref.next; prevF = fast_ref; fast_ref = fast_ref.next.next; } prevS.next = slow_ref.next; prevF.next.next = slow_ref;...
C++ questions QUESTION 1 What kind of linked list begins with a pointer to the first...
C++ questions QUESTION 1 What kind of linked list begins with a pointer to the first node, and each node contains a pointer to the next node, and the pointer in the last node points back to the first node? A. Circular, singly-linked list. B. Circular, doubly-linked list. C. Singly-linked list. D. Doubly-linked list. E. None of the above.    QUESTION 2 _________ is not an advantage of linked lists when compared to arrays. A. Dynamic memory allocation. B. Efficient...
1. Design and implement a CircularLinkedList, which is essentially a linked list in which the next...
1. Design and implement a CircularLinkedList, which is essentially a linked list in which the next reference of the tail node is set to refer back to the head of the list (rather than null) . Start from the SinglyLinkedList provided in the Lecture (not the built-in Java LinkedList class). 2. Starting from  SinglyLinkedList you will need to modify or complete methods: first(), last(), addFirst(), addLast(), removeFirst(), toString(), and also create a new rotate() method which will move the tail to...
C++ PROGRAMMING Submit the code for each problem separately. Important: Please place all appropriate coding comments...
C++ PROGRAMMING Submit the code for each problem separately. Important: Please place all appropriate coding comments inside of the code. Problem 1    Create a simple linked list program to create a class list containing class node {             void *info;              node *next; public:              node (void *v) {info = v; next = 0; }              void put_next (node *n) {next = n;}              node *get_next ( ) {return next;}              void *get_info (...
A Queue is a linked list with pointers to both the head of the list as...
A Queue is a linked list with pointers to both the head of the list as well as the tail (or the last element) of the list. Given a queue Q, Q.head gives the head of the queue and Q.tail gives the tail of the queue. Give O(1) time algorithms for the following tasks. Enqueue • Input: A queue Q of distinct integers and a queue element a not in Q. 1 • Output: Q with the element a added...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT