| | |
Inserting in a sorted linked list(sorted alphabetically)
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2005
Posts: 30
Reputation:
Solved Threads: 0
[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;
}
}
***********************************************
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;
}
}
***********************************************
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:
Now sort your array of Nodes based on the first letter of each name:
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:
That's it. You now have a head_ptr that points to beginning of a sorted linked list.
(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)
Node **array = new Node*[size]; Node *temp = head_ptr; while(temp) { Node[i] = temp; temp = temp->next; }
Now sort your array of Nodes based on the first letter of each name:
C++ Syntax (Toggle Plain Text)
#include<algorithm> //This function sorts to ascending order sort(Node[0]->name[0], Node[size]->name[0]);
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)
head_ptr = Nodes[0]; for(int i=0; i<size-1; i++) Nodes[i]->next = Nodes[i+1];
That's it. You now have a head_ptr that points to beginning of a sorted linked list.
•
•
Join Date: Dec 2005
Posts: 43
Reputation:
Solved Threads: 0
This assumes that, if the list already exist, it is in 'name' order
C++ Syntax (Toggle Plain Text)
... temp->next = NULL; // add this to temp just after cin curr = head; if (curr == NULL) // empty list { head = temp; } else { while (curr != NULL) { if (curr->next == NULL) // end of list { curr->next = temp; break; } else if (temp->name < curr->name) // insert temp { temp->next = curr->next; curr->next = temp; break; } curr = curr->next; } }
![]() |
Similar Threads
- Removing an item from head of linked list (C)
- Quicksorting linked list - simple algorithm (C)
- Sorted linked list help (C++)
- sort linked list help please (C)
- help with linked lists (C++)
- Why Data Structures???...QUESTIONS INSIDE (C++)
Other Threads in the C++ Forum
- Previous Thread: Parse integers from string?
- Next Thread: Form won't close
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





