Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Pointer

Posted on 2000-03-25
4
Medium Priority
?
266 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 150 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

879 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