C++ Hash Table Program Hangs after Entering a Value - c++

If you can follow my main below, I run the program, I am able to enter an integer, it finds the next prime number, then asks for data. Once I enter data once, the program hangs. Seems to be in an infinite loop, or something. It doesn't crash. When I pause it, it brings up read.c file with an arrow on line 256. Not sure what this means whatsoever. Any help would be much appreciated.
I have the following class and member function declarations in hashtable.h:
#ifndef HASHTABLE_H
#define HASHTABLE_H
#define TRUE 1
#define FALSE 0
#define VERBOSE 0x01
#define NON_VERBOSE 0x02
#include "linkedlist.h"
class hashTable{
public:
int keys;
int tableSize;
linkedList<int> **table;
hashTable(const int n);
//~hashTable();
void hash(int value);
int search(int value);
int divisionMethod(int value, int sizeOfTable);
int midSquareMethod(int value, int sizeOfTable);
int total();
void printHashTable();
int next_prime(int value, char flag);
};
// constructor
hashTable::hashTable(const int n){
linkedList<int> newList;
tableSize = next_prime(n, VERBOSE);
cout << "Table size is: " << tableSize << "\n"; // for debugging
system("pause"); // for debugging
table = new linkedList<int>*[tableSize];
for (int i = 0; i < tableSize; i++)
table[i] = { new linkedList<int> };
}
// Compute the Hash Function and "Hash" element into table
void hashTable::hash(int value){
table[value % tableSize]->addToHead(value);
keys++;
//divisionMethod(midSquareMethod(value, tableSize), tableSize)
}
// Simple Search Function
// Returns the element searched for if found, 0 otherwise
int hashTable::search(int value){
return(table[value % tableSize]->search(value));
}
// Divsion Method for producing a semi-unique key
int hashTable::divisionMethod(int value, int sizeOfTable){
int key;
key = value % sizeOfTable;
return(key);
}
// Middle Square Method for producing a semi-unique key
int hashTable::midSquareMethod(int value, int sizeOfTable){
int key;
key = ((value * value) & 0x0ff0) >> 4; // pick the middle 8 bits
return(key);
}
// Returns the total number of keys in the table
int hashTable::total(){
return(keys);
}
// Print the hash table (for demonstration purposes
void hashTable::printHashTable(){
int i = 0, valueToPrint;
while (i < tableSize){
cout << i << ": ";
valueToPrint = table[i]->removeFromHead();
while (valueToPrint != 0){
cout << valueToPrint << " -> ";
valueToPrint = table[i]->removeFromHead();
}
cout << "|" << endl;
i++;
}
}
int hashTable::next_prime(int value, char flag){
int FOUND = FALSE;
int n;
while (!FOUND) {
for (n = 2; (n * n) <= value && (value % n) != 0; ++n);
if ((n * n) <= value) {
if (flag == VERBOSE)
cout << value << " is divisible by " << n << "\n";
value++;
}
else {
if (flag == VERBOSE)
cout << "The next largest prime is " << value << "\n";
FOUND = TRUE;
}
}
return(value);
}
#endif
Here is my linkedlist.h:
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <iostream>
using namespace std;
template <class TYPE>
class Node{
public:
TYPE data;
Node* next;
// constructor
Node(TYPE const& x){
data = x;
next = NULL;
}
};
template <class TYPE>
class linkedList{
//struct Node{
// TYPE data;
// Node *next;
//};
public:
Node<TYPE> *head;
Node<TYPE> *tail;
int size;
// constructor
linkedList(){
head = NULL;
tail = NULL;
size = 0;
}
~linkedList();
void addToHead(TYPE value);
void addToTail(TYPE value);
TYPE removeFromHead();
TYPE removeFromTail();
TYPE search(TYPE searchData);
TYPE isEmpty();
};
//destructor
template <class TYPE>
linkedList<TYPE>::~linkedList(void){
while (head){
Node<TYPE> *temp = head;
head = head->next;
delete temp;
}
}
// Insert an element at the head (start) of the linked list
template <class TYPE>
void linkedList<TYPE>::addToHead(TYPE value){
Node<TYPE> *newNode = new Node<TYPE>(value);
if (isEmpty())
head = newNode;
else{
newNode->next = head;
head = newNode;
}
}
// Add an element to the tail (end) of the linked list
template <class TYPE>
void linkedList<TYPE>::addToTail(TYPE value){
Node<TYPE> *newNode = new Node<TYPE>(value);
Node *tempPtr;
if(isEmpty()){
head = newNode;
tail = newNode;
}
else{
tail->next = newNode;
tail = tail->next;
}
}
// Remove an element from start of Linked List
template <class TYPE>
TYPE linkedList<TYPE>::removeFromHead(){
TYPE tempValue;
Node<TYPE> *temp;
if (head){
tempValue = head->data;
temp = head;
if (head == tail)
head = tail = 0;
else
head = head->next;
delete temp;
return tempValue;
}
else
return 0;
}
// Remove an element from the end of the linked list
template <class TYPE>
TYPE linkedList<TYPE>::removeFromTail(){
TYPE tempValue;
Node *temp;
if (tail){
tempValue = tail->data;
if (head == tail){
delete head;
head = tail = 0;
}
else{
for (temp = head; temp->next != tail; temp = temp->next);
delete tail;
tail = temp;
tail->next = 0;
}
return tempValue;
}
else
return 0;
}
// Search for an element in the linked list
// Will return the element if found, otherwise it returns 0
template <class TYPE>
TYPE linkedList<TYPE>::search(TYPE searchData){
Node<TYPE> *temp;
temp = head;
while (temp->next != tail){
if (tail->data == searchData)
return searchData;
if (temp->data == searchData)
return searchData;
else
temp = temp->next;
}
return 0;
}
// isEmpty() function checks if head == NULL
template <class TYPE>
TYPE linkedList<TYPE>::isEmpty(){
return(head == NULL);
}
#endif
Here is my main:
#include "hashtable.h"
int main(){
int n, input;
cout << "Enter an integer: ";
cin >> n;
cout << "\n\n";
hashTable myHashTable(n);
cout << "Enter some values into the table:" << endl;
cin >> input;
while (input != 0){
myHashTable.hash(input);
cin >> input;
}
myHashTable.printHashTable();
}

