Thread: problem in the Linked list

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    21

    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<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);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void del(struct node **q,int num) 
    { 
        struct node *old,*temp; 
        temp=*q; 
    
        while(temp!=NULL) 
        { 
            if(temp->data=num) /** ERROR HERE **/
            {
    There's a problem. This will always evaluate as true. You are
    using '=' instead of '=='. That is to say, you're assigning the value of num to temp->data every time this if check happens, rather than checking to see if temp->data is equal to num.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM