Path: utzoo!attcan!uunet!cs.utexas.edu!usc!zaphod.mps.ohio-state.edu!mips!prls!pyramid!infmx!briand From: briand@infmx.UUCP (brian donat) Newsgroups: comp.lang.c++ Subject: Re: Beginner's Corner Summary: Feed Back 3 of 4 Keywords: First C++ Code. Message-ID: <4338@infmx.UUCP> Date: 26 May 90 00:22:38 GMT References: <4316@infmx.UUCP> Organization: Informix Software Inc., Menlo Park, CA. Lines: 203 // // 3 of 4 // // Time for the redefined STACK and QUEUE classes. // // Well hell, the new LIST class was easy enough! // Discussion follows below. ---------------------------------Cut HERE--------------------------------- /////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ // \\ // list.h \\ // LIST class derived from VLIST: May 25, 1990, Brian L. Donat \\ // \\ /////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ extern int listError; extern class VLIST; class LIST : public VLIST { }; ---------------------------------Cut HERE--------------------------------- // Instant derived class. A complete alias of VLIST. // Is this an idiotic thing to do or what? // // You should note that there is no #include "vlist.h" here. // Don't do it! You'll be sorry. // Use extern class VLIST instead! And make sure that there's // nothing in either file that's going to cause multiply defined // compiler errors. Unless that is, you like to spin your wheels // for no good reason! // Now, below is the new STACK class. // Pretty simple? Notice how I had to get at listError? // Helper functions are private and the definition of STACK // has VLIST being private also to prevent usage of VLIST's // members directly. // // What's interesting here is that I could have done this // thisaway: // // class STACK { // VLIST list; // public: // // and maybe achieved the same thing. // Would it be the same? // Again, you won't find any of them blased enum booleans in here. // // More Discussion follows below. ---------------------------------Cut HERE--------------------------------- /////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ // \\ // stack.h Version 1.00 \\ // STACK class derived from VLIST: May 25, 1990, Brian L. Donat \\ // \\ /////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ #define MAXSIZE 20 #define stackError listError extern int listError; extern class VLIST; class STACK : private VLIST { int overflow(); int underflow(); public: void push(ELEMENT); ELEMENT pop(); }; ---------------------------------Cut HERE--------------------------------- // And here below, for your perusal are STACK's members. // I've made life a bit easier for genericity by declaring an ELEMENT // EDUMMY which is a default return from pop(), when nothing should // be returned. // And hey! It's OK to use enum boolean here!! // But man o' man, don't get caught with your shorts down by having // it in a '.h' file with a class definition! // // I declared overflow() and underflow() inline. Are these good // inline? I'd think so. They're only used once each! // // More Discussion follows below. ---------------------------------Cut HERE--------------------------------- /////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ // \\ // stack.c Version 1.00 \\ // STACK member functions : May 25, 1990, Brian L. Donat \\ // \\ /////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ #include "vlist.h" #include "stack.h" const ELEMENT EDUMMY = 0; enum boolean { FALSE, TRUE }; // Note that this code is written so that // the stack can never overflow or // underflow. These tests is done during // push() or pop() to see if it will // overflow overflow or underflow will // occur prior to actual execution. inline int STACK::overflow() { return (boolean)(getsize() >= MAXSIZE); } inline int STACK::underflow() { return (boolean)!getsize(); } void STACK::push(ELEMENT E) { stackError = FALSE; if(overflow()) stackError = TRUE; else add(E); } ELEMENT STACK::pop() { ELEMENT E = EDUMMY; stackError = FALSE; if(underflow()) stackError = TRUE; else { E = getval(); remove(); } return E; } ---------------------------------Cut HERE--------------------------------- // Just the Queue stuff follows. You can punch out now if you like. ---------------------------------Cut HERE--------------------------------- /////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ // \\ // queue.h Version 1.00 \\ // STACK class derived from VLIST: May 25, 1990, Brian L. Donat \\ // \\ /////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ #define MAXSIZE 20 #define queueError listError extern int listError; extern class VLIST; class QUEUE : private VLIST { public: int overflow(); int underflow(); void enqueue(ELEMENT); ELEMENT dequeue(); }; ---------------------------------Cut HERE--------------------------------- /////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ // \\ // queue.c Version 1.00 \\ // QUEUE member functions : May 25, 1990, Brian L. Donat \\ // \\ /////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ #include "vlist.h" #include "queue.h" const ELEMENT EDUMMY = 0; enum boolean { FALSE, TRUE }; // Note that this code is written so that // the queue can never overflow or // underflow. These tests is done during // enqueue() or dequeue() to see if it // will overflow overflow or underflow // will occur prior to actual execution. int QUEUE::overflow() { return (boolean)(getsize() >= MAXSIZE); } int QUEUE::underflow() { return (boolean)!getsize(); } void QUEUE::enqueue(ELEMENT E) { queueError = FALSE; if(overflow()) queueError = TRUE; else { headend(); insert(E); } // In this queue, we feed the headend. } ELEMENT QUEUE::dequeue() { ELEMENT E = EDUMMY; queueError = FALSE; if(underflow()) queueError = TRUE; else { tailend(); // In this queue, we ... (No Jokes Please!) E = getval(); remove(); } return E; } ---------------------------------Cut HERE--------------------------------- -- infmx!briand@pyramid