473,473 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

class stack&queue.ineed some help

plz check what i have made wrong what is requierd her is to creat class
queue and class stack and run the push,pop operation .
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class stack
{
public:

spop();
spush();
sout();
void s_Menu();
void Main_Menu();
private:
int list[];
int listsize,item,rear,front;
};

class queue
{
public:
qpop();
qpush();
qout();
void q_Menu();
iempty();
private:
int list2[];
int listsize2,item2;
int rear;
int front;

};
int top=-1;
char ch;
void stack:: Main_Menu()
{

stack s;
queue q;
gotoxy(20,3);cout<<"****************************** ************";
gotoxy(20,4);cout<<"* *";
gotoxy(20,5);cout<<"* *";
gotoxy(20,6);cout<<"****************************** ************";
gotoxy(30,9);cout<<"*************************";
gotoxy(30,10);cout<<"* MAIN PROGRAM MENU *";
gotoxy(30,11);cout<<"*Stack Queue Operations *";
gotoxy(30,12);cout<<"*[S] Stack *";
gotoxy(30,13);cout<<"*[Q] Queue *";
gotoxy(30,14);cout<<"*[E] Exit *";
gotoxy(30,15);cout<<"*************************";
//char ch='y';

while((ch!='E')&&(ch!='e'))
{
cout<<"\nEnter your selection : ";
// cin>>ch;
ch = getche();
switch(ch)

{
case 'S','s':

s.s_Menu();

break;
case 'Q','q':

q.q_Menu();
break;
}
} }
void stack:: s_Menu()
{
stack s;

gotoxy(30,9);cout<<"*************************";
gotoxy(30,10);cout<<"* Stack *";
gotoxy(30,11);cout<<"*[P] Stack push *";
gotoxy(30,12);cout<<"*[O] Stack pop *";
gotoxy(30,13);cout<<"*[D] stack Display *";
gotoxy(30,14);cout<<"*[R] Return *";
gotoxy(30,15);cout<<"*************************";
//char ch;
while(ch!='R')

{
// cout<<"\n Enter your selection : ";
// cin>>ch;
ch = getche();
switch(ch)
{
case 'P','p':

s.spush();

break;

case 'O','o':
item=s.spop();
if(item!=-9999)

cout<<"\n The popped value is..."<<item;

break;

case 'D','d':
cout<<"\n The stack is...:";
s.sout();
break;

case 'R','r':
clrscr();
s.Main_Menu();
}
}

}
void queue::q_Menu()
{
queue q;
stack s;
gotoxy(30,9);cout<<"**************************";
gotoxy(30,10);cout<<"* Queue *";
gotoxy(30,11);cout<<"*[P] Queue push *";
gotoxy(30,12);cout<<"*[O] Queue pop *";
gotoxy(30,13);cout<<"*[D] Queue Display *";
gotoxy(30,14);cout<<"*[R] Return *";
gotoxy(30,15);cout<<"**************************";

while(ch!='R')

{
// cout<<"\n Enter your selection : ";
// cin>>ch;
ch = getche();
switch(ch)
{
case 'p','p':

q.qpush();

break;

case 'o','o':
item2=q.qpop();
if(item2 !=-9999)
cout<<"\n The popped value is .."<<item2;
break;

case 'd','d':
cout<<"\n The queue is...:";
q.qout();
break;

case 'r','r':
clrscr();
s.Main_Menu();
}
}
}


void main()
{

stack s;

s.Main_Menu();

}


stack::spush ()
{

cout<<"\n Enter the valu to be pushed : ";
cin>>item;
if (top<listsize)
{
top++;
list[top]=item;
}
else
cout<<"\n The stack is full";
};

stack::spop()
{
int v;
if(top>=0)
{
v=list[top];
top--;
return v;
}
else
{
cout<<"\n stack is empty";

return(-9999);
}
};
stack::sout()
{
int i;
if(top>=0)
{
cout<<"\n";
for (i=top;i>=0;i--)
cout<<list[i]<<" ";
}

};
//************************************************** **
queue::qpush()
{
cout<<"\n Enter the value to pushed : ";
cin>>item2;
if (rear+1%listsize2==front)
{
cout<<"\n The queue is full";
}
else
rear=(rear+1)%listsize2;
list2[rear]=item2;
}

queue::qpop()
{
int v2;
if(front!=rear)

{
front=(front+1)%listsize2;
v2=list2[front];
return v2;
}
else
cout<<"\n queue is empty";
return(-9999);// returns a value -9999 if the queue is empty
}
queue::qout()
{
int i2;
i2=front;
while(i2!=rear)
{
i2=(i2+1)%listsize2;
cout<<list2[i2]<<" ";
}
}

Nov 22 '05 #1
4 2129
alisaee wrote:
plz check what i have made wrong what is requierd her is to creat class
queue and class stack and run the push,pop operation .
#include<iostream.h>
This header is deprecated. Use <iostream> (you may also want to add
"using namespace std;").

