Question

Given a single linked chain of nodes with positive integers, write an algorithm in pseudocode to...

Given a single linked chain of nodes with positive integers, write an algorithm in pseudocode to rearrange the nodes in one pass in such a way that all integers that are odd appear at the end. The relative order of the elements does not need to remain the same.

List all the steps in the proper order that are necessary to rearrange the nodes, show how the next pointers are being changed. Each data element should “stay” in its original node. The algorithm cannot assume that the chain is not empty.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1). ANSWER :

GIVENTHAT :

Pseudo Code:( I suggest you to have a look on implementation given below :)

ListNode OddInTheEnd(ListNode head){

   ListNode odd = NULL;
   ListNode otail = NULL;
  
   ListNode even = NULL;
   ListNode etail = NULL;
  
ListNode curr = *head;

while(curr != NULL){
  
if (curr->data == 1)
       {
           if (odd == NULL)
           odd = otail = curr; END IF

           else
           {
otail->next = curr;
           otail = otail->next;
           } END ELSE

       } END IF
  
else
       {
           if (even == NULL)
               even = etail = curr; END IF

           else
           {
               etail->next = curr;
               etail = curr;
           }END ELSE
       } END ELSE

curr = curr->next;

} END WHILE


if (even)
   {
       *head = even;
       etail->next = odd;
   } END IF

else
       *head = odd; END ELSE

if (otail)
       otail->next = NULL;
END IF

} END FUNCTION

Implementation in C:

#include <stdio.h>
#include <stdlib.h>

// linked list node
struct Node
{
   int data;
   struct Node* next;
};


// arrange linked list in such a order such that
// odd nodes appear in the end
void OddInTheEnd(struct Node** head)
{
// will point head of odd node list
   struct Node* odd = NULL;
   struct Node* otail = NULL;
  
   // will point head of even node list
   struct Node* even = NULL;
   struct Node* etail = NULL;

   struct Node* curr = *head;

// loop untill curr is not null i.e till end node
   while (curr != NULL)
   {
       if (curr->data & 1) // if current node is odd
       {
           // 1st odd node
           if (odd == NULL)
               odd = otail = curr;
           else
           {
               otail->next = curr;
               otail = otail->next;
           }
       }
       else // else current node is even
       {
           // 1st even node
           if (even == NULL)
               even = etail = curr;
           else
           {
               etail->next = curr;
               etail = curr;
           }
       }
       curr = curr->next;
   }

   // if list contains at-least one even node
   if (even)
   {
       *head = even;
       etail->next = odd;
   }
   // if list contains all odd nodes
   else
       *head = odd;

   // NULL to terminate the list,
   if (otail)
       otail->next = NULL;
}

// insert a new Node in the beginning of the linked list
void push(struct Node** head, int data)
{
   struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
   newnode->data = data;
   newnode->next = *head;
   *head = newnode;
}

// print given linked list
void print(struct Node* head)
{
   struct Node* ptr = head;
   while (ptr)
   {
       printf("%d -> ", ptr->data);
       ptr = ptr->next;
   }

   printf("NULL\n");
}

// main method
int main(void)
{

   struct Node* head = NULL;
  
       push(&head,13);
       push(&head,34);
       push(&head,22);
       push(&head,12);
       push(&head,21);
       push(&head,2);
       push(&head,1);
       push(&head,6);
       push(&head,7);
      
       printf("\n");
      
       printf(" List before Arranging : ");
      
       print(head);
      
       printf("\n");

   OddInTheEnd(&head);
  
   printf(" List After Arranging odd nodes in the end : ");

   print(head);

   return 0;
}

THE ABOVE PROGRAM OUTPUT :-

< input List before Arranging : 7 -> 6 -> 1 -> 2 -> 21 -> 12 -> 22 -> 34 -> 13 -> NULL List After Arranging odd nodes in the

Add a comment
Know the answer?
Add Answer to:
Given a single linked chain of nodes with positive integers, write an algorithm in pseudocode to...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++. Please note the BOLDED ITEMS. You will create a simple linked structure. You will use...

    C++. Please note the BOLDED ITEMS. You will create a simple linked structure. You will use a simple node that has a pointer to a Node. The data for the Node will be a single data member of type char. You will build a structure where the last node you add will point to the first node created, i.e. it will be a circular linked structure. You will create a program that stores characters provided by the user, stored in...

  • Please Write Pseudocode or Python code! Thanks! P1. b) is the following: W1. (6 marks) Write...

    Please Write Pseudocode or Python code! Thanks! P1. b) is the following: W1. (6 marks) Write the pseudocode for removing and returning only the second element from the Stack. The top element of the Stack will remain the top element after this operation. You may assume that the Stack contains at least two items before this operation. (a) Write the algorithm as a provider implementing a Stack using a contiguous memory implementation. You can refer to the implementation given in...

  • I need this in C++. This is all one question Program 2: Linked List Class For...

    I need this in C++. This is all one question Program 2: Linked List Class For this problem, let us take the linked list we wrote in a functional manner in a previous assignment and convert it into a Linked List class. For extra practice with pointers we'll expand its functionality and make it a doubly linked list with the ability to traverse in both directions. Since the list is doubly linked, each node will have the following structure: struct...

  • 16 and 18 C++ 11. Given the dynamic linked implementation of a linked list shown below,...

    16 and 18 C++ 11. Given the dynamic linked implementation of a linked list shown below, write expressions that do the following, assuming that currPtr is somewhere in the middle of the list: a. Access the component member of the first list element. b. Advance currPtr to point to the next element. c. Access the component member of the next element (the one that follows the current element). d. Access the component member of the element that follows the next...

  • Here is the IntegerLinkedList_incomplete class: public class IntegerLinkedList { static class Node { /** The element...

    Here is the IntegerLinkedList_incomplete class: public class IntegerLinkedList { static class Node { /** The element stored at this node */ private int element; // reference to the element stored at this node /** A reference to the subsequent node in the list */ private Node next; // reference to the subsequent node in the list /** * Creates a node with the given element and next node. * * @param e the element to be stored * @param n...

  • Write a program that initializes an array with ten random integers and then prints out the...

    Write a program that initializes an array with ten random integers and then prints out the following: Every element at an even index; Every even element All elements in reverse order; Only the first and last elements; The minimum and maximum element The sum of all elements The alternating sum of all elements, where the alternating sum contains all elements at even index added, and the elements at odd index subtracted. Please write comments above the piece of code that...

  • Data Structures - Singly Linked Lists You will add a method swapNodes to SinglyLinkedList class (below). This method should swap two nodes node1 and node2 (and not just their contents) given reference...

    Data Structures - Singly Linked Lists You will add a method swapNodes to SinglyLinkedList class (below). This method should swap two nodes node1 and node2 (and not just their contents) given references only to node1 and node2. The new method should check if node1 and node2 are the same node, etc. Write the main method to test the swapNodes method. You may need to traverse the list. package linkedlists; public class SinglyLinkedList<E> implements Cloneable {    // ---------------- nested Node class...

  • Problem 1 Given a linked list of integers, write a function getUnique that removes all duplicates elements in the linked list and returns the new linked list of unique elements (The order does not ma...

    Problem 1 Given a linked list of integers, write a function getUnique that removes all duplicates elements in the linked list and returns the new linked list of unique elements (The order does not matter). Example: Input: 1-»2->3->1-2 Output: 1->2->3 public class Node f int iterm Node next; Node(int d) t item = d; next-null; ) import java.util.ArrayList; public class ExtraLab public static void main (String[] args)t PROBLEM 1 System.out.println("PROBLEM 1"); Node head new Node(1); head.next-new Node (2); head.next.next-new Node(3);...

  • This is a c++ class utilizing class templates and linked lists and Nodes. I need to...

    This is a c++ class utilizing class templates and linked lists and Nodes. I need to implement the following member function(s) to LinkedBag.cpp. Node.hpp/cpp should be fine but if you feel like there needs to be a change for compilation or testing, feel free to do so but make sure to comment on why it was done. In this case, I need to join the original items with the user items(a_bag). So if the original has {1,2,3} and a_bag has...

  • Project Description: In this project, you will combine the work you’ve done in previous assignments to...

    Project Description: In this project, you will combine the work you’ve done in previous assignments to create a separate chaining hash table. Overview of Separate Chaining Hash Tables The purpose of a hash table is to store and retrieve an unordered set of items. A separate chaining hash table is an array of linked lists. The hash function for this project is the modulus operator item%tablesize. This is similar to the simple array hash table from lab 5. However, the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT