Link to home
Start Free TrialLog in
Avatar of lewis_loo
lewis_loo

asked on

Pointer

Below is what I have done to my source code to sort the number but it seem to have an logic error, because there is nothing to be out on my screen? I compile with Visual C++

#include <iostream.h>

const YES=1;
const NO=0;

struct link
{
      int data;
      link* next;
};

class linklist
{
private:
      link* first;
public:
      linklist()
      {first=NULL;};
      void additem(int d);
      void display();
};


void linklist::additem(int d)
{
      int sort_done;
      link* newlink=new link;
      link* back=new link;
      link* front=new link;
      
      newlink->data=d;
      sort_done=NO;
      if (first==NULL)
      {
      newlink->next=first;
      first=newlink;
      }
      else
      {
      front=first;
      back=first;
      do
      {
      if (front==NULL)
      {
      sort_done=YES;
      back->next=newlink;
      newlink->next=front;
      }
      else
      {
      if ((front->data)<(newlink->data))
      {
      back=front;
      front=back->next;
      sort_done=NO;
      }
      else
      {
      if (front==first) back=front;
      back->next=newlink;
      newlink->next=front;
      sort_done=YES;
      }
}
}while (sort_done==YES);
}
}

void linklist::display()
{
link* current=first;
while (current!=NULL)
{
      cout << endl << current->data;
      current=current->next;
}
}

void main()
{
      linklist L1;

      L1.additem(12);
      L1.additem(10);
      L1.additem(42);
      L1.additem(50);
      L1.additem(48);
      L1.additem(43);
      L1.additem(52);

      L1.display();
}

this is all my code.
the back and front is to handle the previous and the next pointer so they can't be gone.

if you need some information about my code please ask

thanks

ASKER CERTIFIED SOLUTION
Avatar of vessel
vessel

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lewis_loo
lewis_loo

ASKER

Is there any other way that is efficient than this one?

First, let's add a member function to class LIST to find the element before the position you want to insert.

NODE* LIST::find_previous(int x)
     {
     // finds the element previous to x
     // returns NULL if x is not found

     NODE* ptr = head;

     while (ptr->next != NULL && ptr->next->data != x)
          {
          ptr = ptr->next;
          }

     return ptr;
     }

Then, this makes the insert operation quite simple, with a couple other simplifications:

void LIST::insert(int x)
     {
     NODE* previous = find_previous(x);

     if (previous == NULL)
          {
          // if list is empty or inserting at front of list
          new_node->next = head;
          head = new_node;
          }
     else
          {
          // inserting at middle or end
          // order of these statements are important
          new_node->next = previous->next;
          previous->next = new_node;
          }
     }

Take a look at the insert code that I wrote.. and see if you can figure out the following:

how do we know when a list is empty?

what happens if you call find_previous(x) on an empty list?

what happens if you insert an item that is already in the list?

if you have three items in the list with the same value, what will find_previous point to?

why don't you go ahead and make the NODE class a class instead of a struct?

can you verify (trace an example) that it can insert at the end of the list properly.

Regardless, you should not trust my answer b/c I have not tested it, so test it for yourself and see if it works the way you want it to.