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.

Recommended Answers

All 9 Replies

Add Temp=NULL in your constructor.

Add Temp=NULL in your constructor.

That doesn't seem to have done anything. It's still quitting after I choose a number to add.

Eh silly me, why do you have all those returns? They are useless.

*for the void functions

Eh silly me, why do you haev all those returns? They are useless.

You don't think they're the cause of my problems do you? I know they're pointless, but my professor taught us to use them, so I guess it's just a bad habit.

I use Devc++ as my compiler so i never had to do int main() { code return 0; } so I'm fetching a guess here, but I DO remeber every time I did use it, it would exit out of my program, but it can't hurt to try it, I've frankly never seen a return; for a void function in my life(6 months of coding tbe, not much, I know)

Well I tried it, but as I'd imagined, it didn't change much of anything.

Temp is an unitialized and unallocated pointer. See the two lines I changed below

void Deque::Addfront(int n)
{
    Temp = new node; // <<<< Here
	Temp->value=n;
	if (front==NULL)
	{
		Temp->link=front;
		front=back=Temp;
	}
	else
	{
		Temp->link=front;
		front = Temp; // <<<< here
	}
return;
}

*bangs head against wall* how did I miss that?

*bangs head against wall* how did I miss that?

Ditto. Thanks a lot for the help guys. Really appreciate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.