#include<conio.h>
Non-standard header.
#include<stdio.h>
Unnecessary and prefer iostreams.
class stack
{
public:

spop();
spush();
sout();
You should have return types for these functions. C defaults to int;
standard C++ doesn't. You're (perhaps unwittingly) using a compiler
extension that is best avoided.
void s_Menu();
void Main_Menu();
Main_Menu should not be a member function of stack. Just make it an
ordinary function, or at the very least make it a static member
function. Arguably, s_Menu should not be a member either since it mixes
the functionality of the stack class with the user interface. Prefer
one concept per class.
private:
int list[];
You need a limit here, or you need to use a pointer and allocate the
memory dynamically. If you choose the latter, prefer a smart pointer
like boost::scoped_array.
int listsize,item,rear,front;
};

class queue
{
public:
qpop();
qpush();
qout();
void q_Menu();
iempty();
How about isEmpty() instead of iempty()? Also, add return types, and
make q_Menu() a non-member.
private:
int list2[];
int listsize2,item2;
int rear;
int front;

};
int top=-1;
char ch;

Avoid global variables like these two. Pass necessary parameters into
functions and minimize variable scope as much as possible. It makes
code easier to understand and debug.

void stack:: Main_Menu()
{

stack s;
queue q;
gotoxy(20,3);cout<<"****************************** ************";
gotoxy(20,4);cout<<"* *";
gotoxy(20,5);cout<<"* *";
gotoxy(20,6);cout<<"****************************** ************";
gotoxy(30,9);cout<<"*************************";
gotoxy(30,10);cout<<"* MAIN PROGRAM MENU *";
gotoxy(30,11);cout<<"*Stack Queue Operations *";
gotoxy(30,12);cout<<"*[S] Stack *";
gotoxy(30,13);cout<<"*[Q] Queue *";
gotoxy(30,14);cout<<"*[E] Exit *";
gotoxy(30,15);cout<<"*************************";
//char ch='y';

while((ch!='E')&&(ch!='e'))
{
cout<<"\nEnter your selection : ";
// cin>>ch;
ch = getche();
Prefer cin.
switch(ch)

{
case 'S','s':

[snip]

This won't work. Try:

case 'S':
case 's':

I could make many more comments, but it is obvious that your program
has many issues. If you need more help, please ask specific questions.
Know, however, that we're not going to do your homework for you. You
might also be interested in the group alt.comp.lang.learn.c-c++, which
is concerned with learning the language, the FAQ for this group which
answers many common questions (http://www.parashift.com/c++-faq-lite/),
and our favorite book for learning C++: _Acclerated C++_ by Koenig and
Moo. This forum is for discussions about the language itself (not
applications), so feel free to post again if you have questions about
the language itself.

Cheers! --M

Nov 22 '05 #2
alisaee wrote:
plz check what i have made wrong what is requierd her is to creat class
queue and class stack and run the push,pop operation .


Too much code, and none of it works.

You are doing this the wrong way, write small ammounts of code and get
that code working before writing any more code. You should throw this
code away, it's a waste of time.

Also you've put too much priority on menus and such, that isn't the
point of the exercise. Forgot about fancy menus and concentrate on
getting the queue and stack working.

And this should be obvious by now but do either the stack or the queue
first (your choice). Don't try to do both at once! Follow this advice
and it will be easier.

john
Nov 22 '05 #3
miimber & john ,thanx.

Nov 22 '05 #4
miimber & john ,thanx.

Nov 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: WittyGuy | last post by:
Hi all, Though I know the concepts of both abstract class & virtual function (like derived class pointer pointing to base class...then calling the function with the pointer...), what is the real...
5
by: herrcho | last post by:
int main() { printf("Input a line: "); wrt_it(); printf("\n\n"); return 0; } void wrt_it() {
2
by: Jim Bancroft | last post by:
Hi all, I'm writing an exception handler for one of my VB.Net methods and wondered how best to dynamically put the class and method name in my message string. My code looks like this...
12
by: mast2as | last post by:
Hi everyone... I have a TExceptionHandler class that is uses in the code to thow exceptions. Whenever an exception is thrown the TExceptionHander constructor takes an error code (int) as an...
0
by: Stan SR | last post by:
Hi, It's maybe a stupid question. I have a class that I call from each of my pages. I would like to know if it's possible to "instantiate" this class from my masterpage and call all the...
8
by: mast2as | last post by:
Hi guys, I think from what I found on the net that the code is correct and there's no problem when I compile the files. The problems occurs when I link them. I have a friend function which outputs...
4
by: Donos | last post by:
Hi I have a HANDLE to an Event, like this.. HANDLE h = ::CreateEvent(NULL, FALSE, FALSE, NULL); This is running in one thread in one class. For example we will call that class as "Class A"...
2
by: mnarewec | last post by:
Forgive me if this is a silly question. I am looking for the option where you can set so that like in VB you can see the methods of controls you design on the form when you view its code. For...
0
by: phanipep | last post by:
4 applications of stacks and queues. Justify your choices.
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.