0

Here is the code I have

#include<iostream>
using namespace std;

void push_t(int j)
//---------------------------------------------------
// Accepts a value and pushes it onto the stack top
// Precondition: if keyset = 0 the deque does not yet exits
// deque is a structure with int j for data
// Postcondition: values are pushed onto the stack and
// the value of stackpointer is incremented
//If the structure does not exist it is created.
//in which case the top and bottom are the same cell
//----------------------------------------------------
{
  
  if (keyset==0){keyset=1;//create deque
    Top =new bx;
    Top->prior=NULL;
    Top->next=NULL;
    Top->key=j;
    
    Bottom=Top;
    
  }//push onto top
  else{
    Topx =new bx;
    Topx->key=j;
    Topx->next=NULL;
    Topx->prior=NULL;
    Top->prior=Topx;
    Topx->next=Top;
    Top=Topx;
    
  }
  
}
//end of push_t
//---------------------------------

void pop_t(int &n,int &r)
//---------------------------------------------------
// Pops a number off the top of the queue and returns it
// Precondition:the deque may or may not exist
// Postcondition: values are popped off the stack and
// if empty "empty is returned and r=1 is returned.
//----------------------------------------------------
{r=0;
  if (keyset==0){cout<<"Empty"<<endl<<endl;n=NULL;r=1; return;}//empty deque
  
  if(Top->next==NULL)// One item only in deque
    {
      keyset=0;//reset deque to empty
      n=Top->key;
      delete Top;
      return;
    }
  bxx=Top->next;
  n=Top->key;
  delete Top;
  Top=bxx;
  Top->prior=NULL;
  return;
}//end of pop_t
//----------------------------------------------------


void push_b(int j)
//---------------------------------------------------
// Accepts a value and pushes it onto the bottom of the deque
// Precondition: deque is a structure with int j for data
// if keyset = 0 the deque does not yet exist
// Postcondition: values are pushed onto the bottom of the deque
// If the structure does not exist it is created.
// in which case the top and bottom are the same cell
//----------------------------------------------------
{
  int * Top;
  if (keyset==0){
    keyset=1;//create deque
    Top =new bx;
    Top->key=j;
    Top->prior=NULL;
    Top->next=NULL;
    Bottom=Top;
  }
  //push onto bottom
  else{

    Bottomx =new bx;
    Bottomx->next=NULL;
    Bottomx->prior=NULL;
    
    Bottomx->key=j;
    
    Bottom->next=Bottomx;
    Bottomx->prior=Bottom;
    
    Bottom=Bottomx;
    
  }
  
}
//end of push_b
//-------------------------------------------------------

void pop_b(int &n, int &r)
//---------------------------------------------------
// Pops number off the the bottom of the deque
// Precondition:  the structure may or may not exist
// Postcondition: values are popped off the stack and
// if it does not exist r=1="empty" is returned
//
//----------------------------------------------------
{
  r=0;
  if (keyset==0){cout<<"Empty"<<endl<<endl;n=NULL;r=1; return;}//empty deque
  
  if(Bottom->prior==NULL)// One item only in deque
    {
      keyset=0;//reset deque to empty
      n=Bottom->key;
      delete Bottom;
      return;
    }
  bxx=Bottom->prior;
  n=Bottom->key;
  delete Bottom;
  Bottom=bxx;
  Bottom->next=NULL;
  return;
}//end of pop_b
//--------------------------------------------------

//---------------------------------------------------
void display_deque()
//---------------------------------------------------
// Prints the contents of the deque in order top to bottom
// Precondition: None. 
// Postcondition: No change. If the deque is empty the message
// "Empty" is printed.
//----------------------------------------------------
{cout<<"Data: "<<endl<<endl;
  if (keyset==0){cout<<"Empty"<<endl<<endl; return;}//empty deque
  // cout<<Top->key<<endl;
  bxx=Top;
  while(bxx->next!=NULL){
    cout<<bxx->key<<endl;
    bxx=bxx->next;
  } cout<<bxx->key<<endl<<"End"<<endl<<endl;
  return;
}//end of display_deque
//------------------------------------------------------


//----------------------------------------
//constructor - sets deque stack to zero
//initialze variables
//-----------------------------------------
int dq()
{
  keyset=0;
  int j=0;
  Topx=NULL;
  Bottomx=NULL;
}//end constructor
//--------------------------------------
//end of deque functions

I have to create a double ended queue. I got this code to work with, but I dont know how to declare the variables on this. Especially when you have something like "Top->key=j;" Just looking for some ideas to get started. Thanks.

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by Salem
0

1. This is C++, so where are your classes?
2. Top/Bottom etc would be member data of that class.
3. The push and pop would be member functions of that class.

Then your main code would be like

myQueue var;
var.push_t(22);

> I got this code to work with,
Got from who?
Is this a "Here's some broken code, make it work" exercise?

> Top =new bx; bx is a struct or a class Top is a pointer to a bx

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.