Solved

Pointer

Posted on 2000-03-25
4
262 Views
Last Modified: 2010-07-27
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

0
Comment
Question by:lewis_loo
  • 2
4 Comments
 

Accepted Solution

by:
vessel earned 50 total points
ID: 2656777
Hi lewis_loo.

I've changed 3 lines in linklist::additem(int). Now it works.  Old lines are commented out. Note that your program can be implemented more efficiently.

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;
newlink->next=NULL;
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;
if (front==first) first=newlink; else
back->next=newlink;
newlink->next=front;
sort_done=YES;
}
}
// }while (sort_done==YES);
}while (sort_done!=YES);
}
}
0
 
LVL 1

Author Comment

by:lewis_loo
ID: 2659663
Is there any other way that is efficient than this one?
0
 
LVL 2

Expert Comment

by:VEngineer
ID: 2662101

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;
          }
     }

0
 
LVL 2

Expert Comment

by:VEngineer
ID: 2662111
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.
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

832 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