I'm trying to write a simple deque program that lets you add/delete numbers and display them at will. The code is as follows.
User.cpp:
#include <iostream>
#include "Doth.h"
using namespace std;
int main()
{
char choice='m';
int n;
Deque call;
while(choice!='q'||choice!='Q')
{
cout<<"Menu:"<<endl;
cout<<"[A] Add to the front"<<endl;
cout<<"[B] Add to the back"<<endl;
cout<<"[C] Delete from the front"<<endl;
cout<<"[D] Delete from the back"<<endl;
cout<<"[E] Show the front"<<endl;
cout<<"[F] Show the back"<<endl;
cout<<"[G] Show all"<<endl;
cout<<"[Q] Quit"<<endl;
cin>>choice;
if ((choice=='a')||(choice=='A'))
{
cout<<"Please enter the number you would like to add."<<endl;
cin>>n;
call.Addfront(n);
}
if ((choice=='b')||(choice=='B'))
{
cout<<"Please enter the number you would like to add."<<endl;
cin>>n;
call.Addback(n);
}
if ((choice=='c')||(choice=='C'))
{
call.Delfront();
}
if ((choice=='d')||(choice=='D'))
{
call.Delback();
}
if ((choice=='e')||(choice=='E'))
{
call.Returnf();
if (call.flag!=1)
{
cout<<"The front value is: "<<call.f<<endl;
}
}
if ((choice=='f')||(choice=='F'))
{
call.Returnb();
if (call.flag!=1)
{
cout<<"The last value is: "<<call.b<<endl;
}
}
if ((choice=='g')||(choice=='G'))
{
call.Showall();
}
if ((choice=='q')||(choice=='Q'))
{
return(0);
}
}
return(0);
}
.h
struct node
{
int value ;
node * link;
};
typedef node * Ptr;
class Deque
{
private:
Ptr front,back,Temp,TA;
public:
Deque();
int f,b,flag;
void Addfront(int n);
void Addback(int n);
void Delfront();
void Delback();
void Showall();
int Returnf();
int Returnb();
};
Deque.cpp
#include <iostream>
#include "Doth.h"
using namespace std;
Deque::Deque()
{
flag=0;
front=NULL;
back=NULL;
}
void Deque::Addfront(int n)
{
Temp->value=n;
if (front==NULL)
{
Temp->link=front;
front=back=Temp;
}
else
{
Temp->link=front;
Temp=front;
}
return;
}
void Deque::Addback(int n)
{
Temp->value=n;
if (front==NULL)
{
Temp->link=front;
front=back=Temp;
}
else
{
Temp->link=back;
back=Temp;
}
return;
}
void Deque::Delfront()
{
if (front==NULL)
{
cout<<"The deque is already empty."<<endl;
return;
}
else if (front==back)
{
front=back=NULL;
}
else
{
Temp=front->link;
front=Temp;
delete Temp;
}
return;
}
void Deque::Delback()
{
TA=new(node);
TA=front;
Temp=front;
if (front==NULL)
{
cout<<"The deque is already empty."<<endl;
return;
}
else if (front==back)
{
front=back=NULL;
}
else
{
Temp=Temp->link;
while(Temp!=back)
{
Temp=Temp->link;
TA=TA->link;
}
delete Temp;
back=TA;
}
return;
}
int Deque::Returnf()
{
if (front==NULL)
{
cout<<"The deque is currently empty."<<endl;
flag=1;
}
else
{
int f;
f=front->value;
}
return(f);
}
int Deque::Returnb()
{
if (front==NULL)
{
cout<<"The deque is currently empty."<<endl;
flag=1;
}
else
{
int b;
b=back->value;
}
return(b);
}
void Deque::Showall()
{
Temp=front;
if (front==NULL)
{
cout<<"The deque is currently empty.\n"<<endl;
return;
}
while(Temp!=NULL)
{
cout<<"The deque is as follows:"<<endl;
cout<<Temp->value<<" ";
Temp=Temp->link;
}
return;
}
The problem I seem to be having is that when I try to add a number to either the front or back, the program terminates on me.
I believe the line in question is:
Temp->value=n;
but I don't know why it's causing it to terminate on me. Any help would be greatly appreciated.