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...
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....
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...
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++ 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 (...
C++ Goals  Build single linked lists using pointers  Learn how to manipulate linked lists...
C++ Goals  Build single linked lists using pointers  Learn how to manipulate linked lists In this lab, you will create simple single linked structures consisting of Node objects. Each node will have a pointer to the next node. You will use a head pointer to keep track of the first node in the linked list, and a tail pointer to keep track of the last node in the linked list. Set both head and tail to NULL when...
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...
In this code, I build a single-linked list using a node class that has been created....
In this code, I build a single-linked list using a node class that has been created. How could I change this code to take data of type T, rather than int. (PS: ignore the fact that IOHelper.getInt won't work for the type T... ie second half of main). Here's my code right now: public class SLList { public SLNode head = null; public SLNode tail = null; public void add(int a) {// add() method present for testing purposes SLNode newNode...
Project 1 - NodeList write in c++ with 5 files: main.cpp List.h List.cpp ListNode.h ListNode.cpp Building...
Project 1 - NodeList write in c++ with 5 files: main.cpp List.h List.cpp ListNode.h ListNode.cpp Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3,...