?
Solved

Ordered Linked List ?

Posted on 2002-03-10
4
Medium Priority
?
253 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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
        }
    }
    else
        // increment list
...

Good luck

Chase
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

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…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

765 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