hi
I got stuck with this piece of code...........it is compiling sucessfully but when I run the same it shows a message box with message
this program has performed an illegal operation and will be shut down
LINKEDLIST1 caused an invalid page fault in
module LINKEDLIST1.EXE at 0177:00401264.
Registers:
EAX=cdcdcdcd CS=0177 EIP=00401264 EFLGS=00010282
EBX=00550000 SS=017f ESP=0065fd48 EBP=0065fd98
ECX=cdcdcdcd DS=017f ESI=8171e408 FS=4e2f
EDX=00780eb0 ES=017f EDI=0065fd98 GS=0000
Bytes at CS:EIP:
83 79 04 00 74 0b 8b 55 fc 8b 42 04 89 45 fc eb
Stack dump:
0065fdf8 8171e408 00550000 cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc
I'm using VC++6.0 on windows 98
help me out of this junk!!!!!!!!!!!!!!!!!!
thanx in advance
/*prorgram for the linked list*/
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct node
{
int data;
struct node *link;
};
int main()
{
void display(struct node *q);
void append(struct node **q,int num);
void addatbeg(struct node **q,int num);
void addafter(struct node *q,int loc,int num);
void del(struct node **q,int num);
int count(struct node *q);
struct node *p;
p=NULL;
printf("\nNo. of elements in the linked list = %d",count(p));
append(&p,1);
append(&p,2);
append(&p,3);
//clrscr();
display(p);
addatbeg(&p,999);
addatbeg(&p,888);
addatbeg(&p,777);
display(p);
addafter(p,7,0);
addafter(p,2,1);
addafter(p,1,99);
display(p);
printf("\nNo. of elements in linked list = %d",count(p));
del(&p,888);
del(&p,1);
del(&p,10);
display(p);
printf("\nNo. of elements in linked list = %d",count(p));
getche();
return 0;
}
void append(struct node **q,int num)
{
struct node *temp;
temp=*q;
if(*q==NULL)
{
*q=malloc(sizeof(struct node));
temp=*q;
}
else
{
while(temp->link!=NULL)
temp=temp->link;
temp->link=malloc(sizeof(struct node));
temp=temp->link;
temp->data=num;
temp->link=NULL;
}
}
void addatbeg(struct node **q,int num)
{
struct node *temp;
temp=malloc(sizeof(struct node));
temp->data=num;
temp->link=*q;
*q=temp;
}
void addafter(struct node *q,int loc,int num)
{
struct node *temp;
int i;
for(i=0;i<loc;i++)
{
q=q->link;
if(q==NULL)
{
printf("\nThere are less than %d elements",loc);
return;
}
}
temp=malloc(sizeof(struct node));
temp->data=num;
temp->link=q->link;
q->link=temp;
}
void display(struct node *q)
{
printf("\n");
while(q!=NULL)
{
printf("%d",q->data);
q=q->link;
}
}
int count(struct node *q)
{
int c=0;
while(q!=NULL)
{
q=q->link;
c++;
}
return c;
}
void del(struct node **q,int num)
{
struct node *old,*temp;
temp=*q;
while(temp!=NULL)
{
if(temp->data=num)
{
if(temp==*q)
{
*q=temp->link;
free(temp);
return;
}
else
{
old->link=temp->link;
free(temp);
return;
}
}
else
{
old=temp;
temp=temp->link;
}
}
printf("\nElement %d not found ",num);
}