I am kind of new to c++, i usually code in java and php. This is a program that is supposed to read in an input file with very strict syntax that will either create a new stack/queue (implemented subclasses of simpleList), or push a value onto one of those stacks/queues assuming it exists, or it will pop a value from them, again, assuming they exist.
the acceptable types are int, string, double, therefore templates are used. the first letter of the name of each stack/queue has to be either i,s,d.
example:
create i99 stack
push dHelloWorld -.0343
pop s55
i have most of it working, but I got to segfaults, I've been debugging for hours and I have no idea. the debugger traces the problem back to void setnext(Node *t)....
any help would be much appreciated, below is my code
#include <iostream>
#include <string>
#include <list>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
using namespace std;
//with a lot of help from the internet and the textbook
template <typename Object>
class simpleList{
private:
string name;
int Size;
class Node
{
Object data;
Node *next;
public:
Node ( Object d, Node *n )
{
data = d;
next =n;
}
Object retval()
{ return data; }
Node *retpoint()
{ return next; }
void setdata(Object t)
{ data = t; }
void setnext(Node *t )
{ next = t; }
};
public:
string getName()
{
return name;
}
Node *head;
Node *tail;
simpleList()
{
Object *d = new Object; //dummy value
head = new Node(*d, NULL);
head = tail;
}
int size()
{ return Size;}
void changeSize(int i)
{ Size = i; }
virtual void push(Object x) = 0; //pushes to the front for both queue and stack, the difference will lie in how they are removed};
virtual Object pop() = 0;
protected: // change all the -> to Node functions
void push_front(Object x)
{
Node *p = new Node(x, head->retpoint());
Size++;
head->setnext(p);
}
void push_back(Object x)
{
Node *p = new Node(x, NULL);
if(tail == head)
head->setnext(p);
else
tail->setnext(p);
Size++;
tail = p;
}
Object remove()
{
Node *p = head->retpoint();
Size--;
head->setnext( p->retpoint()) ;
Object data = p->retval();
delete p;
return data;
}
void changeName(string s)
{
name = s;
}
};
template<typename Object>
class Stack : public simpleList<Object>
{
private:
public:
Stack(string naam)
{
simpleList<Object>::changeName(naam);
//simpleList<Object>::head = new Node(*d, tail);
simpleList<Object>::tail = NULL;
simpleList<Object>::changeSize(0);
}
void push(Object x)
{
push_front(x);
}
Object pop()
{
return simpleList<Object>::remove();
}
};
template<typename Object>
class Queue : public simpleList<Object>
{
private:
public:
Queue(string naam)
{
simpleList<Object>::changeName(naam);
//simpleList<Object>::head = new Node(*d, tail);
simpleList<Object>::tail = NULL;
simpleList<Object>::changeSize(0);
}
void push(Object x)
{
push_back(x);
}
Object pop()
{
return simpleList<Object>::remove();
}
};
template <typename Object>
simpleList<Object> * search(list<simpleList<Object> *> a, string name)
{
int index=0;
for(class list<simpleList<Object> *>::iterator ci = a.begin(); ci !=a.end(); ++ci)
{
index++;
if((*ci)->getName().compare(name) == 0)
return *ci;
}
if(index == a.size())
return NULL;
};
int main () {
list<simpleList<int> *> listSLi; //all the integer stacks or queues
list<simpleList<double> *> listSLd; //all the double stacks or queues
list<simpleList<string> *> listSLs; //all the string stacks or queues
string line;
char file[50];
string str1 = "pop";
cout << "write a filename!\t";
cin >> file;
//simpleList<int> *sl = new Stack<int>("ihello");
//listSLi.push_back(sl);
ifstream myfile (file);
if (myfile.is_open())
{
while (! myfile.eof() )
{
getline (myfile,line);
cout << line << endl; //remove
string cmd = line;
if(line.substr(0,3).compare(str1) < 0) // create
{
size_t pos = line.find_last_of(' ');
string adt = line.substr(pos+1,line.size() -1); //stack or queue
line.resize(pos);
size_t pos2 = line.find_first_of(' ');
string name = line.substr(pos2,line.size() -1); //name of stack or queue
char type = name.at(1);//i,d,s
if(adt == "stack")
{
if(type == 'i')
{
simpleList<int> *pSLi = new Stack<int>(name);
if(listSLi.size() == 0)
{
listSLi.push_front(pSLi);
cout<<name<<"Added"<<endl;
}
else{
if(search(listSLi,name) != NULL)
cout<<"Error:this name already exists"<<endl;
else
{
listSLi.push_front(pSLi);
cout<<name<<"Added"<<endl;
}
}
}
else if(type == 'd')
{
simpleList<double> *pSLd = new Stack<double>(name);
if(listSLd.size() == 0)
{
listSLd.push_front(pSLd);
cout<<name<<"Added"<<endl;
}
else{
if(search(listSLd,name) != NULL)
cout<<"Error:this name already exists"<<endl;
else
{
listSLd.push_front(pSLd);
cout<<name<<" Added"<<endl;
}
}
}
else if(type == 's')
{
simpleList<string> *pSLs = new Stack<string>(name);
if(listSLs.size() == 0)
{
listSLs.push_front(pSLs);
cout<<name<<" Added"<<endl;
}
else{
if(search(listSLs,name) != NULL)
cout<<"Error:this name already exists"<<endl;
else
{
listSLs.push_front(pSLs);
cout<<name<<" Added"<<endl;
}
}
}
}
else if(adt == "queue")
{
if(type == 'i')
{
simpleList<int> *pSLi = new Queue<int>(name);
if(listSLi.size() == 0)
{
listSLi.push_front(pSLi);
cout<<name<<" Added"<<endl;
}
else
{
if(search(listSLi,name) != NULL)
cout<<"Error:this name already exists"<<endl;
else
{
listSLi.push_front(pSLi);
cout<<name<<" Added"<<endl;
}
}
}
else if(type == 'd')
{
simpleList<double> *pSLd = new Queue<double>(name);
if(listSLd.size() == 0)
{
listSLd.push_front(pSLd);
cout<<name<<" Added"<<endl;
}
else{
if(search(listSLd,name) != NULL)
cout<<"Error:this name already exists"<<endl;
else
{
listSLd.push_front(pSLd);
cout<<name<<" Added"<<endl;
}
}
}
else if(type == 's')
{
simpleList<string> *pSLs = new Queue<string>(name);
if(listSLs.size() == 0)
{
listSLs.push_front(pSLs);
cout<<name<<" Added"<<endl;
}
else
{
if(search(listSLs,name) != NULL)
cout<<"Error:this name already exists"<<endl;
else
{
listSLs.push_front(pSLs);
cout<<name<<" Added"<<endl;
}
}
}
}
}
else if(line.substr(0,3).compare(str1) == 0) // pop
{
size_t pos = line.find_last_of(' ');
string name = line.substr(pos,line.size() -1);
char type = name.at(1); //i,s,d
if(type == 'i')
{
if(listSLi.size() != 0)
{
simpleList<int> *pLi = search(listSLi,name);
if(pLi == NULL)
cout<<"Error: this name does not exist"<<endl;
else if(pLi->size() == 0)
cout<<"Error: This list is empty!"<<endl;
else
cout<< pLi->pop()<<endl;
}
else
cout<<"Error: this name does not exist"<<endl;
}
if(type == 'd')
{
if(listSLd.size() != 0)
{
simpleList<double> *pLd = search(listSLd,name);
if(pLd == NULL)
cout<<"Error: this name does not exist"<<endl;
else if(pLd->size() == 0)
cout<<"Error: This list is empty!"<<endl;
else
cout<< pLd->pop()<<endl;
}
else
cout<<"Error: this name does not exist"<<endl;
}
if(type == 's')
{
if(listSLs.size() != 0)
{
simpleList<string> *pLs = search(listSLs,name);
if(pLs == NULL)
cout<<"Error: this name does not exist"<<endl;
else if(pLs->size() == 0)
cout<<"Error: This list is empty!"<<endl;
else
cout<< pLs->pop()<<endl;
}
else
cout<<"Error: this name does not exist"<<endl;
}
}
else if(line.substr(0,3).compare(str1) > 0) // push
{
size_t pos = line.find_last_of(' ');
string val = line.substr(pos+1,line.size() -1);
char cval[val.size()];
for(int i =0; i<val.size(); i++)
cval[i] = val.at(i);
line.resize(pos);
size_t pos2 = line.find_first_of(' ');
string name = line.substr(pos2,line.size() -1); //name of stack or queue
char type = name.at(1);//i,d,s
if(type == 'i')
{
if(listSLi.size() != 0)
{
simpleList<int> *pSi = search(listSLi,name);
if(pSi == NULL)
cout<<"ERROR: This name does not exist!"<<endl;
else
{
int ival = atoi(cval);
pSi->push(ival);
}
}
else
cout<<"Error: this name does not exist"<<endl;
}
else if(type == 'd')
{
if(listSLd.size() != 0)
{
simpleList<double> *pSd = search(listSLd,name);
if(pSd == NULL)
cout<<"ERROR: This name does not exist!"<<endl;
else
{
double dval = atof(cval);
pSd->push(dval);
}
}
else
cout<<"Error: this name does not exist"<<endl;
}
else if(type == 's')
{
if(listSLs.size() !=0)
{
simpleList<string> *pSs = search(listSLs,name);
if(pSs == NULL)
cout<<"ERROR: This name does not exist!"<<endl;
else
pSs->push(val);
}
else
cout<<"Error: this name does not exist"<<endl;
}
}
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}