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
  • Programming Assignment 1 Data structure Java Implement Shellsort for a linked list, based on a variant...

    Programming Assignment 1 Data structure Java Implement Shellsort for a linked list, based on a variant of bubble sort. The rather severe constraints imposed by the singly-linked list organization presents special problems for implementing Shellsort. Your task is to overcome these constraints and develop a simple, elegant implementation that does not sacrifice efficiency or waste space. Step 1. First, implement a routine to build a list: read integers from standard input, and build a list node for each item consisting...

  • 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...

  • Question: SWAPPING NODES IN A SINGULARLY LINKED LIST: I am attempting to create a program that...

    Question: SWAPPING NODES IN A SINGULARLY LINKED LIST: I am attempting to create a program that swaps 2 nodes (no matter where they are in the list) and am having some difficulty. I can't seem to figure out why this swap method is throwing an error. Code: (SWAPPING METHOD AND TEST LINE BOLDED) package linkedlists; public class SinglyLinkedList<E> implements Cloneable { //---------------- nested Node class ---------------- /** * Node of a singly linked list, which stores a reference to its...

  • 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...

  • Creating linked list data structure Overview The purpose of this assignment is for you to write...

    Creating linked list data structure Overview The purpose of this assignment is for you to write a data structure called a Linked List, which utilizes templates (similar to Java’s generics), in order to store any type of data. In addition, the nature of a Linked List will give you some experience dealing with non-contiguous memory organization. This will also give you more experience using pointers and memory management. Pointers, memory allocation, and understand how data is stored in memory will...

  • 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...

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