class stack&queue.ineed some help

A

alisaee

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<<"* 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<<" ";
}

};
//****************************************************
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]<<" ";
}
}
 
M

mlimber

alisaee said:
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<<"* 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
 
J

John Harrison

alisaee said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top