New Posts  All Forums:Forum Nav:

[C++] stacks/queues

post #1 of 4
Thread Starter 
I need to write the main, header, and the implementation file for some stacks and some queues.
the header and the implementation are going to be a new class.
and in the main i gonna need to declare a stack and a queue of this new class.
now, i have used stack and queue and created new class before, but not together.

My questions are:
1) Do I have to rewrite the member functions for the stack/queue in the new class?? Or I can just use it (tried it but it didnt work so i guess this is a no)??
2) How do I write in the main when I need to get a value??
pseudo code for new class (Click to show)
Code:
#include ... 
namespace name
{
class classname
{
public:
TYPEDEFS AND MEMBER CONSTANT..
CONSTRUCTOR ...
classname ();
classname (const classname& source);
MOD MEMBER CONSTANT...
CONST MEMBER CONSTANT ...
value_type read() const {return data;}
int read2() const{return data2;}

private:
value_type data;
int data2;
}
}

Thank you
Edited by TLCH723 - 4/24/12 at 9:05pm
post #2 of 4
If I understand correctly, you need to create a Stack class and a Queue class. So yes, you need to manually code the functions of stacks and queues as member functions of each class.

Your second question is a little less clear, but you should have a member function for both Stack and Queue called pop() which returns and deletes the value which is at the top of the stack or front of the queue.
    
CPUMotherboardGraphicsRAM
Intel Core i5-760 @ 3.8GHz ASRock P55 Extreme4 XFX Radeon RX 570 4GB G.SKILL Ripjaws X 8GB DDR3-1600 
Hard DriveHard DriveOptical DriveCooling
Samsung 840 Pro 256GB SSD Samsung Spinpoint F3 500GB HDD Lite-On DVD-RW CM Hyper 212+ 
OSMonitorMonitorKeyboard
Windows 10 Pro Acer KG240 Acer KG240 Razer BlackWidow Tournament Edition 
PowerCaseMouseMouse Pad
Corsair HX750 Modular Antec Three Hundred Razer DeathAdder Chroma Corsair MM300 
AudioOther
ASUS Xonar DG DXRacer Formula Series 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Intel Core i5-760 @ 3.8GHz ASRock P55 Extreme4 XFX Radeon RX 570 4GB G.SKILL Ripjaws X 8GB DDR3-1600 
Hard DriveHard DriveOptical DriveCooling
Samsung 840 Pro 256GB SSD Samsung Spinpoint F3 500GB HDD Lite-On DVD-RW CM Hyper 212+ 
OSMonitorMonitorKeyboard
Windows 10 Pro Acer KG240 Acer KG240 Razer BlackWidow Tournament Edition 
PowerCaseMouseMouse Pad
Corsair HX750 Modular Antec Three Hundred Razer DeathAdder Chroma Corsair MM300 
AudioOther
ASUS Xonar DG DXRacer Formula Series 
  hide details  
Reply
post #3 of 4
Thread Starter 
Quote:
Originally Posted by pnoozi View Post

If I understand correctly, you need to create a Stack class and a Queue class. So yes, you need to manually code the functions of stacks and queues as member functions of each class.
Your second question is a little less clear, but you should have a member function for both Stack and Queue called pop() which returns and deletes the value which is at the top of the stack or front of the queue.

pop() only remove the top item. I think you mean top(), which return the value.

And i having a problem declaring it in main.
Dont you declare it like: stack var_name; ??
post #4 of 4
Quote:
Originally Posted by TLCH723 View Post

pop() only remove the top item. I think you mean top(), which return the value.
And i having a problem declaring it in main.
Dont you declare it like: stack var_name; ??

If you are using the stack class from the C++ STL then you declare it:
Code:
stack<T> myStack; // where T is the data type

Unfortunately I can't verify this as I can't get Visual Studio to work. Some libraries require you to specify the implementation type (list, array, vector, etc.) but I'm not sure if those libraries are older or newer, or how it is implemented in lieu of a specified type.

If you are using your own class which you have implemented, then you declare it using the new operator and the constructor function you created. You may choose to implement it using any data structure you like. I prefer linked list because it is dynamic.
Edited by pnoozi - 4/25/12 at 6:04pm
    
CPUMotherboardGraphicsRAM
Intel Core i5-760 @ 3.8GHz ASRock P55 Extreme4 XFX Radeon RX 570 4GB G.SKILL Ripjaws X 8GB DDR3-1600 
Hard DriveHard DriveOptical DriveCooling
Samsung 840 Pro 256GB SSD Samsung Spinpoint F3 500GB HDD Lite-On DVD-RW CM Hyper 212+ 
OSMonitorMonitorKeyboard
Windows 10 Pro Acer KG240 Acer KG240 Razer BlackWidow Tournament Edition 
PowerCaseMouseMouse Pad
Corsair HX750 Modular Antec Three Hundred Razer DeathAdder Chroma Corsair MM300 
AudioOther
ASUS Xonar DG DXRacer Formula Series 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Intel Core i5-760 @ 3.8GHz ASRock P55 Extreme4 XFX Radeon RX 570 4GB G.SKILL Ripjaws X 8GB DDR3-1600 
Hard DriveHard DriveOptical DriveCooling
Samsung 840 Pro 256GB SSD Samsung Spinpoint F3 500GB HDD Lite-On DVD-RW CM Hyper 212+ 
OSMonitorMonitorKeyboard
Windows 10 Pro Acer KG240 Acer KG240 Razer BlackWidow Tournament Edition 
PowerCaseMouseMouse Pad
Corsair HX750 Modular Antec Three Hundred Razer DeathAdder Chroma Corsair MM300 
AudioOther
ASUS Xonar DG DXRacer Formula Series 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming