By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,150 Members | 1,052 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,150 IT Pros & Developers. It's quick & easy.

class stack&queue.ineed some help

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
miimber & john ,thanx.

Nov 22 '05 #4

P: n/a
miimber & john ,thanx.

Nov 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.