www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/05/16/16:46:38

From: "Richard Lohuis" <richhlo AT snowcrest DOT net>
Newsgroups: comp.os.msdos.djgpp
Subject: pointer newbie: 1st prog works, 2nd pros is modification: Error: traceback
Date: Sun, 16 May 1999 13:16:42 -0700
Organization: SnowCrest.Net
Lines: 120
Message-ID: <7hn92n$9j5$1@mail.snowcrest.net>
NNTP-Posting-Host: stkfra100.snowcrest.net
X-Newsreader: Microsoft Outlook Express 4.72.3110.5
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

I'm trying to learning pointers, AGAIN!  The first program is from the book
(LIFO) the second is my modification to add items at end(FIFO).

1st program:
// linklist.cpp
// linked list
#include <iostream.h>

struct Link             // one element of list
       {short data;     // data item
       Link* next;      // pointer to next link
       };

class LinkList          // a list of links
      {private:
           Link* first; // pointer to first link
      public:
           LinkList()   //0-arg constructor
           {     first=NULL;} // no first link
           void additem( short d ); // add data item (one link)
           void display();    // display all links
      };
      void LinkList::additem( short d ) // add data item
      {    Link* newlink=new Link;      // make a new link
           newlink->data=d;             // give it data
           newlink->next=first;         // it points to next link
           first=newlink;               // now first points to this
      }
      void LinkList::display()          // display all links
      {    Link* current=first;         // set pointer to first link
           while( current!=NULL )       // quit on last link
           {     cout << endl << current->data;// print data
                 current=current->next;        // move to next link
           }
      }
void main()
{    LinkList Li;       //make linked list

     Li.additem(25);    // add four items to list
     Li.additem(36);
     Li.additem(49);
     Li.additem(64);

     Li.display();      //display entire list
}



2nd program:
// llistfwd.cpp
/*
Problem: Revise the additem() member function from linklist.cpp program so
that it add the item at the end of the list, rather than at the
beginning.  This will cause the first item inserted to be the first item to
be displayed, so the output of the program will be;
         25
         36
         49
         64
To add the item you'll need to follow the chain of pointers to the end of
the list, then change the last link to point to the new link.
*/
#include <iostream.h>

struct Link             // one element of list
       {short data;     // data item
       Link* next;      // pointer to next link
       };

class LinkList          // a list of links
      {private:
           Link* first; // pointer to first link
           Link* current; //pointer to current link
      public:
           LinkList()   //0-arg constructor
           {     Link* first=new Link; // prepare first link for data
                 Link* current=first; // current is first
                 current->next=NULL; // pointer to next link is null
           }
           void additem( short d ); // add data item (one link)
           void display();    // display all links
      };
      void LinkList::additem( short d ) // add data item
      {    Link* nextlink=new Link;      // make a new link
           current->data=d;             // give current the data
           current->next=nextlink;         // current points to next link
           nextlink->next=NULL;            // nextlink has no NEXT to go to
           current=nextlink;               // now current is the next link
      }
      void LinkList::display()          // display all links
      {    Link* current=first;         // set pointer to first link
           while( current!=NULL )       // quit on last link
           {     cout << endl << current->data;// print data
                 current=current->next;        // move to next link
           }
      }
void main()
{    LinkList Li;       //make linked list

     Li.additem(25);    // add four items to list
     Li.additem(36);
     Li.additem(49);
     Li.additem(64);

     Li.display();      //display entire list
}

The error: I've debugged all I can & now when I run I get:
    Call frame traceback:
    llistfdw.cpp(59) in function LinkList::display(void)
    llistfwd.cpp(71) in function main
    in function __crt1_startup+174

I'm using DJGPP & RHIde, & now I'm lost, thus: "HELP PLEASE!!"

Thank you in advance,
richhlo AT snowcrest DOT net



- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019