Solved

Queue

Posted on 2004-03-23
8
436 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C#, VS15, StructLayout 1 134
SCANF - LIMIT THE NUMBER OF CHARARACTERS 1 62
Problem to ASCII 1 176
Arduino EDI - Programming language 3 94
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

776 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