Something must be wrong, you have in attribute of your hashTable class ... a hashTable pointer. It must be a linkedlist pointer, nop ?

I did find out what was causing all this. It was the way that I was implementing the linked lists in my array of pointers. Pretty much just programmer error from long nights. Of course there is a lot wrong with my code that I posted here, which I fixed it all, e.g. search function in my hash class, etc.,
Here is what I changed that pretty much fixed a good portion of my problem posted here:
hashTable::hashTable(const int n, char hFunction){
keys = 0;
hashFunction = hFunction;
tableSize = next_prime(n, VERBOSE);
cout << "Table size is: " << tableSize << "\n\n"; // for debugging
system("pause"); // for debugging
table = new linkedList<int>[tableSize];
I also changed my linkedList<int> **table to linkedList<int> *table. If anyone else needs any pointers on the rest of this NOW working hash function, just get a hold of me.

Related

how can i get the front or top element of a vector queue?

I didn't put the full code because it was very long and i only need help with the small portion which is the **** area. i can't seem to use front() or top() to get the top element of the queue. I tried making top() function List keep getting error : 1) class List has no memeber named 'top' which means i don't have a function top in List, when i make it it says 2) no match for 'operator=' in printer_cpu[i] = SList::top() with T=PCB]()'
template <class T>
class node{
public:
T data;
node *next;
};
template <class T>
class List{
node<T> *head;
node<T> *tail;
public:
List()
{
head = tail = NULL;
}
bool isEmpty()
{
if(head == NULL) return true;
else return false;
}
void enqueue(T new_data){
node<T> *temp = new node<T>;
temp->data = new_data;
temp->next = NULL;
if(isEmpty()){
head = temp;
tail = temp;
}
else{
tail->next = temp;
tail = temp;
}
}
void dequeue(){
if(isEmpty())
{
cout << "The list is already empty" << endl;
}
node<T>* temp;
if(head == tail){
temp->data=head->data;
delete head;
head = tail = NULL;
}
else{
temp->data = head->data;
head = head->next;
delete temp;
}
}
node<T> top() // need help here ****
{
return head;
}
void display(){
node<T> *current = head;
while(current != NULL){
cout << current->data << endl;
current = current->next;
}
}
};
struct PCB
{
int ProcessID;
int ProcessorSize;
int priority;
string name;
};
typedef List<PCB> printing;
typedef List<PCB> disk;
void gen(vector<printing> &printer_queue,string printer_name[], int printers)
{
for(int i = 0; i < printers; i++)
{
int num = i+1;
ostringstream convert;
convert << num;
printer_name[i] = "p" + convert.str();
printer_queue.push_back(printing());
}
int main()
{
int numOfPrinter = 5;
string interrupt;
cin >> interrupt;
PCB cpu;
PCB printer_cpu[numOfPrinter];
string printer_name[numOfPrinter];
vector<printing> PQ;
gen(PQ,printer_name,numOfPrinter);
for(int i = 0; i < numOfPrinter; i++)
{
if(interrupt == printer_name[i])
{
cout << "Enter a name for this printer file: " << endl;
cin >> cpu.name;
PQ[i].enqueue(cpu);
printer_cpu[i] = PQ[i].top(); //need help here ****
}
}
}
It looks like you're missing an asterisk, because you need to return of type pointer, because that's what head is.
You should have
node<T> * top()
{
...
}
You also need to overload the = operator, because you are trying to compare type PCB with type node *.
Well, I compile your code successfully after correcting some mistakes.
I didn't meet class List has no memeber named 'top' problem.
Then your top() function returns the value of head, so you should change it to: node<T>* top() because head is a pointer to node<T>.
And the reason you got no match for 'operator=' error is that printer_cpu[i]'s type is PCB while PQ[i].top()'s type should be node<T>*
I have also found that the code you post lacks a } just before int main().

Positive integer N as the sum of positive integers using stack

So, I am working on this project, where we have to write a positive integer number n as the of positive integers. And all solutions should be ordered, for instance:
n = 3
1: 1 1 1
2: 1 2
3: 3
n = 5
1: 1 1 1 1 1
2: 1 1 1 2
3: 1 1 3
4: 1 2 2
5: 2 3
7: 5
I got (I think) very good algorithm on paper; yet, I have a trouble putting it into the code. The first time it gets into the inner do-while loop it runs smoothly, but then 'top' doesn't get a right value from the getTop function.
Here is my Stack.h file
#include <iostream>
using namespace std;
template <class T>
class Stack
{
private:
// Structure for the stach nodes
struct StackNode
{
T value; // Value in the node
StackNode *next; // Pointer to next node
};
StackNode *top; // Pointer to the stack top
int count;
public:
//Constructor
Stack(){top = NULL; count = 0;}
// Destructor
~Stack();
// Stack operations
bool push(T);
bool pop(T &);
bool isEmpty();
int getCount();
int getTop();
void print();
};
template <class T>
Stack<T>::~Stack()
{
StackNode *currNode, *nextNode;
// Position nodePtr at the top of the stack.
currNode = top;
// Traverse the list deleting each node.
while (currNode) //while (currNode != NULL)
{
nextNode = currNode->next;
delete currNode;
currNode = nextNode;
}
}
template <class T>
bool Stack<T>::push(T item)
{
StackNode *newNode; // Pointer to a new node
// Allocate a new node and store num there.
newNode = new StackNode;
if (!newNode)
return false;
newNode->value = item;
// Update links and counter
newNode->next = top;
top = newNode;
count++;
return true;
}
template <class T>
bool Stack<T>::pop(T &item)
{
StackNode *temp; // Temporary pointer
// empty stack
if (count == 0)
return false;
// pop value off top of stack
item = top->value;
temp = top->next;
delete top;
top = temp;
count--;
return true;
}
template <class T>
bool Stack<T>::isEmpty()
{
return count == 0;
}
template <class T>
int Stack<T>::getCount()
{
return count;
}
template <class T>
int Stack<T>::getTop()
{
if (this->top->next == NULL)
{
cout << "EXCEPTION" << endl;
}
else
{
return this->top->next->value;
}
//return top->value;
}
template <class T>
void Stack<T>::print()
{
StackNode *newNode;
newNode = top;
for (int i = count; i > 0; i--)
{
cout << newNode -> value;
cout << " ";
newNode = newNode -> next;
}
}
#endif
and here is my main.cpp
#include <iostream>
#include "Stack.h"
using namespace std;
int main()
{
int input = 0;
int counter = 0;
int pop1 = 0;
int pop2 = 0;
int top;
int CurrSum = 0;
Stack<int> *stack = new Stack<int>;
Stack<int> *tmpStack = new Stack<int>;
cout << "Please enter a integer: " << endl;
cin >> input;
while(input < 0)
{
cout << "Please enter a POSITIVE integer: " << endl;
cin >> input;
}
if ( input != 0)
{
counter ++;
for (int n = 1; n <= input; n ++)
{
stack -> push(1);
}
do
{
do
{
top = stack->getTop();
pop1 = stack->pop(top);
top = stack->getTop();
pop2 = stack->pop(top);
//increment the pop2 and push it back
stack->push(pop2 + 1);
top = stack->getTop();
CurrSum = CurrSum - pop1 - pop2 + top;
if (CurrSum < input)
{
stack->push(top);
CurrSum = CurrSum + top;
}
else if (CurrSum > input)
{
pop1 = stack->pop(top);
top = stack->getTop();
pop2 = stack->pop(top);
CurrSum = CurrSum - pop1 - pop2 + top;
}
}while (CurrSum == input);
cout << counter << ": ";
stack->print();
top = stack->getTop();
}while (top != input);
system("pause");
}
else
cout << "Thank you for using this program. Bye!" << endl;
return 0;
}
First, you do not need to write that Stack class for yourself, it's already done for you: it's called std::stack, and does exactly what you need. Second, you really don't need to dynamically allocate it, whether you use your own Stack or std::stack, though it does the same functionally, it is less efficient to allocate dynamically.
If that still doesn't work, you can now be sure that your algorithm is wrong (I didn't check it). Since it is a typical recursive problem, I'd try writing it recursively (now you don't need a Stack class at all), since recursive algorithms like this are usually easier to understand when you write them recursively. Later you can rewrite it iteratively if you want, once you understand what went wrong.

Why is my basic PriorityQueue sorting algorithm causing a seg fault?

I've been working on this linked list priority queue for the better part of four or five hours now, and for the life of me I can't find the root of this seg fault. It's driving me insane.
I know that the root of the problem is in my swapUp() function (swapping the positioning of two nodes based on their priority), because the list works great up until it is called. The seg fault is not actually being caused by swapUp(), it's being caused by peekAt(), which returns the element in the node at position n. But the error does not occur unless swapUp() is called first, so that is where the issue is (I think).
There is also a seg fault being caused in the destructor, which I believe may have the same root cause in swapUp().
I have gone over the code over and over and I've been debugging it all night but I just can't figure out exactly what is going wrong. I would really, really appreciate some help with this.
PRIORITY QUEUE:
#ifndef JMF_PriorityQueue
#define JMF_PriorityQueue
#include <iostream>
#include <string>
template <typename T>
class PriorityQueue{
public:
struct Node{
T data;
int priority;
Node * next;
Node * prev;
};
PriorityQueue();
PriorityQueue & operator=(const PriorityQueue &rhs);
bool isEmpty(); //Returns true if queue is empty
int getLength(); //Returns length of queue
void enqueue(T data, int p); //Enqueues data T with priority p
void enqueue(T data); //Enqueues data T with priority 1
T dequeue(); //Dequeues and returns data at head of queue
void clearQueue(); //Empties queue
T peek(); //Returns data at head of queue without dequeing it
T peekAt(int n); //Returns data element n without dequeuing it
int getPriority(int n); //Returns priority of item at position n
void display(); //Prints list of data elements to screen
void revDisplay();
void swapUp(Node * target); //Swaps target node with it's neighbor next in line
bool contains(T data); //Returns true if data exists as an element anywhere on the queue
~PriorityQueue();
private:
int size;
Node * head, *tail;
};
template <typename T>
PriorityQueue<T>::PriorityQueue(){
size = 0;
head = 0;
tail = 0;
}
template <typename T>
PriorityQueue<T> & PriorityQueue<T>::operator=(const PriorityQueue &rhs){
clearQueue();
for(int n = 0; n < rhs.size(); n++)
enqueue(rhs.peekAt(n));
return *this;
}
template <typename T>
int PriorityQueue<T>::getLength(){
return size;
}
template <typename T>
bool PriorityQueue<T>::isEmpty(){
return(!size);
}
template <typename T>
void PriorityQueue<T>::enqueue(T data){
enqueue(data, 1);
}
template <typename T>
void PriorityQueue<T>::enqueue(T data, int p){
Node * newNode = new Node();
newNode -> data = data;
newNode -> priority = p;
if(isEmpty()){
head = newNode;
tail = newNode;
} else {
newNode -> next = tail;
tail -> prev = newNode;
tail = newNode;
//WHEN THIS WHILE LOOP IS COMMENTED OUT (IE NO SORTING), NO SEG FAULT ISSUES
while(newNode != head && newNode->priority < newNode->next->priority)
swapUp(newNode);
std::cout << "\n";
}
tail->prev = 0;
head->next = 0;
size++;
}
template <typename T>
T PriorityQueue<T>::dequeue(){
if(isEmpty()){
std::cout << "\n\nWARNING: Trying to dequeue empty queue\n\n";
throw 3;
} else {
Node * frontNode = head;
T result = frontNode -> data;
if(size == 1){
head = 0;
tail = 0;
} else {
head = frontNode -> prev;
head -> next = 0;
}
delete frontNode;
size--;
return result;
}
}
template <typename T>
void PriorityQueue<T>::clearQueue(){
while(!isEmpty())
dequeue();
}
template <typename T>
T PriorityQueue<T>::peek(){
return peekAt(0);
}
template <typename T>
T PriorityQueue<T>::peekAt(int n){
T result;
Node * thisNode;
if(isEmpty()){
std::cout << "\n\nWARNING: Trying to peek empty queue\n\n";
throw 3;
} else if( n < 0 || n > size){
std::cout << "\n\nWARNING: Trying to peek queue at non-existent index " << n << "\n\n";
throw 3;
} else {
thisNode = head;
if(thisNode->prev == 0)
for(int k = 0; k < n; k++)
thisNode = thisNode -> prev;
result = thisNode -> data; //Crashes program if swapUp() is ever called
}
return result;
}
template <typename T>
int PriorityQueue<T>::getPriority(int n){
int result;
if(isEmpty()){
std::cout << "\n\nWARNING: Trying to get priority from empty queue\n\n";
result = -1;
} else if( n < 0 || n > size){
std::cout << "\n\nWARNING: Trying to get priority from non-existent index " << n << "\n\n";
result = -1;
} else{
Node * thisNode = head;
for(int k = 0; k < n; k++)
thisNode = thisNode -> prev;
result = thisNode -> priority;
}
return result;
}
template <typename T>
void PriorityQueue<T>::display(){
if(isEmpty()){
std::cout << "\nQueue is empty\n";
} else {
std::cout << "\nINDEX\tDATA\tPRIORITY\n";
std::cout << "-----------------------\n";
Node * thisNode = head;
for(int n = 0; n < size; n++){
std::cout << n << "\t" << thisNode->data << "\t" << thisNode->priority << "\n";
thisNode = thisNode -> prev;
}
std::cout << "\n";
}
}
template <typename T>
void PriorityQueue<T>::revDisplay(){
if(isEmpty()){
std::cout << "\nQueue is empty\n";
} else {
std::cout << "\nINDEX\tDATA\tPRIORITY\n";
std::cout << "-----------------------\n";
Node * thisNode = tail;
for(int n = 0; n < size; n++){
std::cout << n << "\t" << thisNode->data << "\t" << thisNode->priority << "\n";
thisNode = thisNode -> next;
}
std::cout << "\n";
}
}
template <typename T>
void PriorityQueue<T>::swapUp(Node * target){
if(target == head)
return;
Node * partner = target->next;
if(partner == head){
head = target;
target->next = 0;
} else
target->next = partner->next;
if(target == tail){
tail = partner;
partner->prev = 0;
} else
partner->prev = target->prev;
}
template <typename T>
bool PriorityQueue<T>::contains(T data){
bool result = false;
if(!isEmpty()){
Node * thisNode = head;
for(int n = 0; n < size; n++){
if(thisNode->data == data){
result = true;
break;
}
thisNode = thisNode -> prev;
}
}
return result;
}
template <typename T>
PriorityQueue<T>::~PriorityQueue(){
clearQueue();
}
#endif
TEST PROGRAM:
#include <iostream>
#include <string>
#include <ctype.h>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include "PriorityQueue.hpp"
int main(){
PriorityQueue<char> test;
test.enqueue('c',1);
test.enqueue('a',2);
test.enqueue('t',3);
test.display();
std::cout <<"\nREVERSE:\n";
test.revDisplay();
std::cout<<"\nWITH SORTING:\n";
test.enqueue('d',5);
test.enqueue('s',9);
test.enqueue('g',7);
test.enqueue('o',6);
test.enqueue('&',4);
test.display();
std::cout <<"\n\nALL DONE\n\n";
return 0;
}
Okay, so I've tried implementing SwapUp() in two different new ways, both of which are still giving me errors.
Failed attempt #1:
template <typename T>
void PriorityQueue<T>::swapUp(Node * target){
Node * partner = target->next; //Partner = target next
Node * temp = new Node; // temp spot to hold partner neighbors
temp->next = partner->next;
temp->prev = partner->prev;
partner->next = target->next;
partner->prev = target->prev;
target->next = temp->next;
target->prev = temp->prev;
if(target == tail)
tail = partner;
if(partner == head)
head = target;
delete temp;
}
Failed attempt #2:
template <typename T>
void PriorityQueue<T>::swapUp(Node * target){
Node * partner = target->next; //Partner = target next
target->next = partner->next; //Target next = partner next
partner->prev = target->prev; //Partner prev = target prev
partner->next = target; //Partner next = target
target->prev = partner; //Target prev = partner
if(target == tail)
tail = partner;
if(partner == head)
head = target;
}
This is driving me absolutely mad. This is such an elementary logic problem I don't know why I'm having so much trouble with it. Any help would be greatly, greatly appreciated!
In swapUp, you have a handful of problems. The clause that addresses if(partner == head) will never be called, because you've already returned if target == head.
swapUp isn't setting the reverse prev and next pointers of both values being swapped, only the next of the target and the prev of the following node. Both the prev and next need to swapped to maintain your doubly-linked list.

