Merge and sort 2 linked lists

Reply

Join Date: Jan 2008
Posts: 19
Reputation: Olsi009 is an unknown quantity at this point 
Solved Threads: 0
Olsi009 Olsi009 is offline Offline
Newbie Poster

Merge and sort 2 linked lists

 
0
  #1
Jan 26th, 2008
Hi guys (again),

Another exercise in Turbo Pascal about linked lists.

Write a program that merges 2 linked lists and sorts elements from the lowest to the greatest.

let suppose the list has this elements

list = ^pointer;
pointer = record
value:integer;
next:list;
end;


Please help, I have an exam after 3 hrs
thanks in advance
Learning is the process whereby knowledge is created, through the tranformation of experience...
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Merge and sort 2 linked lists

 
0
  #2
Jan 26th, 2008
I'm sorry, but you got too much of a bone from that last one.

Method 1:
Get yourself a large piece of paper and a couple pennies. Draw yourself two lists, like
4 7 9 23 -2 14 5
6 5 1 0
place the pennies at the head of each list (one above the four and one above the six). The pennies represent your current index into each list.

Now, looking only at the numbers at the pennies, copy those numbers over into a new list, properly sorted.

Method 2:
Check out the merge-sort algorithm.

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 19
Reputation: Olsi009 is an unknown quantity at this point 
Solved Threads: 0
Olsi009 Olsi009 is offline Offline
Newbie Poster

Re: Merge and sort 2 linked lists

 
0
  #3
Jan 26th, 2008
Look dude,
I have already made that exercise. I just wanted to see how the rest of the world solves it.
Maybe there's a shorter way to do it rather than mine.

thank you anyway for your....help
Learning is the process whereby knowledge is created, through the tranformation of experience...
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Merge and sort 2 linked lists

 
0
  #4
Jan 26th, 2008
Snot and bile, eh? Well, you just earned yourself a spot on my ignore list.

Before I go, though, a little advice: as you've missed several points.
  1. Your professor doesn't care what other people can do. Chances are likely that someone, somewhere, will produce better code than either you or I could do.
  2. Tough beans if you don't like the answer. Don't pander for code. If you are confused, or genuinely need help, read the forum rules and try again. If you just want to compare algorithms, state it explicitly and post yours first.
  3. Don't ask for help, then act rudely toward those spending their valuable time (which is usually much more valuable than yours) trying to help you. Doing so gets you on the negative side of nothing.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 19
Reputation: Olsi009 is an unknown quantity at this point 
Solved Threads: 0
Olsi009 Olsi009 is offline Offline
Newbie Poster

Re: Merge and sort 2 linked lists

 
0
  #5
Jan 27th, 2008
Douas,
I am really sorry for the reply I gave you but last night when I got back from the exam I was pretty angry with myself (for my results) and I was like shouting to anyone on my way.

Thank you very much for all the help you gave me before,

Have a nice week
Learning is the process whereby knowledge is created, through the tranformation of experience...
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Merge and sort 2 linked lists

 
0
  #6
Jan 27th, 2008
Hmm, OK, you're forgiven. Sounds like you have a lot of tests at once?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 5
Reputation: anjaly grace has a little shameless behaviour in the past 
Solved Threads: 0
anjaly grace anjaly grace is offline Offline
Newbie Poster

Re: Merge and sort 2 linked lists

 
0
  #7
Jan 19th, 2009
merge sort in link list. Works in g++/c++ by deleting // 's




#include<iostream>
using namespace std;
//#include<conio.h>
#include<stdio.h>
#include<malloc.h>


struct node
{
int number;
struct node *next;
};

struct node *adnode(int number, struct node *next);//Adding new node
struct node *mergesort(struct node *head); //spliting link list using 2 pointers
struct node *merge(struct node *head_one, struct node *head_two); //merge list1 and list2

int main()
{
struct node *head;
struct node *current;
struct node *next;
int j=0,k,k3;
// clrscr();
head=NULL;

while(j<4)
{
cout<<"Choice \n 1.Enter new element \n 2.Mergesort \n 3.print list \n ";
cin>>j;
switch(j)
{
case 1:
cout<<"Enter number ";
cin>>k3;
head = adnode(k3, head);
break;
case 2:
head = mergesort(head);
break;
case 3:
for(current = head; current != NULL; current = current->next)
cout<< current->number<<",";
cout<<"\n\n";
break;
default:
break;
} //Quit if value greater than 4
}
for(current = head; current != NULL; current = next) //before ending, free list.
next = current->next, free(current);
// getch();
return(0);
}



struct node *adnode(int number, struct node *next)
{
struct node *tnode;
tnode = (struct node*)malloc(sizeof(*tnode));
if(tnode != NULL)
{
tnode->number = number;
tnode->next = next;
}
return tnode;
}


struct node *mergesort(struct node *head)
{
//head works as temporary node/pointer;
struct node *head_one;
struct node *head_two;

if((head == NULL) || (head->next == NULL))
return head;

head_one = head;
head_two = head->next; //Like exam, traversing an array using 2 pointers(interview question)
while((head_two != NULL) && (head_two->next != NULL))
{
head = head->next;
head_two = head->next->next;
}
head_two = head->next;
head->next = NULL; //break list
struct node *temp=merge(mergesort(head_one), mergesort(head_two));
return(temp);
}

struct node *merge(struct node *head_one, struct node *head_two)
{
if(head_one== NULL)
return(head_two);
if(head_two == NULL)
return(head_one);
if(head_two->number > head_one->number)
{
head_one->next= merge(head_one->next,head_two);
return(head_one);
}
else
{
head_two->next =merge(head_two->next,head_one);
return(head_two);
}
}
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Merge and sort 2 linked lists

 
0
  #8
Jan 19th, 2009
Unfortunately this is supposed to be a delphi/pascal forum, not a c/c++ forum.

If you have already made your solution (in pascal I assume) what areas do you feel your solution is weak..
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC