944,098 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 20048
  • C++ RSS
Mar 11th, 2006
0

Inserting in a sorted linked list(sorted alphabetically)

Expand Post »
[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;

}


}

***********************************************
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
crestaldin is offline Offline
30 posts
since Mar 2005
Mar 12th, 2006
0

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

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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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:

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 237
Solved Threads: 117
Practically a Posting Shark
Clinton Portis is offline Offline
822 posts
since Oct 2005
Mar 12th, 2006
0

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

This assumes that, if the list already exist, it is in 'name' order
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 11
Solved Threads: 0
Light Poster
Nedals is offline Offline
43 posts
since Dec 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Parse integers from string?
Next Thread in C++ Forum Timeline: Form won't close





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC