Stacks without classes or STL

I have a school assignment that instructs us to create and manipulate two stacks of linked lists but we cannot use classes or STL, all functions must be user-defined. I understand linked lists fairly well but our textbook only covers stacks within classes. So creating and using stacks by scratch completely looses me. We have to create two different stacks of linked lists but one function for each operation we want to perform. So for example, to create StackA and StackB, I have to call the same CreateEmptyStack function twice, passing it the different parameters. I cannot create separate functions for each stack. The whole thing has just got my mind jumbled. Any help would be appreciated.

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
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <fstream>
using namespace std;

struct vehicleRecord
{
	char vehicleType;
	string license;
	int capacity;
	vehicleRecord *nextVehicle;
};

// Function prototypes
void VerifyDataFiles(string);
vehicleRecord CreateEmptyStack(string);

// main function
int main(int argc, char* argv[])
{
	string autoStack = "autoStack";
	string vanStack = "vanStack";
	vehicleRecord *autoStackTop;
	vehicleRecord *vanStackTop;

	if (argc != 3)
		cout << "Invalid number of parameters.";
	else
	{
		string vehicleDataFile = argv[1];
		string checkretDataFile = argv[2];
		VerifyDataFiles(vehicleDataFile);
		VerifyDataFiles(checkretDataFile);
	}	

	autoStackTop = CreateEmptyStack(autoStack);
	vanStackTop = CreateEmptyStack(vanStack);

	cout << endl << endl;
	return 0;
}

// VerifyDataFiles function
void VerifyDataFiles(string fileName)
{
	ifstream verifyFile;
	verifyFile.open(fileName.c_str());

	if (!verifyFile)
	{
		cout << "Could not open " << fileName << "." << endl;
		verifyFile.close();
	}	
	else
		cout << fileName << " opened successfully." << endl;
}

// CreateEmptyStack function
vehicleRecord CreateEmptyStack(string stackType)
{
	if (stackType == "autoStack")
	{
		vehicleRecord *autoStackTop = NULL;
		return *autoStackTop;
	}

	if (stackType == "vanStack")
	{
		vehicleRecord *vanStackTop = NULL;
		return *vanStackTop;
	}
}
Think about a stacks attributes and properties.

- A stack consist of zero or more items usually of the same item type.
- You may push an item on top of a stack. So there should be a method called push. It takes an item as argument and pushes it on top of the stack so that its size increases by one.
- You may have a look at the top of the stack to see its top most item. We'll call this method top. it will return a copy or reference of the top most item leaving its original value on the stack unchanged.
- To f.e. have a look at the item below the top most we need a way to pop the top most item away. So we'll call this method pop. It simply will remove the top most item so that its size decreases by one.
- Sometimes you may want to know if a stack is empty. So we'll implement a method called empty returning the boolean value true if it is empty. Otherwise the method will return false.

There are several ways to implement stacks. Usually you will need some kind of item buffering and the stack pointer.
- You may use a linked list of items like your 'vehicleRecord' which is linked to the next by its attribute 'nextVehicle'. If using a linked list, there's no need for a separate top pointer. You could simple use the front end of the list as top pointer.
- You may use an array of items. The top pointer may internally be realized of type int indexing the array. It will point to the right most item of the array. In case of an empty stack its value may be -1.

Now have a try...
Topic archived. No new replies allowed.