• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 647
  • Last Modified:

Queue application/Algorithms


I am tring to build two queues , then pass the data between them using the enqueue and the dequeue.Now I know I will need to loop , and check to one for end of file,while the other must check that the queue (1)is Not empty.
this is where I am coming up with different solutions.

So here is what I came up with . Maybe you can help me to understand what the output would be.

1 Q1  = createQueue
2 Q2  = createQueue
3 loop ( not end of file ) this one I think should read
                           should read the data.
       1 read number
       2 enqueue  (Q1 , number)
       3 enqueue  (Q2 , number)
       4 loop (not empty Q1)
          1 dequeue (Q1, x)
          2 enqueue (Q2, x)
data to use = 5,7,12,4,0,4,6

I think the Not ( logical ) is throwing me off. the first loop should read the data into both Queues. The second loop as long as the as Q1 is not empty do loop.
But I never get the same answer twice.  
I hope you can help me , thank you
  • 3
2 Solutions
What are you trying to do exactly? You will get the following if you input 5(first),7,12,4,0,4,6:

Q1: (empty)
Q2: 5<-5<-7<-7<-12<-12<-4<-4<-0<-0<-4<-4<-6<-6

How did I get this? After you get a value(5), you enqueue both Q1 and Q2 with the value(5). Then, you go into another loop that will loop until Q1 is empty, but Q1 will only have one value(5) at that point, so you dequeue it and enqueue it into Q2... that's why it will have duplicates values in Q2... After that, you get the next number...
mary-25Author Commented:
I am a programmer for a Major oil co. (IT) and I am trying to learn Data Structures using Algorithm on my own.Something reccommended by my company.Tho there is no there to help us , we are on our own.
This book uses a approach to Pseudocode to C.....
What I am tring to determine that if I had two Queue's and the data as listed , and the algorithm I am writing ( hope it is right ) what are the contents of the two Queue's after the execution of the example shown.
Now since I am new to this Data Structure and have been programming for a number of years( with several languages learned). I am not sure what one can do with this , CAN I put THIS to C code to test it(HOW)  or can you only test it by hand?
I tried to contact the company where I purchased the book , but they will not sell the answer book, only to school, etc  if you know what I mean. Sure makes it hard to continue your upgrade .Written by Gilberg and Forouzan.
I am tring to improve my skills in programming.
I came across your site and started to look thru the listing but didnt find what I needed. Is there a way to search for what I need as listed above? For I will be using your site as I use this book.
thank you
Hope this answered your question
FIRST, this is not my website. It's more like a place for people to gather to get help from each other.

I never done a C program to create a Q before, only use C++... Anyway, here is the program of what your PseudoCode described. Make sure the comments are not in the second line (the comments that I made after the //).

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

// Node Structure
struct QNode
  struct QNode *next;
  int data;

// Pointers to First Node and Last Node of both Queue
struct QNode *FirstNodeQ1 = NULL;
struct QNode *FirstNodeQ2 = NULL;
struct QNode *LastNodeQ1 = NULL;
struct QNode *LastNodeQ2 = NULL;

// Declaration of the functions
void Enqueue(int QNumber, int val);
int Dequeue(int QNumber);

int main()
  int val=1;

  // Open file for input (5 7 12 4 0 4 6)
  FILE *inp;
  inp = fopen("input.txt","r");

  // The outer while-loop, Enqueue the value to both queue
  while(fscanf(inp, "%d", &val) != EOF) // While not the end-of-file
    Enqueue(1, val);
    Enqueue(2, val);
    // The inner while-loop, Enqueue value to Queue 2 that
    // you get from dequeueing Queue 1.
    while(FirstNodeQ1 != NULL) // While Queue 1 is not empty

  // Print all value from Q2.
  while(FirstNodeQ2 != NULL) // While Queue 2 is not empty
    printf("%d ", Dequeue(2)); // Print the value.
  return 0;

void Enqueue(int QNumber, int val)
  // Allocate space for NEW Qnode
  struct QNode *newNode = (struct QNode *)malloc(sizeof(struct QNode));

  // Initialize value for the NEW QNode with value that you enqueue
  newNode->data = val;
  newNode->next = NULL;

  // Insert NEW QNode into the Queue 1 or Queue 2
  if(QNumber == 1) // For Queue 1
    if(FirstNodeQ1==NULL) // When Queue 1 is empty
    else // When Queue 1 is not empty
  else // For Queue 2
    if(FirstNodeQ2==NULL) // WHen Queue 2 is empty
    else // When Queue 2 is not empty

int Dequeue(int QNumber)
  struct QNode *delNode;
  int val;
  if(QNumber == 1) // For QUeue 1
    delNode = FirstNodeQ1;
    val = FirstNodeQ1->data;
    if(FirstNodeQ1 == LastNodeQ1) // When there is only one node in Q1
    else // When there is more than one node in Q1
      val = FirstNodeQ1->data;
  else // For QUeue 2
    delNode = FirstNodeQ2;
    val = FirstNodeQ2->data;
    if(FirstNodeQ2 == LastNodeQ2) // When there is only one node in Q2
    else // When there is more than one node in Q2
      val = FirstNodeQ2->data;
  // Deallocate space no longer used back to the memory

  return val; // Return the value to the calling function
As for places to learn about C programming languages, try:


As for pseudocode, it should make sense, as it is, I guess... I hope... that's why pseudo-code is written in a first place to make a complicate code look clearer...

Personally, I would recommend learning about C++, instead, for this kind of stuffs. It's much simpler... For example, instead of using malloc() to allocate space in C, you just need a "new" to allocate space in C++...

This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Post your closing recommendations!  No comment means you don't care.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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