Solved

Self Organizing LinkedList

Posted on 1998-08-21
8
366 Views
Last Modified: 2010-04-01
I am working on a self organizing insert function for a linked list.  This is what I have so far can you please help me?

void LinkedList::Insert(char firstname[], char lastname[])
{
  Node * l = new Node(firstname, lastname);
  Node * p = first;
  Node * z;
  Node * x;

  assert(l !=0 );

  if(first == NULL)
    first = l;


   while(p != NULL && first -> next != NULL)
   {
      if(strcmp(p -> next -> r.First_Name,
         p -> r.First_Name) < 0)
         {
            l = p -> next;
            p -> next = l;

            p = p -> next;
         }

      else
      {
         z = p;
         z -> next = p -> next;
         x = p -> next;
         x -> next = p -> next -> next;
         p -> next = x -> next;
         x -> next = p;
         z = x = NULL;
      }
   }
}
0
Comment
Question by:hawhite
  • 6
8 Comments
 
LVL 8

Accepted Solution

by:
Answers2000 earned 50 total points
ID: 1170929
Code in the comment below
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1170930
#include <stdio.h>
#include <string.h>

class member ;

class member
{
public:
      char first[256] ;
      char last[256] ;
      member * next ;
} ;

class mylist
{
private:
      member * head ;
public:
      mylist()
      {
            head = NULL ;
      }

      void Insert( const char * first, const char * last )
      {
            // allocate new member
            member * newmember = new member ;
            strcpy( newmember->first, first ) ;
            strcpy( newmember->last, last ) ;

            // find position
            member * prev = NULL ;
            member * current = head ;

            while ( current != NULL )
            {
                  // sort by first name as in your sample
                  // you may want a more sophisticated comparison to sort by first + last name
                  if ( strcmp( first, current->first ) < 0 )
                  {
                        if ( prev != NULL )
                        {
                              prev->next = newmember ;
                        } else
                        {
                              // may first item in list
                              head = newmember ;
                        }
                        newmember->next = current ;
                        return ;
                  }
                  prev = current ;
                  current = current->next ;
            }

            // insert at the end if last in list
            if ( prev != NULL )
            {
                  prev->next = newmember ;
            }
            newmember->next = NULL ;

            // this may be the only (first) item in the list,
            if ( head == NULL )
            {
                  head = newmember ;
            }
      }


      void DeleteAll()
      {
            member * temp = head ;

            while ( temp != NULL )
            {
                  member * temp2 = temp->next ;
                  delete temp ;
                  temp = temp2 ;
            }
      }

      void ListAll()
      {
            member * temp = head ;

            while ( temp != NULL )
            {
                  printf( "%s %s\n", temp->first, temp->last ) ;
                  temp = temp->next ;
            }
      }

      virtual ~mylist()
      {
            DeleteAll() ;
      }

} ;


int main( int * argc, char * argv[] )
{
      mylist alist ;
      alist.Insert( "John", "Smith" ) ;
      alist.Insert( "John", "Doe" ) ;
      alist.Insert( "Fred", "Flintstone" ) ;
      alist.Insert( "Zack", "Taylor" ) ;
      alist.ListAll() ;

      return 0 ;
}
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1170931
You can nest the member class into mylist if you only want one class.

I used strcpy, char[] and printf, you may wish to change to STL strings and cout - but that's up to you
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1170932
Sorry about the indents, it lost em when I copied from VC.

My biggest tips is
1. Use descriptive variable names
2. Declare variables near where you use them where-ever possible, especially temporaries.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 22

Expert Comment

by:nietod
ID: 1170933
Answers2000, this is probably a schoolwork assignment.  You might want to consider providing HELP not an exact answer on this type of question.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1170934
Answers2000, EE swallows tabs.  Use spaces for indents.
BTW, I agree with nietod's comment.
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1170935
MMm u could be right

had a look at the guy's Q history and there's a couple of things in there that make me a bit suspicious, and also a couple of things that don't.

I'll watch what he posts next time.

BTW I posted code (not my preference) rather than a correction because I couldn't follow his variable names :-)
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1170936
One other thought, if it is maybe the prof might ask him why

virtual ~mylist()

rather than

~mylist()

which could be interesting...
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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.

705 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

19 Experts available now in Live!

Get 1:1 Help Now