Howdy, Stranger!

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

Categories

whats wrong in this code?

1101111011 DehradunMember Posts: 5

I am trying to implement queue using linklist but only 1 element gets added to the queue.Can anyone point out the error?.thank You!

include<memory.h>

include<stdio.h>

include<stdlib.h>

struct node{
int data;
struct nodelink;
}
;
void addq(struct node,struct node,int);
int delq(struct node,struct node);
int main()
{
struct node *front,
rear;
int item;
front=rear=NULL;

addq(&front,&rear,11);
addq(&front,&rear,12);
addq(&front,&rear,13);
addq(&front,&rear,14);
addq(&front,&rear,15);
addq(&front,&rear,16);
addq(&front,&rear,17);
q_display(front);
printf("\n No. of items in the queue %d",count(front));
printf("\nItems extracted from the queue ");
item=delq(&front,&rear);
printf("\n%d",item);
item=delq(&front,&rear);
printf("\n%d",item);
item=delq(&front,&rear);
printf("\n%d",item);
printf("\n");
q_display(front);
printf("\n No. of items in the queue %d",count(front));
}

void addq(struct nodef,struct noder,int item)
{
struct nodeq;
q=malloc(sizeof(struct node));
q->data=item;
q->link=NULL;
if(
f==NULL)//emptty
{
*f=q;//front now points to q which is the first node

}
else
{
  (*r)->link=q;//r was pointing to last node r=&lastnode now last node'slink is q (*r)=q

  }
  *r=q;

}
int delq(struct node**f,struct node **r)
{
struct nodeq;
int item;
if(
f==NULL)
{
printf("queue is empty");

}
else
    {
     q=*f;
item=q->data;
*f=q->link;
free(q);
/*if on deletion queue has become empty*/
if(*f==NULL)
    *r=NULL;
return(item);
}

}
q_display(struct nodeq)
{
printf("\n front->");
while(q!=NULL)
{
if(q->link=NULL)
printf("<-rear");
printf("%2d",q->data);
q=q->link;
}
printf("\n");
}
int count(struct node
q)
{
int c=0;
while(q!=NULL)
{
q=q->link;
c++;
}
return c;
}

Comments

  • SixtyHzSixtyHz USMember Posts: 13

    Does that even compile, i see a bunch of strange pointer operations that make no sense??? in your node struct "struct node link" should be "struct node *link", in your main "struct node *front, rear" should be "struct node *front, *rear"... your function calls are taking actual structs instead of pointer to structs as well... is this actual code? or is something being lost in this site's formatting?

  • 1101111011 DehradunMember Posts: 5
    edited February 2015

    > @SixtyHz said:

    Does that even compile, i see a bunch of strange pointer operations that make no sense??? in your node struct "struct node link" should be "struct node *link", in your main "struct node *front, rear" should be "struct node *front, *rear"... your function calls are taking actual structs instead of pointer to structs as well... is this actual code? or is something being lost in this site's formatting?

    It is lost in site formatting and yes it does compile>I will try again and this time i will try to put a snapshot along

    include<memory.h>

    include<stdio.h>

    include<stdlib.h>

    struct node{
    int data;
    struct nodelink;
    }
    ;
    void addq(struct node,struct node,int);
    int delq(struct node,struct node);
    int count(struct node
    );
    int delq(struct node**,struct node **);
    void q_display(struct node*);

    int main()
    {
    struct node front,rear;
    int item;
    front=rear=NULL;

    addq(&front,&rear,11);
    addq(&front,&rear,12);
    addq(&front,&rear,13);
    addq(&front,&rear,14);
    addq(&front,&rear,15);
    addq(&front,&rear,16);
    addq(&front,&rear,17);
    q_display(front);
    printf("\n No. of items in the queue %d",count(front));
    printf("\nItems extracted from the queue ");
    item=delq(&front,&rear);
    printf("\n%d",item);
    item=delq(&front,&rear);
    printf("\n%d",item);
    item=delq(&front,&rear);
    printf("\n%d",item);
    printf("\n");
    q_display(front);
    printf("\n No. of items in the queue %d",count(front));
    }
    

    void addq(struct nodef,struct noder,int item)
    {
    struct nodeq;
    q=(struct node *)malloc(sizeof(struct node));
    q->data=item;
    q->link=NULL;
    if (
    f == NULL)
    f=q;
    //q->link=NUll;//or(
    f)->link=NULL;
    else
    (*r)->link=q;
    *r=q;
    }
    int delq(struct node**f,struct node **r)
    {
    struct node*q;
    int item;
    if(*f==NULL)
    {
    printf("queue is empty");

    }
    else
        {
         q=*f;
    item=q->data;
    *f=q->link;
    free(q);
    /*if on deletion queue has become empty*/
      if(*f==NULL)
    {
        *r=NULL;
    }
    
    return(item);
    }
    

    }

    void q_display(struct node*q)
    {
    printf("\n front->");
    while(q!=NULL)
    {
    if(q->link=NULL)
    printf("<-rear");
    printf("%d",q->data);
    q=q->link;
    }
    printf("\n");
    }

    int count(struct node*q)
    {
    int c=0;
    while(q!=NULL)
    {
    q=q->link;
    c++;
    }
    return (c);
    }

  • 1101111011 DehradunMember Posts: 5
    edited February 2015

    it is lost again.any pointers how to upload proper code on this site?

  • 1101111011 DehradunMember Posts: 5
  • 1101111011 DehradunMember Posts: 5

    code is lost in formatting.are there any better sites?

  • SixtyHzSixtyHz USMember Posts: 13
    edited February 2015

    yeah that is strange. And yeah the code formatting is quite random, i would try posting on stackoverflow.com...

Sign In or Register to comment.