problem in the Linked list

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
#include
#include

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("
No. 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("
No. of elements in linked list = %d",count(p));
del(&p,888);
del(&p,1);
del(&p,10);
display(p);
printf("
No. 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;ilink;
if(q==NULL)
{
printf("
There 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("
");
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("
Element %d not found ",num);
}



Comments

  • Sorry, I dont have the time to read all your code, but usually you can get the command where the exception occured. You are propably trying to free already deleted or misused memory. Check all your allocations / deallocations carefully.

    : 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
    : #include
    : #include
    :
    : 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("
    No. 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("
    No. of elements in linked list = %d",count(p));
    : del(&p,888);
    : del(&p,1);
    : del(&p,10);
    : display(p);
    : printf("
    No. 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;ilink;
    : if(q==NULL)
    : {
    : printf("
    There 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("
    ");
    : 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("
    Element %d not found ",num);
    : }
    :
    :
    :
    :



  • if(temp->data=num) should be if(temp->data==num) you are doing a comparison not an asignement.

    Also try using new instead of malloc as new handles memory more effectivley and has good exception handling, which will help when debugging.

    Any way malloc returns a void* so if you keep using it tyr changing your allocation code to

    : struct node *temp;
    :
    : temp=(node *)malloc(sizeof(struct node));

    Cremin









    : 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
    : #include
    : #include
    :
    : 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("
    No. 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("
    No. of elements in linked list = %d",count(p));
    : del(&p,888);
    : del(&p,1);
    : del(&p,10);
    : display(p);
    : printf("
    No. 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;ilink;
    : if(q==NULL)
    : {
    : printf("
    There 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("
    ");
    : 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("
    Element %d not found ",num);
    : }
    :
    :
    :
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories