Implementing a stack using an array - c++

I just implemented a stack using an array and am curious as to why people start their tops at -1. Is it more inefficient to start at 0? I have a programming assignment to implement a stack that performs basic functions, and tried doing it on my own first.
After I got it to work I looked around to see other implementations. Most people start their tops at -1. Is there a benefit to that? Is it wrong to start at 0?
here's my working code:
header file:
#ifndef H_Stack
#define H_Stack
#include <iostream>
using namespace std;
struct nodeType
{
int info;
nodeType *link;
};
class arrayStack
{
private:
int stackSize;
int stackTop;
int *stackArray;
public:
arrayStack(const int &x);
void push(const int &x);
bool is_full();
bool is_empty();
int size();
int top();
void pop();
~arrayStack();
};
class linkedStack
{
private:
nodeType *stackTop;
public:
linkedStack();
void push(const int &x);
int size();
int top();
void pop();
bool is_empty();
~linkedStack();
};
#endif
Imp file:
#include "stack.h"
#include <iostream>
#include <cassert>
using namespace std;
arrayStack::arrayStack(const int &x)
{
if (x <= 0)
{
stackSize = 20;
stackArray = new int[stackSize];
}
else
{
stackTop = 0;
stackSize = x;
stackArray = new int[stackSize];
}
}
bool arrayStack::is_full()
{
return (stackTop == stackSize);
}
void arrayStack::push(const int &x)
{
if (!is_full())
{
stackArray[stackTop] = x;
stackTop++;
}
}
bool arrayStack::is_empty()
{
return (stackTop == 0);
}
int arrayStack::size()
{
return stackSize;
}
int arrayStack::top()
{
assert(stackTop != 0);
return stackArray[stackTop - 1];
}
void arrayStack::pop()
{
if (!is_empty())
stackTop--;
else
{
cout << "can't pop from an empty stack.";
}
}
arrayStack::~arrayStack()
{
delete[] stackArray;
}
linkedStack::linkedStack()
{
stackTop = nullptr;
}
void linkedStack::push(const int &x)
{
nodeType *newNode;
newNode = new nodeType;
newNode->info = x;
newNode->link = stackTop;
stackTop = newNode;
}
int linkedStack::size()
{
int count = 0;
nodeType *temp;
temp = stackTop;
while (temp != nullptr)
{
temp = temp->link;
count++;
}
return count;
}
int linkedStack::top()
{
assert(stackTop != nullptr);
return stackTop->info;
}
void linkedStack::pop()
{
assert(!is_empty());
nodeType *temp = stackTop;
stackTop = stackTop->link;
delete temp;
}
bool linkedStack::is_empty()
{
return (stackTop == nullptr);
}
linkedStack::~linkedStack()
{
while (stackTop != nullptr)
{
pop();
}
}
it successfully pops/pushes. It is not circular so its not efficient or very useful... but I had to write it for school.

