0

"The code works if it has the int datatype in struct but doesn't work for char datatype in struct."

#include    <stdio.h>     
#include   <stdlib.h>
#define null 0

struct node{
        char data;//works fine if it is int data
        struct node *next;
       };

 void push(struct node **head)//add element to the queue 
 {
 struct node *newnode,*p,*q;
 char d;
 newnode=(struct node *)malloc(sizeof(struct node));
 printf("\nenter the data you want to insert:");
 scanf("%c",&d);
 newnode->data=d;
 if(*head!=null)
 {
     p=*head;
     while(p!=null)
     {
         q=p;
         p=p->next;
    }
     q->next=newnode;
     newnode->next=p;
 }
 else
    *head=newnode;
 printf("the data is %c\n",newnode->data);
  }

void pop(struct node **head)//pops element of the queue
{
struct node *p;
if(*head!=null)
{   
    p=*head;
    *head=p->next;
    printf("The data popped is %c \n",p->data);
    free(p);
}
else
    printf("no data to pop\n");
 }

void traverse(struct node **head)//displays the queue
{
struct node *k;
if(*head!=null)
{
    k=*head;
    printf("\nthe data of the queue is:\n");
    while(k!=0)
    {
        printf("%c\n",k->data);
        k=k->next;
    }
}
else 
    printf("no data\n");
   }

 void main()
 {
struct node *head=null;
int i,n;
printf("how many data you want to enter\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
    push(&head);
}
traverse(&head);
pop(&head);
traverse(&head);
 }

Output: ./queue

how many data you want to enter

3

enter the data you want to insert:the data is

enter the data you want to insert:a the data is a

enter the data you want to insert:the data is

the data of the queue is:

a

The data popped is

the data of the queue is: a

"

Paku
  • 93
  • 2
  • 2
  • 13
  • what does the debugger say? – pm100 Feb 26 '14 at 16:45
  • fine no problem..debugs ok – Paku Feb 26 '14 at 16:47
  • The question is ambiguous.. Could you be more specific? – Prabesh Feb 26 '14 at 16:30
  • Yes see the struct node.It works if it has int data.But for char it gives the output that is listed. – Paku Feb 26 '14 at 16:32
  • What doesn't work exactly? It seems to me that the output is ok. What did you expect? – exilit Feb 26 '14 at 16:38
  • No if i enter queue :a b c d e The it should show the data of the queue is : a b c d e The data popped is a the data in the queue is: b c d e.. – Paku Feb 26 '14 at 16:44
  • Try entering `abcde` (no space between). I think element 1 and 3 of your queue are whitespace characters. – Klas Lindbäck Feb 26 '14 at 16:45
  • A little bit more specific please. What is with the queue: a b c d e? The example above doesn't say anything about that case. – exilit Feb 26 '14 at 16:46
  • If i wanted to enter queue a b c d.Then it should have been like: how many data you want to enter 4 enter the data you want to insert:a the data is a enter the data you want to insert:b the data is b enter the data you want to insert:c the data is c enter the data you want to insert:d the data is d the data in the stack is: a b c d the data popped off isa the data in the stack is: b c d But this is not the output. – Paku Feb 26 '14 at 16:55
  • if the 'debugs ok' comment is in response to my debugger comment. Then no its not 'fine no problem' otherwise you would not be posting here. I mean 'did you step through the code in the debugger and watch whats happening?' , doing that will show you where things start going wrong – pm100 Feb 27 '14 at 00:10

3 Answers3

1

This may not be the only problem, but when you push the first element you never set newnode->next = null.

The problems with your output is because your queue consists of \n, a and (space).

Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
  • How is it that \n and space are a part?I am hardly using it as an input.And newnode->next=p works fine. – Paku Feb 26 '14 at 16:51
  • In contrast with most other format specifiers for scanf, `%c` doesn't skip whitespace. `newnode->next=p` is fine, but for the first element the code choses the `else` part of the `if` statement, and the `else` part doesn't have any assignment of `newnode->next`. – Klas Lindbäck Feb 27 '14 at 07:35
1

You need to clear the input buffer. The problem is because the enter(\n) you press after the first input (the number of inputs) is read a first input character.
read this link to know how to clear input buffer. It addresses the same problem of input followed by \n.

Community
  • 1
  • 1
LearningC
  • 3,182
  • 1
  • 12
  • 19
1

As a quick fix (ignoring safety, etc.), you can change scanf("%c,&d) to scanf("\n%c",&d) in the push function. This way, you will pick up the newline character and the actual character you want to push. Using the integer format in scanf will remove the newline, which is why it was working with integer.

see: scanf() leaves the new line char in buffer?

Community
  • 1
  • 1
emsworth
  • 1,149
  • 10
  • 21