Dec 9, 2014 at 1:31pm UTC
//cant find what is wrong with the code.. please help guys.. it gives some
//unhandled exception error
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
#include<iostream>
#include"conio.h"
using namespace std;
class DoublyNode
{
private :
int data;
DoublyNode* nxtPtr;
DoublyNode* prvPtr;
public :
DoublyNode()
{
data=0;
nxtPtr=NULL;
prvPtr=NULL;
}
void setData(int object)
{
data=object;
}
int getData()
{
return data;
}
void setNxtPtr(DoublyNode* nextNode)
{
nxtPtr=nextNode;
}
DoublyNode* getNxtPtr()
{
return nxtPtr;
}
void setPrvPtr(DoublyNode* prvNode)
{
prvPtr=prvNode;
}
DoublyNode* getPrvPtr()
{
return prvPtr;
}
};
class DoublyList
{
private :
DoublyNode* headNode;
DoublyNode* currentNode;
int size;
public :
DoublyList()
{
headNode=new DoublyNode();
headNode->setNxtPtr(NULL);
headNode->setPrvPtr(NULL);
currentNode=headNode;
size=0;
}
void add(int object)
{
DoublyNode* newNode=new DoublyNode();
newNode->setData(object);
if (headNode->getNxtPtr()!=NULL)
{
newNode->setNxtPtr(currentNode->getNxtPtr());
newNode->setPrvPtr(currentNode);
(currentNode->getNxtPtr())->setPrvPtr(newNode);
currentNode->setNxtPtr(newNode);
currentNode=newNode;
}
else
{
headNode->setNxtPtr(newNode);
newNode->setPrvPtr(headNode);
newNode->setNxtPtr(NULL);
currentNode=newNode;
}
size++;
}
void remove()
{
if (headNode->getNxtPtr()==NULL)
cout<<"\nThe List is empty" ;
else
{
if (currentNode->getNxtPtr()!=NULL)
{
cout<<"\nList Element " <<currentNode->getData()<<" will be removed now" ;
DoublyNode* temp=currentNode;
(currentNode->getPrvPtr())->setNxtPtr(currentNode->getNxtPtr());
(currentNode->getNxtPtr())->setPrvPtr(currentNode->getPrvPtr());
currentNode=currentNode->getNxtPtr();
delete temp;
}
else
{
DoublyNode* temp=currentNode;
(currentNode->getPrvPtr())->setNxtPtr(currentNode->getNxtPtr());
currentNode=currentNode->getPrvPtr();
delete temp;
}
size--;
}
}
int find(int object)
{
DoublyNode* temp=headNode->getNxtPtr();
int i=1;
while (temp!=NULL&&temp->getData()!=object)
{
temp=temp->getNxtPtr();
i++;
}
if (!temp) return 0;
else
{
currentNode=temp;
return i;
}
delete temp;
}
void start()
{
if (headNode->getNxtPtr()!=NULL)
{
currentNode=headNode->getNxtPtr();
}
}
int length()
{
return size;
}
int get()
{
if (headNode->getNxtPtr()!=NULL&¤tNode!=NULL)
return currentNode->getData();
}
bool next()
{
if (currentNode->getNxtPtr()!=NULL)
{
currentNode=currentNode->getNxtPtr();
return true ;
}
else
return false ;
}
bool prev()
{
if (currentNode!=headNode->getNxtPtr())
{
currentNode=currentNode->getPrvPtr();
return true ;
}
else
return false ;
}
void display()
{
DoublyNode* temp=headNode->getNxtPtr();
if (temp==NULL)
cout<<"\nThe List is empty" ;
else
{
cout<<"\nList:" <<endl;
while (temp!=NULL)
{
cout<<temp->getData()<<"," ;
temp=temp->getNxtPtr();
}
}
}
};
int main()
{
DoublyList a;
a.display();
a.add(2);
a.add(4);
a.add(7);
a.add(9);
a.add(8);
a.display();
getch();
return 0;
}
Last edited on Dec 9, 2014 at 3:15pm UTC
Dec 9, 2014 at 3:21pm UTC
I've tried running it in a debugger.. it shows a specific error window which says "Unhandled exception at 0x013C53C9 in DoublyLinkedList.exe: 0xC0000005: Access violation writing location 0x00000008."
Dec 9, 2014 at 3:57pm UTC
You need to look at the call stack at the point when the exception is thrown, to see where it's happening.
Dec 9, 2014 at 4:44pm UTC
I dont understand what you are saying o.o
Dec 9, 2014 at 6:06pm UTC
Do you know what a debugger is, and what features a debugger will provide to help you diagnose problems with your software?
Dec 9, 2014 at 6:26pm UTC
Exchange Line 71 and 72. It is a wrong logic if you don't exchange the 2 lines. Since you expect the node next to the current node is not null and you access it. It is yet null since you did not set a valid node next to the current node so null pointer access violation occured
Last edited on Dec 9, 2014 at 6:27pm UTC