C++: Expected Constructor, Destructor or Type Conversion

Trying to learn C++. Was wondering if someone could point me in the right direction on this error (pun intended).
The class is a LinkedList (reproduced in its entirety below). The error occurs on this line:
LinkedList<T>::ListNode LinkedList::find(int pos) const {
and says:
expected constructor, destructor, or type conversion before
'LinkedList'
Any tips would be greatly appreciated.
#ifndef LINKEDLIST_HPP
#define LINKEDLIST_HPP
#include <iostream>
using namespace std;
template <class T>
class LinkedList {
private:
// a ListNode consists of the item and a pointer to the next ListNode
struct ListNode {
T data;
ListNode *next;
};
int size; //Size of the list
ListNode *head; //the beginning of the list
public:
LinkedList(); // default oonstructor
~LinkedList(); // destructor
virtual bool isEmpty ();
virtual int getLength ();
virtual void insert (int pos, T item);
virtual T remove (int pos);
virtual T retrieve (int pos);
//helper methods
LinkedList(LinkedList &copyList); //copy constructor
ListNode *find (int pos) const; // internal find function
};
//default constructor
template <class T>
LinkedList<T>::LinkedList() {
size = 0;
head = NULL;
}
//destructor
template <class T>
LinkedList<T>::~LinkedList() {
//loop through each node, and delete it
ListNode* current = head;
while (current != NULL) {
ListNode* next = current->next;
delete current;
current = next;
}
head = NULL; // set head node to back to null
}
//copy constructor
template <class T>
LinkedList<T>::LinkedList(LinkedList& copyList) {
size = copyList.size;
// if copyList is empty
if (copyList.head == NULL)
head = NULL;
else {
//create a new head
head = new ListNode;
head->data = copyList.head->data;
//create a new list
ListNode *newPtr = head; // start at the head
// iterate through rest of list to be copied
for (ListNode *copyPtr = copyList.head->next; copyPtr != NULL; copyPtr = copyPtr->next) {
newPtr->next = new ListNode;
newPtr->data = copyPtr->data;
}
//make last ListNode's next point to NULL
newPtr->next = NULL;
}
}
template <class T>
bool LinkedList<T>::isEmpty() {
if (size == 0)
return true;
return false;
}
template <class T>
int LinkedList<T>::getLength() {
return size;
}
// used in other methods to find a given index
template <class T>
LinkedList<T>::ListNode LinkedList::find(int pos) const {
// check that pos is in bound of LinkedList
if ((pos < 1) || pos > getLength()) {
cout << "Find position of out bounds" << endl;
return NULL;
} else { //search through ListNodes
ListNode *temp = head; // start at the head
for (int i = 1; i < pos; ++i)
temp = temp->next;
return temp;
}
}
template <class T>
T LinkedList<T>::retrieve(int pos) {
T tempData; // to hold retrieved data
try {
if ((pos < 1) || (pos > getLength())) {
cout << "Retrieve request outside LinkedList's bounds" << endl;
return NULL;
}
else { //traverse list
ListNode *temp = find(pos);
tempData = temp->data;
return tempData;
}
} catch (int e) {
cout << "Could not retrieve position " << pos << endl;
}
}
template <class T>
void LinkedList<T>::insert(int pos, T item) {
//check bounds
if ((pos < 1) || (pos > getLength() +1))
cout << "Must insert at a position between 1 and getLength() + 1" << endl;
else {
try {
//create new ListNode
ListNode *temp = new ListNode;
temp->data = item;
//if the new item is at the first position
if (pos == 1) {
temp->next = head;
head = temp;
}
else {
ListNode *prev = find(pos - 1);
temp->next = prev->next;
prev->next = temp;
}
//increment size
++size;
} catch (int e) {
cout << "Error inserting " << item << " at position " << pos << endl;
}
}
}
template <class T>
T LinkedList<T>::remove(int pos) {
//check bounds
if ((pos < 1) || (pos > getLength()))
cout << "Must remove a position between 1 and getLength()" << endl;
else {
try {
ListNode *temp; //to hold shifted node
//if node to be deleted is first node
if (pos == 1) {
temp = head;
head = head->next;
}
//for anything but the head
//write over the node before the pos'th index
else {
ListNode *prev = find(pos - 1);
temp = prev->next;
prev->next = temp->next;
}
//destroy temp, to free up memory
temp->next = NULL;
delete temp;
//decrement size
--size;
} catch (int e) {
cout << "Error removing item from position " << pos << endl;
}
}
}
#endif /* LINKEDLIST_HPP */
ListNode is a dependant type, so it needs to be qualified with typename. Also, your find function was declared to return a pointer-to-ListNode, but the definition returns a ListNode by value. This seems like a simple typo:
template <class T>
typename LinkedList<T>::ListNode* LinkedList<T>::find(int pos) const {
Qualify the dependant type with typename and add <T> (I'm not sure if this is necessary, might as well do it though):
typename LinkedList<T>::ListNode* LinkedList<T>::find(int pos) const {
See: Where and why do I have to put the "template" and "typename" keywords?
Also, your definition doesn't match. In-class it returns a pointer, but outside it returns a value.
Should be
LinkedList<T>::ListNode LinkedList<T>::find(int pos) const {

Trouble adding nodes to a doubly Linked List

Hi I am trying to make a doubly linked list to store individual numbers as nodes of a doubly linked list and then add them together and print them out for a homework assignment. I am having a lot of trouble getting this to work and have traced my problem to my add node functions as they don't update the pointers correctly. For example on the AddToFront() function I can't understand how I can get the prev pointer to work and point to the node behind it.
I can't use the STL and have to implement the LL myself in case anyone is wondering. Thanks!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
using namespace std;
/////// PART A
template <class T>
class List {
private:
struct Node {
T data;
Node *next;
Node *prev;
};
Node *front, *current, *rear;
public:
List();
~List();
void AddtoFront (T newthing);
void AddtoRear (T newthing);
bool FirstItem (T & item);
bool LastItem (T & item);
bool NextItem (T & item);
bool PrevItem (T & item);
};
template <class T>
List<T>::List() {
front = NULL; current = NULL; rear = NULL;
}
template <class T>
List<T>::~List() {
}
template <class T>
void List<T>::AddtoFront (T newthing) {
if (front == NULL) {
Node *temp;
temp = new Node;
temp->data = newthing;
temp->next = front;
temp->prev = NULL;
front = temp;
} else {
Node *temp;
temp = new Node;
front->prev = temp;
temp->data = newthing;
temp->next = front;
temp->prev = NULL;
front = temp;
}
}
template <class T>
void List<T>::AddtoRear (T newthing) {
if (rear == NULL) {
Node *temp;
temp = new Node;
temp->data = newthing;
temp->prev = rear;
temp->next = NULL;
rear = temp;
} else {
Node *temp;
temp = new Node;
rear->next = temp;
temp->data = newthing;
temp->prev = rear;
temp->next = NULL;
rear = temp;
}
}
template <class T>
bool List<T>::FirstItem (T & item) {
if (front == NULL) { return false; }
current = front;
item = front->data;
return true;
}
template <class T>
bool List<T>::LastItem (T & item) {
if (rear == NULL) { return false; }
current = rear;
item = rear->data;
return true;
}
template <class T>
bool List<T>::NextItem (T & item) {
if (current != NULL) current = current->next;
if (current == NULL) { return false; }
item = current->data;
return true;
}
template <class T>
bool List<T>::PrevItem (T & item) {
if (current == NULL) { return false; }
if (current->prev != NULL) current = current->prev;
item = current->data;
return true;
}
/////// PART B
class BigNumber {
private:
//complete here...
//include here a List of integers, or shorts etc
List<int>L;
public:
//complete here...
//what methods do you need?
//e.g., ReadFromString, PrintBigNumber, AddBigNumbers
BigNumber();
~BigNumber();
void ReadFromString(char * decstring);
void PrintBigNumber();
void AddBigNumbers(BigNumber B1, BigNumber B2);
};
BigNumber::BigNumber(){
// anything here?
}
BigNumber::~BigNumber(){
//you can keep that empty
}
void BigNumber::ReadFromString (char * decstring ) {
//read a string, adding a new node per digit of the decimal string
// To translate 'digits' to integers: myinteger=decstring[index]-48
//You need to use the AddtoFront()
int temp;
for (unsigned i=0; i < strlen(decstring); ++i) {
//cin >> decstring[i];
temp = decstring[i]-48;
//L.AddtoFront(temp);
L.AddtoRear(temp);
//cout <<"Number added!" <<endl;
}
}
void BigNumber::PrintBigNumber () {
//complete here, print the list (i.e., use FirstItem() and NextItem() )
int val;
if (L.FirstItem(val)) {
cout << val;
} else {
cout << "print failed";
}
//if (L.FirstItem(val)) { cout << "true-first";} else { cout <<"false-first";};
//if (L.LastItem(val)) { cout << "true";} else { cout <<"false";};
//L.FirstItem(val);
//cout << val;
/*while (L.PrevItem(val)){
cout << val;
//cout <<"Print error!Value not here.";
}*/
}
void BigNumber::AddBigNumbers(BigNumber B1,BigNumber B2){
//complete here.
//use FirstItem(), NextItem() and AddNode()
//to add two big numbers, what do you have to do? Be careful about the carry
//Remember to add the last carry, the resulting number can have one more digit than B1 or B2
}
/////// PART C
BigNumber B1, B2, RES;
int main (int argc, char ** argv) {
//use command line arguments
if(argc!=3){printf("usage: executable number1 number2\n");exit(0);}
B1.ReadFromString(argv[1]);
B2.ReadFromString(argv[2]);
//print
cout << endl<< "Add the following numbers " << endl;
B1.PrintBigNumber();
cout << " + ";
B2.PrintBigNumber();
cout << " = " << endl;
//compute the addition
RES.AddBigNumbers(B1,B2);
//print the result
RES.PrintBigNumber();
cout << endl;
return 0;
}
EDIT: I added in a line each in AddToFront() and AddToRear(). Is this on the right track?
AddtoFront needs to also update the prev pointer for front. That is, you're currently doing
temp -> front <-> rest_of_list
where it needs to be
temp <-> front <-> rest_of_list.
Also, you might notice that the two branches of your if statement in AddtoFront are identical... :)