Making stack with linked list - Confused

I know we're not supposed to post homework here and I'm not. I'm hoping someone can clarify exactly why we've been told to do a linked list in this way. Previously when I did linked list the head was the same as every other node. However, we were told to do it differently whereas the head is a different struct (see top of code). Well, when I make a function to load a text file into this list (which is supposed to be a stack) I can't figure out how to connect the two. I don't understand how to connect two structs that don't match when I'm loading in data that populates the larger struct. See bottom half of code. I get errors on the file input part and I think it's because I'm not linking them properly.

Thanks for any help to clarify this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
struct vehicleDataNode {       // node structure
   char type;
   string license;
   int capacity;
   vehicleDataNode *Next;
};

struct aList {          // hides implementation of list
   vehicleDataNode *Head;
   int count;
};

int main (int argc, char* argv[])
{
    // Opening input files for parked and checked out/returned vehicles
    ifstream parkedVehicle;          // Input data file stream for parked vehicles
    ifstream checkoutReturn;         // Input data file stream for checked out & returned vehicles

    parkedVehicle.open(argv[COMMAND_LINE_INPUT1]);
    checkoutReturn.open(argv[COMMAND_LINE_INPUT2]);
    
    if ( !parkedVehicle)               // input file error check for parked vehicles input file
         {
            cout << endl << "Error opening file!  " << endl << "Parked Vehicle file does not exist." << endl 
                 << "Program ending!" << endl << endl;
                 
                system("PAUSE");
                return 0;
         }
         
    else
    if ( !checkoutReturn)               // input file error check for checked out & returned input file
         {
            cout << endl << "Error opening file!  " << endl << "Checkout & Return file does not exist." << endl 
                 << "Program ending!" << endl << endl;
                 
                system("PAUSE");
                return 0;
         }
         
    //creating head stacks for parked and checked out/returned vehicles
    aList* aStack;                      // head of stack for parked vehicles
    aList* vStack;                     // head of stack for vans vehicles
    
    aStack = createEmptyStack();        // make head of stack for parked vehicles empty
    vStack = createEmptyStack();       // make head of stack for checked out & returned vehicles empty
    
    FileLoadParked(aStack, vStack, parkedVehicle);  // load data from file into Parked Vehicle Stack
    
    cout << endl << endl;
    system("PAUSE");
    return 0;
}

//*************************************************************************
//  FUNCTION:  FileLoad
//  DESCRIP:   
//  INPUT:     
//  OUTPUT:    
//**************************************************************************
void FileLoadParked(aList* &aStack, aList* &vStack, ifstream& parkedVehicle)
{
     // local variables
     vehicleDataNode *newNode,
                     *last = NULL;
     char tempType;                              
     string tempLicense;                             
     int tempCapacity;
     
     parkedVehicle >> tempType;                      // prime data entry
     
     while(parkedVehicle)
        {
           parkedVehicle >> tempLicense >> tempCapacity;
           
           newNode = new (nothrow) vehicleDataNode;
           
            if (newNode == NULL)
                   cout << "Heap error - could not allocate memory" << endl;
                else
        		{				// Memory allocated successfully
        
        		    newNode->type = tempType;
        		    newNode->license = tempLicense;
        		    newNode->capacity = tempCapacity;
         	        newNode->Next = aStack;        
                    
        			aStack = newNode; 
                }
                
                vehicleDataNode >> tempType;
        }    
}
> Previously when I did linked list the head was the same as every other node.
> However, we were told to do it differently whereas the head is a different struct (see top of code).
In the code that you've posted, the `head' is a pointer to `vehicleDataNode', as it would be any other link of the nodes in your list.
I don't understand why are you saying that the head is a different struct.


> I get errors on the file input part and I think it's because I'm not linking them properly.
If you get error messages, post the error messages.
Also, make sure that your testcase is selfcontained http://www.eelis.net/iso-c++/testcase.xhtml
Topic archived. No new replies allowed.