I have a linked list where the input is read into. The 2 types of input have to go into a queue. 1 queue for each type.

I suppose that the output line of the linked list can be used to read it into the queue ? I know it should be possibile somehow by using cout.

cout<< "List Contains:"<< endl;
DisplayList(list)
FreeList(list)
return 0;

I suppose a dynamic queue. But how? Linked List implementation? The way the book did the linked list implementation is by using a header file. I don't like that, though. Would I just add the delete function, push, pop and isempty? I think that would take care of it. Then I load it into it and work with the input. But how would I split the input? I would have to use a loop to decide what goes where. So how would I use a loop to put the input into the queue?

Recommended Answers

All 3 Replies

as u know that queue has two ends front and rare .you can use the loop like (while<=rear)
if(rare=front+1)then no entry else enter the element;
check it i think it will work.

The priority queue as an ADT is a container in which elements are stored acordingly to their priority.
Have a look at how is implemented in C++:
Click me.
As for firdousahmad you can't access a rear element in the Priority Queue (talking about the C++ implementation). The basic functions that C++ offers are:

empty   Test whether container is empty (public member function)
size    Return size (public member function)
top     Access top element (public member function)
push    Insert element (public member function)
pop     Remove top element (public member function)

Even if you have a Min priority queue, you will still be storing the smallest element in front. Priority queues are based on queues, in which elements are inserted like FIFO (First In First Out). So, whether you want a Max priority queue, or a Min priority queue, the elements will still be stored in front (as on top).
There is another way, say like an extension, in which you can add an iterator over the queue, but that doesn't make much sense, from my point of view.
Now, the question that arises is if you are or aren't allowed to use the STL Priority queues to your project.

But how would I split the input?

Not that depends on your implementation of a linked list. If you do use one of your own, you can simply modify the struct node of the list so that it can hold as information two types.

For a DLL (doubly linked list):
Node: record
    info1: <Type1>
    info2: <Type2>
    next: ^Position
    prev: ^Position
end

For a SLL (single linked list):
Node: record
    info1: <Type1>
    info2: <Type2>
    next: ^Position
end

Having that you can put the 1st type of the input in the info1 and the 2nd type of the input in the info2.

But that's a stack implementation, is it not? HWhat's the advantage of that? The book has one using DynIntStack.h, which is a pre-defined headerfile that's already exisitng, correct?

#include <iostream>
#include "DynIntStack.h"
using namespace std;

//**************************************************
// Destructor                                      *
// This function deletes every node in the list.   *
//**************************************************

DynIntStack::~DynIntStack()
{
   StackNode *nodePtr, *nextNode;

   // Position nodePtr at the top of the stack.
   nodePtr = top;

   // Traverse the list deleting each node.
   while (nodePtr != NULL)
   {
      nextNode = nodePtr->next;
      delete nodePtr;
      nodePtr = nextNode;
   }
}

//************************************************
// Member function push pushes the argument onto *
// the stack.                                    *
//************************************************

void DynIntStack::push(int num)
{
   StackNode *newNode; // Pointer to a new node

   // Allocate a new node and store num there.
   newNode = new StackNode;
   newNode->value = num;

   // If there are no nodes in the list
   // make newNode the first node.
   if (isEmpty())
   {
      top = newNode;
      newNode->next = NULL;
   }
   else  // Otherwise, insert NewNode before top.
   {
      newNode->next = top;
      top = newNode;
   }
}

//****************************************************
// Member function pop pops the value at the top     *
// of the stack off, and copies it into the variable *
// passed as an argument.                            *
//****************************************************

void DynIntStack::pop(int &num)
{
   StackNode *temp; // Temporary pointer

   // First make sure the stack isn't empty.
   if (isEmpty())
   {
      cout << "The stack is empty.\n";
   }
   else  // pop value off top of stack
   {
      num = top->value;
      temp = top->next;
      delete top;
      top = temp;
   }
}

//****************************************************
// Member function isEmpty returns true if the stack *
// is empty, or false otherwise.                     *
//****************************************************

bool DynIntStack::isEmpty()
{
   bool status;

   if (!top)
      status = true;
   else
      status = false;

   return status;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.