Manipulating a LinkedList - c++

So, i'm attempting to create a LinkedList using dummy nodes, however, when running through the functionality of each function, I get errors when it comes to the overloaded brackets operator, which is suppose to returns the item at the given index: list[n] Though i'm not exactly sure what may be wrong with the code.
I've spent hours looking at why i'm getting errors when trying to run each function. Sorry the question is so vague, but I feel as though I'm missing something, or something is coded poorly that I can't seem to detect.
Here's the code.
#ifndef _LINKED_LIST_GUARD
#define _LINKED_LIST_GUARD 1
#include <iostream>
#include <string>
#include "ListInterface.h"
template <typename T>
class LinkedList : public ListInterface<T>
{
private:
// Created in order to keep track of list size
int count = 0;
public:
// Default contructor
LinkedList()
{
_head = new Node;
_tail = new Node;
_head->_next = _tail;
}
private:
//
// Private node class to facilitate linked list
//
class Node
{
public:
T _data;
Node* _next;
// Constructor: default
Node(T d = T{}, Node* n = nullptr) : _data(d), _next(n) {}
~Node() { _next = nullptr; }
};
//
// Prevent copying and assigning
//
LinkedList(const LinkedList& rhs) {}
const LinkedList& operator=(const LinkedList& rhs) {}
public:
//
// LinkedList instance variables; we use dummy head and tail nodes in this implementation
unsigned _size;
Node* _head;
Node* _tail;
// Returns the value that the head points to, sinze _head is a dummy node.
T& first() const
{
return _head->_next->_data;
}
// Adds an item to the LEFT side of the linked list
void push(const T& x)
{
// Creates a new node with the user input data.
Node* current = new Node;
current->_data = x;
//Sets the node next to this node to be equal to what the head was previously pointing to.
current->_next = _head->_next;
//Finally, sets the user created node to be the first node within the list, then adds to the count.
_head->_next = current;
count++;
}
T& operator[](unsigned n)
{
//Starts at position one, since this would never return zero.
int position = 1;
//If the user input value is greater than the amount of items in the list
if (n > count)
{
//Throws a string exception.
throw std::string("Invalid Positon");
}
//Loops through the LinkedList
for (Node* current = _head->_next; current != _tail; current = current->_next)
{
//If the node at the current position is equal to the user input value
if (position == n)
{
//returns the current positions data, since it would be the data at user input position
return current->_data;
}
// If not, adds to position, and checks again.
position++;
}
return _head->_data;
}
//uses the pop function over and over until the list is empty, leaving only the _head and _tail dummy nodes.
void clear()
{
do
{
pop();
} while (count != 0);
}
bool contains(const T& target) const
{
//Loops through the LinkedList.
for (Node* current = _head->_next; current != _tail; current = current->_next)
{
//While looping, if the current node's data is equal to the user input target data, returns true.
if (current->_data == target)
return true;
}
// If it never finds anything, returns false.
return false;
}
bool pop()
{
//The head would only point to the tail if the list were empty.
if (_head->_next == _tail)
{
std::cout << "Unable to pop, list is empty ";
return false;
}
if (_head->_next != _tail)
{
//Creates a new node that has its values equal to the first node within the list.
Node* deleteNode = _head->_next;
//Sets the front of the node to be equal to the first node after the heads next node.
_head->_next = _head->_next->_next;
//Finally, deletes the node that deleteNode points to, and reduces the count
delete deleteNode;
count--;
return true;
}
//If the pop is unsucessful, returns false.
return false;
}
T& last() const
{
// There can't be a last value if the list is empty, so throws an exception
if (_head->_next == _tail)
{
//throw std::string("List is empty.");
std::cout << "List is empty";
}
if (_head->_next != _tail)
{
for (Node* current = _head->_next; current != _tail; current = current->_next)
{
//Loops through the list until the node that points to tail is located, then returns it.
if (current->_next == _tail)
return current->_data;
}
}
return _tail->_data;
}
int search(const T& target = T{}) const
{
//Once again, starts a position 1, since 0 is not a valid position
int position = 1;
//Loops through list
for (Node* current = _head->_next; current != _tail; current = current->_next)
{
//While looping, if the current node's data is equal to the user input target, returns the position
if (current->_data == target)
{
return position;
}
//Otherwise increases the position, and loops again.
position++;
}
// If it is unable to find the target in the list, returns -1.
return -1;
}
bool isEmpty() const
{
// If the _head points to the tail, that means there are no nodes in between, meaning it is empty.
if (_head->_next == _tail)
{
return true;
}
//Otherwise, return false.
return false;
}
unsigned size() const
{
//Returns the count, which is altered by remove, pop, and push.
return count;
}
bool remove(const T& target)
{
//Creates a node, whose data is initially null.
Node* deleteNode = nullptr;
//Creates another node to follow behind it.
Node* trailer = _head;
//Finally, creates a node that is to represent the current node that is being pointed at.
Node* current = _head->_next;
//Loops
while (current != _tail && current->_data != target)
{
trailer = current;
current = current->_next;
}
//If the data is current is equal to the target
if (current->_data == target)
{
// Sets the node to be deleted to current
deleteNode = current;
//then, sets current to the node right after.
current = current->_next;
//Finally, sets the trailer to the current node, since it's no longer needed
trailer->_next = current;
//Then, simply delets the node, and subtracts from the count.
delete deleteNode;
count--;
return true;
}
else
// If it is unable to find the targeted value, returns false.
std::cout << "unable to remove, item not in list" << std::endl;
return false;
}
//
// Internal consistency check
//
public:
virtual bool check() const
{
bool sizeConsistent = isSizeConsistent();
bool headTailConsistent = isEndConsistent();
if (!sizeConsistent) std::cerr << "Size inconsistent" << std::endl;
if (!headTailConsistent) std::cerr << "Head / Tail inconsistent" << std::endl;
return sizeConsistent && headTailConsistent;
}
//
// Stated size is accurate to the entire list
//
bool isSizeConsistent() const
{
int count = 0;
for (Node* current = _head->_next; current != _tail; current = current->_next)
{
count++;
}
return size() == count;
}
//
// Checks that the head and tail are defaulted properly and the
// respective next pointers are appropriate.
//
bool isEndConsistent() const
{
if (_head->_data != T{}) return false;
if (_tail->_data != T{}) return false;
if (_head->_next == nullptr) return false;
if (_tail->_next != nullptr) return false;
if (isEmpty() && _head->_next != _tail) return false;
if (!isEmpty() && _head->_next == _tail) return false;
return true;
}
};
#endif
Any overall suggestions for a fix?

