Positive integer N as the sum of positive integers using stack - c++

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.

Related

C++ - Inserting Linked list (A) into another Linked list (B) at position n

Example:
Linked List A: 1->2->3
Linked List B: 4->5->6
My task is to make a function, that passes List B into List A at any given position (n).
For instance: After "2" = 1->4->5->6->2->3 (output).
I wasn't really sure, how to do this, so I used:
// Function that finds a number in a list and gets an address of that node.
Node* List::find(int i)
{
for (start();!end();next())
if(current->num==i) return current;
return NULL;
};
// Function, that puts freely desired number at position n in the list;
Example cin >> i; // i = 6;
1->2->6->desired number->...
Node* List::addAfter(int pos, int num)
{
Node* p = NULL; i
current = find(pos);
if (current != NULL)
{
p = new Node(num);
p->next = current->next;
current->next = p;
}
if (last == current) last = p;
current = p;
return p;
}
Both things works, but only as:
cout << "After which number?" << endl;
cin >> i; // **2**
l.addAfter(i, 1); // Before: 2->3->4 After: 2->1->3->4
l.print();
This works perfectly! But if I have two lists - l1 (2->3->4 and l2 6->7)
how can I put both together using this function?
Node* List::addAfter(int pos, I HAVE TO PASS L2 VALUES HERE)
How can I give this function l2 values as parameter?
Is there maybe a better way, to do this? I'd appreciate any help.
WHOLE CODE:
#include <iostream>
using namespace std;
class Node
{
public:
int num;
Node *next;
Node (int n) { num = n; next = NULL; };
};
class List
{
protected:
Node *first, *last;
public:
Node *current;
public:
List () { first = last = current = NULL; };
void add_element (int n);
void delete_element ();
void print(); // Izdrukā sarakstu
bool is_empty () { return (first == NULL); };
void start () { current = first; };
bool end () { return (current == NULL); };
void next(){if (!end())current = current -> next;};
Node* find(int i);
Node* addAfter(int i, List * l2);
~List();
};
int main()
{
List l, l2;
int k, i;
cout << "Type number: (0,to stop):\n";
cin >> k;
while (k!=0)
{
l.add_element (k);
cout << "Type number: (0, to stop):\n";
cin >> k;
};
cout << endl;
cout << "Type 2nd list numbers: (0,to stop):\n";
cin >> k;
while (k!=0)
{
l2.add_element (k);
cout << "Type 2nd list numbers: (0,to stop):\n";
cin >> k;
};
cout << endl;
l.print();
l2.print();
cout << "After which element do you want to insert second list?" << endl;
cin >> i;
l.addAfter(i, l2); // GETTING ERROR HERE.
l.print();
return 0;
}
simplest and easiest form is to pour out both stacks into an array
const int size = stackA.size() + stackB.size();
const stackASize = stackA.size();
const stackBSize = stackB.size();
int arrayOfStackA [size];
int arrayOfStackB [stackBSize];
//Pop StackA into ArrayA
for(int i=stackASize-1; i>0; i++)
{
arrayOfStackA[i] = stackA.pop();
}
//Pop StackB into ArrayB
for(int i=stackBSize-1; i>=0; i++)
{
arrayOfStackB[i] = stackB.pop();
}
Now find the index where you want to insert the data in array A.
In your case you want to enter stack b after 1 which in case of array is the index:1
int count = 0
for(int i=0;i<size;i++){
if(i<requiredIndexNumber){
//do nothing
}
else if(i>=requiredIndexNumber && count!=stackBSize){
int temp = arrayOfStackA[i];
arrayOfStackA[i] = arrayOfStackB[count++];
arrayOfStackA[stackASize+i] = temp;
}
}
This is the most easiest from of popping one stack into an other at any index

Implementing A Sort Using 3 Stacks. (Segmentation Fault - Core Dump)

I'm getting a Segmentation Fault - Core Dump during runtime.
I'm trying to figure out how to get rid of the Segmentation Fault.
It's a class homework.
EDIT: Added Input and Output below.
//client.cpp
#include <iostream>
#include "stack.h"
using namespace std;
int main(void){
stack unsorted;
stack stack1;
stack stack2;
stack stack3;
int const MAX_VALUES = 5;
int input;
cout << "Please input "<< MAX_VALUES << " unique integers.\n";
cout << "Click the ENTER key after each integer inputted." <<endl;
for (int i = 0; i < MAX_VALUES; i++)
{
cin >> input;
stack1.Push(input);
unsorted.Push(input);
}
cout << "Unsorted Stack: " <<endl;
for (int i = 0; i < MAX_VALUES; i++)
{
cout<<unsorted.Pop()<<endl;
}
cout << "Sorted Stack: "<<endl;
while((!stack1.IsEmpty())&&(!stack3.IsEmpty())){
if ((stack1.IsEmpty())&&(!stack3.IsEmpty()))
{
stack2.Push(stack3.Pop());
}
if (stack2.Top() > stack1.Top())
{
stack3.Push(stack2.Pop());
}
if (stack3.Top() < stack1.Top())
{
stack2.Push(stack3.Pop());
}
else
{
stack2.Push(stack1.Pop());
}
}
while (!stack2.IsEmpty()){
cout << stack2.Pop() << endl;
}
}
//stack.h
#include <cstddef>
struct node;
class stack
{
public:
stack();
~stack();
bool IsFull();
bool IsEmpty();
void Push(int input);
int Pop();
int Top();
private:
node* top;
int const MAX_VALUES = 5;
int count;
};
//stack.cpp
#include "stack.h"
struct node
{
int input;
node* next;
};
stack::stack()
{
top = NULL;
count = 0;
}
bool stack::IsFull()
{
if (MAX_VALUES > count)
{
return false;
}
return true;
}
bool stack::IsEmpty(){
if (top == NULL){
return true;
}
else{
return false;
}
}
void stack::Push(int num)
{
if(IsFull() == false)
{
node* newNode = new node;
newNode->input = num;
newNode->next = top;
top = newNode;
count ++;
}
}
int stack::Top()
{
int topval;
topval = top->input;
return topval;
}
int stack::Pop()
{
int Popped;
if (top != NULL)
{
node* temp = top;
top = top->next;
Popped = temp->input;
delete temp;
return Popped;
}
count--;
}
stack::~stack()
{
node* current = top;
while( top != NULL)
{
node* next = current->next;
delete current;
current = next;
}
top = NULL;
}
Input:
Please input 5 unique integers.
Click the ENTER key after each integer inputted.
7
1
56
67
8
Output:
Unsorted Stack:
8
67
56
1
7
Sorted Stack:
Segmentation fault (core dumped)
There are several problems.
First, your Pop:
int stack::Pop()
{
int Popped;
if (top != NULL)
{
node* temp = top;
top = top->next;
Popped = temp->input;
delete temp;
return Popped;
}
count--;
}
which decrements count only when the stack is empty, but doesn't return anything if it is.
You need something more like this:
int stack::Pop()
{
int Popped = -1; // Make sure the return value is well-defined
if (!IsEmpty())
{
node* temp = top;
top = top->next;
Popped = temp->input;
delete temp;
count--; // Only decrement if we actually popped something.
}
return Popped;
}
and the destructor:
stack::~stack()
{
node* current = top;
while( top != NULL)
{
node* next = current->next;
delete current;
current = next;
}
top = NULL;
}
The loop will never stop because you have the wrong termination condition - unless the stack is empty, top will never become NULL.
It should be
stack::~stack()
{
node* current = top;
while (current != NULL)
{
node* next = current->next;
delete current;
current = next;
}
top = NULL;
}
And the loop in main starts like this (I've removed some unnecessary parentheses):
while (!stack1.IsEmpty() && !stack3.IsEmpty()) {
if (stack1.IsEmpty() && !stack3.IsEmpty())
where the if condition will never be true if the while condition is - stack1 can't be both empty and not empty.
Side note: having a size limit when you implement a stack as a linked list is a bit odd.

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++ Hash Table Program Hangs after Entering a Value

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.

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... :)