Ordered Linked List ?

Posted on 2002-03-10
Medium Priority
Last Modified: 2010-04-01
Here is my structure :

struct Node{
  char arryLast[26];
  char arryFirst[26];
  Node *next;

here is my code :

void insert(char arryTmpLast[], char arryTmpFirst[]){
  Node *temp, *prev, *newData = new Node;

  strcpy(newData->arryLast, arryTmpLast);
  strcpy(newData->arryFirst, arryTmpFirst);

  if (head == NULL)
    head = newData;
    temp = head;
    while((temp != NULL) && ((strcmp(temp->arryLast, arryTmpLast) <= 0) && (strcmp(temp->arryFirst, arryTmpFirst) < 0))){
       prev = temp;
       temp = temp->next;
     if (temp == head){
         newData->next = head;
         head = newData;
    else if (temp == NULL){
       prev->next = newData;
         prev->next = newData;
         newData->next = temp;

I would like to order by arryLast, then arryFirst.
But when I ran that function, I did not have an order list. How to solve it ???
Question by:tcy08
  • 2
LVL 30

Expert Comment

ID: 6854778
Why are you not using the built in types?
Like std::list, std::map, std::set.
LVL 30

Expert Comment

ID: 6854782
>>How to solve it ???

If this is a C++ program use the built in types.

Author Comment

ID: 6855458
I don't want to use built in type. I am learning linked list now.

Accepted Solution

Chase707 earned 600 total points
ID: 6855917
The problem with your function is in the while loop.

Take this example data set:

insert("d", "e");
insert("u", "a");

Then our order ends up:

u, a
d, e

but this is obviously incorrect; why?  Examine your while statement (forgive the psuedocode):
while ((oldLast <= newLast) && (oldFirst < newFirst))

As you can see from this sample data set "d" is less than "u", BUT "e" is NOT less "a", so the linked list does not advance to the next member.

Code similar to this should work:

while (oldLast <= newLast)
    if (oldLast == newLast) {
        while (oldFirst < newFirst) {
            //increment linked list
        // increment list

Good luck


Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

600 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question