Link to home
Start Free TrialLog in
Avatar of Mayur00
Mayur00

asked on

Queue

#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct qu
{      int data[128] ;
      int front ;
      int rear ;
} *q ;
void init()
{
      q->front = 0 ;
      q->rear = 0 ;
}
void add(int x)
{
q->rear=q->rear + 1 ;
q->data[q->rear] = x ;
//printf(" %d. %d ",q->rear,q->data[q->rear]);
}

void getdata()
{
int n ;
printf("\n\n\n");
lab1:
printf("\nEnter data into node or ' 0 ' to quit : ");
scanf("%d",&n);
if(n!=0)
{
      add(n);
      goto lab1 ;
}
}
void del()
{
if(q->front==q->rear)
{
      printf("\n\n\n     The Queue is Empty");
      getch();
}
else
{
      q->front=q->front + 1;
      q->data[q->front] = 0 ;
}
}
void display()
{
int i ;
printf("\n\n");
for(i=(q->front);i<(q->rear);i++)
printf("\n\n     %d",q->data[i+1]);
getch();
}
void main()
{
char ch;
int i ;
init();
while(1)
{
clrscr();
printf("\n\n\n\n      1.  Enter data into the Queue  ");
printf("\n\n      2.  Display data from the queue ");
printf("\n\n      3.  Delete an element from the queue ");
printf("\n\n      4.  Exit ");
ch=getch();
switch(ch)
{
      case '1':
      getdata();
      break ;
      case '2':
      display();
      break ;
      case '3':
      del();
      break;
      case '4':
      exit(1);
}
}
}
The above program implements simple queue using array.It runs successfully if i remove the comment (which i have put in the void add(int) function..which is //printf(" %d. %d ",q->rear,q->data[q->rear]);
)
else the program fails to run.This is strange ..as the printf statement only does the job of printing.
So why it doesnt print otherwise ?(without that printf statement as display function is actually doing printing)what is the significance of that printf statement?
ASKER CERTIFIED SOLUTION
Avatar of GlennJ
GlennJ

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of guynumber5764
guynumber5764

or add
q = malloc (sizeof(struct qu));
near the beginning of main.  It won't fix everything but it'll deal with the biggest issue.
Avatar of Narendra Kumar S S
I have changed your Init() and add() functions. Here is the code:
void init()
{
     q = (struct qu *) malloc(1 * sizeof(struct qu));

     q->front = 0 ;
     q->rear = 0 ;
}

void add(int x)
{
    q->data[q->rear] = x ;
    q->rear=q->rear + 1 ;
   
    printf(" %d. %d ",q->rear,q->data[q->rear]);
}

I have few suggestions.
1. You are maintaining queue in the array data[] inside the structure. So, you don't need a pointer to the structure to handle data. A simple variable of type "struct qu" would do. But, if you change that, then you will have to access the members of the structure by . operator instead of ->.
2. In the del() function you are checking if the queue is empty. In the same way, in the add function, you must check if the queue is full.

-ssnkumar
If you don't malloc more space in add(), you will run into the same problem if you try to add more than 1 element in your queue (after all, that's the intention, right?).

Mayur00, this looks like an assignment - why don't you try again and let us know if you have any problems...
Avatar of Mayur00

ASKER

Sorry for replying late as my server was down.Thanks GlennJ and ssnkumar .My pointer basics are getting better .. :)
I don't know if you took my suggestions. It would have improved your program and without that your program will have many bugs!

-ssnkumar
Avatar of Mayur00

ASKER

hi ssnkumar,
YES i TOOK ur suggestions ssnkumar.I did that program just because of my "inquisitiveness" (when we are creating a struct why not have an array in the struct itself(though it is not efficient)) and i also didnt bother about queue details( like checking whether it is full) SINCE i was very much surprised when i run the program half way -it was working without that simple "printf" statement.Thank you for ur suggestions though..
-Mayur .
But, you didn't wanted to give points to me!!:-(

-ssnkumar