473,395 Members | 1,915 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

problem with pointers when sorting a double linked list using merge sort

nabh4u
62
hi friends,

i have a program where i have to sort a double linked list using merge sort. i am having problem with changing the pointers of the list, like the previous and the next pointers of a node. i am unable to understand how can i change those. i tried something but that is not working. please help me as soon as possible as i have a deadline. here is a sample :

Expand|Select|Wrap|Line Numbers
  1. int merge(low,mid,high)
  2. {
  3.    int i,j,k;
  4.    i=low;
  5.    j=mid+1;
  6.    list *temp,*temp1;
  7.    while(i<=mid && j<=high)
  8.    {
  9.       if(current->n <= current->next->n)
  10.       {
  11.          current=current->next;
  12.          i++;
  13.       }
  14.       else
  15.       {
  16.          temp->prev=current->prev;
  17.          temp->next=current->next;
  18.          current->prev=current->next->prev;
  19.          current->next=current->next->next;
  20.          temp1->prev=temp->prev;
  21.          temp1->next=temp->next;
  22.          j++;
  23.       }
  24.    }
current is the pointer pointing to the current node of the list.

this is a kind of implementation i tried. please tell me how should i change the pointers in the list. plese help me, its urgent.

thanx in advance.
Mar 16 '07 #1
2 3523
Ganon11
3,652 Expert 2GB
Is this the exact code you wre using? The function header needs to declare the types of the parameters (i.e. "int low, int mid, int high" instead of "low, mid, high"). Also, there is a curly bracket missing to close the function.

Next, this line

Expand|Select|Wrap|Line Numbers
  1. current->prev=current->next->prev;
is kind of redundant. current->next->prev should bring you back to current, so why not write

Expand|Select|Wrap|Line Numbers
  1. current->prev = current;
Mar 16 '07 #2
nabh4u
62
Is this the exact code you wre using? The function header needs to declare the types of the parameters (i.e. "int low, int mid, int high" instead of "low, mid, high"). Also, there is a curly bracket missing to close the function.

Next, this line

Expand|Select|Wrap|Line Numbers
  1. current->prev=current->next->prev;
is kind of redundant. current->next->prev should bring you back to current, so why not write

Expand|Select|Wrap|Line Numbers
  1. current->prev = current;
well tht was the sample code i was using but now i changed it. now when i print the list it goes into an infinte loop but when i print the list in reverse it doesnt print all the nodes and the sequence is also wrong (prints the earlier reverse version where the list is not sorted).
eg: the list is 4 3 1 2
after sorting it should be 1 2 3 4 (forward print)
but when printing in forward it goes into infinte loop with 4
and for reverse print i get 1 2 3 only but it should be 4 3 2 1.

here's the changed code:

Expand|Select|Wrap|Line Numbers
  1. int merge(int low,int mid,int high)
  2. {
  3.    int i,j,k;
  4.    i=low;
  5.    j=mid+1;
  6.    list *temp = new list();
  7.    list *temp1=new list();
  8.    current=first;
  9.    temp=current->next;
  10.    temp1->next=current->next;
  11.    temp1->prev=current->prev;
  12.  
  13.    while(i<=mid && j<=high)
  14.    {
  15.       if(current->n <= temp->n)
  16.       {
  17.          current=current->next;
  18.          temp=current->next;
  19.          temp1->next=current->next;
  20.          temp1->prev=current->prev;
  21.          i++;
  22.       }
  23.       else
  24.       {
  25.          current->next=temp->next;
  26.          current->prev=temp;
  27.          temp->next=current;
  28.          temp->prev=temp1->prev;     
  29.          j++;
  30.       }
  31.    } 
  32. }
what about the remaining part of the list? please tell me if this code successfully sorts the link list or not and what is the problem.
please tell me how exactly i should change the pointers.. this is creating a lot of confusion..please correct my code.
thank you in advance...
Mar 16 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Eugen J. Sobchenko | last post by:
Hi! I'm writing function which swaps two arbitrary elements of double-linked list. References to the next element of list must be unique or NULL (even during swap procedure), the same condition...
4
by: JS | last post by:
I have a file called test.c. There I create a pointer to a pcb struct: struct pcb {   void *(*start_routine) (void *);   void *arg;   jmp_buf state;   int    stack; }; ...
4
by: Peter Schmitz | last post by:
Hi, in my application, I defined a linked list just as follows: typedef struct _MYLIST{ int myval; void *next; }MYLIST; MYLIST head;
1
by: PhilB | last post by:
I have been having issues trying to merge sort a double linked list. (I would supply the code, but it is locked away on an inaccessable computer.) The list always results in an unsorted list, but...
4
by: shaveta | last post by:
pls help me to write a program in c that reads n elements of type float, stores them in a linked list ,sort it in the descending order using selection sort methods, and finally output the sorted...
4
by: peo_leo | last post by:
I have a simple implementation of double linked list this code is crashing if i enter values not present in the linked list(during insertion) or if i try to delete values not present in the list...
2
by: zl2k | last post by:
hi, I have one program runs fine on my i386 linux box but get the "glibc detected *** corrupted double-linked list" error on the x86_64 linux box. Please help me to figure out if it is my...
9
by: Sheldon | last post by:
Hi, I am trying to understand linked lists and the different ways to write a linked list and double linked list. I have been trying to get this function called insert_word to work but to no...
6
by: tgnelson85 | last post by:
Hello, C question here (running on Linux, though there should be no platform specific code). After reading through a few examples, and following one in a book, for linked lists i thought i would...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.