Solved

Queue

Posted on 2004-03-23
8
434 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 75 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
 

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now