Related

Add integer to each item of unordered linked list

I want to write a function that adds an integer (passed as an argument to the function) to each item in the unordered linked list. Here is the complete program.
#include <iostream>
using namespace std;
//creates a node class
class Node {
//defines data, and next as a pointer.
private:
int data; //data in the beginning node
Node *next; //pointer to the next node
public:
Node(int initdata) {
data = initdata; //the initialized data is set as the head
next = NULL; //the next node is set as NULL, as there is no next node yet.
}
int getData() { //function that return data of a given node.
return data;
}
Node *getNext() { // pointer that gets the next node
return next;
}
void setData(int newData) { // sets data in node
data = newData;
}
void setNext(Node *newnext) {
next = newnext;
}
};
// creates unorderedlist that points to the head of the linked list
class UnorderedList {
public:
Node *head;
UnorderedList() { // makes the head node equal to null
head = NULL;
}
bool isEmpty() { // the head node is empty if it is null
return head == NULL;
}
void add(int item) { //cerates a "temp" pointer that adds the new node to the head of the list
Node *temp = new Node(item);
temp->setNext(head);
head = temp;
}
int size() { //cereates a "current" pointer that iterates through the list until it reaches null
Node *current = head;
int count = 0;
while (current != NULL) {
count++;
current = current->getNext();
}
return count;
}
// creates "current" pointer that iterates through the list
// untli it finds the item being searched for, and returns a boolean value
bool search(int item) {
Node *current = head;
while (current != NULL) {
if (current->getData() == item) {
return true;
} else {
current = current->getNext();
}
}
return false;
}
void addInteger(int item){
Node *current = head;
while (current != NULL) {
current->getData() = current->getData() + item;
}
}
// uses current and previous pointer to iterate through the lists
// finds the items that is searched for, and removes it
void remove(int item) {
Node *current = head;
Node *previous = NULL;
bool found = false;
while (!found) {
if (current->getData() == item) {
found = true;
} else {
previous = current;
current = current->getNext();
}
}
if (previous == NULL) {
head = current->getNext();
} else {
previous->setNext(current->getNext());
}
}
friend ostream& operator<<(ostream& os, const UnorderedList& ol);
};
ostream& operator<<(ostream& os, const UnorderedList& ol) {
Node *current = ol.head;
while (current != NULL) {
os<<current->getData()<<endl;
current = current->getNext();
}
return os;
}
int main() {
UnorderedList mylist;
mylist.add(1);
mylist.add(2);
mylist.add(3);
mylist.add(4);
mylist.add(5);
mylist.add(6);
cout<<"MY LIST: "<<endl<<mylist;
mylist.addInteger(5);
cout<<"=========================================================\n";
cout<<"After adding 5 to each element, the list now is\n";
cout<<"MY LIST: "<<endl<<mylist;
return 0;
}
Now the program shows an error in the following function from the program above regarding the assignment operation.
void addInteger(int item){
Node *current = head;
while (current != NULL) {
current->getData() = current->getData() + item;
}
}
How can I add a number to each element of the linked list?
Any help is appreciated.
You probably want something like the following:
current->setData(current->getData() + item);
Note that now you are retrieving a return value in the left-hand side, then trying to assign to it. This is what your compiler is telling you, presumably.

Counting occurrence in singly linked list by nodes

I am writing a simple app that gets a list and saves the objects as nodes in a singly linked list and we can add(), remove(), copy(), etc. each node depending on the given data set. each node has a char value which is our data and an int count which counts the occurrence of the related char.
e.g. for a list like
a, a, b, b, c, a
there would be three nodes (since there are three different characters) which are:
[a,3,*next] -> [b,2,*next] -> [c,1,*next] -> nullptr
bool isAvailable() checks if the data is already in the list or not.
Q: When inserting a data there are two options:
The data has not been entered: so we have to create a newNodewith the given data, count=1and *next=NULL.
The data is already entered: so we have to count++ the node that has the same data.
I know if the given data is available or not, but how can I point to the node with same data?
Here's the code:
#include "stdafx.h"
#include<iostream>
using namespace std;
class Snode
{
public:
char data;
int count;
Snode *next;
Snode(char d, int c)
{
data = d;
count = c;
next = NULL;
}
};
class set
{
private:
Snode *head;
public:
set()
{
head = NULL;
tail = NULL;
}
~set();
void insert(char value);
bool isAvailable(char value);
};
set::~set()
{
Snode *t = head;
while (t != NULL)
{
head = head->next;
delete t;
}
}
bool set::isAvailable(char value)
{
Snode *floatingNode = new Snode(char d, int c);
while(floatingNode != NULL)
{
return (value == floatingNode);
floatingNode->next = floatingNode;
}
}
void set::insert(char value)
{
Snode *newNode = new Snode(char d, int c);
data = value;
if (head == NULL)
{
newNode->next = NULL;
head = newNode;
newNode->count++;
}
else
{
if(isAvailable)
{
//IDK what should i do here +_+
}
else
{
tail->next= newNode;
newNode->next = NULL;
tail = newNode;
}
}
}
I know if the given data is available or not, but how can I point to the node with same data?
You'll need to start at the head of the list and iterate along the list by following the next pointers until you find the node with the same data value. Once you've done that, you have your pointer to the node with the same data.
Some other notes for you:
bool set::isAvailable(char value)
{
Snode *floatingNode = new Snode(char d, int c);
while(floatingNode != NULL)
{
return (value == floatingNode);
floatingNode->next = floatingNode;
}
}
Why is this function allocating a new Snode? There's no reason for it to do that, just initialize the floatingNode pointer to point to head instead.
This function always returns after looking at only the first node in the linked list -- which is not the behavior you want. Instead, it should return true only if (value == floatingNode); otherwise it should stay inside the while-loop so that it can go on to look at the subsequent nodes as well. Only after it drops out of the while-loop (because floatingNode finally becomes NULL) should it return false.
If you were to modify isAvailable() slightly so that instead of returning true or false, it returned either floatingPointer or NULL, you'd have your mechanism for finding a pointer to the node with the matching data.
e.g.:
// Should return either a pointer to the Snode with data==value,
// or NULL if no such Snode is present in the list
Snode * set::getNodeWithValueOrNullIfNotFound(char value) const
{
[...]
}
void set::insert(char value)
{
Snode * theNode = getNodeWithValueOrNullIfNotFound(value);
if (theNode != NULL)
{
theNode->count++;
}
else
{
[create a new Snode and insert it]
}
}
You had a lot of problems in your code, lets see what are they:
First of all, Snode doesn't need to be a class, rather you can go with a simple strcut; since we need everything public.(not a mistake, but good practice)
You could simple initialize count = 1 and next = nullptr, so that no need of initializing them throw constructor. The only element that need to be initialized through constructor is Snod's data.
Since c++11 you can use keyword nullptr instead of NULL, which denotes the pointer literal.
Member function bool set::isAvailable(char value) will not work as you think. Here you have unnecessarily created a new Snode and cheacking whether it points to nullptr which doesn't allow you to even enter the loop. BTW what you have written in the loop also wrong. What do you mean by return (value == floatingNode); ? floatingNode is a Snode by type; not a char.
Hear is the correct implementation. Since we don't wanna overwrite the head, will create a Node* pointer and assign head to it. Then iterate through list until you find a match. If not found, we will reach the end of the isAvailable() and return false.
inline bool isAvailable(const char& value)
{
Node *findPos = head;
while(findPos != nullptr)
{
if(findPos -> data == value) return true;
else findPos = findPos->next_node;
}
return false;
}
In void set::insert(char value), your logic is correct, but implementation is wrong. Following is the correct implementation.(Hope the comments will help you to understand.
void insert(const char& value)
{
if(head == nullptr) // first case
{
Node *newNode = new Node(value);
newNode->next_node = head;
head = newNode;
}
else if(isAvailable(value)) // if node available
{
Node *temp = head;
while(temp->data != value) // find the node
temp = temp->next_node;
temp->count += 1; // and count it by 1
}
else // all new nodes
{
Node *temp = head;
while(temp->next_node != nullptr) // to find the null point (end of list)
temp = temp->next_node;
temp = temp->next_node = new Node(value); // create a node and assign there
}
}
Your destructor will not delete all what you created. It will be UB, since your are deleting newly created Snode t ( i.e, Snode *t = head;). The correct implementation is as bellow.(un-comment the debugging msg to understand.)
~set()
{
Node* temp = head;
while( temp != nullptr )
{
Node* next = temp->next_node;
//std::cout << "deleting \t" << temp->data << std::endl;
delete temp;
temp = next;
}
head = nullptr;
}
Last but not least, the naming (set) what you have here and what the code exactly doing are both different. This looks more like a simple linked list with no duplicates. This is however okay, in order to play around with pointers and list.
To make the code or iteration more efficient, you could do something like follows. In the isAvailable(), in case of value match/ if you found a node, you could simply increment its count as well. Then in insert(), you can think of, if node is not available part.
Hope this was helpful. See a DEMO
#include <iostream>
// since you wanna have all of Node in public, declare as struct
struct Node
{
char data;
int count = 1;
Node* next_node = nullptr;
Node(const char& a) // create a constrcor which will initilize data
: data(a) {} // at the time of Node creation
};
class set
{
private:
Node *head; // need only head, if it's a simple list
public:
set() :head(nullptr) {} // constructor set it to nullptr
~set()
{
Node* temp = head;
while( temp != nullptr )
{
Node* next = temp->next_node;
//std::cout << "deleting \t" << temp->data << std::endl;
delete temp;
temp = next;
}
head = nullptr;
}
inline bool isAvailable(const char& value)
{
Node *findPos = head;
while(findPos != nullptr)
{
if(findPos -> data == value) return true;
else findPos = findPos->next_node;
}
return false;
}
void insert(const char& value)
{
if(head == nullptr) // first case
{
Node *newNode = new Node(value);
newNode->next_node = head;
head = newNode;
}
else if(isAvailable(value)) // if node available
{
Node *temp = head;
while(temp->data != value) // find the node
temp = temp->next_node;
temp->count += 1; // and count it by 1
}
else // all new nodes
{
Node *temp = head;
while(temp->next_node != nullptr) // to find the null point (end of list)
temp = temp->next_node;
temp = temp->next_node = new Node(value);
}
}
void print() const // just to print
{
Node *temp = head;
while(temp != nullptr)
{
std::cout << temp->data << " " << temp->count << "\n";
temp = temp->next_node;
}
}
};
int main()
{
::set mySet;
mySet.insert('a');
mySet.insert('a');
mySet.insert('b');
mySet.insert('b');
mySet.insert('c');
mySet.insert('a');
mySet.print();
return 0;
}

Binary Tree Destructor Issue

I have recently been implementing a binary tree in my free-time and I do believe I have it working correctly. However, I have ran into a mysterious segmentation fault when running a simple test. Here is my implementation of the binary search tree:
//the binary search tree class
template <class M>
class BS_Tree {
private:
//node structure
struct Node {
M data; //the key of the node
Node* left,* right,* parent; //node pointers to left right and parent
Node(M key, Node* p) //parameterized node constructor
: data(key), left(nullptr), right(nullptr), parent(p) {}
};
Node* root; //the root node of the binary search tree
bool is_left(Node* n) {return n->parent->left == n;} //utility function
void destroy(Node* n); //used for tree destruction
void duplicate(Node* const &, Node* &); //used for copy construction
public:
BS_Tree() {root = nullptr;} //constructor
~BS_Tree() {destroy(root);} //destructor
BS_Tree(const BS_Tree &); //copy constructor
BS_Tree &operator=(const BS_Tree &); //copy assignment operator
Node* find(M key); //find function
void insert(M); //insert function
void erase(Node*); //erase function
Node* get_root() {return root;}
};
//destroy function used for tree destruction
template <class M>
void BS_Tree<M>::destroy(Node* n) {
if(n) { //recursively erase the tree
if(n->left) destroy(n->left);
if(n->right) destroy(n->right);
delete n;
}
}
//duplicate function used for copy construction
template <class M>
void BS_Tree<M>::duplicate(Node* const &one, Node* &two) {
if(!one) two = nullptr;
else { //recursively duplicate the tree
two = new Node(one->data);
duplicate(one->left, two->left);
duplicate(one->right, two->right);
}
}
//copy constructor
template <class M>
BS_Tree<M>::BS_Tree(const BS_Tree &b) {
if(!b.root) root = nullptr; //update root
else duplicate(b.root, this->root); //call duplicate function
}
//copy assignment operator
template <class M>
BS_Tree<M> &BS_Tree<M>::operator=(const BS_Tree &b) {
if(!b.root) root = nullptr; //update root
else { //destroy current tree and duplicate source tree
this->~BS_Tree();
duplicate(b.root, this->root);
}
}
//function to find a key and return a pointer
template <class M>
typename BS_Tree<M>::Node* BS_Tree<M>::find(M key) {
Node* i = root; //create an index
while(i) {
//try to find the key
if (i->data == key) return i;
if(i->data > key) i = i->left;
else i = i->right;
}
//return a pointer to the key, nullptr if not found
return i;
}
//function to insert a new node
template <class M>
void BS_Tree<M>::insert(M key) {
if(!root) { //if no tree, make new node the root
root = new Node(key, nullptr);
return;
}
Node* i = root; //create an index
while(true) { //find insertion point and insert new node
if(i->data > key) {
if(!i->left) {
i->left = new Node(key, i);
return;
}
else i = i->left;
}
if(i->data <= key) {
if(!i->right) {
i->right = new Node(key, i);
return;
}
else i = i->right;
}
}
}
//Function to erase a node
template <class M>
void BS_Tree<M>::erase(Node* n) {
if(n) {
//no children case
if(!n->left && !n->right) {
if(root == n) root = nullptr; //if node is root, make root null
else { //if node is a child, update parent's children
if(is_left(n)) n->parent->left = nullptr;
else n->parent->right = nullptr;
}
delete n; //erase the node
return;
}
//one child cases
if(!n->left) {
if(n == root){ //if node is root, update root
root = n->right;
n->right->parent = nullptr;
} else { //if node is a child, update parent's children and nodes parent
if(is_left(n)) n->parent->left = n->right;
else n->parent->right = n->right;
n->right->parent = n->parent;
}
delete n; //erase the node
return;
}
if(!n->right) {
if(n == root){ //if node is root, update root
root = n->left;
n->left->parent = nullptr;
} else { //if node is a child, update parent's children and nodes parent
if(is_left(n)) n->parent->left = n->left;
else n->parent->right = n->left;
n->left->parent = n->parent;
}
delete n; //erase the node
return;
}
//two children case
else {
Node* i = n; //create an index
i = i->right; //find successor
while(i->left) i = i->left;
n->data = i->data; //set nodes data to successor's data
if(is_left(i)) i->parent->left = i->right; //update successor's parent and its child
else i->parent->right = i->right;
if(i->right) i->right->parent = i->parent;
delete i; //erase successor node
return;
}
}
}
As for the test, it is a simple linear vector element insert vs time performance check. It takes a max number of elements and an increment of elements for each test. It then runs the test for each increment and times the insertions. It works fine for the first few iterations (i.e n = 10000, 20000, 30000, 40000), but then I reach a segmentation fault after insertion when the 50000 element tree destructor is called. It is hinting at the line:
if(n->right) destroy(n->right);
I understand that I am doing an incremental insertion so all of my elements are on the right side of the tree, but I am having a hard time figuring out where I went wrong and why it only messes up on this iteration.
My test implematation:
int main(){
//number of elements, max test elements, and test increment
int n, t = 100000, i = 10000;
//run the test a number of times
for(n = i; n <= t; n += i) {
//get an incremental vector of size n
std::vector<int> test(n);
std::iota(std::begin(test), std::end(test), 0);
//run the insert test print the time interval
BS_Tree<int> m;
auto ibs_start = clock();
for(auto i : test){
m.insert(i);
}
auto ibs_stop = clock();
std::cout << n << ' ' << (ibs_stop - ibs_start)/double(CLOCKS_PER_SEC)*1000 << "\n";
}
return 0;
}
If someone could help I would greatly appreciate it!
Edit: Could it simply be my stack overflowing due to the destroy function having to store all of the elements before reaching the bottom and actually deleting anything?
If it works for some number of items (40000), but not for a larger, it might be a stackoverflow. Looking at the code also confirms this: destroying is recursive, and in the case of extremely unbalanced tree, it means as many stack frames as many nodes.
The question is how this could be solved without recursion? This is not so obvious, as there are two recursive calls. My solution is to traverse the binary tree, post-order, and delete leaf nodes.
Here is a solution -- an iterative version of the destroy method:
//destroy function used for tree destruction
template <class M>
void BS_Tree<M>::destroy(Node* n) {
if (n) { // iteratively erase the tree
Node* c = n; // current node
while (true) {
if (c->right) {
// it has right child, descend to it
c = c->right;
continue;
}
if (c->left) {
// it has left child, descend to it
c = c->left;
continue;
}
// this node has no (more) children, destroy it
Node* p = c->parent;
if (p) {
// update its parent to no longer point to it
if (p->right == c) p->right = NULL; // we came from left
else if (p->left == c) p->left = NULL; // we came from left
}
// destroy the node
delete c;
// go back to its parent
c = p;
if (c == n) {
// where at back at start node, stop
delete n;
break;
}
}
}
}
Update: I have tested my solution, and made some small changes.

linked list function c++ "which is of non-class type 'int'"?

below is the problem in one of the header of a code:
#include <iostream>
#include <fstream>
#include <string>
#include "extPersonType.h"
#include "orderedLinkedList.h"
using namespace std;
class addressBookType: public orderedLinkedListType
{
public:
void print() const;
void printNameInTheMonth(int month);
void printInfoOf(string lName);
void printNamesWithStatus(string status);
void printNamesBetweenLastNames(string last1,
string last2);
void insertNode(const extPersonType& eP);
void searchName(string lName);
void saveData(ofstream&);
addressBookType();
private:
nodeType* searchList(string lName);
};
void addressBookType::print() const
{
nodeType* current = first;
while (current != NULL)
{
current->info.printInfo();
cout << endl;
current= current->link;
}
}
void addressBookType::printNameInTheMonth(int month)
{
nodeType* current = first;
while(current != NULL)
{
if (current->info.isMonth(month))
{
current->info.print();
cout << endl;
}
current = current->link;
}
}
void addressBookType::printInfoOf(string lName)
{
nodeType* location = searchList(lName);
if (location != NULL)
location->info.printInfo();
else
cout << lName << " is not in address book." << endl;
}
void addressBookType::printNamesWithStatus(string status)
{
nodeType* current = first;
while (current != NULL)
{
if (current->info.isStatus(status))
{
current->info.print();
cout << endl;
}
current = current->link;
}
}
void addressBookType::printNamesBetweenLastNames(string last1,
string last2)
{
string lName;
nodeType* current = first;
while(current != NULL)
{
lName = current->info.getLastName();
if (last1 <= lName && lName <= last2)
{
current->info.print();
cout << endl;
}
current = current->link;
}
}
void addressBookType::insertNode(const extPersonType& eP)
{
orderedLinkedListType::insertNode(eP);
}
void addressBookType::searchName(string lName)
{
nodeType* location = searchList(lName);
if (location != NULL)
cout << lName << " is in the address book" << endl;
else
cout << lName << " is not in the address book" << endl;
}
nodeType* addressBookType::searchList(string lName)
{
nodeType* current = first;
bool found = false;
while (current != NULL)
{
if (current->info.isLastName(lName))
{
found = true;
break;
}
current = current->link;
}
return current;
}
void addressBookType::saveData(ofstream& outFile)
{
string firstN;
string lastN;
int month;
int day;
int year;
string street;
string city;
string state;
string zip;
string phone;
string pStatus;
nodeType* current = first;
while (current != NULL)
{
current->info.getDOB(month, day, year);
current->info.getAddress(street,city,state,zip);
current->info.getPhoneNumber();
current->info.getStatus();
outFile << current->info.getFirstName() << " "
<< current->info.getLastName() << endl;
outFile << month << " " << day << " " << year << endl;
outFile << street << endl << city << endl << state << endl << zip
<< endl;
outFile << current->info.getPhoneNumber() << endl
<< current->info.getStatus() << endl;
current = current->link;
}
}
addressBookType::addressBookType()
{
}
these are the two headers file that i suspect that has a problem with it but they have NO ERRORS at all
#include <iostream>
#include "linkedList.h"
using namespace std;
class orderedLinkedListType: public linkedListType
{
public:
bool search(const int& searchItem) const;
//Function to determine whether searchItem is in the list
//Postcondition: Returns true if searchItem is in the list;
// otherwise, the value false is returned.
void insertNode(const int& newItem);
//Function to insert newItem in the list
//Postcondition: first points to the new list and
// newItem is inserted at the proper place in the list
void deleteNode(const int& deleteItem);
//Function to delete deleteItem from the list
//Postcondition: If found, then the node containing the
// deleteItem is deleted from the list;
// first points to the first node of
// the new list
// If deleteItem is not in the list,
// an appropriate message is printed
void printListReverse() const;
//This function prints the list in reverse order
//Because the original list is in ascending order, the
//elements will be printed in descending order
private:
void reversePrint(nodeType *current) const;
//This function is called by the public member
//function to print the list in reverse order
};
bool orderedLinkedListType::search(const int& searchItem) const
{
bool found = false;
nodeType *current; //pointer to traverse the list
current = first; //start the search at the first node
while (current != NULL && !found)
if (current->info >= searchItem)
found = true;
else
current = current->link;
if (found)
found = (current->info == searchItem); //test for equality
return found;
}//end search
void orderedLinkedListType::insertNode(const int& newitem)
{
nodeType *current; //pointer to traverse the list
nodeType *trailCurrent; //pointer just before current
nodeType *newNode; //pointer to create a node
bool found;
newNode = new nodeType; //create the node
assert(newNode != NULL);
newNode->info = newitem; //store newitem in the node
newNode->link = NULL; //set the link field of the node
//to NULL
if (first == NULL) //Case 1
{
first = newNode;
count++;
}
else
{
current = first;
found = false;
while (current != NULL && !found) //search the list
if (current->info >= newitem)
found = true;
else
{
trailCurrent = current;
current = current->link;
}
if (current == first) //Case 2
{
newNode->link = first;
first = newNode;
count++;
}
else //Case 3
{
trailCurrent->link = newNode;
newNode->link = current;
count++;
}
}//end else
}//end insertNode
void orderedLinkedListType::deleteNode(const int& deleteItem)
{
nodeType *current; //pointer to traverse the list
nodeType *trailCurrent; //pointer just before current
bool found;
if (first == NULL) //Case 1
cout << "Cannot delete from an empty list." << endl;
else
{
current = first;
found = false;
while (current != NULL && !found) //search the list
if (current->info >= deleteItem)
found = true;
else
{
trailCurrent = current;
current = current->link;
}
if (current == NULL) //Case 4
cout << "The item to be deleted is not in the "
<< "list." << endl;
else
if (current->info == deleteItem) //item to be
//deleted is in the list
{
if (first == current) //Case 2
{
first = first->link;
delete current;
}
else //Case 3
{
trailCurrent->link = current->link;
delete current;
}
count--;
}
else //Case 4
cout << "Item to be deleted is not in the "
<< "list." << endl;
}
} //end deleteNode
void orderedLinkedListType::reversePrint
(nodeType *current) const
{
if (current != NULL)
{
reversePrint(current->link); //print the tail
cout << current->info << " "; //print the node
}
}
void orderedLinkedListType::printListReverse() const
{
reversePrint(first);
cout << endl;
}
this is the second one:
#include <iostream>
#include <cassert>
#include <assert.h>
using namespace std;
//Definition of the node
struct nodeType
{
int info;
nodeType *link;
};
class linkedListType
{
public:
const linkedListType& operator=
(const linkedListType&);
//Overload the assignment operator.
void initializeList();
//Initialize the list to an empty state.
//Postcondition: first = NULL, last = NULL, count = 0;
bool isEmptyList() const;
//Function to determine whether the list is empty.
//Postcondition: Returns true if the list is empty,
// otherwise it returns false.
void print() const;
//Function to output the data contained in each node.
//Postcondition: none
int length() const;
//Function to return the number of nodes in the list.
//Postcondition: The value of count is returned.
void destroyList();
//Function to delete all the nodes from the list.
//Postcondition: first = NULL, last = NULL, count = 0;
int front() const;
//Function to return the first element of the list.
//Precondition: The list must exist and must not be
// empty.
//Postcondition: If the list is empty, the program
// terminates; otherwise, the first
// element of the list is returned.
int back()const;
//Function to return the last element of the list.
//Precondition: The list must exist and must not be
// empty.
//Postcondition: If the list is empty, the program
// terminates; otherwise, the last
// element of the list is returned.
bool search(const int& searchItem) const;
//Function to determine whether searchItem is in the list.
//Postcondition: Returns true if searchItem is in the
// list, otherwise the value false is
// returned.
void insertFirst(const int& newItem);
//Function to insert newItem at the beginning of the list.
//Postcondition: first points to the new list, newItem is
// inserted at the beginning of the list,
// last points to the last node in the list,
// and count is incremented by 1.
void insertLast(const int& newItem);
//Function to insert newItem at the end of the list.
//Postcondition: first points to the new list, newItem
// is inserted at the end of the list,
// last points to the last node in the list,
// and count is incremented by 1.
void deleteNode(const int& deleteItem);
//Function to delete deleteItem from the list.
//Postcondition: If found, the node containing
// deleteItem is deleted from the list.
// first points to the first node, last
// points to the last node of the updated
// list, and count is decremented by 1.
linkedListType();
//default constructor
//Initializes the list to an empty state.
//Postcondition: first = NULL, last = NULL, count = 0;
linkedListType(const linkedListType& otherList);
//copy constructor
~linkedListType();
//destructor
//Deletes all the nodes from the list.
//Postcondition: The list object is destroyed.
protected:
int count; //variable to store the number of
//elements in the list
nodeType *first; //pointer to the first node of the list
nodeType *last; //pointer to the last node of the list
private:
void copyList(const linkedListType& otherList);
//Function to make a copy of otherList.
//Postcondition: A copy of otherList is created and
// assigned to this list.
};
bool linkedListType::isEmptyList() const
{
return(first == NULL);
}
linkedListType::linkedListType() //default constructor
{
first = NULL;
last = NULL;
count = 0;
}
void linkedListType::destroyList()
{
nodeType *temp; //pointer to deallocate the memory
//occupied by the node
while (first != NULL) //while there are nodes in the list
{
temp = first; //set temp to the current node
first = first->link; //advance first to the next node
delete temp; //deallocate the memory occupied by temp
}
last = NULL; //initialize last to NULL; first has already
//been set to NULL by the while loop
count = 0;
}
void linkedListType::initializeList()
{
destroyList(); //if the list has any nodes, delete them
}
void linkedListType::print() const
{
nodeType *current; //pointer to traverse the list
current = first; //set current so that it points to
//the first node
while (current != NULL) //while more data to print
{
cout << current->info << " ";
current = current->link;
}
}//end print
int linkedListType::length() const
{
return count;
} //end length
int linkedListType::front() const
{
assert(last != NULL);
return first->info; //return the info of the first node
}//end front
int linkedListType::back() const
{
assert(last != NULL);
return last->info; //return the info of the first node
}//end back
bool linkedListType::search(const int& searchItem) const
{
nodeType *current; //pointer to traverse the list
bool found = false;
current = first; //set current to point to the first
//node in the list
while (current != NULL && !found) //search the list
if (current->info == searchItem) //searchItem is found
found = true;
else
current = current->link; //make current point to
//the next node
return found;
}//end search
void linkedListType::insertFirst(const int& newItem)
{
nodeType *newNode; //pointer to create the new node
newNode = new nodeType; //create the new node
assert(newNode != NULL); //if unable to allocate memory,
//terminate the program
newNode->info = newItem; //store the new item in the node
newNode->link = first; //insert newNode before first
first = newNode; //make first point to the
//actual first node
count++; //increment count
if (last == NULL) //if the list was empty, newNode is also
//the last node in the list
last = newNode;
}//end insertFirst
void linkedListType::insertLast(const int& newItem)
{
nodeType *newNode; //pointer to create the new node
newNode = new nodeType; //create the new node
assert(newNode != NULL); //if unable to allocate memory,
//terminate the program
newNode->info = newItem; //store the new item in the node
newNode->link = NULL; //set the link field of newNode
//to NULL
if (first == NULL) //if the list is empty, newNode is
//both the first and last node
{
first = newNode;
last = newNode;
count++; //increment count
}
else //the list is not empty, insert newNode after last
{
last->link = newNode; //insert newNode after last
last = newNode; //make last point to the actual last node
count++; //increment count
}
}//end insertLast
void linkedListType::deleteNode(const int& deleteItem)
{
nodeType *current; //pointer to traverse the list
nodeType *trailCurrent; //pointer just before current
bool found;
if (first == NULL) //Case 1; the list is empty.
cout << "Cannot delete from an empty list."
<< endl;
else
{
if (first->info == deleteItem) //Case 2
{
current = first;
first = first->link;
count--;
if (first == NULL) //the list has only one node
last = NULL;
delete current;
}
else //search the list for the node with the given info
{
found = false;
trailCurrent = first; //set trailCurrent to point
//to the first node
current = first->link; //set current to point to
//the second node
while (current != NULL && !found)
{
if (current->info != deleteItem)
{
trailCurrent = current;
current = current-> link;
}
else
found = true;
}//end while
if (found) //Case 3; if found, delete the node
{
trailCurrent->link = current->link;
count--;
if (last == current) //node to be deleted
//was the last node
last = trailCurrent; //update the value
//of last
delete current; //delete the node from the list
}
else
cout << "The item to be deleted is not in "
<< "the list." << endl;
}//end else
}//end else
}//end deleteNode
void linkedListType::copyList
(const linkedListType& otherList)
{
nodeType *newNode; //pointer to create a node
nodeType *current; //pointer to traverse the list
if (first != NULL) //if the list is nonempty, make it empty
destroyList();
if (otherList.first == NULL) //otherList is empty
{
first = NULL;
last = NULL;
count = 0;
}
else
{
current = otherList.first; //current points to the
//list to be copied
count = otherList.count;
//copy the first node
first = new nodeType; //create the node
assert(first != NULL);
first->info = current->info; //copy the info
first->link = NULL; //set the link field of
//the node to NULL
last = first; //make last point to the
//first node
current = current->link; //make current point to
//the next node
//copy the remaining list
while (current != NULL)
{
newNode = new nodeType; //create a node
assert(newNode != NULL);
newNode->info = current->info; //copy the info
newNode->link = NULL; //set the link of
//newNode to NULL
last->link = newNode; //attach newNode after last
last = newNode; //make last point to
//the actual last node
current = current->link; //make current point
//to the next node
}//end while
}//end else
}//end copyList
linkedListType::~linkedListType() //destructor
{
destroyList();
}//end destructor
linkedListType::linkedListType
(const linkedListType& otherList)
{
first = NULL;
copyList(otherList);
}//end copy constructor
//overload the assignment operator
const linkedListType& linkedListType::operator=
(const linkedListType& otherList)
{
if (this != &otherList) //avoid self-copy
{
copyList(otherList);
}//end else
return *this;
}
i got an error saying error: request for member 'printInfo' in 'current->nodeType::info', which is of non-class type 'int'. There are few more headers file linking to this header, which all have template coding. However i managed to remove template coding from all the other headers with no errors at all with the exception of this header. can anyone tell me how to solve it?
You defined struct nodeType with member int info;. But in addressBookType::printInfoOf (and addressBookType::print) you have the line location->info.printInfo();
Since info is an int, it has no methods to call, printInfo or otherwise, thus the error.
It looks like the problem is a result of another type mismatch in your code. addressBookType's insertNode takes a const extPersonType& (which I assume is defined elsewhere and implements the methods you're trying to use?), but passes it to orderedLinkedListType::insertNode, which takes and stores const int&. This normally wouldn't work, but at a guess, extPersonType is a type (possibly a C++11 strongly-typed enum) that has an implicit conversion to int, which might avoid compile errors during assignment, but still effectively strips all the extPersonType-specific behaviors on insertNode, it goes from extPersonType to just an int.
If you want a general purpose linked list, you need to template it to work with more than just int, e.g. changing nodeType to:
template<typename T>
struct nodeType {
T info;
nodeType<T> *link;
};
and updating all the methods associated with nodeType and the orderedLinkedListType to template as appropriate to the specific type, rather than int. If you're being lazy, you could instead just change all uses of int in nodeType and orderedLinkedListType to extPersonType, but now the code is no good for ints, so pick your poison.

