Doubly linked list implementation
Jul 20, 2016 at 5:01pm UTC
I have been trying to implement a barebones version of a doubly linked list(which supports only insert and printing all nodes in it).However upon executing the following code,I get a segmentation fault error.I tried debugging the code.The code fails when the constructor for Doubly_Linked_List class is executed.The code is given below,which was executed on Ubuntu 14.04 using g++.What am I doing wrong?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
#include <iostream>
using namespace std;
class Node
{
private :
int key;
Node *prev;
Node *next;
public :
Node()
{}
void set_key(int x)
{ key=x; }
void set_prev(Node *x)
{ prev=x; }
void set_next(Node *x)
{ next=x; }
int get_key()
{ return this ->key; }
Node *get_prev()
{ return this ->prev; }
Node *get_next()
{ return this ->next; }
};
class Doubly_Linked_List
{
private :
Node *head;
public :
Doubly_Linked_List()
{
cout<<"Reached here" <<endl;
head->set_key(0);
head->set_prev(NULL);
head->set_next(NULL);
}
void insert(Node x)
{
x.set_next(head->get_next());
if (head->get_next()!=NULL)
(head->get_next())->set_prev(&x);
head->set_next(&x);
x.set_prev(NULL);
}
void print_nodes()
{
Node *x=head->get_next();
while (x!=NULL)
{
cout<<x->get_key()<<endl;
x=x->get_next();
}
}
};
int main()
{
int n,j;
Doubly_Linked_List l;
cout<<"Enter number of elements" <<endl;
for (int i=0;i<n;i++)
{
cin>>j;
Node x;
x.set_key(j);
l.insert(x);
}
l.print_nodes();
return 0;
}
Jul 20, 2016 at 5:12pm UTC
Line 30: head is an uninitialized pointer (garbage).
Line 35-37: You're trying to store member variables using an uninitialized pointer. This is the cause of your crash.
Line 39: next is passed by value. You're going to change the value in a local copy. You want to pass Node by reference if you want to change it.
Line 46: x goes out of scope. References to x are no longer valid.
Line 60: n is uninitialized
Line 63: Your termination condition references an uninitialized variable (n).
Line 69: x goes out of scope for each iteration.
Jul 20, 2016 at 7:06pm UTC
Thanks for the quick reply.Followed the flaws you pointed out and got correct code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
#include <iostream>
using namespace std;
class Node
{
private :
int key;
Node *prev;
Node *next;
public :
Node()
{}
void set_key(int x)
{ key=x; }
void set_prev(Node *x)
{ prev=x; }
void set_next(Node *x)
{ next=x; }
int get_key()
{ return this ->key; }
Node *get_prev()
{ return this ->prev; }
Node *get_next()
{ return this ->next; }
};
class Doubly_Linked_List
{
private :
Node *head=new Node();
public :
Doubly_Linked_List()
{
head->set_key(0);
head->set_prev(NULL);
head->set_next(NULL);
}
void insert(int x)
{
// Insertion done at front of the list
Node *n=new Node();
n->set_key(x);
n->set_next(head->get_next());
if (head->get_next()!=NULL)
(head->get_next())->set_prev(n);
head->set_next(n);
n->set_prev(NULL);
}
void print_nodes()
{
Node *x=head->get_next();
while (x!=NULL)
{
cout<<x->get_key()<<endl;
x=x->get_next();
}
}
};
int main()
{
int n,x;
Doubly_Linked_List l;
cout<<"Enter number of elements" <<endl;
cin>>n;
cout<<"Enter the elements:" <<endl;
for (int i=0;i<n;i++)
{
cin>>x;
l.insert(x);
}
cout<<"List traversal" <<endl;
l.print_nodes();
return 0;
}
Last edited on Jul 20, 2016 at 7:07pm UTC
Topic archived. No new replies allowed.