"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
"