Using an initial top of -1 allows you to implement push with just:
stackArray[++stackTop] = x;
That said, an initial top of 0 would just need an equally efficient, if slightly more verbose two-liner:
stackArray[stackTop] = x;
++stackTop;
or to keep it a one-liner:
stackArray[stackTop++] = x;
where the latter is perfectly fine as long as the top is a primitive type (for user-defined classes, post-increment is significantly less efficient, as it necessarily involves a complete copy of the state; some people avoid post-increment in C++ in general to develop habits that don't cause problems for user-defined classes).
Point is, there is no special benefit to -1 vs. 0; there may be conventions shared by the code you're looking at, but all of it works.

Related

strdup for converting const char* to char*

I have designed for Huffman tree convert binary code with shorter bin code. In main if you call a Binary tree.init(q), then the tree would come out with key: frequency and value: bin code. The problem is converting const char* with char*. I've looked at some codes, and here I converted it by using strdup. Sometimes works fine but sometimes doesn't work. so I checked out the parameter for the function. Is there wrong in calling strdup or maybe others?
#pragma once
#include <stdio.h>
#include <queue>
#include <iostream>
#include "pch.h"
#include <string.h>
#include <string>
#define _CRT_SECURE_NO_WARNINGS
//this is a header file
using namespace std;
class Node
{
public:
//key : frequency, value : code
int f;
char* code;
Node* left;
Node* right;
int getFrequency()
{
return f;
}
char* getCode()
{
return code;
}
void init(int frequency, char* codestring)
{
f = frequency;
code = codestring;
}
Node* getLeft() {
return left;
}
Node* getRight()
{
return right;
}
void setLeft(Node* L)
{
left = L;
}
void setRight(Node* R)
{
right = R;
}
void setFrequency(int frequency)
{
f = frequency;
}
void setCode(char* string)
{
code = string;
}
};
class BinaryTree
{
public:
typedef priority_queue<int, vector<int>, greater<int>> pq;
pq q;
Node* proot;
int sizeofqueue;
void init(pq PriorityQueue)
{
q = PriorityQueue;
sizeofqueue = q.size();
N = 0;
int comparetimes = q.size() - 1;
for (int i = 0; i < comparetimes; i++)
{
if (i == 0)
{
put_first_two_nodes();
}
else
{
if (proot->getFrequency() <= q.top())
{
put_right_node();
}
else if (proot->getFrequency() > q.top())
{
put_left_node();
}
q.pop();
}
}
}
void put_first_two_nodes()
{
Node* pleft = new Node();
(*pleft).setFrequency(q.top());
(*pleft).setCode("0");
q.pop();
Node* pright = new Node();
(*pright).setFrequency(q.top());
(*pright).setCode("1");
put(pleft, pright);
q.pop();
}
void put_right_node()
{
Node* pright = new Node();
pright->setFrequency(q.top());
pright->setCode("1");
put(proot, pright);
appendcode(0);
}
void appendcode(int prefix)
{
string pre;
if (prefix == 1) pre = "1";
else pre = "0";
Node* targetNode = proot->getRight();
char* rcode = targetNode->getRight()->getCode();
char* lcode = targetNode->getLeft()->getCode();
string lefts = pre;
string rights = pre;
lefts.append(lcode);
rights.append(rcode);
char* leftstring = strdup(lefts.c_str());
char* rightstring = strdup(rights.c_str());
targetNode->getLeft()->setCode(leftstring);
targetNode->getRight()->setCode(rightstring);
free(leftstring);
free(rightstring);
}
void put_left_node()
{
Node* pleft = new Node();
pleft->setFrequency(q.top());
pleft->setCode("0");
put(pleft, proot);
appendcode(1);
}
char* get(int k)
{
return getItem(*proot, k);
}
char* getItem(Node root, int k)
{
//if there's no node
if (&root == nullptr) return "";
//if f or root > k, search left sibling
if (root.getFrequency() > k) return getItem(*(root.getLeft()), k);
//else, search right sibling
else if (root.getFrequency() < k) return getItem(*(root.getRight()), k);
//get it
else return root.getCode();
}
void put(Node* left, Node* right)
{
put_item(left,right);
}
void put_item(Node* left, Node* right)
{
//make new node that has sibling with left and right
Node* newnode = new Node();
newnode->setLeft(left);
newnode->setRight(right);
//exchange the new node and root without losing data
Node* temp;
temp = proot;
proot = newnode;
newnode = temp;
//proot's frequency : left f + right f
(*proot).setFrequency((*left).getFrequency() + (*right).getFrequency());
}
void printpost()
{
postorder(proot);
}
void postorder(Node* root)
{
if (root != nullptr)
{
if (root->getLeft() != nullptr) postorder(root->getLeft());
if (root->getRight() != nullptr) postorder(root->getRight());
printf("%d : %s ",root->getFrequency(), root->getCode());
}
}
private:
int N;
Node root;
};
You shouldn't use const char* and char* at all in c++ (unless when sometimes dealing with legacy or foreign interfaces).
Switch up your code to use eg. std::string or std::string_view (c++17) instead (string_view requires a bit more understanding to handle correctly and is const so to speak - so I would stick to string off the bat). Pass std::string by reference or by const reference where neccesary. The overhead of std::string is for most programs negliable.

C++Linked list non-member function to reverse print

So I understood how to print a single linked list in reverse order using recursion. I'm having trouble with doing it non member functions.
For example in int print_reverse(IntSLList & list)) function how do you print reverse in an iterative way?
************************ .h file **************************
class IntSLLNode {
public:
IntSLLNode() {
next = 0;
}
IntSLLNode(int el, IntSLLNode *ptr = 0) {
info = el; next = ptr;
}
int info;
IntSLLNode *next;
};
class IntSLList {
public:
IntSLList() {
head = 0;
}
~IntSLList();
int isEmpty() {
return head == 0;
}
void addToHead(int);
void addToTail(int);
int deleteFromHead(); // delete the head and return its info;
int deleteFromTail(); // delete the tail and return its info;
bool isInList(int) const;
void printAll() const;
private:
IntSLLNode *head;
};
and here is the main
************************ main **************************
#include <iostream>
using namespace std;
#include "intSLList.h"
int print_reverse(IntSLList & list){
if (head == NULL)
return;
printReverse(head->next);
cout << head->data << " ";
//How to compelete this in an iterative(or recursive if iterative is too much work)way ?
//like this?
}
int main() {
IntSLList list;
list.print_reverse(list);
}
Added the functions
The header gives literally no way to access the contents of the list, other than by destroying it. So ... that's what we're going to do.
int deleteFromTail(); // delete the tail and return its info;
Except we need to go the extra step and rebuild it, because nobody expects printing the container to destory its contents. See https://en.wikipedia.org/wiki/Principle_of_least_astonishment
#include <iostream>
#include <stack>
#include <list>
class IntSLList {
public:
int isEmpty() { return m_list.empty(); }
void addToHead(int i) { m_list.push_front(i); }
void addToTail(int i) { m_list.push_back(i); }
int deleteFromHead() {
int temp = m_list.front();
m_list.pop_front();
return temp;
}
int deleteFromTail() {
int temp = m_list.back();
m_list.pop_back();
return temp;
}
private:
// no implementation given so I'm using std::list internally.
std::list<int> m_list;
};
int print_reverse(IntSLList& mylist) {
// store the data we are destroying in temp
IntSLList temp;
// literally the only way we can access the contents of the container is destructive so ... guess we're going there
while (!mylist.isEmpty()) {
int back = mylist.deleteFromTail();
std::cout << back << std::endl;
temp.addToHead(back);
}
// now rebuild the original list. I told you this would be bad.
while (!temp.isEmpty()) {
mylist.addToHead(temp.deleteFromTail());
}
// maybe this was supposed to be length, but not documented so I can return whatever I want.
return -1;
}
int main() {
IntSLList mylist;
mylist.addToTail(1);
mylist.addToTail(2);
mylist.addToTail(3);
print_reverse(mylist);
}
3
2
1

How to fix Access Violation Reading location error? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am currently working on a program that uses a Hash table. I have worked on my own Hash table class and the program works but then crashes after it has already done the work involving the hash table. The error I get is a Access Violation reading location error. I have spent hours going through my code and still cannot find what I'm doing wrong or why the program is crashing. Here are my problem classes below:
Hashtable.h:
#ifndef HASHTABLE_H
#define HASHTABLE_H
#include <string>
#include "LinkedList.h"
#include <iostream>
using namespace std;
class hashTable
{
public:
hashTable();
virtual ~hashTable();
void insertNode(string nodeData);
bool removeNode(string nodeKey);
Node * checkForDuplicate( string nodeData );
private:
LinkedList * tableArray;
int length;
int hash(string stateKey);
};
#endif // HASHTABLE_H
Hashtable.cpp:
#include "hashTable.h"
hashTable::hashTable()
{
length = 181667;
tableArray = new LinkedList[length];
}
int hashTable::hash(string stateKey) {
int multiplier = 1;
int total = 0;
int l = stateKey.length();
for(int i = l - 1; i > -1; --i) {
int temp;
temp = (stateKey[i] - '0') * multiplier;
total += temp;
multiplier = multiplier * 10;
}
return(total) % length;
}
void hashTable::insertNode(string stateData) {
Node * newNode;
newNode = new Node;
newNode->data = stateData;
int index = hash(newNode -> data);
tableArray[index].insertNode(newNode);
delete newNode;
}
bool hashTable::removeNode(string nodeKey) {
int index = hash(nodeKey);
return tableArray[index].removeNode(nodeKey);
}
Node * hashTable::checkForDuplicate( string nodeData )
{
int index = hash( nodeData );
return tableArray[ index ].getNode(nodeData);
}
hashTable::~hashTable()
{
delete [] tableArray;
//dtor
}
LinkedList.h:
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include<string>
#include<iostream>
using namespace std;
struct Node {
string data;
Node *next;
};
class LinkedList
{
public:
LinkedList();
void insertNode(Node * newNode);
bool removeNode(string stateData);
Node * getNode(string stateData);
int getLength();
virtual ~LinkedList();
private:
Node * top;
int length;
};
#endif // LINKEDLIST_H
LinkedList.cpp:
#include "LinkedList.h"
LinkedList::LinkedList()
{
top = new Node;
top->next = NULL;
length = 0;
}
void LinkedList :: insertNode(Node * newNode) {
Node * a = top;
Node * b = top;
while(b) {
a = b;
b = a -> next;
if (a== NULL) { break; }
}
a -> next = newNode;
newNode -> next = NULL;
length++;
}
bool LinkedList :: removeNode(string stateData) {
if(!top -> next){
return false;
}
Node * a = top;
Node * b = top;
while(b) {
if(b->data == stateData) {
a->next = b->next;
delete b;
length--;
return true;
}
a = b;
b = a ->next;
}
return false;
}
Node * LinkedList :: getNode(string stateData) {
if(top == NULL) { return NULL ;}
Node * current = top;
while (current->next != NULL) {
if((current->data == stateData)) {
return current;
}
current = current -> next;
}
return NULL;
}
int LinkedList :: getLength() {
return length;
}
LinkedList::~LinkedList()
{
Node * a = top;
Node * b = top;
while (b) {
a = b;
b = a->next;
if(b) delete a;
}
}
Your hashTable::insertNode() method is allocating a new Node object, then passing it to LinkedList::insertNode() to take ownership of the object, but then delete's it afterwards, thus leaving the LinkedList with a dangling pointer to invalid memory. Any access to that node will cause undefined behavior. DO NOT delete the new node after LinkedList takes ownership of it.
It would be better if LinkedList::insertNode() took a string as input instead of a Node* pointer. Let LinkedList allocate the new node internally.
Also, there are some other minor issues with your LinkedList() implementation in general (like not following the Rule of Three, and not using a double-linked list for more efficient inserts and removals).
Try something more like this instead:
Hashtable.h:
#ifndef HASHTABLE_H
#define HASHTABLE_H
#include <string>
#include "LinkedList.h"
class hashTable
{
public:
hashTable();
hashTable(const hashTable &src);
~hashTable();
void insertNode(const std::string &nodeData);
bool removeNode(const std::string &nodeData);
bool checkForDuplicate(const std::string &nodeData);
hashTable& operator=(const hashTable &rhs);
private:
std::vector<LinkedList> tableArray;
int length;
int hash(const std::string &nodeData);
};
#endif // HASHTABLE_H
Hashtable.cpp:
#include "hashTable.h"
hashTable::hashTable()
: length(181667), tableArray(new LinkedList[length])
{
}
hashTable::hashTable(const hashTable &src)
: length(src.length), tableArray(new LinkedList[length])
{
for (int i = 0; i < length; ++i)
tableArray[i] = src.tableArray[i];
}
hashTable::~hashTable()
{
delete[] tableArray;
}
hashTable& hashTable::operator=(const hashTable &rhs)
{
hashTable tmp(rhs);
std::swap(tableArray, tmp.tableArray);
std::swap(length, tmp.length);
return *this;
}
int hashTable::hash(const std::string &nodeData)
{
int multiplier = 1;
int total = 0;
int l = nodeData.length();
for(int i = l - 1; i > -1; --i)
{
int temp = (nodeData[i] - '0') * multiplier;
total += temp;
multiplier *= 10;
}
return total % length;
}
void hashTable::insertNode(const std::string &nodeData)
{
int index = hash(nodeData);
tableArray[index].insertNode(nodeData);
}
bool hashTable::removeNode(const std::string &nodeData)
{
int index = hash(nodeData);
return tableArray[index].removeNode(nodeData);
}
bool hashTable::checkForDuplicate(const std::string &nodeData)
{
int index = hash(nodeData);
return (tableArray[index].getNode(nodeData) != NULL);
}
LinkedList.h:
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <string>
struct Node
{
std::string data;
Node *previous;
Node *next;
};
class LinkedList
{
public:
LinkedList();
LinkedList(const LinkedList &src);
~LinkedList();
void insertNode(const std::string &nodeData);
bool removeNode(const std::string &nodeData);
Node* getNode(const std::string &nodeData);
int getLength();
LinkedList& operator=(const LinkedList &rhs);
private:
Node *head;
Node *tail;
int length;
};
#endif // LINKEDLIST_H
LinkedList.cpp:
#include "LinkedList.h"
#inclue <algorithm>
LinkedList::LinkedList()
: head(NULL), tail(NULL), length(0)
{
}
LinkedList::LinkedList(const LinkedList &src)
: head(NULL), tail(NULL), length(0)
{
Node *current = src.top;
while (current != NULL)
{
insertNode(current->data);
current = current->next;
}
}
LinkedList::~LinkedList()
{
Node *current = top;
while (current != NULL)
{
Node *next = current->next;
delete current;
current = next;
}
}
LinkedList& LinkedList::operator=(const LinkedList &rhs)
{
LinkedList tmp;
Node *current = rhs.top;
while (current != NULL)
{
tmp.insertNode(current->data);
current = current->next;
}
std::swap(top, tmp.top);
std::swap(bottom, tmp.bottom);
std::swap(length, tmp.length);
return *this;
}
void LinkedList::insertNode(const string &nodeData)
{
Node *newNode = new Node;
newNode->data = nodeData;
newNode->previous = NULL;
newNode->next = NULL;
if (top == NULL) top = newNode;
if (bottom != NULL)
{
newNode->previous = bottom;
bottom->next = newNode;
}
bottom = newNode;
length++;
}
bool LinkedList::removeNode(const string &nodeData)
{
Node* node = getNode(nodeData);
if (node != NULL)
{
if (node->next != NULL)
node->next->previous = node->previous;
if (node->previous != NULL)
node->previous->next = node->next;
if (top == node)
top = node->next;
if (bottom == node)
bottom = node->previous;
delete node;
length--;
return true;
}
return false;
}
Node* LinkedList::getNode(const string &nodeData)
{
Node *current = top;
while (current != NULL)
{
if (current->data == nodeData)
return current;
current = current->next;
}
return NULL;
}
int LinkedList::getLength()
{
return length;
}
With that said, you can then get rid of LinkedList altogether by using std::list instead, and simplify hashTable's memory management by using std::vector:
Hashtable.h:
#ifndef HASHTABLE_H
#define HASHTABLE_H
#include <string>
#include <list>
#include <vector>
class hashTable
{
public:
hashTable();
void insertNode(const std::string &nodeData);
bool removeNode(const std::string &nodeData);
bool checkForDuplicate(const std::string &nodeData);
private:
std::vector< std::list<std::string> > tableArray;
int hash(const std::string &stateKey);
};
#endif // HASHTABLE_H
Hashtable.cpp:
#include "hashTable.h"
#include <algorithm>
hashTable::hashTable()
: tableArray(181667)
{
}
int hashTable::hash(const std::string &nodeData)
{
int multiplier = 1;
int total = 0;
int l = nodeData.length();
for(int i = l - 1; i > -1; --i)
{
int temp = (nodeData[i] - '0') * multiplier;
total += temp;
multiplier *= 10;
}
return total % length;
}
void hashTable::insertNode(const std::string &nodeData)
{
int index = hash(nodeData);
tableArray[index].push_back(nodeData);
}
bool hashTable::removeNode(const string &nodeData)
{
int index = hash(nodeData);
std::list<std::string>::iterator iter = std::find(tableArray[index].begin(), tableArray[index].end(), nodeData);
if (iter != tableArray[index].end())
{
tableArray[index].erase(iter);
return true;
}
return false;
}
bool hashTable::checkForDuplicate(const std::string &nodeData)
{
int index = hash(nodeData);
std::list<std::string>::iterator iter = std::find(tableArray[index].begin(), tableArray[index].end(), nodeData);
return (iter != tableArray[index].end());
}

