Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Queue

Posted on 2004-03-23
8
Medium Priority
?
445 Views
Last Modified: 2010-04-15
#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?
0
Comment
Question by:Mayur00
  • 3
  • 2
  • 2
  • +1
8 Comments
 

Accepted Solution

by:
GlennJ earned 225 total points
ID: 10661658
It has nothing (really) to do with the printf statement.  In reality, you have NOT created an array...

You create a pointer (q) to a struct "qu", however, you allocate no storage.  So whatever q happens to be when your program fires up, is where it *thinks* is a copy of the struct.  In reality, it is random memory.

If you want to handle this as an array (I don't recommend it), then you need to allocate storage, such as

struct qu
{     int data[128] ;
     int front ;
     int rear ;
} q[10] ;

which allocates 10 qu's of space.

However, the concept of Add and Delete are really not valid (the storage is there, and you wouldn't manage it with a queue anyhow.

Therefore, each time you add, you would have to allocate space, e.g.

qu *x = malloc(sizeof(qu));

However, you have to handle your pointers within your struct differently (instead of front, you'd have a prev).  In a LIFO queue, you don't really need the rear (or if you did have a rear, you'd need a next pointer too to do anything useful).


0
 
LVL 3

Expert Comment

by:guynumber5764
ID: 10663766
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.
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 10665592
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:GlennJ
ID: 10667677
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...
0
 

Author Comment

by:Mayur00
ID: 10671028
Sorry for replying late as my server was down.Thanks GlennJ and ssnkumar .My pointer basics are getting better .. :)
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 10674058
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
0
 

Author Comment

by:Mayur00
ID: 10793814
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 .
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 10803487
But, you didn't wanted to give points to me!!:-(

-ssnkumar
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question