Question

In: Computer Science

(15 pts) Describe an O(n)-time algorithm that partitions a doubly linked list L into two doubly...

(15 pts) Describe an O(n)-time algorithm that partitions a doubly linked list L into two doubly linked lists L1 and L2, where L1 contains the odd-number-th elements in L and L2 contains the even-number-th elements in L. Elements in both L1 and L2 should appear in the same order as they appear in L. For example, if L contains the numbers 4, 7, 9, 1, and -3, in that order, then the output L1 should contain 4, 9, and -3, in that order, and L2 should contain 7 and 1, in that order.

Solutions

Expert Solution

  1. First of all we take two head i.e head1 is the head of L1 i.e odd number -th node and head 2 is the head of L2 i.e head of the even number-th node. also we have a counter variable called count=1 which is used to decide that current node odd number-th or even number by calculating count%2==0 means even number otherwise odd number.
  2. Declare three temporary node temp1=head1,temp2=head2,current=head i.e head of list L
  3. while current !=null do:
    • if count %2==0 : i.e even th number node then
      • if(head2==null) // beacause initially head1 and head2 is null
        • head2=current;
        • temp2=current;
        • head2.prev=null;
      • else
        • temp2.next=current
        • current.prev=temp2;
        • temp2=current;
    • else i.e odd th number node
      • if(head1==null)
        • head1=current;
        • temp1=current;
        • head1.prev=null;
      • ​​​​​​​else
        • temp1.next=current
        • current.prev=temp1;
        • temp1=current;
    • count=count+1;
    • current=current.next;
  4. temp1.next=null;
  5. temp2.next=null;
  6. return head1 and head 2.

Related Solutions

Can you make this singular linked list to doubly linked list Create a Doubly Linked List....
Can you make this singular linked list to doubly linked list Create a Doubly Linked List. Use this to create a Sorted Linked List, Use this to create a prioritized list by use. Bring to front those links recently queried. -----link.h------ #ifndef LINK_H #define LINK_H struct Link{ int data; Link *lnkNxt; }; #endif /* LINK_H */ ----main.cpp---- //System Level Libraries #include <iostream> //I/O Library using namespace std; //Libraries compiled under std #include"Link.h" //Global Constants - Science/Math Related //Conversions, Higher Dimensions...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end;...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end; the nodes form a full circle. Instead of keeping track of the node at the front, we keep track of a current node instead. Write a class for a circular doubly-linked list using the attached Job class as your node objects. It should have: • A private instance variable for the current node • A getCurrent() method that returns a reference to the current...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program that will update bank accounts stored in a master file using updates from a transaction file. The program will maintain accounts using a doubly linked list. The input data will consist of two text files: a master file and a transaction file. See data in Test section below.  The master file will contain only the current account data. For each account, it will contain account...
I was supposed to conver a singly linked list to a doubly linked list and everytime...
I was supposed to conver a singly linked list to a doubly linked list and everytime I run my program the output prints a bunch of random numbers constantly until I close the console. Here is the code. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> struct node { int data; struct node *next; struct node *prev; }; //this always points to first link struct node *head = NULL; //this always points to last link struct node *tail = NULL;...
Describe an O(n)-time algorithm that checks if brackets in a given arithmetic expression is properly placed,...
Describe an O(n)-time algorithm that checks if brackets in a given arithmetic expression is properly placed, i.e., all left brackets are matched with their right counterparts and there are no unmatched brackets between any pair of matched brackets. Note that there are generally more than one types of brackets and only those of the same type can match against each other. For simplicity, you can assume that all operands are represented as ‘a’ and addition, +, is the only available...
What is the Average time complexity of sequential search algorithm in a linked list?
What is the Average time complexity of sequential search algorithm in a linked list?
Write the following algorithms for a Doubly Linked List Inserting an item                              
Write the following algorithms for a Doubly Linked List Inserting an item                                                                                                                              [7] Deleting an item                                                                                                                               [7] Question two Take a queue containing numbers 10, 15, 5, 25, 30 in which 30 has been inserted first. After performing the following operations, what would be the contents of the queue? Delete two elements                                                                                                                      [2] Insert 7 and then 20                                                                                                                        [2] Delete an element                                                                                                                          [2]
Pseudocode and algorithm of merging k sorted list with running time O(Nlogk).
Pseudocode and algorithm of merging k sorted list with running time O(Nlogk).
I know this code takes in a set of numbers into a doubly linked list and...
I know this code takes in a set of numbers into a doubly linked list and sorts it using insertion sort. Could you explain exactly how this code is working? main.c Source Code: #include #include #include "node.h" int main() { struct mynode *head=NULL; int value; printf("Give first value: \n"); scanf("%d",&value); printf("Give next values, and input 0 to end list: \n"); do{ if(value>0){    head = pushNode(head, value);    scanf("%d",&value); } }while (value>0); printf("Before insertion sort: "); printlist(head); head=insertsort(head); printf("After insertion...
This is the code what I have for doubly linked list for STACK. This is Python...
This is the code what I have for doubly linked list for STACK. This is Python language and I want anyone to help me with the following questions. Can you check for me if it is good Doubly Linked List? ####THIS IS THE ENTIRE ASSIGNMENT#### ADD the Following feature: Include a class attribute in the container class called name. In the implementation - Pod: You should ask the user to enter the name of the container and the program should...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT