Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,426 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,926 people online right now. Registration is fast and FREE... Join Now!




Linked List Loop - Private Next

 
Reply to this topicStart new topic

Linked List Loop - Private Next

INHCNN
26 Jan, 2008 - 03:43 PM
Post #1

New D.I.C Head
*

Joined: 26 Jan, 2008
Posts: 8


My Contributions
Yes - This is homework related. I'm not looking for a direct answer, just a pointer (ha ha - pun intended).

This pertains to linked lists.

We have a class such as:

CODE
  
class item
{ public:
    item();
    item(string,int);
    ~item();
    void insert(item *p);
    void list();
    void forwards();
    void backwards();
    void put(ostream &out);
  private:
    string name;
    int count;
    item *next;
};


Assume at this point that this list populates correctly.

So this seemingly simple problem goes:

QUOTE

Create a loop at the end of the main() function, before the items are deleted, that will traverse the linked list and display the contents of each item.


So, INHCNN says:

CODE

p = head;
while(p != NULL)
{
  p->put(cout);
  p = p->next;
}


...but of course, next is private, so I can't roll like that. I considered making a public getNext function, but that seems like it defeats the purpose, or whatever goal, is being sought after here.

Help? Just a little?

Here's the driver... If you need Item.h or Item.cpp, holler.
CODE

/**********************************************
* Lab 01
* Your name goes here
**********************************************/

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

#include "Item.h"

/**********************************************
* main
**********************************************/
void main()
{ int count;
  string name;
  fstream infile;
  item *head,*p;

  head = NULL;
// Open file

  cout << "Enter file name: ";
  cin >> name;
  infile.open(name.data(),ios::in);

// Loop through file

  while(!infile.eof())
    { infile >> name >> count;

// Process valid input

      if(infile.good())
        {
p = new item(name,count);
if(!head)
head = p;
else
head->insert(p);
        };
    };

// Close file

  infile.close();

// Loop through contents

  p = head;
  while(p != NULL)
  {
  p->put(cout);
  p = p->next;
  }

  cout << "Finished!" << endl;
  if(head) delete head;
  system("pause");
}


And as an aside (hopefully to prove that I'm not completely lost here), the other part of this was to create the forward and backward member functions, which I have wrote (in the implementation file):


CODE

void item::forwards()
{
this->put(cout);
if(this->next != NULL)
this->next->forwards();
}

void item::backwards()
{
if(this->next != NULL)
this->next->backwards();
this->put(cout);
}


User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Linked List Loop - Private Next
26 Jan, 2008 - 04:37 PM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,009



Thanked: 5 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
I suggest you go with getNext()

This post has been edited by PennyBoki: 26 Jan, 2008 - 04:38 PM
User is offlineProfile CardPM
+Quote Post

INHCNN
RE: Linked List Loop - Private Next
26 Jan, 2008 - 04:44 PM
Post #3

New D.I.C Head
*

Joined: 26 Jan, 2008
Posts: 8


My Contributions
I thought about that, but it seems like that would go around whatever point this is supposed to make. By reading the question (and the rest of the assignment) I don't think he wants us to add to the implementation file to accomplish this.

Is there any other method?
User is offlineProfile CardPM
+Quote Post

GWatt
RE: Linked List Loop - Private Next
26 Jan, 2008 - 09:08 PM
Post #4

human inside
Group Icon

Joined: 1 Dec, 2005
Posts: 2,202



Thanked: 20 times
Dream Kudos: 450
My Contributions
Well, if you want outside code to somehow be able to reference the next item in the linked list, you must somehow give them access. If you're truly paranoid, you can use your .next(), or .getNext() method to return a copy of the real deal.
CODE

item* next()
{
    this.name = next.name;
    this.count = next.count;
    this.next = net.next;
    item* copy = new item(name, count);
    copy->next = NULL;
    /*
     * Not entirely sure if you want a null next, but returning
     * a pointer to an item in your list seems to defeat
     * the purpose of hiding the element you want to get
    */
    return copy;
}


This post has been edited by GWatt: 26 Jan, 2008 - 09:12 PM
User is online!Profile CardPM
+Quote Post

Bench
RE: Linked List Loop - Private Next
27 Jan, 2008 - 05:59 AM
Post #5

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 630



Thanked: 16 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
I would clarify this assignment question with your tutor if I were you. The task seems somewhat pointless, being that you already have a member function whose job it is to output the list.

Perhaps your tutor wishes for you to use a different program structure rather than your class?

Using your class, the only solution worth considering (From a C++ programmer's perspective) is to modify the class with some kind of getNext method as has been prescribed above. I personally don't think 'get' functions defeat the purpose, since you'll only be providing "read" access; this is certainly preferable over the alternative of allowing direct public access, where any part of the program would be free to modify that data

- No doubt, there will exist some horrific ugly "hack" which allows access to private data without modifying the class, but that would be a gross abuse of the C++ language (And that most certainly would defeat the purpose). suffice to say that this kind of thing should not be done under any circumstances.

This post has been edited by Bench: 27 Jan, 2008 - 06:13 AM
User is offlineProfile CardPM
+Quote Post

INHCNN
RE: Linked List Loop - Private Next
27 Jan, 2008 - 07:46 AM
Post #6

New D.I.C Head
*

Joined: 26 Jan, 2008
Posts: 8


My Contributions
Thanks for all the replies. I used the getNext() as suggested.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 04:43AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month