User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 402,000 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,463 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Views: 4189 | Replies: 3 | Solved
Reply
Join Date: Jun 2004
Posts: 2
Reputation: onickel is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
onickel onickel is offline Offline
Newbie Poster

Help help by sorting a simply linked list

  #1  
Jun 8th, 2004
Hi!
Pls, somebody help me!! I'm stuck here since days. I don't know, what i'm doing wrong.
Attached Files
File Type: cpp bank.cpp (4.2 KB, 23 views)
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Mar 2004
Posts: 1,514
Reputation: kc0arf is a jewel in the rough kc0arf is a jewel in the rough kc0arf is a jewel in the rough 
Rep Power: 10
Solved Threads: 48
Colleague
kc0arf kc0arf is offline Offline
Posting Virtuoso

Re: help by sorting a simply linked list

  #2  
Jun 8th, 2004
Hello,

I looked at your code, and it appears to be a bubble-sort. Here is what I would do.

1) Get yourself a nice sheet of paper, and a pencil.

2) Draw out your data structure.

[code]

+-------+ +------------------+ +------------------+
| head | | data | point | | data | point |
+-------+ +------------------+ +------------------+

[\CODE]

Remember to draw little boxes for the temp pointers and stuff too.

3) Go through your code by hand, and fill in the paper as the computer would during the iterations. Do not assume when doing this by hand. Go through the motions, and stick with it. You will find your error.

4) In my coding days, I was always sure to make a duplicate head pointer, just in case I messed up the transition somewhere. Guess it was mild parinoia.

Christian
Reply With Quote  
Join Date: Jun 2004
Posts: 10
Reputation: abu_sager is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 2
abu_sager abu_sager is offline Offline
Newbie Poster

Re: help by sorting a simply linked list

  #3  
Jun 9th, 2004
Hello my brother
there is two way to sort a single linked list
First , swapping data but it is unsufficient (( suppose the data is very bug))
Second, swapping pointer is better but it's harder
here you are the two ways
Swapping the pointers
void Sortable_List::sort()
{
	Node *ptr1,*ptr2,*temp,*Imithead1,*Imithead2,*before;
	if (head != NULL) {
		ptr2 = head;
		ptr1 = head->next;
		//First Step Set the smallest value to head
		while(ptr1 != NULL ) {
			if (ptr2->data > ptr1->data ) {
				temp = ptr2;
				while(temp->next != ptr1) 
					temp = temp->next;
				temp->next = ptr1->next;
				ptr1->next = ptr2;
				head = ptr1;}
			ptr1 = ptr1->next;
			ptr2 = head;}
		
////////////////////////////////////////////
////////////////////////////////////////////
		before = head;
		Imithead1 = Imithead2 = head->next;
		while (Imithead1 != NULL) {
			
		while (Imithead2 != NULL ) {
			ptr1 = ptr2 =Imithead2;
			while(ptr1 != NULL ) {
				if (ptr2->data > ptr1->data ) {
					temp = ptr2;
					while(temp->next != ptr1) 
						temp = temp->next;
					before->next = ptr1;
					temp->next = ptr1->next;
					ptr1->next = ptr2;
					Imithead2 = ptr1;
			
				}
				ptr1 = ptr1->next;
				ptr2 = Imithead2;
			}
			before = Imithead2;
			Imithead2 = Imithead2->next;
			
		}
		
		Imithead1 = Imithead1->next;
		Imithead2 = Imithead1;
		}

		
	}
	
	
}

Swapping Data
	NodeEmp *Ptr1,*Ptr2;
	Ptr1 = Ptr2 = Emp;
	if (Emp != NULL && Emp->next != NULL){
		while (Ptr1 != NULL)
		{		
			Ptr2= Ptr1;
		             while(Ptr2!= NULL){
			swap(Ptr1,Ptr2);				
			Ptr2 = Ptr2->next;				
		}
		Ptr1 = Ptr1->next;
		}
	}
Reply With Quote  
Join Date: Jun 2004
Posts: 2
Reputation: onickel is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
onickel onickel is offline Offline
Newbie Poster

Solution Re: help by sorting a simply linked list

  #4  
Jun 11th, 2004
Originally Posted by onickel
Hi!
Pls, somebody help me!! I'm stuck here since days. I don't know, what i'm doing wrong.

Thanks to all people that replied.
I finally found the solution. I'm posting the method for sorting below:
void List::listsort(){
  element *p, *n, *flag, *anterior;
  bool sortiert;
  do {
    p = head;
    n = head -> next;
    for (int i = 0; i< anzahl; i++) {
      if (p -> nummer < n -> nummer) {
        p = n;
        n = n-> next;
        sortiert = 1;
      } else {
        //jetzt wird geordnet
        sortiert = 0;
        flag = n;
        p -> next = flag -> next; //p (tail) mußt zu 0 zeigen. Bevor: p -> n -> 0
        n -> next = p; //n -> p statt n -> 0

        anterior = head;
        for (int j=1; j<i; j++) { //damit j=1 wird in eine vor p gehalten
          anterior = anterior -> next; //anterior wird bewegt genau vor p (anterior -> p -> n -> 0)
        }
        anterior -> next = n; //damit wird anterior Nachfolgers n

        if (p -> next == 0) {  //d.h. tail!!!!
          tail = p;
          n -> next = tail;
          number = (tail -> nummer) + 1; //der nächste nummer von tail
        }       else {
        n = n -> next -> next;
        }
        //        p = head;
        //      n = head -> next;
      }
    }
  } while (!sortiert);
}
P..S. i live in germany and this programm is for the university of münchen. Therefore, the comments are in german. And also, i come from ecuador, so my mother language is spanish. So, anterior means previous.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 9:00 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC