Solved

Ordered Linked List ?

Posted on 2002-03-10
4
208 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now