MHB Algorithm for Ordering Odds & Evens in Linked List L

  • Thread starter Thread starter evinda
  • Start date Start date
  • Tags Tags
    Algorithm List
AI Thread Summary
The discussion centers around creating an algorithm to rearrange a singly-linked list, where elements with odd numbers appear before those with even numbers, while maintaining their original order. The initial attempt at the algorithm is critiqued for referencing null pointers and for traversing the list twice—once for odd numbers and once for even numbers. A complete solution is provided, which correctly implements the idea of appending odd and even nodes to a new list. The solution includes a function that constructs the linked list and another that rearranges it. However, it is suggested that the exercise likely intended for a single traversal of the list to achieve the desired outcome, which would be a more efficient approach. The discussion concludes with a query about whether the provided function only appends odd nodes, indicating a need for clarification on the complete functionality of the rearrangement process.
evinda
Gold Member
MHB
Messages
3,741
Reaction score
0
Hello! (Wave)

Consider a singly-linked list [m] L[/m] each element of which is a struct with two fields, an integer [m]num[/m] and a pointer [m]next[/m] to the next element of the list.

Describe an algorithm that gets as argument a pointer to the first element of the list [m]L[/m] and that creates a new list [m]L'[/m] that will contain all the elements of [m]L[/m] ordered in the following way:

The elements of [m]L[/m] for which the field [m]num[/m] is an odd number have to appear in [m]L'[/m] before all the elements of which the field [m]num[/m] is an even number. The elements with an odd number at the field [m]num[/m] should keep in [m]L'[/m] the display order that they had at the initial list [m]L[/m].

The same should hold for the elements with an even number at the field [m]num[/m].

For example, if the initial list is [m]L=13->15->20->17->24->26->9->30->53->44[/m] the final list should be [m]L'=13->15->17->9->53->20->24->26->50->44[/m].That's what I have tried:

Code:
   List(L){
      if (L==NULL) return;
      node *p=NULL, *q=L, *l=L, *L3=NULL, *head2=NULL, *tail1=NULL, *tail2=NULL;
      while (q!=NULL){
            if (q-> num mod 2==1){
                if (p==NULL){
                   p->num=q->num;
                   L3=p;
                }
                else {
                   p=p->next;
                   p->num=q->num;
               }
            }
      }
      tail=p;
      while (l!=NULL){
            if (l-> num mod 2==0){
               if (l==NULL){
                   l->num=q->num;
                   head2=l;
                }
                else {
                   l=l->next;
                   l->num=q->num;
               }
            }
      }
      tail2=q;
      tail1->next=head2;
      tail1=tail2;
      return L3;
   }
Could you tell me if it is right?
 
Technology news on Phys.org
Evinda,
No, it's not right. You continue to make the error of referencing a field of a null pointer. However, your main idea is okay. You traverse the list L twice with the first traversal appending all "odd" nodes to an initially empty list L3, and the second traversal appending the "even" nodes to L3. So your first while loop is pretty much correct. I could make no sense of the second while loop.
Here's a complete solution using your idea:
Code:
class Node {
public:
  int num;
  Node* next;

  Node(int v) : num(v), next(0) {
  }
};

Node* makeList(int a[], int n) {
  if (n == 0) {
    return (NULL);
  }
  int i;
  Node* first = new Node(a[0]), *p = first;
  for (i = 1; i < n; i++) {
    p->next = new Node(a[i]);
    p = p->next;
  }
  return (first);
}

Node* rearrange(Node* L) {
  Node* L3 = NULL, *p, *q;
  if (L == NULL) {
    return (NULL);
  }
  int i;
  for (i = 1; i >=0; i--) {
    q = L;
    while (q != NULL) {
      if (q->num % 2 == i) {
        if (L3 == NULL) {
          L3 = new Node(q->num);
          p = L3;
        } else {
          p->next = new Node(q->num);
          p = p->next;
        }
      }
      q = q->next;
    }
  }
  return(L3);
}

int main(int argc, char** argv) {
  int a[] = {13, 15, 20, 17, 24, 26, 9, 30, 53, 44};
  Node *L = makeList(a, 10), *p = L;
  while (p != NULL) {
    cout << p->num << " ";
    p = p->next;
  }
  cout << endl;
  Node* Lprime = rearrange(L);
  p = Lprime;
  while (p != NULL) {
    cout << p->num << " ";
    p = p->next;
  }
  cout << endl;
  return (0);
}

However, I strongly suspect the exercise meant for you to write a function that traverses the original list only once. This is only slightly more complicated. I advise you to write this function.
 
johng said:
Evinda,
No, it's not right. You continue to make the error of referencing a field of a null pointer. However, your main idea is okay. You traverse the list L twice with the first traversal appending all "odd" nodes to an initially empty list L3, and the second traversal appending the "even" nodes to L3. So your first while loop is pretty much correct. I could make no sense of the second while loop.
Here's a complete solution using your idea:
Code:
class Node {
public:
  int num;
  Node* next;

  Node(int v) : num(v), next(0) {
  }
};

Node* makeList(int a[], int n) {
  if (n == 0) {
    return (NULL);
  }
  int i;
  Node* first = new Node(a[0]), *p = first;
  for (i = 1; i < n; i++) {
    p->next = new Node(a[i]);
    p = p->next;
  }
  return (first);
}

Node* rearrange(Node* L) {
  Node* L3 = NULL, *p, *q;
  if (L == NULL) {
    return (NULL);
  }
  int i;
  for (i = 1; i >=0; i--) {
    q = L;
    while (q != NULL) {
      if (q->num % 2 == i) {
        if (L3 == NULL) {
          L3 = new Node(q->num);
          p = L3;
        } else {
          p->next = new Node(q->num);
          p = p->next;
        }
      }
      q = q->next;
    }
  }
  return(L3);
}

int main(int argc, char** argv) {
  int a[] = {13, 15, 20, 17, 24, 26, 9, 30, 53, 44};
  Node *L = makeList(a, 10), *p = L;
  while (p != NULL) {
    cout << p->num << " ";
    p = p->next;
  }
  cout << endl;
  Node* Lprime = rearrange(L);
  p = Lprime;
  while (p != NULL) {
    cout << p->num << " ";
    p = p->next;
  }
  cout << endl;
  return (0);
}

However, I strongly suspect the exercise meant for you to write a function that traverses the original list only once. This is only slightly more complicated. I advise you to write this function.

With the function [m] rearrange [/m] we only append all "odd" nodes to the list L3, right?
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
2
Views
2K
Replies
7
Views
3K
Replies
1
Views
1K
Replies
1
Views
2K
Replies
17
Views
1K
Replies
29
Views
4K
Back
Top