I wrote a program for doubly linked list in visual studio 2013, and it threw unhandled exception error at the line marked with comment: –
#pragma once #include<iostream> template <typename T> struct Node { T data; Node *next, *prev; }; template <typename T> class doubleLinkedList { private: Node<T> *top; public: doubleLinkedList() { top = nullptr; } void add(T data) { Node<T> *temp = new Node<T>; temp->prev = nullptr; temp->next = top; top->prev = temp; //unhandled exception here top = temp; } void display() { Node<T> *temp; temp = top; std::cout<<"nullptr<--->\n"; while(temp) { std::cout<<temp->data<<"<--->"; temp = temp->next; } std::cout<<"nullptr\n"; } };
The error is: –
Unhandled exception at 0x011C4B79 in dataStructures2.exe: 0xC0000005: Access violation writing location 0x00000008.
I have never written a doubly linked list before. I am not sure if there any logical error in the program. Any kind of help would be appreciated.