Solved

Ordered Linked List ?

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

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 …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 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.

785 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