Question

In: Computer Science

Question: Rotate and sort the list:-   In this problem, you have to first implement a singly...

Question:

Rotate and sort the list:-  

In this problem, you have to first implement a singly linked list whose each node should have the following attributes,

● key - a positive integer

● next - address/pointer/reference to the next node in the linked list You will receive Q1 queries of following types,

● 1 x - Append a node to the linked list whose key should be x. After appending, print, in a new line, the key of each node in the linked list separated by a single space.

● 2 x - Delete the node in the linked list whose key is x.If such a node is not present in the linked list, no changes should be made. After this, print, in a new line, the key of each node in the linked list separated by a single space. The deleted node, if any, should be added in another linked list. Then you will receive Q2 queries of the following type,

● k - The linked list should be rotated towards right by |k| steps if k > 0, otherwise the linked list should be rotated towards left by |k| steps. k will always be an integer. Once all of the above Q2 queries are done, print, in a new line, the key of each node in the linked list separated by a single space. Then you have to merge the two linked lists together after sorting (by key), each of them using the merge sort algorithm. Once done, print in a new line, the key of each node in the linked list separated by a single space. Note that the linked list in the end should also be sorted by key.

Input Format - The first line will contain a positive integer, Q1 , following which there will be Q1 lines containing queries as described above. Then the very next line will contain a positive integer Q2 following which will be Q2 lines containing queries as described above.

Output Format - Q1 + 2 lines with space separated values of the key of each node in the linked list as explained above.

Constraints ● 0 < Q1 < 1000

● 0 < Q2 < 10000

● 0 < key, x < 1000

● -100 < k < 100

Solutions

Expert Solution

#include <iostream>
using namespace std;
//defining node of a linked list
class Node  
{  
    public: 
    int data;  
    Node *next;  
};
//appending node to the end of linked list
void append(Node** head_ref, int new_data)   
{   
    Node* new_node = new Node();  
    Node *last = *head_ref;  
    new_node->data = new_data;   
    new_node->next = NULL;   
    if (*head_ref == NULL)   
    {   
        *head_ref = new_node;   
        return;   
    }    
    while (last->next != NULL)   
        last = last->next;   
    last->next = new_node;   
    return;   
}   
//deleting node of the linked list
void deleteNode(struct Node **head_ref, int key) 
{ 
   if (*head_ref == NULL) 
      return; 
   Node* temp = *head_ref; 
   if (temp->data==key) 
    { 
        *head_ref = temp->next;   // Change head 
        free(temp);               // free old head 
        return; 
    } 
  
    // Find previous node of the node to be deleted 
    for (int i=0; temp!=NULL && temp->data!=key; i++) 
         temp = temp->next; 
  
    // If position is more than number of nodes 
    if (temp == NULL || temp->next == NULL) 
         return; 
    Node *next = temp->next->next; 
    free(temp->next);  // Free memory 
    temp->next = next;  // Unlink the deleted node from list 
}
//rotating a linked list if k is negative(i.e counter clockwise)
void rotate_left(Node** head_ref, int k)
{
    if (k == 0)
        return;
    Node* current = *head_ref;
    int count = 1;
    while (count < k && current != NULL) {
        current = current->next;
        count++;
    }
    if (current == NULL)
        return;
    Node* kthNode = current;
    while (current->next != NULL)
        current = current->next;
    current->next = *head_ref;
    *head_ref = kthNode->next;
    kthNode->next = NULL;
} 
//function to rotate linked list towards right clockwise by k
Node* rotate_right(Node* head, int k) 
{ 
    if (!head) 
        return head; 
    Node* tmp = head; 
    int len = 1; 
    while (tmp->next != NULL) { 
        tmp = tmp->next; 
        len++; 
    } 
    if (k > len) 
        k = k % len; 
    k = len - k; 
    if (k == 0 || k == len) 
        return head; 
    Node* current = head; 
    int cnt = 1; 
    while (cnt < k && current != NULL) { 
        current = current->next; 
        cnt++; 
    } 
    if (current == NULL) 
        return head; 
    Node* kthnode = current; 
    tmp->next = head; 
    head = kthnode->next; 
    kthnode->next = NULL; 
    // Return the updated head pointer 
    return head; 
} 
//function which return size of the node
int linked_list_size(Node *head)
{
    Node *temp = head;
    int count = 0;
    while(temp!=NULL)
    {
       temp = temp->next;
       count++;
    }
    return count;
}
//function to print the node
void printList(Node* head)
{
    while (head != NULL) {
        cout << head->data << " ";
        head = head->next;
    }
}
int main() {
    int q1,q2;
    cin>>q1;
    Node* head = NULL;
    for(int i=0;i<q1;i++)
    {
        int sub_query_no,key;
        cin>>sub_query_no>>key;
        if(sub_query_no==1)
        {
            append(&head,key);
        }
        else
        {
            deleteNode(&head,key);
        }
        printList(head);
        cout<<endl;
    }
    int size = linked_list_size(head);
    cin>>q2;
    for(int i=0;i<q2;i++)
    {
        int k;
        cin>>k;
        if(k>=0)
        { 
           k = k%size;
           head =  rotate_right(head,k);
           printList(head);
           cout<<endl;
        }
        else
        {
            k = -1*k;
            k = k%size;
            rotate_left(&head,k);
            printList(head);
            cout<<endl;
        }
    }
}

