Pointers and linked lists

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 2061 Views - Last Post: 22 November 2010 - 11:48 AM Rate Topic: ***** 1 Votes

#1 shaunarman  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 64
  • Joined: 21-January 10

Pointers and linked lists

Posted 18 November 2010 - 06:51 PM

Ok I am trying to code my final project that involves pointers and linked lists. My instructor has basically gone through some things in the book, then said this stuff is not in the book but this is what I want. So very kind of him to drop the ball on as and not even teach us this S**T! For the most part I have all the code wrote, but I cant seam to figure how to add the following variables into a single pointer.
        int sn;	       	    //serial number of ship
        char ShipName;      //Ship Name
	int location;       //Location of ship in canal
	float start;        //Start time
	float stop;         //Stop time
	int speed;          //Speed of ship


I am creating a linked list using pointers to create a FIFO queue. I am also implementing a ship class, but how to I enter all this data into a single pointer of my linked list?
When we went over pointers it was a brief lesson, all of about 20 min. and I got lost when he started to talk about a pointer of a pointer pointing to some pointers pointer???????????

Please Help me!!!!! :helpsmilie:

Is This A Good Question/Topic? 0
  • +

Replies To: Pointers and linked lists

#2 BetaWar  Icon User is online

  • #include "soul.h"
  • member icon

Reputation: 1503
  • View blog
  • Posts: 8,249
  • Joined: 07-September 06

Re: Pointers and linked lists

Posted 18 November 2010 - 07:02 PM

Okay, so if you are building a ship class I suggest you have that hold all the variables you want associated with the ship (such as the ones you have listed above) then have your linked list hold instances of the ship class. That way you can have all the variables in a nice orderly structure which just happens to be held in a linked list (and you don't have to worry about having a ton of lists or fudging it in some other way to get all the variables in a single pointer).

So, here is a nice little diagram:
list item
- payload = instance of ship class
-- ship class has
--- sn
--- ShipName
--- location
--- start
--- stop
--- speed
- next = pointer to the next item (ship) in the list
... and so on


Hopefully that makes sense.

Additionally, is this linked list templatized? (I would assume not but it may be).
Was This Post Helpful? 0
  • +
  • -

#3 shaunarman  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 64
  • Joined: 21-January 10

Re: Pointers and linked lists

Posted 18 November 2010 - 07:16 PM

Thats basically what I am wanting to do, but what type a declaration do I make for

Quote

payload
is it something like
 char payload(int sn,cahr shipname,int location, ...)

that is what i am getting lost trying to figure out.
Was This Post Helpful? 0
  • +
  • -

#4 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Pointers and linked lists

Posted 18 November 2010 - 08:32 PM

I think what you are looking for is a struct. Google that.
Was This Post Helpful? 0
  • +
  • -

#5 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2030
  • View blog
  • Posts: 5,427
  • Joined: 27-December 05

Re: Pointers and linked lists

Posted 18 November 2010 - 09:08 PM

You said, in post #1, "I am also implementing a ship class." Is that true? Then aren't all of those variables that you listed member variables of the ship class?

If so, then all you need to put in your linked list are ship pointers.

Specifically, if you defined:
class ship {
int sn;	       	    //serial number of ship
char ShipName;      //Ship Name
int location;       //Location of ship in canal
float start;        //Start time
float stop;         //Stop time
int speed;          //Speed of ship
// ...
};


then you declare a
ship* s_ptr;


pointing to each of your ship objects, and make a linked list of those ship* pointers.
Was This Post Helpful? 0
  • +
  • -

#6 shaunarman  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 64
  • Joined: 21-January 10

Re: Pointers and linked lists

Posted 19 November 2010 - 02:53 PM

I appreicat all the help ypu guys have given, but I found my answer.
struct ship
{
int sn;
char shipname[3];
float start;
float stop;
int location;
ship* next;
}
//---------------------------------------
class shiplist
{
private:
    ship* first;
public: 
    shiplist()
    {
        first = NULL;
    }
    void additem(int serial, char shipna[3], float star, float sto, int local);
};
//---------------------------------------
void shiplist::additem(int serial, char shipna[3], float star, float sto, int local)
{
    ship* newship = new ship;
    newship->sn = serial;
    newship->shipname = shipna;
    newship->start = star;
    newship->stop = sto;
    newship->location = local;
    newship->next = first;
    first = newship;
}
//---------------------------------------


If I am not mistaken, this should do the trick. Let me know if I am wrong.
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: Pointers and linked lists

Posted 19 November 2010 - 03:07 PM

Uh, no you haven't.
char shipname[3];
...
newship->shipname = shipna;

that's wrong, wrong, wrong. You're doing FIFO queues and linked lists without knowing you can't assign arrays like that?
Was This Post Helpful? 0
  • +
  • -

#8 shaunarman  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 64
  • Joined: 21-January 10

Re: Pointers and linked lists

Posted 19 November 2010 - 04:42 PM

So how would I do that then?
What about the rest of my solution?
Was This Post Helpful? 0
  • +
  • -

#9 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2030
  • View blog
  • Posts: 5,427
  • Joined: 27-December 05

Re: Pointers and linked lists

Posted 19 November 2010 - 05:05 PM

How do you copy a c-string? Use the strcpy() function in <cstring>.

Or, since you are coding in C++, why are you using a c-string in the first place? Why not use a C++ string. Then you can use = to assign its value.

Oh, and why are you setting newship->next to first? Are you trying to make a circular list?

This post has been edited by r.stiltskin: 19 November 2010 - 05:09 PM

Was This Post Helpful? 0
  • +
  • -

#10 shaunarman  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 64
  • Joined: 21-January 10

Re: Pointers and linked lists

Posted 19 November 2010 - 05:14 PM

How would I create a c++ string to use in-place of char shipname[3]?
And you this is a circular queue/link list.
Was This Post Helpful? 0
  • +
  • -

#11 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2030
  • View blog
  • Posts: 5,427
  • Joined: 27-December 05

Re: Pointers and linked lists

Posted 19 November 2010 - 05:19 PM

string class
Was This Post Helpful? 0
  • +
  • -

#12 shaunarman  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 64
  • Joined: 21-January 10

Re: Pointers and linked lists

Posted 19 November 2010 - 06:06 PM

Ok I think I got the string part understood, but I am getting issues with the compiler now with my new implementation of my code.
// nodelist.cpp
// nodeed list
#include <iostream>
#include <string>
using namespace std;
////////////////////////////////////////////////////////////////
struct ship                           //one element of list
{
    int serialnum;				//serial number of ship
    string ShipName;      //Ship Name
	int location;       //Location of ship in canal
	int start;        //Start time
	int stop;         //Stop time
	int speed;          //Speed of ship
    ship* next;                        //pointer to next node
};
////////////////////////////////////////////////////////////////
class shiplist                        //a list of nodes
{
private:
    ship* first;                    //pointer to first node
public:
    shiplist()                      //no-argument constructor
    {
        first = NULL;    //no first node
    }
    void additem(int sn, char shipname, int local, int star, int sto, int mph);            //add data item (one node)
    void display();                 //display all nodes
    void advance();
    void exit();
};
//--------------------------------------------------------------
void shiplist::additem(int sn, char shipname, int local, int star, int sto, int mph)         //add data item
{
    ship* newship = new ship;          //make a new node
    newship->serialnum = sn;            //stores ships serial number
    newship->ShipName = shipname;       //stores ships name
    newship->location = local;          //stores ships location
    newship->start = star;              //stores ships starting time
    newship->stop = sto;                //stores ships finishing time
    newship->speed = mph;               //stores ships speed
    newship->next = first;             //it points to next node
    first = newship;                   //now first points to this
    int test=1;                        //created to ensure loop is used atleast once
        do
            {
                cout<<"what is the name of your ship: "<<endl;
                cin>>shipname;
                cout<<"How fast can your ship travel: "<<endl;
                cin>>mph;
                    while(mph>30)
                    {
                        cout<<"You have entered a speed that is beyond the scope of reality."<<endl;
                        cout<<"Please reenter your ship speed: "<<endl;
                        cin>>mph;
                    }
                sn++;
                star=0;
                sto=78;
                local=0;
            }while(test!=1);
}
//--------------------------------------------------------------
void shiplist::display()              //display all nodes
{
    ship* current = first;             //set ptr to first node
    while ( current != NULL )          //quit on last node
    {
        //The following will display the ships in the queue.
        cout<<"Serial Number\tShip Name\tLocation in Canal\tStart time\tTime to Finish"<<endl;
        cout<<"--------------------------------------------------------------------------------"<<endl;
        cout<<current->serialnum'\t'current->ShipName'\t'current->location'\t'current->start'\t'current->stop<<end;

        current = current->next;        //move to next node
    }
}
//--------------------------------------------------------------
void shiplist::advance()
{
    ship* current = first;
    while (current != NULL)
    {
        current->start+75;        //advances the clock by 75 min
        current->stop-75;         //decrements time ti finish by 75
        current->location=(current->speed*current->start);       //gives the new location in the canal
    }
    cout<<sl.display()<<endl;
}
////////////////////////////////////////////////////////////////
int main()
{
    {
    int ch;
    ship sl;
    while( 1)           //creates a case statment that runs the program All the \033[22;3xm is to change the collor of the text
        {
        cout<<"\n1.Insert Ship       2.Show ships in transit        3.Advance clock by 75 min.    \t\t       4.EXIT\n         \n\t                  Enter your choice";
        cin >> ch;
            switch(ch)
                {
                case 1: sl.additem(); break;
                case 2: sl.display(); break;
                case 3: sl.advance(); break;
                case 4: exit(0);
                }
            return 0;
        }
    }
}


Here is what the compiler says
/home/armans/Documents/Data Structures Fall 2010/Banama Canal/main.cpp||In member function ‘void shiplist::display()’:|
/home/armans/Documents/Data Structures Fall 2010/Banama Canal/main.cpp|72|error: expected ‘;’ before '\x9'|
/home/armans/Documents/Data Structures Fall 2010/Banama Canal/main.cpp||In member function ‘void shiplist::advance()’:|
/home/armans/Documents/Data Structures Fall 2010/Banama Canal/main.cpp|83|warning: statement has no effect|
/home/armans/Documents/Data Structures Fall 2010/Banama Canal/main.cpp|84|warning: statement has no effect|
/home/armans/Documents/Data Structures Fall 2010/Banama Canal/main.cpp|87|error: ‘sl’ was not declared in this scope|
/home/armans/Documents/Data Structures Fall 2010/Banama Canal/main.cpp||In function ‘int main()’:|
/home/armans/Documents/Data Structures Fall 2010/Banama Canal/main.cpp|101|error: ‘struct ship’ has no member named ‘additem’|
/home/armans/Documents/Data Structures Fall 2010/Banama Canal/main.cpp|102|error: ‘struct ship’ has no member named ‘display’|
/home/armans/Documents/Data Structures Fall 2010/Banama Canal/main.cpp|103|error: ‘struct ship’ has no member named ‘advance’|
/home/armans/Documents/Data Structures Fall 2010/Banama Canal/main.cpp|104|error: ‘exit’ was not declared in this scope|
||=== Build finished: 6 errors, 2 warnings ===|


Why would it tell me that lines 83, and 84 have no affect, and that sl.additem and such was not declared in this scope?
Was This Post Helpful? 0
  • +
  • -

#13 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Pointers and linked lists

Posted 19 November 2010 - 06:40 PM

Quote

Why would it tell me that lines 83, and 84 have no affect


Here's line 83: current->start+75; . What variable gets changed on that line?
Same argument for line 84.

Quote

and that sl.additem and such was not declared in this scope?
Let's take the example of additem. I see: sl.additem() So you have a member function called additem that takes no arguments. Can you show me on which line you have a function called additem that takes no arguments?
Was This Post Helpful? 0
  • +
  • -

#14 shaunarman  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 64
  • Joined: 21-January 10

Re: Pointers and linked lists

Posted 19 November 2010 - 07:28 PM

Ok, for
current->start+75; 
I am wanting to increment
int start
by 75. I have tried both start, and star, but neither work. Will I need to declare a variable to pass into that?

As for
sl.additem()
what you are telling me is it should be
sl.additem(int sn, char shipname, int local, int star, int sto, int mph)
?

This post has been edited by shaunarman: 19 November 2010 - 07:29 PM

Was This Post Helpful? 0
  • +
  • -

#15 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Pointers and linked lists

Posted 19 November 2010 - 08:19 PM

Quote

I have tried both start, and star, but neither work. Will I need to declare a variable to pass into that?
Well, I don't see why you would type star, given that the name of the member variable is start...?

You want to change the variable start. So you need to assign to it. This is basic variables 101. How do you assign to a variable?

As for sl.additem(int sn, char shipname, int local, int star, int sto, int mph) , well, that's the function signature. I mean, you made this up. You are telling me that to add a ship, I need to provide a ship number, a letter for a name, a local number, a start number, a stop number, a mph number, etc.

That's how you designed your code. Not me. You said you wanted that, so you need to supply that information appropriately right? I mean, sl.additem() doesn't make much sense. What should the shipname be? A? B? C? D? E? What? You haven't supplied that information, so clearly that function call doesn't make sense.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2