/*Queue using doubly linked list*/
#include <iostream>
#include<stdlib.h>
usingnamespace std;
template<class t>
class dll{
public:
t data;
dll<t> *p,*n;
};
template<class t>
class que{
public:
dll<t> *temp,*newn,*f,*r;
que(){
f=r=NULL;
}
void insert(t);
void remove();
void display();
int search(t);
t get(int);
};
template<class t>void que<t>::insert(t x){
newn->data=x;
if(r==NULL){
newn->n=newn->p=NULL;
f=r=newn;
}
else{
newn->n=NULL;
newn->p=r;
r->n=newn;
r=newn;
}
}
template<class t>void que<t>::remove(){
if(r==NULL)
cout<<"Queue is empty."<<endl;
elseif(f==r){
newn=f;
f=r=NULL;
delete(newn);
}
else{
newn=f;
f=f->n;
f->p=NULL;
delete(newn);
}
}
template<class t>void que<t>::display(){
if(r==NULL)
cout<<"Queue is empty."<<endl;
elseif(f==r)
cout<<f->data;
else{
for(newn=f;newn->p->n!=NULL;newn=newn->n)
cout<<newn->data<<endl;
}
}
template<class t>int que<t>::search(t x){
int i=1;
if(r==NULL)
cout<<"Queue is empty."<<endl;
elseif(f==r){
if(f->data==x)
return i;
}
else{
for(newn=f;newn->p->n!=NULL;newn=newn->n){
if(newn->data==x)
return i;
else i++;
}
}
return 0;
}
template<class t>t que<t>::get(int x){
int i;
newn=f;
for(i=0;i<x;i++){
if(i+1==x)
return newn->data;
newn=newn->n;
}
}
main()
{
char c;
que<int> q;
do{
int o,x;
cout<<"1.Insert\n2.Remove\n3.Display\n4.search\n5.get\n6.exit\nchoose an option: ";
cin>>o;
switch(o){
default:
cout<<"You didn't choose correct option, by default queue is displayed"<<endl;
case 3:
q.display();
break;
case 1:
cout<<"what element you want to insert :";
cin>>x;
q.insert(x);
break;
case 2:
q.remove();
break;
}
cout<<"Do you want to continue[y/n] :";
cin>>c;
}while(c=='y'||c=='Y');
}
I'm getting
Segmentation fault (core dumped)
when I try to remove any element,
and I've also obsered that always that below statements
1 2
elseif(f==r)
cout<<f->data;
in the program(line no. 66,67) are getting executed irrespective of the no. of elements in the queue, where 'f' and 'r' are the pointers to front and rear elements of the queue.
Thank you, for replying but I didn't understand. newn is declared inline 21 and what happens if they are in public mode newn is a data member of que which can be used anywhere in the class and are objects of dll.
1) Making newn and temp public members of que is sloppy coding. There is no reason they should be public members of que. Variables should be as local as possible. Since newn and temp are not used across calls, they should be local where they are used and not public members of que.
2) Regarding line 33, where is newn initialized? This is what I mean by sloppy coding. Had you made newn local to insert, you probably would have recognized sooner that it was never initialized. Because newn is uninitialized, it is going to cause undefined behavior. Probably an address violation.
Like AbstractionAnon said, maybe something is not initialised. Set a break point at the lines where the error occurs, run debug build and have a look at the values of the variables.
1 2
elseif(f==r)
cout<<f->data;
My guess is that the variable f is pointing to something invalid.
/*Queue using doubly linked list*/
#include <iostream>
#include<stdlib.h>
usingnamespace std;
template<class t>
class dll{
public:
t data;
dll<t> *p,*n;
};
template<class t>
class que{
dll<t> *newn,*f,*r;
public:
que(){
newn=NULL;
f=NULL;
r=NULL;
}
void insert(t);
void remove();
void display();
int search(t);
t get(int);
int count();
};
template<class t>int que<t>::count(){
if(r==NULL)
return 0;
else{
int i=1;
for(newn=f;newn!=r;newn=newn->n);
return i;
}
}
template<class t>void que<t>::insert(t x){
newn->data=x;
if(r==NULL){
newn->n=newn->p=NULL;
f=r=newn;
}
else{
newn->n=NULL;
newn->p=r;
r->n=newn;
r=newn;
}
}
template<class t>void que<t>::remove(){
if(r==NULL)
cout<<"Queue is empty."<<endl;
elseif(f==r){
newn=f;
f=r=NULL;
delete(newn);
}
else{
newn=f;
f=f->n;
f->p=NULL;
delete(newn);
}
}
template<class t>void que<t>::display(){
if(r==NULL)
cout<<"Queue is empty."<<endl;
elseif(f==r)
cout<<f->data;
else{
for(newn=f;newn->p->n!=NULL;newn=newn->n)
cout<<newn->data<<endl;
}
}
template<class t>int que<t>::search(t x){
int i=1;
if(r==NULL)
return 0;
elseif(f==r){
if(f->data==x)
return i;
}
else{
for(newn=f;newn->p->n!=NULL;newn=newn->n){
if(newn->data==x)
return i;
else i++;
}
}
return 0;
}
template<class t>t que<t>::get(int x){
int i;
newn=f;
for(i=0;i<x;i++){
if(i+1==x)
return newn->data;
newn=newn->n;
}
}
int main()
{
char c;
que<int> q;
do{
int o,x;
int y;
cout<<"1.Insert\n2.Remove\n3.Display\n4.search\n5.get\n6.exit\nchoose an option: ";
cin>>o;
switch(o){
default:
cout<<"You didn't choose correct option, by default queue is displayed"<<endl;
case 3:
q.display();
break;
case 1:
cout<<"what element you want to insert :";
cin>>x;
q.insert(x);
break;
case 2:
q.remove();
break;
case 4:
cout<<"What do you want to search :";
cin>>x;
y=q.search(x);
if(y==0)
cout<<"Element not found"<<endl;
else
cout<<x<<" is in position "<<y<<"."<<endl;
break;
case 5:
cout<<"Which element do you want to know :";
cin>>y;
if(y>q.count()||y<=0)
cout<<"There's no element in that position"<<endl;
else{
x=q.get(y);
cout<<x<<" is in position "<<y<<endl;
}
break;
case 6:
exit(0);
}
cout<<"Do you want to continue[y/n] :";
cin>>c;
}while(c=='y'||c=='Y');
return 0;
}
Here I've observed that I am not able to insert an element, what I am getting is
1.Insert
2.Remove
3.Display
4.search
5.get
6.exit
choose an option: 1
what element you want to insert :1
Segmentation fault (core dumped)
Similar to what I was getting when I tried to remove an element before, and I've also observed that I'm not getting this error for inserting an element if I don't initialize newn value to NULL in the constructor in line 23 as,
I've also observed that if I add a new element to the queue the previous element is automatically getting removed, as if I didn't use break; in the switch block. And I've checked the values in the data in f and r after inserting a new element and both are pointing towards the new element instead of f pointing the first element, if I try to print a NULL value I'm getting
Always 0 is getting displayed, irrespective of the no. of elements in queue whereas remove is working fine if we take no. of elements into consideration. If I try to print the data in f and r, 0 is getting displayed.(may be I've done more blunders than before)
1.Insert
2.Remove
3.Display
4.search
5.get
6.exit
choose an option: 1
what element you want to insert :9
Do you want to continue[y/n] :y
1.Insert
2.Remove
3.Display
4.search
5.get
6.exit
choose an option: 1
what element you want to insert :8
Do you want to continue[y/n] :y
1.Insert
2.Remove
3.Display
4.search
5.get
6.exit
choose an option: 3
Segmentation fault (core dumped)
Thank you very much, no node's 'n' pointer will be NULL if next element actually exists, I shouldn't have done it so blindly. Thank you once again for helping me complete the program, I've just inserted another cout<<newn->data<<endl; after removing ->p.