?
Solved

Queue

Posted on 2004-03-23
8
Medium Priority
?
446 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
Identify and Prevent Potential Cyber-threats

Become the white hat who helps safeguard our interconnected world. Transform your career future by earning your MS in Cybersecurity. WGU’s MSCSIA degree program was designed in collaboration with national intelligence organizations and IT industry leaders.

 

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

Live webcast with Pinal Dave

Pinal Dave will teach you tricks to help identify the real root cause of database problems rather than red herrings. Attendees will learn scripts that they can use in their environment to immediately figure out their performance Blame Shifters and fix them quickly.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-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 nested-loops in the C programming language.
Suggested Courses

601 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