Writing print function for linked list

I am having trouble solving this problem I need to write a print function for my linked list and I am not sure how to accomplish this. Any help would be appreciated. Here are the requirements:

The printList Function:You will also need to create a stand-alone function and add it to the driver that has been provided for you. Your printList function takes a LinkedList object as it's parameter and prints out the data for each CarPart object pointed to by the Node objects in the list. The output for a CarPart object should look something like

X20345-t Radiator Cap $3.95

I will post all of my code just so you can see it all.

implementation
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "CarPart Header.h"

CarPart::CarPart()
{
	price = 0.00;
	partNumber = "";
	partDescription = "";
}

CarPart::CarPart(std::string partNum, std::string partDesc, double partPrice)
{
	price = partPrice;
	partNumber = partNum;
	partDescription = partDesc;
}

void CarPart::setDescription(std::string)
{
	partDescription;
}

void CarPart::setPartNumber(std::string)
{
	partNumber;
}

void CarPart::setPrice(double)
{
	price;
}

std::string CarPart::getDescription() const
{
	return partDescription;
}

std::string CarPart::getPartNumber() const
{
	return partNumber;
}

double CarPart::getPrice() const
{
	return price;
}

Node::Node()
{
	next = NULL;
}

Node::Node(CarPart*)
{
	
}

Node::~Node()
{
	if (carPart != NULL)
	{
		delete carPart;
	}
}
List::List()
{
	numNodes = NULL;
	firstNode = NULL;
	lastNode = NULL;
}

List::~List()
{
	Node* tempPtr = firstNode;
	firstNode = firstNode->next;
	while (firstNode != NULL)
	{
		delete tempPtr;
		--numNodes;
		tempPtr = firstNode;
		firstNode = firstNode->next;
	}
	lastNode = NULL;
}

void List::push_back(Node* newNode)
{
	if (lastNode != NULL)
	{
		newNode->setNext(lastNode);
		lastNode = newNode;
		numNodes++;
	}
}

void List::push_front(Node* newNode)
{
	if (firstNode != NULL)
	{
		newNode->setNext(firstNode);
		firstNode = newNode;
		numNodes++;
	}
}

Node* List::pop_back()
{
	if (lastNode = NULL)
	{
		return NULL;
	}
	if (lastNode != NULL)
	{
		Node* tempPtr = lastNode;
		lastNode = lastNode->getNext();
		numNodes--;
		return tempPtr;
	}
}

Node* List::pop_front()
{
	if (firstNode = NULL)
	{
		return NULL;
	}
	if (firstNode != NULL)
	{
		Node* tempPtr = firstNode;
		firstNode = firstNode->getNext();
		numNodes--;
		return tempPtr;
	}
}

Node* List::getFirstNode()
{
	return firstNode;
}

Node* List::getLastNode()
{
	return lastNode;
}


Header
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <fstream>
#include <stdexcept>
#include <array>

class CarPart
{

private:
	std::string partNumber;
	std::string partDescription;
	double price;

public:
	 CarPart();

	 CarPart(std::string partNum, std::string partDesc, double partPrice);

	 std::string getPartNumber() const;

	 void setPartNumber(std::string);

	 std::string getDescription() const;

	 void setDescription(std::string);

	 double getPrice() const;

	 void setPrice(double);
};

 class Node
 {
 private:
	 Node* next;
	 CarPart* carPart;

 public:
	 Node();

	 Node(CarPart*);

	 ~Node();

	 // Getter Name: getNext
	 // Purpose: Initialize data
	 // Parameters: None
	 // Returns: None
	 Node* getNext();

	 // Setter Name: setNext
	 // Purpose: Initialize data
	 // Parameters: A pointer to next node
	 // Returns: None
	 void setNext(Node*);
 };

 class List
 {
 private:
	 // Variable Name: numNodes
	 // Purpose: Variable that contains the number of nodes
	 // Parameters: None
	 // Returns: None
	 // Pre-conditions: Must be of type int
	 // Post-conditions: None
	 int numNodes;

	 // Variable Name: next
	 // Purpose: Points to the first node
	 // Parameters: None
	 // Returns: None
	 Node* firstNode;

	 // Variable Name: next
	 // Purpose: Points to the last node
	 // Parameters: None
	 // Returns: None
	 Node* lastNode;

 public:
	 // Default Constructor Name: List
	 // Purpose: Default constructor to initialize
	 // Parameters: None
	 // Returns: None
	 List();

	 //Destructor
	 //Purpose:Deletes any dynamically allocated storage
	 //Parameters: None
	 //Returns: None
	 ~List();

	 // Function Name: push_back
	 // Purpose: Take the node and place it at the end of the list
	 // Parameters: Node pointer
	 // Returns: None
	 // Pre-conditions: Node cannot be NULL
	 // Post-conditions: None
	 void push_back(Node*);

	 // Function Name: push_front
	 // Purpose: Take the node and place it at the front of the list
	 // Parameters: Node pointer
	 // Returns: None
	 // Pre-conditions: Node cannot be NULL
	 // Post-conditions: None
	 void push_front(Node*);

	 // Function Name: pop_back
	 // Purpose: Removes last node from list
	 // Parameters: None
	 // Returns: A pointer to this node
	 // Pre-conditions: None
	 // Post-conditions: None
	 Node* pop_back();

	 // Function Name: pop_front
	 // Purpose: Removes first node from list
	 // Parameters: None
	 // Returns: A pointer to this node
	 // Pre-conditions: None
	 // Post-conditions: None
	 Node* pop_front();

	 // Function Name: getFirstNode
	 // Purpose: To get the first node
	 // Parameters: None
	 // Returns: A pointer to the first node
	 // Pre-conditions: None
	 // Post-conditions: None
	 Node* getFirstNode();

	 // Function Name: getLastNode
	 // Purpose: To get the last node
	 // Parameters: None
	 // Returns: A pointer to the last node
	 // Pre-conditions: None
	 // Post-conditions: None
	 Node* getLastNode();
 };


driver
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
94
95
96
97
98
// CS 1410 Final Project
// add your file prologue information here
#include <iostream>
#include "CarPart Header.h"
using namespace std;

// the printlist function
// Purpose: Prints out the contents for each Node in the List
// Parameter: A list object, by const reference
// Returns: none
void printList(const List&);

// the printFirstNode function
// Purpose: Prints out the contents for the 1st Node in the List
// Parameter: A list object, passed by value to test the copy constructor
// Returns: none
void printFirstNode(List);

int main()
{

	// set up cout for displaying prices
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(2);

	// create a List object
	List partsList;

	cout << "\nPart I: multiple node test: push_front and pop_front\n";
	cout << "\n----------------------------------\n";
	// build a List using push_front
	partsList.push_front(new Node(new CarPart("FL2016", "Oil Filter", 18.95)));
	partsList.push_front(new Node(new CarPart("RS12YC", "Spark Plug", 4.15)));
	partsList.push_front(new Node(new CarPart("D5941", "Digital Tire Guage", 12.15)));
	partsList.push_front(new Node(new CarPart("G19216", "Car Wash Solution", 8.15)));

	cout << "\nThe original nodes in the List:\n";
	printList(partsList);
	cout << "\n----------------------------------\n";

	// test push_front function
	cout << "\nAdding to the front of the List:\n";
	cout << "\n----------------------------------\n";
	partsList.push_front(new Node(new CarPart("X-5077a", "Wiper Blades", 15.45)));
	partsList.push_front(new Node(new CarPart("T-280RA", "Turtle Wax Chrome Polish", 3.15)));

	printList(partsList);
	cout << "\n----------------------------------\n";

	// test pop-front
	cout << "\nRemoving the first node from the list.\n";
	cout << "\n----------------------------------\n";
	Node* item = partsList.pop_front();
	printList(partsList);
	if (item != NULL)
		delete item;

	cout << "\n----------------------------------\n";
	cout << "\nPart Two: Push_back and pop_back";

	// test push_back
	partsList.push_back(new Node(new CarPart("C120-X", "Assorted Fuses", 7.25)));
	partsList.push_back(new Node(new CarPart("CTK-120706", "Mechanic Tool set", 126.00)));

	cout << "\nAdding two nodes at the end\n";
	cout << "\n----------------------------------\n";
	printList(partsList);

	// test pop-back
	cout << "\n----------------------------------\n";
	cout << "\nRemove last node from the list\n";
	cout << "\n----------------------------------\n";
	item = partsList.pop_back();
	printList(partsList);
	if (item != NULL)
		delete item;



	cout << "\n-------------------------------------------\n";
	cout << "\nEnd of Test";
	cout << "\n-------------------------------------------\n";
	system("PAUSE");
	return 0;
}

// you have to fill in the code for this function
void printList(const List& theList)
{

}

// you have to fill in the code for this function
void printFirstNode(List theList)
{
	cout << theList.getFirstNode;
}
closed account (SECMoG1T)
You'll need to use alot of getters, if the funct on line 95 is a helping function to that on 89 "driver"
I can give u something close to what your function might do. But I guess you must have atleast a copy constructor or an overloaded assignment operator in your List class since you are passing a list object by value.


printFirstNode//pseudo-code am assuming this prints info on a single first node on a list
1. You must have a getCarPart () function in your node class returning a the private *Carpart member
2. Assing it to a temporal Carpart* ptr; ptr=frstnde.gtcrprt ()
3. Use the getters in class Carpart to display info contained in your ptr above

printListassuming this prints the whole list
1. Create a local temporal pointer to you firstNode.
2. Pass the pointer to the 1st function
3. Move the temporal pointer to the next node by calling a getter.
4. Use a loop to repeate this process

Create a temporal pointer
while (temptr is not at the end of list)
{
Call the printfirstnode (*tempptr);
Move the temporal pointer to the next node
}
Last edited on
Okay so for the printFirstNode

on step one would it look like this in the header

Node getCarPart(); what data member would I assign to it and what private carpart member would be returned?
closed account (SECMoG1T)
Well your aim is to print the details of the Carpart* member in each node, your class node needs a getter that return this CarPart* carPart; members so that it is accessible outside the class.
So your getter returns a Carpart*
Topic archived. No new replies allowed.