Function problem
May 17, 2013 at 1:12pm UTC
Hi there, having a bit of trouble with my assignment. I am stuck at the point in the instructions where it says 'We need to verify that our insertAtFront() function works properly. Inside main.cpp above the main function, create a function called testInsertAtFront_EmptyList. Inside this function, declare a LinkedList variable and use our newly created insertAtFront function to insert a value into the list. Then test that it works properly by printing your list using one of LinkedList’s getter functions accordingly.' I tried all three getter functions from my LinkedList class and still nothing. Can someone please point me in the right direction. Any help would be greatly appreciated! :)
Here is the code:
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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
//LinkedList.cpp
#include "LinkedList.h"
#include "Node.h"
#include <iostream>
#include <string>
using namespace std;
LinkedList::LinkedList(void )
{
setFirstPtr(0);
setLastPtr(nullptr );
}
LinkedList::LinkedList(Node* nullPtr){
nullPtr;
}
LinkedList::~LinkedList(void )
{
}
void LinkedList::setFirstPtr(Node* aPtr){
this ->firstPtr = aPtr;
}
Node* LinkedList::getFirstPtr() const {
return firstPtr;
}
void LinkedList::setLastPtr(Node* aPtr){
this ->lastPtr = aPtr;
}
Node* LinkedList::getLastPtr() const {
return lastPtr;
}
bool LinkedList::isEmpty() const
{
if (firstPtr == nullptr && lastPtr == nullptr )
{
return true ;
}
else
{
return false ;
}
}
Node* LinkedList::getNewMode(const int value){
return new Node(value);
}
void LinkedList::insertAtFront(const int value){
Node* nodeToInsert = getNewMode(value);
if (isEmpty() )
{
setFirstPtr(nodeToInsert);
setLastPtr(nodeToInsert);
}
else
{
// if it is NOT empty
nodeToInsert->setNextPtr(firstPtr);
setFirstPtr(nodeToInsert);
}
}
//LinkedList.h
#include "Node.h"
class LinkedList
{
private :
Node* firstPtr;
Node* lastPtr;
public :
LinkedList(void );
LinkedList(Node* nullPtr);
~LinkedList(void );
Node* newMode(const int value);
Node* getFirstPtr() const ;
Node* getLastPtr() const ;
bool isEmpty() const ;
Node* getNewMode(const int value);
void insertAtFront(const int value);
void setFirstPtr(Node* aPtr);
void setLastPtr(Node* aPtr);
};
//Node.cpp
#include "Node.h"
//default
Node::Node(void )
{
setData(0);
setNextPtr(nullptr );
}
Node::~Node(void )
{
}
//2nd constructors
Node::Node(int value){
setData(value);
setNextPtr(nullptr );
}
void Node::setNextPtr(Node* nextNode){
this ->nextPtr = nextNode;
}
Node* Node::getNextPtr() const {
return nextPtr;
}
void Node::setData(int value){
this ->data=value;
}
int Node::getData() const {
return data;
}
//Node.h
#pragma once
class Node
{
public :
Node(void );
~Node(void );
Node(int value);
void setNextPtr(Node* nextNode);
Node* getNextPtr() const ;
void setData(int value);
int getData() const ;
Node* getFirstPtr() const ;
void setFirstPtr(Node* aPtr);
private :
int data;
Node* nextPtr;
};
//main.cpp
#include <iostream>
#include <string>
using namespace std;
#include "Node.h"
#include "LinkedList.h"
///LEFT OFF HERE
void testInsertAtFront_EmptyList(){
LinkedList insert;
//LinkedList *insertPtr;
//insertPtr = &insert;
insert.insertAtFront(3);
//INSTRUCTIONS: PRINT YOUR LIST USING ONE
//OF THE LINKEDLIST GETTER FUNCTIONS. I HAVE 3 GETTER FUNCTIONS AND
//EITHER OF THEM WORK SO WHERE IS THIS HIDDEN GET FUNCTION THAT I USE?
cout << insert.getFirstPtr() << endl;
}
void testNodeClass(){
//two node objects
Node head;
Node tail;
//declare one node pointer
Node* nodePtr = &tail;
head.setData(10);
tail.setData(20);
head.setNextPtr(nodePtr);
cout << head.getNextPtr() << endl;
cout << nodePtr->getData() << endl; //pointer
nodePtr = &head;
tail.setNextPtr(nodePtr);
cout << tail.getNextPtr() << endl;
cout << nodePtr->getData() << endl;
}
void testIsEmpty()
{
cout << "Hello" << endl;
//Linked List object called verify
LinkedList verify;
LinkedList *verifyPtr;
verifyPtr = &verify;
cout << "Pointer = " << verify.isEmpty() << endl;
}
int main(){
//testNodeClass();
//testIsEmpty();
testInsertAtFront_EmptyList();
system("pause" );
}
May 17, 2013 at 3:50pm UTC
flyingEagle wrote:I tried all three getter functions from my LinkedList class and still nothing.
What do you mean "and still nothing"? What happens for each one you tried?
Topic archived. No new replies allowed.