|
From: Jason H. <jas...@bt...> - 2001-12-15 02:56:12
|
Why is your pop method wrong? I don't really understand why using a node
would be a problem. I assume that you are refering to the node pointer? The
only problem that I can see (at first look) is that initialize doesn't
delete any pointers that may be stored in the stack, but just discards them.
That would be a memory leak.
To use the stack all you need to do is declare an object of stacktype and
call it's members in the usual fasion.
//........... At top of code with other includes
#include "StackHeader.h"
//........... in code body
stacktype MyS;
// push some values onto stack
MyS.push(10);
for(int v = 0; v < 100; v += 5) MyS.push(v);
display(MyS);
add(MyS);
// pop off top value
int x = MyS.pop();
cout << "The value is: " << x << endl;
This code doesn't do anything in particular, it's just a demo.
If I have missed the point please try to explain in a little more detail.
Jason.
----- Original Message -----
From: "chromatonus 712" <chr...@ho...>
To: <dev...@li...>
Sent: Saturday, December 15, 2001 1:57 AM
Subject: [Dev-C++] linked list as stack
> hi, I was given the following header (can't change it, sorry) to write a
> calculator prgm using linked lists as stack (reverse polish notation or
> postfix)
>
> #include <iostream.h>
> #include <conio.h>
>
> struct node
> {
> int data;
> node *next;
> };
>
> class stacktype
> {
> public:
> // initial-value constructor
> stacktype( int );
> // postcondition: initialize list as STACK = NULL
> // top of stack will be the lead node
> // and will set screen row to initial value
> void initialize();
> // postcondition: object stack list is set to NULL
> // the screen display is cleared
> bool is_empty();
> // postcondition: return FALSE if stack = NULL otherwise
> // return TRUE
>
> void push( int );
> // postcondition: the entered data value will be placed at the HEAD
> // of the list, making it the TOP.
> // The second parameter is for the output to the screen
>
> int pop();
> // postcondition: the data at the HEAD (top) will be returned, and
the
> // head will be deleted from the list.
>
> private:
> node *mystack;
> int mytop, my_first_top;
> };
>
>
//==========================================================================
===
> //============================= Free functions
> ================================
>
//==========================================================================
===
> void draw_stack( int );
> // postcondition: draws stack using value from constructor as base value
> void add( stacktype & );
> // precondition: stack must have at least two elements.
> // postcondition: top two elements of stack are popped, added then pushed
> // back onto the stack
> // result will be displayed in output screen
> void subt( stacktype & );
> // precondition: stack must have at least two elements.
> // postcondition: top two elements of stack are popped, first one
> // subtracted from the second and the result
puched
> back
> // onto the stack
> // result will be displayed in output screen
> void mult( stacktype & );
> // precondition: stack must have at least two elements.
> // postcondition: top two elements of stack are popped, multiplied
> // together, and the product pushed back onto the stack
> // result will be displayed in output screen
> void display( stacktype );
> // postcondition: the top of the stack will be displayed in the output
> // screen. This wil be performed after each keystroke.
>
>
> :::::::This was part of what I wrote:
>
//==========================================================================
===
> //============================== Implementation
> ===============================
>
//==========================================================================
===
>
> stacktype::stacktype( int )
> {
> mystack = NULL;
> mytop = 0;
> my_first_top = 0;
> }
> void stacktype::initialize()
> {
> mystack = NULL;
> mytop = 0;
> my_first_top = 0;
> clrscr();
> //then it just displays a pretty picture
> int h, k;
> gotoxy(3,2);
> cout<< "> ";
> for(k=2; k<=22; k++)
> {
> gotoxy(60,k);
> cout<< "||";
> gotoxy(70,k);
> cout<< "||";
> }
> for(h=60; h<=71; h++)
> {
> gotoxy(h,21);
> cout<< "-";
> gotoxy(h,22);
> cout<< "-";
> }
> gotoxy(59,22);
> cout<< "-";
> gotoxy(72,22);
> cout<< "-";
> for(k=3; k<=20; k++)
> {
> for(h=62; h<=69; h++)
> {
> gotoxy(h,k);
> cout<< " ";
> }
> }
> gotoxy(50,10);
> cout<< "OUTPUT:";
> for(h=50; h<=56; h++)
> {
> gotoxy(h,11);
> cout<< "-";
> gotoxy(h,14);
> cout<< "-";
> }
> for(k=12; k<=13; k++)
> {
> gotoxy(49,k);
> cout<< "|";
> gotoxy(57,k);
> cout<< "|";
> }
> }
> bool stacktype::is_empty()
> {
> return (mystack == NULL);
> }
> void stacktype::push( int x )
> {
> mytop++;
> node *mm = new node;
> mm -> data = x;
> mm -> next = mystack;
> mystack = mm;
> gotoxy(62,21-mytop);
> cout<< x;
> gotoxy(50,13);
> cout<< x << " ";
> }
> int stacktype::pop()
> {
> gotoxy(62,20-mytop);
> cout<< " ";
> if( !is_empty() )
> {
> int x = mystack -> data;
> mytop--;
> node *j;
> j = mystack;
> mystack = mystack -> next;
> delete *j;
> gotoxy(50,13);
> cout<< x << " ";
> return x;
> }
> }
>
> my pop is wrong because i used nodes, but i don't know how to avoid the
> error, and how would you declare a stacktype using this header? thank you
> very much for your help :)
>
>
>
> _________________________________________________________________
> Join the world's largest e-mail service with MSN Hotmail.
> http://www.hotmail.com
>
>
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|