SIGSEGV error with BFS Algorithm

I have a BFS algorithm error.
Have tried to debug with the gdb but I don't understand why I get this.
Can anyone tell me why i get a SIGSEGV error with the code below. Does it depend on the compiler that you use how the pointers are addressed? As there is an invalid pointer error in the code
#include<iostream>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
using namespace std;
const int MAX = 8;
struct Node
{
int data;
Node *next;
};
class Graph
{
private:
int visited[MAX];
int q[8];
int front, rear;
public:
Graph();
void BFS(int v, Node **p);
Node *getNode_Write(int val);
static void addQueue(int *a, int vertex, int *f, int *r);
static int deleteQueue(int *q, int *f, int *r);
static int isEmpty(int *f);
void del(Node *n);
};
// initialize data memeber
Graph::Graph()
{
for(int i = 0; i < MAX; i++)
visited[i] = FALSE;
front = rear = -1;
}
// function that implements breadth first search (BFS) algorithm
void Graph::BFS(int v, Node **p)
{
Node *u;
visited[v-1] = TRUE;
cout<<v<<"\t";
addQueue(q, v, &front, &rear);
while(isEmpty(&front) == FALSE)
{
v = deleteQueue(q, &front, &rear);
u = *(p+v-1);
while(u != NULL)
{
if(visited[u->data-1] == FALSE)
{
addQueue(q, u->data, &front, & rear);
visited[u->data-1] == TRUE;
cout<<u->data<<"\t";
}
u = u->next;
}
}
}
// Creates a node
Node *Graph::getNode_Write(int val)
{
Node *newNode = new Node;
newNode->data = val;
return newNode;
}
//Adds node to the queue
void Graph::addQueue(int *a, int vertex, int *f, int *r)
{
if(*r == MAX -1)
{
cout<<"\nQueue Overflow.";
exit(0);
}
(*r)++;
a[*r] = vertex;
if(*f == -1)
*r = 0;
}
// Deletes a node from the queue
int Graph::deleteQueue(int *a, int *f, int *r)
{
int data;
if(*f == -1)
{
cout<<"\nQueue Underflow";
exit(0);
}
data = a[*f];
if(*f == *r)
*f = *r = -1;
else
(*f)++;
return data;
}
// checks if queque is empty
int Graph::isEmpty(int *f)
{
if(*f == -1)
return TRUE;
return FALSE;
}
// deallocate the memory
void Graph::del(Node *n)
{
Node *temp;
while(n != NULL)
{
temp = n->next;
delete n;
n = temp;
}
}
int main()
{
Node *arr[MAX];
Node *v1,*v2,*v3,*v4;
Graph g;
v1 = g.getNode_Write(2);
arr[0] = v1;
v1->next = v2 = g.getNode_Write(3);
v2->next = NULL;
v1 = g.getNode_Write(1);
arr[1] = v1;
v1->next = v2 = g.getNode_Write(4);
v2->next = v3 = g.getNode_Write(5);
v3->next = NULL;
cout<<endl;
g.BFS(1,arr);
for(int i = 0; i<MAX; i++)
g.del(arr[i]);
}
There is an uninitialized array arr in the stack frame of main. Only arr[0] and arr[1] become initialized. At the end of main it is iterated over the whole array and delete is called in Graph::del(Node *n) on a garbage value.
"Does it depend on the compiler that you use how the pointers are addressed?"
No, it doesn't depend on the compiler primarily. As Joachim pointed out in his comment:
To see the real source of the error, you should just step up the stack trace, and check out how all the variables and parameters were actually set.
Most likely you've been calling some undefined behavior, due to missing, or wrong variable initializations.

