Hi All,
I am a newbie and am traking a course in Aptech for C++. Now my instructor asked me to make a program to create, display and insert nodes in a circular link list. I have made the following code:
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <new>
class clinklist
{
public:
int data;
clinklist *link;
}*start;
void create();
void display();
void IFB();
void IFE();
void IAD();
void IBD();
using namespace std;
int main()
{
int choice = 0;
while(choice != 7)
{
system("CLS");
cout << "Circular Link List" << endl << endl;
cout << "1. Create" << endl;
cout << "2. Display" << endl;
cout << "3. Insert From Beginning" << endl;
cout << "4. Insert From Ending" << endl;
cout << "5. Insert After Data" << endl;
cout << "6. Insert Before Data" << endl;
cout << "7. Exit" << endl << endl;
cout << "Enter Your Choice: ";
cin >> choice;
switch(choice)
{
case 1:
create();
break;
case 2:
display();
break;
case 3:
IFB();
break;
case 4:
IFE();
break;
case 5:
IAD();
break;
case 6:
IBD();
break;
case 7:
cin.get();
break;
}
}
}
void create()
{
clinklist *n, *ptr;
ptr = start;
n = new (nothrow) clinklist;
if(n==0)
{
cout <<"Memory Could Not Be Allocated";
cin.get();
return;
}
cout << endl << endl <<"Enter Data For New Node: ";
cin >> n->data;
if(start == NULL)
{
start = n;
n->link=start;
}
else
{
n->link=start;
while(ptr->link!=start)
ptr = ptr->link;
}
ptr->link = n;
}
void display()
{
int i = 1;
clinklist *ptr = start;
while(ptr->link != start)
{
cout << endl << endl << "Node " << i << ": " << ptr ->data << endl;
ptr = ptr -> link;
i++;
}
i++;
cout << cout << "Node " << i << ": " << ptr ->data << endl;
}
void IFB()
{
cout << "Under Development!" << endl;
}
void IFE()
{
cout << "Under Development!" << endl;
}
void IAD()
{
cout << "Under Development!" << endl;
}
void IBD()
{
cout << "Under Development!" << endl;
}
Now the VS2010 compiler/debugger is showing a problem at line "ptr->link = n;" with the following error message:
Unhandled exception at 0x001019bb in Circular Link List.exe: 0xC0000005: Access violation writing location 0x00000004.
Can anyone tell me the reason ASAP, as I have to submit the code this monday?
Regards,
Shikhin