[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 353
  • Last Modified:

Help on dqueue program

In the below attached code, while adding using option 1, option 3&4 to print works ok. But after using opton 2 (dqueue), the printing options 3&4 does not work ok. Where am I making the mistake.



after selecting option 2 to dequeue, option 3 and

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>


/* ------------------------------------------------------- */
/* Definitions */

#define READAHEAD 1
#define READBACK 2

typedef struct queuenode
{
      int val;
      struct queuenode *next,*prev;
}node;

node *front=NULL, *rear=NULL;


/* ------------------------------------------------------- */
/* Declarations */
void enqueue(int);
int dequeue(void);
void printqueue(char);


/* ------------------------------------------------------- */
/* Entry Point - Start here */
void main()
{
      int ch,num;

      do
      {
            /* Read User Input */
            clrscr();
            printf("\n\n  DQUEUE SOLUTION - MAIN MENU");
            printf("\n-----------------------------\n");
            printf("\n1 - ADD (Add value to Queue)");
            printf("\n2 - REMOVE (Remove node from Queue)");
            printf("\n3 - Front to Rear Printing");
            printf("\n4 - Rear to Front Printing");
            printf("\n5 - Exit\n");
            printf("\nChoose the option :- ");

            /* accepting user's choice */
            scanf("%d",&ch);

            switch(ch)
            {
                  case 1: /* inserting value to queue at front */
                        printf("\nEnter the value to Enqueue :- ");
                        scanf("%d", &num);
                        enqueue(num);
                        break;

                  case 2: /* deleting value from rear side of queue */
                        if(front != NULL) /* checking if queue exists */
                        {
                              num=dequeue();

                              /* print the dequeued value */
                              printf("\nThe Dequeued value is - %d.",num);
                        }
                        else
                              printf("\n\nEmpty Queue.");
                        break;

                  case 3: /* printing from front to rear */
                        if(front != NULL)
                        {
                              printf("\nThe Queue from Front to Rear is -\n");
                              printqueue(READAHEAD);
                              getch();
                        }
                        else
                              printf("\n\nEmpty Queue.");
                        break;
                  
                  case 4:
                        if(front != NULL) /* printing from rear to front */
                        {
                              printf("\nThe Queue from Rear to Front is -\n");
                              printqueue(READBACK);
                              getch();
                        }
                        else
                              printf("\n\nEmpty Queue.");
                        break;

                  case 5:
                        break;

                  default:
                        printf("\nKindly enter the correct choice (1-5).");
                        break;
            }
      }while(ch!=5);
}



/* -------------------------------------------------------  */
/* Below function is to add new queues at rear end of the queue */
void enqueue(int num)
{
      node *temp=(node*)malloc(sizeof(node));
      
      temp->val=num;
      temp->next=temp->prev=NULL;

      if(front==NULL)
            front=temp;
      else
      {
            rear->next=temp;
            temp->prev=rear; /* value added after rear  */
      }
      rear=temp; /* rear represents the new member */
}



/* -------------------------------------------------------  */
/* this function deletes the front value from queue and returns it */
int dequeue()
{
      node *temp;
      int delval;

      if(front==NULL)
            return -999;

      temp=front;
      front=front->next; /* front moves to next member in queue */
      
      if(front==NULL)
            rear=NULL; /* rear also changes if there was only one value in queue */
      else
            front->next=NULL;

      delval=temp->val;
      free(temp); /* memory free */
      
      return delval; /* deleted value returned */
}



/* ------------------------------------------------------- */
/* As per user choice, print the queue */

void printqueue(char ch)
{
      node *temp;

      if(front==NULL)
      {
            printf("\n\nEmpty Queue.");
            return;
      }

      /* looping from front to rear or rear to front depending on choice */

for(temp=(ch==READAHEAD?front:rear);temp!=NULL;temp=(ch==READAHEAD?temp->next:temp->prev))
            printf("\n%d",temp->val); /* printing queue values */
getchar();

}
0
swtirs
Asked:
swtirs
  • 4
  • 4
  • 2
2 Solutions
 
sunnycoderCommented:
    front=front->next; /* front moves to next member in queue */
>>>>>>> you moved fromt to next node
     
     if(front==NULL)
          rear=NULL; /* rear also changes if there was only one value in queue */
     else
          front->next=NULL;
>>>>>>>>>>> here you set its next to NULL !!!!!!!


int dequeue()
{
     node *temp;
     int delval;

     if(front==NULL)
          return -999;

     temp=front;
     front=front->next; /* front moves to next member in queue */
     
     if(front==NULL)
          rear=NULL; /* rear also changes if there was only one value in queue */
 
    temp->next=NULL; // this will not be in else .. you should delink the reference anyway

     delval=temp->val;
     free(temp); /* memory free */
     
     return delval; /* deleted value returned */
}
0
 
sunnycoderCommented:
if(front==NULL)
          rear=NULL; /* rear also changes if there was only one value in queue */
else
         temp->next=NULL; //oops you were right .. this would be in else part .. just that you will have to use temp instead of front
0
 
ozoCommented:
Why do you do front->next=NULL; ?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
sunnycoderCommented:
ignore my second post .. first one was right ..
Something is wrong with me today... sorry for the multiple posts

You need to set the next of deleted node to NULL .. this would happen irrespecive of value of front node ... The deleted node is pointed to by temp and not front .. so, outside the if condition temp->next = NULL will do that job

Keeping it in the else part of if would not hurt either ... In that case it would not be called when front == NULL in which case temp->next would already be NULL but the first approach is more intuitive and easy to understand.
0
 
ozoCommented:
I think you must have meant front->prev=NULL
0
 
swtirsAuthor Commented:
Thank you for the prompt reply. After selecting option 2 i still have an issue.
started the program and tried entries as below

selected option 1, entered value 10
selected option 1, entered value 5
selected option 2 (dqueue)
selected option 3 - it prints ok as
5

but when option 4 selected, it prints below
5
3530

I do not know where this 3530 coming from



0
 
ozoCommented:
It is coming from front->prev
0
 
swtirsAuthor Commented:
To get rid what I should use in dqueue()
0
 
ozoCommented:
front->prev=NULL
0
 
sunnycoderCommented:
    temp=front;
     front=front->next; /* front moves to next member in queue */
     
     if(front==NULL)
          rear=NULL; /* rear also changes if there was only one value in queue */
      else
          front->prev=NULL;
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now