C++ "No appropriate default constructor available"

I am trying to create a linked list of arrays without using the STL. However, I am having difficulties passing the array to my Linked List...
When I compile I get the error listed above. How do I need to pass the array to the linked list? Thanks! (The code in question is marked by **, please remove if testing.)
SinglyLinkedList.h
#pragma once
#ifndef SinglyLinkedList_h
#define SinglyLinkedList_h
#include<iostream>
template <typename Type>
struct node
{
Type value;
node *next;
};
template <typename Object>
class SinglyLinkedList
{
private:
node<Object> *head;
public:
SinglyLinkedList();
~SinglyLinkedList();
bool insert(Object x);
bool empty();
};
template <typename Object>
SinglyLinkedList<Object>::SinglyLinkedList()
{
this->head = NULL;
}
template <typename Object>
bool SinglyLinkedList<Object>::insert(Object x)
{
node<Object> *temp = new node<Object>;
temp->value = x;
temp->next = NULL;
if (this->head==NULL)
{
this->head = temp;
}
else
{
node<Object> *S = this->head, *P = S;
while ((S->value < temp->value)&&(S != NULL))
{
S = S->next;
P = S;
}
if(S == NULL)
temp->next = P;
else
{
temp->next = S;
P->next = temp;
}
}
return true;
}
template <typename Object>
bool SinglyLinkedList<Object>::empty()
{
if(this->head == NULL)
return true;
else
return false;
}
template <typename Object>
SinglyLinkedList<Object>::~SinglyLinkedList()
{
delete this->head;
}
#endif
DynamicArrayClass.h
#pragma once
#ifndef DynamicArrayClass_h
#define DynamicArrayClass_h
#include<iostream>
template <class T>
class DynamicArrayClass
{
private:
T *array;
int size, numItems;
public:
DynamicArrayClass(int newSize)
{
size = newSize;
numItems=0;
array = new T[size];
}
int GetSize(){ return size;}
int GetNumItems() const { return numItems; }
bool isEmpty() const { return numItems==0; }
bool isFull() const { return numItems==size; }
bool addItem (const T &object)
{
if(isFull())
{
return false;
}
else
{
array[numItems++] = object;
return true;
}
}
const T& getItem(int index) {return array[index];}
void makeEmpty()
{
numItems = 0;
}
~DynamicArrayClass()
{
if(array !NULL)
delete [] array;
}
};
#endif
main.cpp
#include "DynamicArrayClass.h"
#include "SinglyLinkedList.h"
#include "stopwatch.h"
#include<iostream>
int main()
{
int totalCapacity = 0;
int arrayAddSize = 0;
while(totalCapacity < 10000)
{
if(totalCapacity==0)
{
DynamicArrayClass<int> *array1 = new DynamicArrayClass<int>(25);
totalCapacity = 25;
SinglyLinkedList<DynamicArrayClass<int>> *list = new SinglyLinkedList<DynamicArrayClass<int>>();
for(int i = 0; i<25; i++)
{
array1->addItem(1);
}
**list->insert(*array1);**
}
else
{
arrayAddSize = (totalCapacity/2);
totalCapacity = totalCapacity + arrayAddSize;
DynamicArrayClass<int> *array = new DynamicArrayClass<int>(arrayAddSize);
SinglyLinkedList<DynamicArrayClass<int>> *list = new SinglyLinkedList<DynamicArrayClass<int>>();
for(int i=0; i <arrayAddSize; i++)
{
array->addItem(1);
}
}
}
return 0;
}
The problem is in this part of insert:
node<Object> *temp = new node<Object>;
where the node contains an Object. To construct that, the Object needs a default constructor.
Perhaps you can add a constructor to node that copies the value it has to store? That would make it, for example:
node<Object> *temp = new node<Object>(x, NULL);
node<Object> *temp = new node<Object>;
This line in SinglyLinkedList::insert causes the error I assume. The problem is, that your node struct looks like this:
template <typename Type>
struct node
{
Type value;
node *next;
};
That Type value; will be default constructed by the new node<Object> call. Provide an appropriate constructor for the node struct and you should be fine.