guys i was working on the link list program and its is encountring a problem its says some thing like

un handled exaption handling acess writing location error

i have no idea howto over come it so i thought may be u guys might help me with it
here is the code

// abdul muqeet khan.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include <cstdlib>
using namespace std;

struct node
{
int data;
node* next ;
node* previous;
};
node* head=NULL;
node*
previous=NULL;
char ch;
/////////////////////////////////////////////////////////////////////////////////////////
 // functio declarations 
 node* getnode();
 void addstart(int);
 void display();
 int  dataentry();
 ////////////////////////////////////////////////////////////////////////////////////////
int _tmain(int argc, _TCHAR* argv[])

{  while(ch!='y'||'Y')
{int c;
   c=dataentry();
   
   addstart(c);
   display();}   
   cout<<"do u wanna an other go ";
   cin>>ch;
   system("pause");	
   return 0;


}


int dataentry(void)
{ int c;
  cout<<"enter the num";
  cin>>c;
  return c;
}
 //////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
void addstart(int c)
{
 struct node*p;
 struct node*q;
  p=getnode();
  q=getnode();
  q=NULL;
  
  if(head==NULL)
  {p=head;
  p->next=q;
  q->next=NULL;
  }
  if(head!=NULL)
  
  {   p=head;
	  while(p->next!=NULL)
	  {
	  p=p->next;}
  } 
  p->next=q;
  q->data=c;
  q->next=NULL;
  system("pause");
  system("cls");
  
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
node* getnode()
{
 node*p;
 p=(node*)malloc(sizeof(node));
 return p;
}


void display()
{
 node*p;
 p=getnode();
 p=head;
 while(p->next!=NULL)
	 cout<<p->data;
 p=p->next;
}

Oh Lord, that's why the std containers were invented on the first place. Dealing with pointers can be a pain in the neck. They are hard to debug, and one mistake can easily lead to memory leaks.

One advice, in C++ use the new operator, instead of using malloc that's the C way. :)

I can see what you are trying to do, you want two pointers initialized as 'NULL' as kind of an anchor. One for the beginning and one for the ending, and in between all the nodes would connect to each other with, their own two pointers. So the end result would look like this:

NULL <- node -> <- node -> <- node -> <- node -> <- node -> NULL

By the looks of it, it would take some serious debugging to find where the error is, but there is a good news. In the STD library, you can find a predefined container called 'list' it works almost the same, except it really works(no offense). :)

All of the list elements, have two pointers, so inserting end removing from it is pretty darn fast. You can read about its advantages and disadvantages in the link: cplusplus.com - list.

Good luck with your code, I hope you find the error.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.