My program crashes after sequence of inserting head, then inserting tail, every other cases it seems to work. I cant figure it out.
struct Node {
int key;
Node *next;
};
struct List {
Node *head, *tail;
};
void init(List& l) {
l.head = l.tail = NULL;
}
void insertHead(List& l, int x) {
Node *temp=new Node;
temp->next=NULL;
temp->key=x;
temp->next=l.head;
l.head=temp;
}
void insertTail(List& l, int x) {
Node *temp=new Node;
temp->key=x;
temp->next=NULL;
if(l.head==NULL) {
l.head = temp;
l.tail = temp;
} else {
l.tail->next=temp;
l.tail=temp;
}
}
Thats only part of my code, but I think it will be enough, otherwise here is remaining part http://pastebin.com/WxmYJ0uE
temp->next=NULL;in the insertHead above, try cleaning up your code, and it will be a lot easier for you or any other person to fix your code.