INPUT AND OUTPUT


Related Solutions

8. Assume you have a singly linked list with no tail pointer. Implement removeTail(). Raise an...
8. Assume you have a singly linked list with no tail pointer. Implement removeTail(). Raise an exception of the method is called on an empty list. template<typename Object> class LinkedList { private: class Node { Object data; Node* next; }; Node *head; public: LinkedList() : head(nullptr) {} Object removeTail(Object data); }; 9. What are iterators? What purpose do they serve? 10. What does it mean to invalidate an iterator? 11. Explain the difference between separate chaining and open addressing in...
Using C++, Create a singly Linked List of patients list so that you can sort and...
Using C++, Create a singly Linked List of patients list so that you can sort and search the list by last 4 digits of the patient's Social Security number. Implement Node insertion, deletion, update and display functionality in the Linked List. Each patient's record or node should have the following data: Patient Name: Age: Last 4 digits of Social Security Number: The program should first display a menu that gives the user the option to: 1) Add a Patient's record...
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
Data Structure and Algorithm: 1. You are asked to use insertion sort to sort a singly...
Data Structure and Algorithm: 1. You are asked to use insertion sort to sort a singly linked list in ascending order. Why is this a bad idea? How does insertion sort’s runtime complexity change if you were to use it to sort a linked list? 2. Your classmate thinks that input arrays to the merge operation of merge sort don’t need to be in sorted order. Is your classmate right or wrong? Why?
4) Implement the Selection Sort algorithm discussed in class to sort a list of a certain...
4) Implement the Selection Sort algorithm discussed in class to sort a list of a certain size. The list is to be implemented using a dynamic array as well as a singly linked list. You are required to compare the performance (sorting time) for the dynamic array and singly-linked list-based implementations. You are given the startup codes for the dynamic array and singly-linked list based implementations. You are required to implement the Selection Sort function in each of these codes....
Java program to implement the merge sort your own and test it to sort a list...
Java program to implement the merge sort your own and test it to sort a list of names based on the frequency.
All code in JAVA please 1. Implement Insertion Sort 2. Implement Selection Sort *For problem 1...
All code in JAVA please 1. Implement Insertion Sort 2. Implement Selection Sort *For problem 1 and 2, please: a. Let the program generate a random array. b. Output both the original random array and the sorted version of it
Design, Develop and Implement the following operations on Singly Linked List (SLL) with a single field...
Design, Develop and Implement the following operations on Singly Linked List (SLL) with a single field data a.   Create a SLL for N Data by using front insertion. b.   Display the status of SLL and count the number of nodes in it c.   Perform Insertion and Deletion at End of SLL d.   Perform Insertion at the third position. e.    Delete the element at the Front of SLL f.     Perform Deletion at second position of SLL g.     Display the content. Design,...
In python: implement a singly linked list with following functions: - add_head(e) - add_tail(e) - find_3rd_to_last()...
In python: implement a singly linked list with following functions: - add_head(e) - add_tail(e) - find_3rd_to_last() - returns element located at third-to-last in the list - reverse() - reveres the linked list, note, this is not just printing elements in reverse order, this is actually reversing the list
Problem 2 For the same list as above, sort the list in such a way where...
Problem 2 For the same list as above, sort the list in such a way where the first set of numbers are ascending even and the second set of numbers are ascending odd. Your output should look like the following: [ 2, 4, 6, … 1, 3, 5 .. ]. The list is A = [ 6, 9, 0, 4, 2, 8, 0, 0, 8, 5, 2, 1, 3, 20, -1 ]
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT