GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 16-Oct-2009, 16:53
justyn82 justyn82 is offline
New Member
 
Join Date: Oct 2009
Posts: 5
justyn82 is on a distinguished road

Linklist program with structs


In this assignment, you will write a program that holds a small collection of vehicles. Each vehicle will be represented by an instance of a struct that holds only the vehicle’s description, color, another field of your choice and a pointer to the next element in the list.

Write a C++ program that

1) Has a struct that contains fields (members) for the description of the vehicle, the color of the vehicle, the field that you choose and a pointer to that same struct type.
2) Main should have a pointer that holds the address of the first node in the list.
3) Nodes should be created as dynamic data (using the new keyword)
4) The list should contain exactly 3 vehicles.
5) The program should print out the vehicle description, color, and the field you chose from the list in the order they occur in the list.



This is what my teacher wants to complete and Ive worked out some of the struct part of it, but I'm confused about how to do the rest. Any help would be appreciated thanks.

CPP / C++ / C Code:
#include <iostream>
using namespace std;

int main()
{
        // Defining a structure
        struct node
        {
                char *description;
                char *color;
				int  year;
				node *next;
		}; 

		node car1,car2,car3;

       
        car1.description = "Honda Accord";
        car1.color = "Green";
        car1.year = 1992;
        car2.description = "Saturn Ion";
		car2.color = "Red";
		car2.year = 2002;
		car3.description = "Hyuandai Elantra";
		car3.color = "Blue";
		car3.year = 2007;

        // Print the data out
		cout << "Vehicle one is a: " << car1.description << endl;
		cout << "Vehicle one's color is: " << car1.color<< endl;
        cout << "Vehicle one's creation year is: " << car1.year<< endl;

        return 0;
}
Last edited by LuciWiz : 16-Oct-2009 at 17:59. Reason: Please insert your C++ code between [cpp] & [/cpp] tags
  #2  
Old 17-Oct-2009, 03:29
Kimmo Kimmo is offline
Regular Member
 
Join Date: Mar 2007
Location: Finland
Posts: 342
Kimmo is a jewel in the roughKimmo is a jewel in the roughKimmo is a jewel in the rough

Re: Linklist program with structs


Tsk tsk, you didn't read the guidelines.


1) Has a struct that contains fields (members) for the description of the vehicle, the color of the vehicle, the field that you choose and a pointer to that same struct type.

CPP / C++ / C Code:
struct node
        {
                char *description;
                char *color;
				int  year;
				node *next;
		}; 

The list structure aside (you can find Bob's rant about that in the C forum. Except that in C++ I'd implement it with classes), I wouldn't implement it that way. Why not use the much safer std::string instead of char*? I also wouldn't call the struct node. You're right, it is a node, but since it's not ONLY a node but also a vehicle, I'd call it vehicle.

So I could change it to
CPP / C++ / C Code:
namespace
{
    struct Vehicle
    {
        std::string m_description;
        std::string m_color;
        int m_year;
        Vehicle* m_next;
    };
    
    const int NUMBER_OF_VEHICLES = 3;
}


2) Main should have a pointer that holds the address of the first node in the list.
3) Nodes should be created as dynamic data (using the new keyword)

CPP / C++ / C Code:
node car1,car2,car3;
Your nodes (cars) are not dynamically allocated. Also note that you don't have to three variables to allocate three cars. What if you had 100 nodes in the list? car1, car2, ... , car100? The way you can have an arbitrary number of cars without declaring a separate variables for each one is using a temp node and a loop. An easy way could be something like
CPP / C++ / C Code:
    Vehicle* head;
    Vehicle* temp;
    Vehicle* current;
    
    for (int i = 0; i < NUMBER_OF_VEHICLES; i++)
    {
        temp = new Vehicle; // dynamic allocation of a new vehicle
        std::cout << "Enter the description for vehicle " << i + 1 << ": ";
        std::getline(std::cin, temp->m_description);  // read a line
        std::cout << "Enter the color for vehicle " << i + 1 << ": ";
        std::getline(std::cin, temp->m_color);
        std::cout << "Enter the year for vehicle " << i + 1 << ": ";
        std::cin >> temp->m_year; // the lazy way
        std::cin.get();
        temp->m_next = 0;    // set pointer to next vehicle to 0 so we know the end of list
        
        // If first vehicle in question, set head to point to it.
        // current is there to keep of the current vehicle we're at
        // Otherwise we'd lose track of the pointers
        if (i == 0)
        {
            head = temp;
            current = temp;
        }
        // If we're past the first iteration, we can just assign the newly allocated
        // temp to current->m_next and then move current to the new current
        else
        {
            current->m_next = temp;
            current = current->m_next;
        }
    }
Note that after the loop is done, the head pointer is the only reference we have to the nodes, so it's important that you never assign anything to head.

Traversing the list and cleaning up (deleting all the nodes) is up to you, but the principle is very similar to the construction of the list. In the deletion, just first remember to store the m_next pointer somewhere before deleting the current node.

If that didn't help, then you should ask a question.
  #3  
Old 17-Oct-2009, 16:27
justyn82 justyn82 is offline
New Member
 
Join Date: Oct 2009
Posts: 5
justyn82 is on a distinguished road

Re: Linklist program with structs


Thanks so much only problems im getting with that is a syntax error in the 'for' statement and on line 26 it says I am missing the function header
  #4  
Old 17-Oct-2009, 16:56
justyn82 justyn82 is offline
New Member
 
Join Date: Oct 2009
Posts: 5
justyn82 is on a distinguished road

Re: Linklist program with structs


woot i figured it out thanks!
 
 

Recent GIDBlogProblems with the Navy (Enlisted) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Equation solver RazoR C Programming Language 3 18-May-2008 09:24
Two-Tier data dissemination code installation problem nidhibansal1984 Computer Software Forum - Linux 6 16-Sep-2007 10:13
Help with a complex program lordfuoco C++ Forum 5 24-Jun-2006 06:03
Help With A Program Using Structs fevershark C++ Forum 17 07-Dec-2005 20:50

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:59.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.