class stack&queue.ineed some help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • alisaee

    #1

    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<iostre am.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,r ear,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);co ut<<"********** *************** *************** **";
    gotoxy(20,4);co ut<<"* *";
    gotoxy(20,5);co ut<<"* *";
    gotoxy(20,6);co ut<<"********** *************** *************** **";
    gotoxy(30,9);co ut<<"********** *************** ";
    gotoxy(30,10);c out<<"* MAIN PROGRAM MENU *";
    gotoxy(30,11);c out<<"*Stack Queue Operations *";
    gotoxy(30,12);c out<<"*[S] Stack *";
    gotoxy(30,13);c out<<"*[Q] Queue *";
    gotoxy(30,14);c out<<"*[E] Exit *";
    gotoxy(30,15);c out<<"********* *************** *";
    //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);co ut<<"********** *************** ";
    gotoxy(30,10);c out<<"* Stack *";
    gotoxy(30,11);c out<<"*[P] Stack push *";
    gotoxy(30,12);c out<<"*[O] Stack pop *";
    gotoxy(30,13);c out<<"*[D] stack Display *";
    gotoxy(30,14);c out<<"*[R] Return *";
    gotoxy(30,15);c out<<"********* *************** *";
    //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);co ut<<"********** *************** *";
    gotoxy(30,10);c out<<"* Queue *";
    gotoxy(30,11);c out<<"*[P] Queue push *";
    gotoxy(30,12);c out<<"*[O] Queue pop *";
    gotoxy(30,13);c out<<"*[D] Queue Display *";
    gotoxy(30,14);c out<<"*[R] Return *";
    gotoxy(30,15);c out<<"********* *************** **";

    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%listsiz e2==front)
    {
    cout<<"\n The queue is full";
    }
    else
    rear=(rear+1)%l istsize2;
    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)%lists ize2;
    cout<<list2[i2]<<" ";
    }
    }

  • mlimber

    #2
    Re: class stack&amp;queue .ineed some help

    alisaee wrote:[color=blue]
    > 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<iostre am.h>[/color]

    This header is deprecated. Use <iostream> (you may also want to add
    "using namespace std;").

    [color=blue]
    > #include<conio. h>[/color]

    Non-standard header.
    [color=blue]
    > #include<stdio. h>[/color]

    Unnecessary and prefer iostreams.
    [color=blue]
    > class stack
    > {
    > public:
    >
    > spop();
    > spush();
    > sout();[/color]

    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.
    [color=blue]
    > void s_Menu();
    > void Main_Menu();[/color]

    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.
    [color=blue]
    > private:
    > int list[];[/color]

    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_a rray.
    [color=blue]
    > int listsize,item,r ear,front;
    >
    >
    > };
    >
    > class queue
    > {
    > public:
    > qpop();
    > qpush();
    > qout();
    > void q_Menu();
    > iempty();[/color]

    How about isEmpty() instead of iempty()? Also, add return types, and
    make q_Menu() a non-member.
    [color=blue]
    > private:
    > int list2[];
    > int listsize2,item2 ;
    > int rear;
    > int front;
    >
    > };
    > int top=-1;
    >
    >
    > char ch;[/color]


    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.

    [color=blue]
    > void stack:: Main_Menu()
    > {
    >
    > stack s;
    > queue q;
    > gotoxy(20,3);co ut<<"********** *************** *************** **";
    > gotoxy(20,4);co ut<<"* *";
    > gotoxy(20,5);co ut<<"* *";
    > gotoxy(20,6);co ut<<"********** *************** *************** **";
    > gotoxy(30,9);co ut<<"********** *************** ";
    > gotoxy(30,10);c out<<"* MAIN PROGRAM MENU *";
    > gotoxy(30,11);c out<<"*Stack Queue Operations *";
    > gotoxy(30,12);c out<<"*[S] Stack *";
    > gotoxy(30,13);c out<<"*[Q] Queue *";
    > gotoxy(30,14);c out<<"*[E] Exit *";
    > gotoxy(30,15);c out<<"********* *************** *";
    > //char ch='y';
    >
    > while((ch!='E') &&(ch!='e'))
    > {
    > cout<<"\nEnter your selection : ";
    > // cin>>ch;
    > ch = getche();[/color]

    Prefer cin.
    [color=blue]
    > switch(ch)
    >
    > {
    > case 'S','s':[/color]
    [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.l earn.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

    Comment

    • John Harrison

      #3
      Re: class stack&amp;queue .ineed some help

      alisaee wrote:[color=blue]
      > 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 .
      >
      >[/color]

      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

      Comment

      • alisaee

        #4
        Re: class stack&amp;queue .ineed some help

        miimber & john ,thanx.

        Comment

        • alisaee

          #5
          Re: class stack&amp;queue .ineed some help

          miimber & john ,thanx.

          Comment

          Working...