🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: csu.cs.200
Article #391

Re: Assignment 4 HELP needed

#391
From: Corby S. Hudnall
Date: Tue, 24 Feb 1998 00:00
167 lines
5730 bytes
Before I begin on my discussion, I'd like to remind everyone that I have
office hours in the 3rd floor North CS lab on MW from 9 - 12 and R from
11 - 1 so if you are in need of help, don't hesitate to come and see
me.  With that said, let me see what I can do to help you out.


[snipped for brevity]

> So far I have figured out that we have to have the class "LIST" at the top
> of the .h file.  Do I have to have a second (struct/list?) inside with a
> pointer to next-correct? 

You will likely have a struct defined within class LIST that contains
the appropriate data.  There is no semantic sense in the class LIST
itself having a pointer, since the whole point of the class is to
encapsulate your SLL.

I then copy LISTdata data to LIST?
> 
> class LIST
> {
> 
> public:      // everything is public for now until it all  works.
>                  int  job_ID;
>                  int  job_value;
>                  char file[MAX_FILE_NAME];
>                  LIST*next;

Again, the instance of class LIST does not contain a single node of data
(as you've modeled above) but in fact contains all the nodes in such a
way that from outside of class LIST, the SLL isn't really visible (it's
hidden from the LIST users' view).  So I think a more appropriate class
definition would go something like this:

class LIST
{
public:
	struct data 
	{
		int jobID;
		int jobValue;
		char file[MAX];
		data* next;
	}

private:
	data* headptr;  // <-- head ptr for our internal SLL
	data* currptr;  // <-- to be used by the traverse func and such

public:
// member function definitions go here ...
};

You get the idea...


> void LIST::LIST_AddAtEnd(LISTdata &data);
> LISTdata LIST::LIST_Get(int i);
> };
> LIST qlist;
> 
> A.) First we call qlist.LIST_AddAtEnd(data); How do I declare this so I can
> pass back out my data that I copy into LIST(the way I have it declared now
> I get garbage)?   I am able to read in jobid and value ok. but have a
> problem reading in file.
> 
> void LIST::LIST_AddAtEnd(LISTdata &data)
> {
>     ifstream in;
>     newptr=new LIST ;
>     newptr->job_ID=data.job_ID;
>     newptr->job_value=data.job_value;

A couple of quick notes about the above code segment.  You do not want
to create a NEW ifstream within this function.  You also do NOT want to
create a new LIST in this function.  See next paragraph for more detail.

I'm not quite sure what you're asking here.  AddAtEnd will traverse to
the end of the internal list (pointed by be headptr in my sample class
defn above) and add a new 'data' node there with identical information
obtained from the parameter data.  If this isn't what you asked, come
and see me during lab hours.

> 
> Here is my "file copy"- I get a "cannot convert int of char*" error-
> data.file is declared as a char in LIST - what is my problem?  I have
> messed with this for hours and still cant get this one line- any
> suggestions?
> 
>           strcpy(filename[MAX_FILE_NAME],data.file[MAX_FILE_NAME]);
> 

strcpy does not take an array, it takes a pointer to memory.  Try this
instead:

strcpy( filename, data.filename );

But to clarify even further, you'll have created a new temp 'data' node
in your AddAtEnd function and that will be the destination of the strcpy
(i. e. strcpy( temp->filename, data.filename )).

>     newptr->next=NULL;
>     //prevptr->next=newptr;
>      prevptr=newptr;
> }
> 
> B.) How do I do the LIST_GET(int); routine to traverse any given list- all
> we are given in Main4.C is a parameter of int for this function, dont we
> also need to pass in a call to a list name and headptr, or somesuch?  This
> is my biggest problem with this assignment.
> 
> LISTdata LIST::LIST_Get(int i)
> {
>         int j;
>         LIST *newptr,*headptr;
>         newptr=headptr->next;
>         LISTdata crap;
>         //headptr=NULL;  // compiler dont like this line either
> 

Again, you'll be modifying the internal headptr and currptr (and any
other pointers you deem necessary) in your member functions.

> ??//TRAVERSE//??
> 
> crap.job_ID=job_ID;
> crap.job_value=job_value;
> //crap.file[MAX_FILE_NAME]=file[MAX_FILE_NAME];
> return crap;
> }
> 

I think you're a bit confused about the purpose of using a class here. 
Every instance of a LIST object that I create will in fact be a separate
SLL.  So when I say LIST.get( 1 ), I mean to do that to the list
contained within that LIST instance.

LIST one;
LIST two;

LIST::data data1;
LIST::data data2;

// initialize data1 and data2
...

one.addToEnd( data1 );
one.addToEnd( data2 );

two.addToEnd( data2 );
two.addToEnd( data1 );

I've just created two separate lists that I can add and remove stuff
from.  The lists contain the same data but in a different order.  This
is a trivial example and does not really demonstrate the power you
achieve by using classes.  Get the basic idea?

> Any and ALL help would be greatly appreciated. THANKS.
> 

Hope that offers a little guidance...

cheers
-- 
Corby S. Hudnall                 Senior, Spring 1998
CS200 TA
Technical Details
Message-ID: <34F39422.42D3DFD9@cs.colostate.edu>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <01bd415f$a1b600c0$LocalHost@default>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 127144,1c68dbc9631ca0f6
X-Google-Attributes: gid127144,public
From: "Corby S. Hudnall" <hudnall@cs.colostate.edu>
Subject: Re: Assignment 4 HELP needed
Date: 1998/02/24
Message-ID: <34F39422.42D3DFD9@cs.colostate.edu>
X-Deja-AN: 328391837
Content-Transfer-Encoding: 7bit
References: <01bd415f$a1b600c0$LocalHost@default>
Mime-Version: 1.0
To: mark casteel <casteelm@info2000.net>
Content-Type: text/plain; charset=us-ascii
Organization: Colorado State University
Newsgroups: csu.cs.200