[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Ordered Linked List ?

Posted on 2002-03-10
4
Medium Priority
?
260 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 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

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. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

831 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