C++ RemoveFront function for Doubly Linked List - c++

I'm working on a lab for school and I'm stuck on a function for a doubly linked list. When I try to remove the front node from the list with the removeFront() function it returns NULL and couts for me to create a list even though there already is one. I'll post my cpp file and main below and hopefully someone can help me understand what's going wrong with it.
//DLinkedList.h
#pragma once
#include <string>
using namespace std;
typedef string Elem;
struct DNode
{
Elem value;
DNode* next;
DNode* prev;
};
class DLinkedList
{
public:
DLinkedList() { header_ = NULL; }
~DLinkedList() { };
bool empty() const;
const Elem& front() const;
const Elem& back() const;
void addFront(const Elem& e);
void addBack(const Elem& e);
void removeFront();
void removeBack();
private:
DNode* header_;
DNode* trailer_;
protected:
void add(DNode* v, const DNode& e);
void remove(DNode* v);
};
//DLinkedList.cpp
#include <iostream>
#include "DLinkedList.h"
using namespace std;
const Elem& DLinkedList::front() const
{
if (header_ == NULL)
{
cout << "Please create a doubly linked list first.\n\n";
}
else
{
return header_->value;
}
}
void DLinkedList::addFront(const Elem& e) //DONE
{
// If there is no header
// create a temporary node and set
// the header to it
if (header_ == NULL)
{
DNode *temp;
temp = new(struct DNode);
temp->prev = NULL;
temp->value = e;
temp->next = NULL;
header_ = temp;
trailer_ = temp;
}
else
{
//Create current node to point to header
// and temp node to be the new front node
DNode *current;
DNode *temp;
current = header_;
temp = new(struct DNode);
temp->prev = NULL;
temp->value = e;
temp->next = current->next;
header_->prev = temp->next;
header_ = temp;
}
cout << "Element Inserted at the front." << endl;
}
void DLinkedList::removeFront()
{
// Check to see if there is anything
// in the list first
if (header_ == NULL)
{
cout << "Create a doubly linked list first.";
}
// Check to see if the list has more than one item.
// If it only has one node erase it.
DNode *current;
current = header_;
if (current->next == NULL)
{
header_->next = NULL;
header_->value = "";
header_->prev = NULL;
header_ = NULL;
}
else
{
current = current->next;
//header_->next = NULL;
//header_->value = "";
//header_->prev = NULL;
header_ = current;
header_->prev = NULL;
}
}
//Main.cpp
#include <iostream>
#include <string>
#include <cassert>
#include "DLinkedList.h"
using namespace std;
int main()
{
DLinkedList album;
album.addFront("Word");
album.addFront("Turn");
album.addFront("Bird");
album.addFront("Weird");
cout << album.front() << endl;
album.removeFront();
cout << album.front() << endl;
system("pause");
return 0;
}

This how i would do it.
void DLinkedList::removeFront()
{
// Check to see if there is anything
// in the list first
if (header_ == NULL)
{
cout << "Create a doubly linked list first.";
}
// Check to see if the list has more than one item.
// If it only has one node erase it.
if (header_->next == NULL)
{
delete header_;
}
else
{
DNode* next = header_->next;
delete header_;
header_ = next;
header_->prev = NULL;
}
}

I think your addFront is also wrong, i would do it like that:
void DLinkedList::addFront(const Elem& content) //DONE
{
// If there is no header
// create a temporary node and set
// the header to it
if (header_ == NULL)
{
header_ = new(struct DNode);
header_->prev = NULL;
header_->value = content;
header_->next = NULL;
trailer_ = header_;
}
else
{
//Create current node to point to header
// and temp node to be the new front node
DNode *tmp;
tmp = new(struct DNode);
tmp->prev = NULL;
tmp->value = content;
tmp->next = header_;
header_->prev = tmp;
header_ = tmp;
}
cout << "Element Inserted at the front." << endl;
}

Related

c++ operator == overloading in linked list

I'm wondering why my overloaded == function is not working. I'm confused about the private head. Would the private head be the head of the linked list that was made last? So if I compared the last linked list with the inputted LinkedList then wouldn't it work?
code for append
void LL::append(string pName,string phone)
{
Node *newNode = new Node;
newNode->name = pName;
newNode->phoneNumber = phone;
newNode->next = nullptr;
if (head == nullptr)
{
head = newNode;
}
else
{
Node *nodePtr = head;
while (nodePtr->next != nullptr)
{
nodePtr = nodePtr->next;
}
nodePtr->next = newNode;
}
}
code for deep copy
LL::LL(const LL& source)
{
head = nullptr;
Node *nodePtr = source.head;
while(nodePtr)
{
append(nodePtr->name,nodePtr->phoneNumber);
nodePtr = nodePtr->next;
}
}
main.cpp
#include <iostream>
#include "node.h"
using namespace std;
int main()
{
LL list;
list.append("jack","2");
list.append("jack2","1");
list.append("jack3","3");
list.append("jack4","4");
list.insertatBegin("notjack","0");
list.print();
list.searchByName("jack");
cout << "cloning------------------" <<endl;
LL list2(list);
//list.destroy();
//list2.append("jack","223");
list2.print();
if(list == list2)
{
cout << "same" <<endl;
}
else
{
cout << "not same" <<endl;
}
}
.h file
#ifndef NODE_H
#define NODE_H
#include <iostream>
using namespace std;
class Node
{
public:
string name; //data
string phoneNumber;
Node* next; //pointer to next
};
class LL
{
private:
Node* head; // list header
public:
LL();
~LL();
LL(const LL& source);
void append(string pName,string phone);
void insertatBegin(string pName,string phone);
void searchByName(string pName);
void print();
void destroy();
bool operator== (const LL& L1);
};
#endif
cpp file for class functions
bool LL::operator == (const LL &L1)
{
bool status = true;
Node *nodePtr = L1.head;
Node *nodePtr2 = head;
//cout << tmp.head <<endl;
while (nodePtr != nullptr)
{
if (nodePtr == nodePtr2)
{
nodePtr = nodePtr->next;
nodePtr2 = nodePtr2->next;
}
else
{
status = false;
}
}
return status;
}
I do not know what is your implementation of Copy Constructor if it is working fine then this should work.
bool LL::operator==(const LL& L1) const{
if (&L1==this){
return true;
}
else{
Node* current = L1.head;
Node* lhs = this->head;
while(current != nullptr){
if(current->name != lhs->name || current->phoneNumber != lhs->phoneNumber)
return false;
current = current->next;
lhs = lhs->next;
}
return true;
}}

Program Crash in Doubly Linked List Iterator print in C++

I am trying to run my linked list iterator to print the data I have entered into the list. It does manage to print all the entries, but as soon as it does, the program crashes. Does anyone see what might be causing it? I am running it on CodeBlocks so I don't know if that might be the problem.
It is the same project I've been dealing with in my other questions. Before I tried running the iterator to print the contents from the function inside the list class. However, the specs of the assignment don't allow the classes to trigger any error or control messaging within the report. The ideal is for the user to access the data completely. Anything like console messages have to be done in main. This is what I've gotten after tweaking it. Again, it prints the contents of the list successfully, but after that, the program stops working.
Code:
#include <iostream>
#include <string>
using namespace std;
typedef string dataType;
typedef class LISTEntry* listPtr;
class DATAClass
{
dataType key;
public:
DATAClass();
DATAClass(dataType key);
DATAClass(DATAClass &dc);
void setKey(dataType keyVal);
dataType getKey();
};
DATAClass::DATAClass() {
key = key;
}
DATAClass::DATAClass(dataType key) {
this->key = key;
}
DATAClass::DATAClass(DATAClass &dc) {
key = dc.key;
}
void DATAClass::setKey(dataType keyVal) {
key = keyVal;
}
dataType DATAClass::getKey() {
return key;
}
class LISTEntry
{
DATAClass data;
listPtr prev;
listPtr next;
public:
LISTEntry() {
this->data = data;
this->prev = nullptr;
this->next = nullptr;
};
LISTEntry(DATAClass l) {
this->data = l;
this->prev = nullptr;
this->next = nullptr;
};
LISTEntry(LISTEntry &le) {
data = le.getData();
prev = nullptr;
next = nullptr;
};
~LISTEntry() {
};
LISTEntry & operator=(const LISTEntry &le) {
if (this != &le)
{
data = le.data;
prev = nullptr;
next = nullptr;
}
};
LISTEntry * getNext() {
return next;
};
void setNext(LISTEntry * entry) {
next = entry;
};
LISTEntry * getPrev() {
return prev;
};
void setPrev(LISTEntry * entry) {
prev = entry;
};
DATAClass getData() {
return data;
};
void setData(DATAClass d) {
data = d;
};
};
class LIST
{
private:
listPtr curr;
listPtr head;
listPtr tail;
public:
LIST();
LIST(DATAClass d);
LIST(LISTEntry l);
LIST(LIST * l);
~LIST();
void addList(dataType n);
void iterateInitialize();
bool iterate_hasNext();
bool searchList(dataType key);
LISTEntry * getHead();
void setHead(LISTEntry * h);
LISTEntry * getTail();
void setTail(LISTEntry * t);
LISTEntry * getCurr();
void setCurr(LISTEntry * c);
bool isEmpty();
bool isFull();
};
LIST::LIST() {
curr = nullptr;
head = nullptr;
tail = nullptr;
};
LIST::LIST(DATAClass d) {
listPtr tmp;
tmp->setData(d);
tmp->getData();
curr = tmp;
head = nullptr;
tail = nullptr;
};
LIST::LIST(LISTEntry l) {
listPtr tmp;
curr = tmp;
head = tmp;
tail = tmp;
};
LIST::LIST(LIST * l) {
curr = nullptr;
head = nullptr;
tail = nullptr;
};
LIST::~LIST()
{}
void LIST::addList(dataType n) {
DATAClass dc;
dc.setKey(n);
listPtr entry = new LISTEntry();
entry->setData(dc);
entry->setNext(nullptr);
entry->setPrev(nullptr);
if (!isEmpty())
{
tail->setNext(entry);
entry->setPrev(tail);
tail = entry;
curr = tail;
curr->setNext(nullptr);
tail->setNext(nullptr);
}
else
{
head = tail = entry;
head->setPrev(nullptr);
tail->setNext(nullptr);
curr = head;
}
} //addList
void LIST::iterateInitialize() {
setCurr(head);
}
bool LIST::iterate_hasNext() {
return curr->getNext() != nullptr;
}
bool LIST::searchList(dataType key) {
curr = head;
while (curr != nullptr)
{
if (curr->getData().getKey() == key)
{
return true;
}
else
{
curr = curr->getNext();
}
}
return false;
}
LISTEntry * LIST::getHead() {
return head;
}
void LIST::setHead(LISTEntry * h) {
head = h;
}
LISTEntry * LIST::getTail() {
return tail;
}
void LIST::setTail(LISTEntry * t) {
tail = t;
}
LISTEntry * LIST::getCurr() {
return curr;
}
void LIST::setCurr(LISTEntry * c) {
curr = c;
}
bool LIST::isEmpty() {
return head == nullptr;
}
bool LIST::isFull() {
return (new(LISTEntry) == nullptr);
}
int main()
{
LIST * burger = new LIST();
burger->addList("meat");
burger->addList("pickles");
burger->addList("onions");
burger->addList("tomato");
burger->addList("cheese");
burger->addList("bun");
burger->iterateInitialize();
listPtr bgrIndex = burger->getCurr();
while (burger->iterate_hasNext())
{
cout << bgrIndex->getData().getKey() << " ";
bgrIndex = bgrIndex->getNext();
if (!(burger->iterate_hasNext()))
cout << endl;
}
dataType searchValue;
while (searchValue != "end")
{
cout << "Enter an item to look up in the list: " << endl;
cin >> searchValue;
if (burger->searchList(searchValue))
cout << "The item " << searchValue << " was found." << endl;
else
cout << "The item " << searchValue << " is not in the list." << endl;
}
delete burger;
return 0;
}

How to fix a type error when working with stack.top() in the <stack> library in c++

I'm trying to implement a function in my linked list that pushes the values of one list into a stack, and then pops off those values into another list.
The problem is, when I try to std::cout << x, the first stack's topmost element, I get this error:
c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\ostream:682:5: error: no type named 'type' in 'struct std::enable_if<false, std::basic_ostream<char>&>'
#include <iostream>
#include <cstddef>
#include <string>
#include <stack>
#include <vector>
using Item = std::string;
class List {
public:
class ListNode { //Changed this to public so I could access it. If this was in private how would I accomplish this?
public:
Item item;
ListNode * next;
ListNode(Item i, ListNode *n=nullptr) {
item = i;
next = n;
}
};
ListNode * head;
ListNode * tail; //Also in private, changed to public, so I could access it. This is the bottom boundry.
class iterator {
ListNode *node;
public:
iterator(ListNode *n = nullptr) {
node = n;
}
Item& getItem() { return node->item; }
void next() { node = node->next; }
bool end() { return node==nullptr; }
friend class List;
};
public:
List() {
// list is empty
head = nullptr;
tail = nullptr;
}
List(const List &other)
{
iterator o = other.begin();
while(!o.end())
{
append(o.getItem());
o.next();
}
}
bool empty() {
return head==nullptr;
}
// Only declared, here, implemented
// in List.cpp
void append(Item a);
bool remove (Item &copy);
void insertAfter(iterator, Item);
void removeAfter(iterator, Item&);
iterator begin() const {
return iterator(head);
}
};
void List::append(Item a) {
ListNode *node = new ListNode(a);
if ( head == nullptr ) {
// empty list
head = node;
tail = node;
} else {
tail->next = node;
tail = node;
}
}
bool List::remove(Item &copy)
{
if (!empty()) {
copy = head->item;
ListNode *tmp = head->next;
delete head;
head = tmp;
if (head==nullptr)
tail = nullptr;
return true;
}
return false;
}
void List::insertAfter(iterator it, Item item)
{
if (it.node == nullptr)
{
// insert at head
ListNode *tmp = new ListNode(item,head);
// if list is empty, set tail to new node
if (tail==nullptr) {
tail = tmp;
}
// set head to new node
head = tmp;
}
else
{
ListNode *tmp = new ListNode(item,it.node->next);
it.node->next = tmp;
// could be a new tail, if so update tail
if (tail==it.node) {
tail = tmp;
}
}
}
void List::removeAfter(iterator it, Item& item)
{
// emtpy list or at tail, just return
if (it.node == tail) return;
if (it.node == nullptr)
{
ListNode * tmp = head;
head = head->next;
delete tmp;
if (head==nullptr)
tail = nullptr;
}
else
{
ListNode *tmp = it.node->next;
it.node->next = tmp->next;
delete tmp;
// could be that it.node is the new nullptr
if (it.node->next == nullptr)
tail = it.node;
}
}
List reverse(const List &l)
{
List::iterator iter1 = l.begin();
std::stack<List::ListNode> first;
while(!(iter1.end())) {
first.push(iter1.getItem());
iter1.next();
}
List lthe3;
int z = first.size();
for (int i=0; i < z; ++i) {
List::ListNode x = first.top();
std::cout << x;
}
return l;
}
void printList(List &l) {
List::iterator i = l.begin();
while(!i.end()) {
std::cout << i.getItem() << ", ";
i.next();
}
std::cout << std::endl;
}
int main()
{
List l;
l.append("one");
l.append("two");
l.append("three");
l.append("four");
std::cout << "List in order: ";
printList(l);
List l2 = reverse(l);
std::cout << "List in reverse order: ";
printList(l2);
return 0;
}
I have no clue what I'm doing wrong, but I'm really close to finishing this function.
Is there any way I could get some feedback?

<LinkedList> Program doesn't give the correct answer

this is my lab and I work all of it. After long time of debugging, fixing errors, finally it can compile. But when it run, it didn't give me the correct answer. It just kept saying : did not find y (may be x was added) and it was 4 line with the same answer.
please look at my code and tell me why it didn't work.
Thanks a lot.
Here is my code:
LinkedList.h:
#ifndef _LINKED_LIST_
#define _LINKED_LIST_
#include <ostream>
class LinkedList
{
public:
LinkedList();
LinkedList(char ch);
LinkedList(const LinkedList& List);
~LinkedList();
void add(const char& ch);
bool find(char ch);
bool del(char ch);
friend std::ostream& operator<<(std::ostream& out, LinkedList& list);
private:
struct node
{
char item;
node * next;
};
node * head;
int size;
};
#endif // _LINKED_LIST_
Linkedlist.cpp
#include "linkedlist.h"
#include <iostream>
#include <fstream>
#include <cassert>
#include <cstring>
using namespace std;
LinkedList::LinkedList() : head(NULL)
{
}
LinkedList::LinkedList(char ch):head(NULL)
{
char currData;
currData = ch;
add(currData);
}
LinkedList::~LinkedList()
{
node * curr = head;
while(head)
{
curr = head->next;
delete head;
head = curr;
}
}
LinkedList::LinkedList(const LinkedList& List)
{
if(List.head == NULL)
head = NULL;
else
{
//copy first node
head = new node;
assert(head != NULL);
head->item = List.head->item;
//copy the rest of the list
node * destNode = head; //points to the last node in new list
node * srcNode = List.head->next; //points to node in aList
while(srcNode != NULL) //or while (srcNode)
{
destNode->next = new node;
assert(destNode->next != NULL); //check allocation
destNode = destNode->next;
destNode->item = srcNode->item;
srcNode = srcNode->next;
}
destNode->next = NULL;
}
}
ostream& operator<<(std::ostream& out, LinkedList& list)
{
while(list.head)
{
out << list.head->item << endl;
list.head = list.head->next;
}
return out;
}
void LinkedList::add(const char& ch)
{
node * prev = NULL;
node * curr = head;
while (curr != NULL && curr->item < ch)
{
prev = curr;
curr = curr->next;
}
if (curr && curr->item != ch)
{
node * newNode = new node;
newNode->item = ch;
newNode->next = NULL;
newNode->next = curr;
if (prev == NULL)
head = newNode;
else
prev->next = newNode;
size++;
}
}
bool LinkedList::del(char ch)
{
char a;
node * prev = NULL;
node * curr = head;
while (curr)
{
a = curr->item;
if ( a == ch)
{
if(!prev)
head = curr->next;
else
prev->next = curr->next;
delete curr;
size--;
return true;
}
prev = curr;
curr = curr->next;
}
return false;
}
bool LinkedList::find(char ch)
{
char a;
node * prev = NULL;
node * curr = head;
while (curr)
{
a = curr->item;
if ( a == ch)
{
return true;
}
prev = curr;
curr = curr->next;
}
return false;
}
app.cpp
#include <iostream>
#include "linkedlist.h"
using namespace std;
void find(LinkedList& list, char ch)
{
if (list.find(ch))
cout << "found ";
else
cout << "did not find ";
cout << ch << endl;
}
int main()
{
LinkedList list;
list.add('x');
list.add('y');
list.add('z');
cout << list;
find(list, 'y');
list.del('y');
cout << list;
find(list, 'y');
list.del('x');
cout << list;
find(list, 'y');
list.del('z');
cout << list;
find(list, 'y');
return 0;
}
Your add method doesn't work, if the item to insert goes at the end it is not inserted (because you are checking for curr == NULL before inserting). Since head is set to NULL when you create the list if(curr && ...) won't ever be true and no item will be inserted.

C++ Linked List Runtime Error: Unhandled Exception - Writing Location Violation

I am trying to build my own implementation of a linked list in C++. My code is compiling but apparently there is some issue with my pointers referring to invalid memory addresses.
Here is my implementation:
#include <iostream>
#include <string>
using namespace std;
class Node
{
private:
string _car;
Node* nextNode;
public:
void setCar(string car)
{
_car = car;
}
string getCar()
{
return _car;
}
void setNextNode(Node* node)
{
nextNode = node;
}
Node* getNextNode()
{
return nextNode;
}
};
Node* findLast(Node* node)
{
Node* nodeOut = NULL;
while (node->getNextNode() != NULL)
{
nodeOut = node->getNextNode();
}
return nodeOut;
}
string toString(Node* node)
{
string output = "";
while (node->getNextNode() != NULL)
{
output += node->getCar() + " ";
node = node->getNextNode();
}
return output;
}
int main()
{
char xit;
//ser head node to NULL
Node* headNode = NULL;
//create node 1
Node* node1 = new Node();
node1->setCar("Mercedes");
//create node 2
Node* node2 = new Node();
node2->setCar("BMW");
//set node links
headNode->setNextNode(node1);
node1->setNextNode(node1);
node2->setNextNode(node2);
headNode = node1;
Node* lastNode = findLast(headNode);
lastNode->setNextNode(NULL);
cout << toString(headNode) << endl;
//pause console
cin >> xit;
}
You need to relook at your code.
headNode = node1;
This assignment should be done before accesing any member function of the instance headNode.
Intially you have assigned NULL to this pointer.
After creating node1 you are setting to headNode that is invalid instance. This is the cause of crash.
Be ensure with your objective and then try to implement do some rough work on paper , make some diagram that way you would be more clear that what you are exactly trying to achive.
why setNextNode ??? i don't undeerstand what you wanted to achieve. be clear first.
As per my undertanding this code should be implemented like below..
#include <iostream>
#include <string>
using namespace std;
class Node
{
private:
string _car;
Node* nextNode;
public:
void setCar(string car)
{
_car = car;
}
string getCar()
{
return _car;
}
void setNextNode(Node* node)
{
nextNode = node;
}
Node* getNextNode()
{
return nextNode;
}
};
Node* findLast(Node* node)
{
Node* nodeOut = node->getNextNode();
while ( nodeOut->getNextNode()!= NULL)
{
nodeOut = nodeOut->getNextNode();
}
return nodeOut;
}
string toString(Node* node)
{
string output = "";
while (node != NULL)
{
output += node->getCar() + " ";
node = node->getNextNode();
}
return output;
}
int main()
{
char xit;
//ser head node to NULL
Node* headNode = NULL;
//create node 1
Node* node1 = new Node();
node1->setCar("Mercedes");
node1->setNextNode(NULL);//Make null to each next node pointer
headNode = node1; //assign the node1 as headNode
//create node 2
Node* node2 = new Node();
node2->setCar("BMW");
node2->setNextNode(NULL);
//set node links
node1->setNextNode(node2);
Node* lastNode = findLast(headNode);
lastNode->setNextNode(NULL);
cout << toString(headNode) << endl;
//pause console
cin >> xit;
}
Hope it would be useful for the beginner who implement ing the linklist in c++.
Reread this:
node1->setNextNode(node1);
node2->setNextNode(node2);
...and think about what you're doing here.
If you're going to write linked-list code, I'd advise at least looking at the interface for std::list. Right now, you're interface is at such a low level that you'd be at least as well off just manipulating pointers directly.
The cause of your actual error is:
headNode->setNextNode(node1);
headNode is still set to NULL, thus you're dereferencing a NULL pointer. As noted by Jerry, you're also calling having nodes point to themselves, which is not what you want.
It would be cleaner if you took the car as a constructor parameter.
When you allocate a new Node, the pointer nextNode is not initialized, it's just random junk. You will need to explicitly set it to NULL (probably in a constructor for Node).
Also, I assume you know that the standard C++ library has a linked list built in and you're just doing this for learning ;-)
Thanks for all the suggestions, here is my final code after major cleanup:
// LinkedListProject.cpp : main project file.
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace System;
using namespace std;
class Node
{
public:
Node()
:_car(""), _nextNode(NULL)
{
}
void SetCar(string car)
{
_car = car;
}
string GetCar()
{
return _car;
}
void SetNextNode(Node *node)
{
_nextNode = node;
}
Node * GetNextNode()
{
return _nextNode;
}
private:
string _car;
Node *_nextNode;
};
string GetData();
Node * AddNode(Node *firstNode, Node *newNode);
Node * DeleteNode(Node *firstNode, string nodeData);
void PrintNodes(Node *firstNode);
int main(int argc, char *argv[])
{
string command = "";
string data = "";
Node *firstNode = NULL;
do
{
cout << "Enter command: ";
cin >> command;
if(command == "add")
{
data = GetData();
Node *newNode = new Node();
newNode->SetCar(data);
firstNode = AddNode(firstNode, newNode);
}
else if(command == "delete")
{
data = GetData();
firstNode = DeleteNode(firstNode, data);
}
else if(command == "print")
{
PrintNodes(firstNode);
}
} while(command != "stop");
return 0;
}
string GetData()
{
string data = "";
cout << "Enter data: ";
cin >> data;
return data;
}
Node * AddNode(Node *firstNode, Node *newNode)
{
//add new node to front of queue
newNode->SetNextNode(firstNode);
firstNode = newNode;
return firstNode;
}
Node * DeleteNode(Node *firstNode, string nodeData)
{
Node *currentNode = firstNode;
Node *nodeToDelete = NULL;
if (firstNode != NULL)
{
//check first node
if(firstNode->GetCar() == nodeData)
{
nodeToDelete = firstNode;
firstNode = firstNode->GetNextNode();
}
else //check other nodes
{
while (currentNode->GetNextNode() != NULL &&
currentNode->GetNextNode()->GetCar() != nodeData)
{
currentNode = currentNode->GetNextNode();
}
if (currentNode->GetNextNode() != NULL &&
currentNode->GetNextNode()->GetCar() == nodeData)
{
nodeToDelete = currentNode->GetNextNode();
currentNode->SetNextNode(currentNode->GetNextNode()->GetNextNode());
}
}
if(nodeToDelete != NULL)
{
delete nodeToDelete;
}
}
return firstNode;
}
void PrintNodes(Node *firstNode)
{
Node *currentNode = firstNode;
while(currentNode != NULL)
{
cout << currentNode->GetCar() << endl;
currentNode = currentNode->GetNextNode();
}
}