mav mav
asked on
What's wrong with the delete function in this program for queue using array?
Here goes the code, and the compiler shows the warning : qmain.c:49:20: error: expected ‘;’, ‘,’ or ‘)’ before ‘.’ token
void qdelete(int* q.front, int* q.rear)
However, corrected codes and programs for college assignment of Data Structures and Algorithms are available on this site -->> http://speccyme.blogspot.in/
If any one needs help and solved assignments in Data Structures and Algorithms you may visit this link.
Thanks.
void qdelete(int* q.front, int* q.rear)
#include<stdio.h>
#include<stdlib.h>
#define size 50
typedef struct queue
{
int a[size];
int front;
int rear;
}ob;
ob q;
void qdelete(int*, int*);
void insert ()
{
int ele;
printf("\nEnter the element to be inserted:\t");
scanf("%d", &ele);
if((q.rear+1)%size==q.front)
// same as this : if((q.front==0)&&(q.rear=size-1)||(q.rear+1==q.front))
{
printf("\nQUEUE IS FULL !!\nNo more elements can be inserted.");
return;
}
else
{
if(q.rear==size-1)
q.rear=0;
else
q.rear++;
}
q.a[q.rear]= ele;
if(q.front==-1)
{
q.front=0;
printf("%d rear = %d front is the first element of the queue.", q.a[q.rear], q.a[q.front]);
}
printf("\n%d Inserted",q.a[q.rear]);
}
void qdelete(int* q.front, int* q.rear)
{
if(q.front==-1)
{
printf("\nQueue empty");
return;
}
else
{
printf("\nElement output %d", q.a[q.front] );
if(q.front==q.rear)
{
q.front=-1;
q.rear=-1;
}
else
{
q.front==(q.front+1)%size;
/* same as this:
if(q.front==size-1)
q.front=0;
else
q.front++; */
}
printf("\nDeleted");
}
}
void display ()
{
int i=q.front, j=q.rear;
printf("\nDISPLAY:\t");
while(i!=j)
{
printf(" %d ", q.a[i++]);
}
printf (" %d", q.a[i]);
printf("\n");
}
void main()
{
int choice;
q.front = -1;
q.rear = -1;
while(1)
{
printf("\n\n\n1. Add data.\n2. Delete data.\n3. Display data.\n4. Exit program.\n\nEnter your choice:\t");
scanf("%d", &choice);
switch(choice)
{
case 1: insert(); break;
case 2: qdelete(&q.front, &q.rear); break;
case 3: display (); break;
case 4: exit(0);
default: printf("\n\nHey!! Choose an option from the choices mentioned!!\n And stop testing my program!!");
}
}
}
However, corrected codes and programs for college assignment of Data Structures and Algorithms are available on this site -->> http://speccyme.blogspot.in/
If any one needs help and solved assignments in Data Structures and Algorithms you may visit this link.
Thanks.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks @evilrix,
I understood what you just told me. But when i do so that is i modified the delete function as this:
But now it compiles, but doesn't delete the entry at front. :|
Kindly help.
I understood what you just told me. But when i do so that is i modified the delete function as this:
void qdelete(int* front, int* rear)
But now it compiles, but doesn't delete the entry at front. :|
Kindly help.
Okay, well that's really a different problem and as such should be asked as a new question.
ASKER
But can anyone or you help me with this??
I want the qdelete function to delete the node entries.
I want the qdelete function to delete the node entries.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yes Thanks....it worked :)
some hints to improve your code:
(1) make the queue dynamic in size
with this code you would omit the global q and do in main
the qinsert and qdelete would get argument queue * pq.
and all q. would turn to pq-> for example
(2) move console input and console output from qinsert and qdelete into separate functions. for example
then in main you do
where qinsert is declared like
you even could return an int as status code where 0 is success and -1 for example means 'queue is full'.
this would allow to move error handling into an error function or into main.
Sara
(1) make the queue dynamic in size
typedef struct queue
{
int * a;
int size;
int front;
int rear;
} queue;
queue * qcreate(int siz)
{
Queue * pq = (Queue *)malloc(sizeof(struct queue));
pq->a = (int *)malloc(sizeof(int)*siz);
pq->siz = size;
return pq;
}
void qfree(queue * pq) { free(pq->a); free (pq); }
with this code you would omit the global q and do in main
queue * pq = qcreate(50);
....
qfree(pq);
the qinsert and qdelete would get argument queue * pq.
and all q. would turn to pq-> for example
pq->front = (pq->front + 1)%pq->size;
(2) move console input and console output from qinsert and qdelete into separate functions. for example
int newelement()
{
int ele;
printf("\nEnter the element to be inserted:\t");
scanf("%d", &ele);
return ele;
}
then in main you do
case 1: qinsert(pq, newelement()); break;
where qinsert is declared like
void qinsert(queue * pq, int ele);
you even could return an int as status code where 0 is success and -1 for example means 'queue is full'.
this would allow to move error handling into an error function or into main.
Sara
void qdelete(ob * q);