Inserting in a sorted linked list(sorted alphabetically)

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2005
Posts: 30
Reputation: crestaldin is an unknown quantity at this point 
Solved Threads: 0
crestaldin crestaldin is offline Offline
Light Poster

Inserting in a sorted linked list(sorted alphabetically)

 
0
  #1
Mar 11th, 2006
[COLOR=Navy]Pls I need help on how to insert elements into a sorted linked list.I actually have a list that contains names and scores for students and I want the elements to be inserted into the right postion such that the names of individual students are arranged alphabetically.

This is my code but something seems to be wrong.

***********************************************

#include "iostream"
using namespace std;

struct Node
{
char *name;
int score;
Node *next;
};
Node *head=NULL;

void InsertItem(Node * &head);


void main()
{
InsertItem(head);
}



void InsertItem(Node * &head)
{
Node *temp, *previous,*current; /*previous stores the node before the insertion point*/

temp=new Node();

cout<<"Enter name";
cin>>temp->name;
cout<<"Enter score";
cin>>temp->score;


if (head == NULL) /* first node in the list */
{

head = temp;
}
else if (strcmp(head->name,temp->name)>=0)/* insert at the head */
{

temp->next = head;
head = temp;
}
else
{
previous = head; /* trailing pointer */
current = head->next;

while ((current != NULL) && strcmp(current->name,temp->name)<0)
{
previous = current;
current = current->next;
}
temp->next=previous->next;
previous->next=temp;

}


}

***********************************************
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 265
Reputation: Clinton Portis is an unknown quantity at this point 
Solved Threads: 25
Clinton Portis's Avatar
Clinton Portis Clinton Portis is online now Online
Posting Whiz in Training

Re: Inserting in a sorted linked list(sorted alphabetically)

 
0
  #2
Mar 12th, 2006
Here is one method you could use to sort your linked list alphabetically:
(I haven't tested this specific code but the idea is sound)

First, push each node into an array of nodes:
  1. Node **array = new Node*[size];
  2. Node *temp = head_ptr;
  3.  
  4. while(temp)
  5. {
  6. Node[i] = temp;
  7. temp = temp->next;
  8. }

Now sort your array of Nodes based on the first letter of each name:
  1. #include<algorithm>
  2.  
  3. //This function sorts to ascending order
  4. sort(Node[0]->name[0], Node[size]->name[0]);
Now.. all you have to do is include a provision on what to do if the first 2 letters of a person's name are the same.


Once you are done with all your sorting operations, turn the array of nodes back into a linked list. Make each node of the newly sorted list point to the next node in line by resetting all the *next pointers:

  1. head_ptr = Nodes[0];
  2.  
  3. for(int i=0; i<size-1; i++)
  4.  
  5. Nodes[i]->next = Nodes[i+1];

That's it. You now have a head_ptr that points to beginning of a sorted linked list.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 43
Reputation: Nedals is an unknown quantity at this point 
Solved Threads: 0
Nedals Nedals is offline Offline
Light Poster

Re: Inserting in a sorted linked list(sorted alphabetically)

 
0
  #3
Mar 12th, 2006
This assumes that, if the list already exist, it is in 'name' order
  1. ...
  2. temp->next = NULL; // add this to temp just after cin
  3.  
  4. curr = head;
  5. if (curr == NULL) // empty list
  6. {
  7. head = temp;
  8. }
  9. else
  10. {
  11. while (curr != NULL)
  12. {
  13. if (curr->next == NULL) // end of list
  14. {
  15. curr->next = temp;
  16. break;
  17. }
  18. else if (temp->name < curr->name) // insert temp
  19. {
  20. temp->next = curr->next;
  21. curr->next = temp;
  22. break;
  23. }
  24. curr = curr->next;
  25. }
  26. }
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