C++ Methods to insert element at beginning of a linked list and at specified index

For a list class, I'm trying to create a method to add a node to a linked list at the beginning of the list such as void List::prepend(const Item& it) as well as one to insert an element at a specific index in the list: void List::insert(const Item& it, int index)
So I could use some help in getting started on each of these.
I have already created a method that adds on to the end of a list:
void List::append(const Item& it) {
Node *nodePtr = new Node(it, NULL);
if(mySize == 0)
{
myFirst = nodePtr;
}
else
{
myLast->myNext = nodePtr;
}
myLast = nodePtr;
mySize++;
}
But these other two are not going quite as well.
In case you're wondering, I do have a typedef here:
typedef double Item;
void List::prepend(const Item& it) {
Node *nodePtr = new Node(it, NULL);
if(mySize == 0)
{
//..
}
else
{
//..
}
++mySize;
}
void List::insert(const Item& it, int index) {
Node *nodePtr = new Node(it, NULL);
if(index == 0)
{
//..
}
Node *currentNode = myFirst;
int currentIndex = 0;
while(currentIndex < index - 1) {
//..
}
// insert before
// ..
++mySize;
}
Fill in the //.. areas yourself.
It seem to be your function (List::append(const Item& it)) has a bit mistake that you have not yet updated the last pointer to NULL.
the problem is that you only save the last item.
You must save the First Item into myFirst.
Answer these questions:
1. What happen if mySize = 1 ?
2. Where does nodePtr point next? ( nodePtr->next)
For your question:
void List::prepend(const Item& it) {
Node *nodePtr = new Node(it, NULL);
if(mySize == 0)
{
myFirst = nodePtr;
}
else{
nodePtr->next = myFirst;
myFirst = nodePtr;
}
mySize++;
}