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

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...
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...
Suppose that we performed the algorithm SELECT whose running time is O(n) on 133 elements, and...
Suppose that we performed the algorithm SELECT whose running time is O(n) on 133 elements, and found the median of medians x by making groups of 5 elements. What is the maximum number of elements which are guaranteed to be greater than equals to x (without counting x, itself)?
Write in C++: create a Doubly Linked List class that holds a struct with an integer...
Write in C++: create a Doubly Linked List class that holds a struct with an integer and a string. It must have append, insert, remove, find, and clear.
Description The purpose of this challenge is to implement a circular doubly-linked list using a dummy...
Description The purpose of this challenge is to implement a circular doubly-linked list using a dummy node. This challenge simulates an operating system’s window manager. Requirements Write the following struct struct Window { string appname; Window *next; Window *prev; }; Create a class called WindowManager. In this class, create a private variable Window * head. This will keep track of the location of the head node. Create private variables Window * current, * dummy. current will keep track of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT