Solved

Ordered Linked List ?

Posted on 2002-03-10
4
239 Views
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;
  else{
    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;
     }
     else{
         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 ???
0
Comment
Question by:tcy08
  • 2
4 Comments
 
LVL 30

Expert Comment

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

Expert Comment

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

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

Author Comment

by:tcy08
ID: 6855458
I don't want to use built in type. I am learning linked list now.
0
 
LVL 4

Accepted Solution

by:
Chase707 earned 150 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
        }
    }
    else
        // increment list
...

Good luck

Chase
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

749 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