Data Structures
Borland Turbo C++ was used.
![]()
![]()
// DS21.CPP -- Display a Point, Circle, and a Rectangle
// Programmer : Lars G Sorensen
//
// Link with GRAPHICS.LIB
// Uses "figures.h" Generously supplied by Borland Intl.
// Uses "figs.cpp" Implementations of Classes for Above
// Code Hijaaked from BORLAND's "FIGDEMO.CPP"
#include "figures.h"
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#include <conio.h>
void DrawCircle(void);
void DrawPoint(void);
void DrawRectangle(void);
void VGADisclaimer(void);
int main() // test the new Arc class
{
int x;
int Choice;
int graphdriver = DETECT, graphmode;
initgraph(&graphdriver, &graphmode, "C:\\tc\\bgi");
while (1)
{ //while
clearviewport();
gotoxy(2,2);
printf(" Shape Objects Menu \n\n");
printf(" 1. Display a Point \n");
printf(" 2. Display a Circle \n");
printf(" 3. Display a Rectangle \n");
printf(" 0. Exit the Program \n\n");
printf(" Enter Choice : ");
cin >> (Choice);
switch (Choice) {
case 1 :
{
DrawPoint();
break;
}
case 2 :
{
DrawCircle();
break;
}
case 3 :
{
DrawRectangle();
break;
}
case 0 :
{
closegraph();
exit(0);
}
} //switch
} //while
}// Main
void DrawCircle(void)
{
int X, Y, Radius, ColorNumber;
clearviewport();
gotoxy(1,1);
VGADisclaimer();
printf("Enter the X Coordinate : ");
cin >> X;
printf("\nEnter the Y Coordinate : ");
cin >> Y;
printf("\nEnter the Radius : ");
cin >> Radius;
printf("\nEnter the Color Number : ");
cin >> ColorNumber;
Circle TheCircle(X, Y, Radius, ColorNumber);
clearviewport();
TheCircle.Show();
getch();
}
void DrawPoint(void)
{
int X, Y, ColorNumber;
clearviewport();
gotoxy(1,1);
VGADisclaimer();
printf("Enter the X Coordinate : ");
cin >> X;
printf("\nEnter the Y Coordinate : ");
cin >> Y;
printf("\nEnter the Color Number :");
cin >> ColorNumber;
Point ThePoint(X, Y, ColorNumber);
clearviewport();
ThePoint.Show();
getch();
}
void DrawRectangle(void)
{
int X, Y, Width, Height, ColorNumber;
clearviewport();
gotoxy(1,1);
VGADisclaimer();
printf("Enter the X Coordinate : ");
cin >> X;
printf("\nEnter the Y Coordinate : ");
cin >> Y;
printf("\nEnter the Width : ");
cin >> Width;
printf("\nEnter the Height : ");
cin >> Height;
printf("\nEnter the Color Number : ");
cin >> ColorNumber;
Rect TheRect(X, Y, Width/2, Height/2, ColorNumber);
clearviewport();
TheRect.Show();
getch();
}
void VGADisclaimer(void)
{
printf("\nThere is a good chance that you \n");
printf("have a VGA monitor with 640x480\n");
printf("resolution. That means your maximum\n");
printf("X value should not be > 640 and the \n");
printf("maximum Y not > 480. Be careful!!\n\n");
}
![]()
// MyStuff.h - Lars Sorensen
//
// Header file of defs and classes for use
// in DS programs and stuff.....
//
#define TRUE 1
#define FALSE 0
typedef int Boolean;
class ErrorMsg
{
// data
public:
// mem funcs
ErrorMsg(){ };
void Display(char* str)
{
cout << "\007\007\007" << str << "\007\007";
getch();
}
};
// ************* End of Header ***************
// Programmer: Lars Sorensen
// Project : DS22.cpp
//
// An OOP Stack using static storage.
//
// Merely threw together the "MyStuff.h"
// header in lue of "MyDSOOP.h" and
// corrected a nasty little bug that told
// you the stack was full as you pushed
// the MAX element. The POP never reset
// the Overflow value to FALSE.
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <process.h>
#include "MyStuff.h"
const MAXSTACK = 10;
typedef int item; //data will be character strings
class Stack
{
//data members
protected:
int Top;
item Entry[MAXSTACK];
Boolean Overflow;
Boolean Underflow;
//member functions
public:
Stack();
void Push (item x);
void Pop ();
void StackTop (item& x);
void GetDataKB();
void Display();
Boolean IsEmpty();
Boolean IsFull();
Boolean GetOverflow() {return Overflow;};
Boolean GetUnderflow() {return Underflow;};
};
Stack::Stack()
{
int j;
Top = 0;
for (j = 1; j <= MAXSTACK; j++)
Entry[j] = 0;
} //Create
void Stack::Push (item x)
{
if (Top == MAXSTACK)
Overflow = TRUE;
else
{
Overflow = FALSE;
Top++;
Entry[Top] = x;
}
} //Push
void Stack::GetDataKB()
{
int i;
int num;
item val;
Boolean flag;
ErrorMsg E;
cout << "How many items do you want on the stack?";
cin >> num;
if ( num > (MAXSTACK-Top))
{
cout << "This Stack has a current limit of " << MAXSTACK-Top << '\n';
num = MAXSTACK-Top;
}
for (i = 1; i <= num; i++)
{
cout << "Enter the value of the item: ";
cin >> val;
Push(val);
}
} //GetDataKB
void Stack::Display()
{
int i;
cout << " STACK \n";
cout << " ÉÍÍÍÍÍÍÍÍÍÍÍ»\n";
if (Top == 0)
cout << " ºSTACK EMPTYº\n";
else
{
for (i = Top; i >= 1; i--)
{
if (i == Top)
cout << "TOP º" << setw(11) << Entry[i] << "º\n";
else
cout << " º" << setw(11) << Entry[i] << "º\n";
}
}
cout << " ÈÍÍÍÍÍÍÍÍÍÍͼ\n\n";
getch();
} //Display
Boolean Stack::IsEmpty()
{
if (Top == 0)
return TRUE;
else
return FALSE;
} //IsEmpty
Boolean Stack::IsFull()
{
if (Top == 10)
return TRUE;
else
return FALSE;
} // IsFull
void Stack::Pop ()
{
if (IsEmpty())
Underflow = TRUE;
else
{
Underflow = FALSE;
Overflow = FALSE;
Top--;
}
} //Pop
void Stack::StackTop (item& x)
{
if (IsEmpty())
Underflow = TRUE;
else
{
Underflow = FALSE;
x = Entry[Top];
}
} //StackTop
void Menu() // Function that is really the driver.
{
int Choice;
Stack S;
ErrorMsg E;
item val;
while (1)
{
clrscr();
textcolor(RED);
cprintf("Simple Stack Demo\n");
textcolor(WHITE);
cprintf(" ");
cout << "\n";
cout << "1. Create the stack (Really Init the Stack.)\n";
cout << "2. Get data for the stack.\n";
cout << "3. Display the stack.\n";
cout << "4. Pop an element from the stack.\n";
cout << "5. Push an element onto the stack.\n";
cout << "6. Retrieve the element on top of the stack.\n";
cout << "7. Is Stack Empty??\n";
cout << "8. Is Stack Full??\n";
cout << "0. Exit\n";
cout << "\n";
cout << "Enter your choice: ";
cin >> Choice;
switch (Choice)
{
case 1 : // can't construct the object here
// because it will be destroyed next time
// through the loop
S.Display();
break;
case 2 : S.GetDataKB();
S.Display();
break;
case 3 : S.Display();
break;
case 4 : S.Pop ();
if (S.GetUnderflow())
E.Display ("Error: Tried to pop from empty stack.");
else
S.Display();
break;
case 5 : cout << "Enter the element to be pushed: ";
cin >> val;
if (S.GetOverflow())
E.Display ("Error: Tried to push onto full stack.");
else
{
S.Push(val);
S.Display();
}
break;
case 6 : S.StackTop (val);
if (S.GetUnderflow())
E.Display ("Error: Tried to retrieve from empty stack.");
else
{
cout << "The element on top of the stack is:" << val << "\n";
S.Display();
}
break;
case 7 : if (S.IsEmpty())
{
cout << "The Stack is Empty.\n";
getch();
}
else
{
cout << "The Stack is not Empty.\n";
getch();
}
break;
case 8 : if (S.IsFull())
{
cout << "The Stack is Full.\n";
getch();
}
else
{
cout << "The Stack is not Full.\n";
getch();
}
break;
case 0 : exit(0);
} //Case
} //while
} //Menu
void main (void)
{
clrscr();
Menu();
getch();
}
// DS22.CPP ****************** End of Source ***********************
![]()
DS23 - A better mousetrap, untyped Stack. | Top
// function protos for demst7ll.cpp
template <class N> class Node
{
N* Retrieve;
Node* Next;
public:
Node(Node* inNext, N inData);
~Node();
void Display();
Node* GetNext() {return Next;}
N* GetRetrieve() {return Retrieve;}
void SetNext(Node* inNext) {Next = inNext;}
void SetRetrieve(N* inRetrieve) {Retrieve = inRetrieve;}
};
//===================================================
//Stripped down ooll.h & ooll.cpp
template <class M> class LinkedList
{
protected:
Node<M>* Head;
Node<M>* Tail;
char Name[25];
unsigned char Size;
public:
LinkedList();
~LinkedList();
void Display();
void GetDataKB();
void InsertAtHead(M x);
void RemoveFromHead();
M ListHead();
Boolean IsEmpty();
M CurrentValue(Node<M>* Cur);
};
//======================================================
template <class T> class Stack : public LinkedList<T>
{
//data members
protected:
Node<T>* Top; //Top of stack is the head of the list
Node<T>* Bottom; //Bottom of stack is the tail of the list
Boolean Overflow; //The stack will overflow only when we run out
// of memory
Boolean Underflow;
//member functions
public:
Stack();
void Push (T x);
void Pop ();
void StackTop (T& x);
void GetDataKB();
void Display();
Boolean IsEmpty();
Boolean GetOverflow() {return Overflow;};
Boolean GetUnderflow() {return Underflow;};
};
// DS23ll.cpp fro use by driver....
#include <iostream.h>
#include <string.h>
#include <iomanip.h>
#include "mystuff.h"
#include "templfnc.h"
template <class N>
Node<N>::Node(Node<N>* inNext, N inData)
{
Next = inNext;
Retrieve = new N (inData);
}
template <class N>
Node<N>::~Node()
{
delete Retrieve;
}
template <class N>
void Node<N>::Display()
{
cout << *Retrieve;
}
// Implementations of the LinkedList Template class
template <class M>
LinkedList<M>::LinkedList()
{
Head = NULL;
Tail = NULL;
strcpy(Name,"");
Size = 0;
}
template <class M>
LinkedList<M>::~LinkedList()
{
Node<M>* Temp;
while (Head != NULL)
{
Temp = Head;
Head = Head->GetNext();
delete (Temp);
}
strcpy(Name,"");
Size = 0;
}
template <class M>
void LinkedList<M>::Display()
{
Node<M>* Ptr;
Ptr = Head;
if (Ptr != NULL)
while (Ptr->GetNext() != NULL)
{
Ptr->Display();
Ptr = Ptr->GetNext();
}
}
template <class M>
void LinkedList<M>::GetDataKB()
{
int Choice;
byte i;
M val;
cout << "Enter the name of the list: ";
cin >> Name;
cout << "Enter the number of elements in the list:";
cin >> Size;
for (i = 1; i <= Size; i++)
{
cout << "Enter the value of the item: ";
cin >> val;
InsertAtHead(val);
}
}
template <class M>
void LinkedList<M>::InsertAtHead(M x)
{
Node<M>* Temp;
Temp = new Node<M>(NULL,x);
if (Head == NULL)
{
Head = Temp;
Tail = Temp;
}
else
{
Temp->SetNext(Head);
Head = Temp;
}
}
template <class M>
void LinkedList<M>::RemoveFromHead()
{
Node<M>* Temp;
Node<M>* Ptr;
if (!IsEmpty())
{
Temp = Head;
Ptr = Head;
Ptr = Ptr->GetNext();
Head = Ptr;
delete Temp;
}
}
template <class M>
M LinkedList<M>::ListHead()
{
Node<M>* Ptr;
if (!IsEmpty())
{
Ptr = Head;
return CurrentValue(Ptr);
}
else
return NULL;
}
template <class M>
Boolean LinkedList<M>::IsEmpty()
{
return (Head == NULL);
}
template <class M>
M LinkedList<M>::CurrentValue(Node<M>* Cur)
{
return *(Cur->GetRetrieve());
}
template <class T>
Stack<T>::Stack() :LinkedList<T>()
{
int j;
Top = Head;
Bottom = Tail;
} //Create
template <class T>
void Stack<T>::Push (T x)
{
// if (Top == MAXSTACK)
// Overflow = TRUE;
// else
// {
Overflow = FALSE;
InsertAtHead(x);
Top = Head;
Bottom = Tail;
// }
} //Push
template <class T>
void Stack<T>::GetDataKB()
{
int i;
int num;
T val;
Boolean flag;
ErrorMsg E;
cout << "How many items do you want on the stack?: ";
cin >> num;
for (i = 1; i <= num; i++)
{
cout << "Enter the value of the item: ";
cin >> val;
Push(val);
if (Overflow)
E.Display ("ERROR: stack full");
}
} //GetDataKB
template <class T>
void Stack<T>::Display()
{
Node<T>* i;
i = Top;
cout << "\n";
if (Top == NULL)
cout << " *STACK EMPTY*\n";
else
{
while (i->GetNext() != NULL)
{
if (i == Top)
cout << "TOP " << setiosflags(ios::left) << *(i->GetRetrieve()) << "\n";
else
cout << " " << setiosflags(ios::left) << *(i->GetRetrieve()) << "\n";
i = i->GetNext();
}
if (i == Top)
cout << "TOP " << setiosflags(ios::left) << *(i->GetRetrieve()) << "\n";
else
cout << " " << setiosflags(ios::left) << *(i->GetRetrieve()) << "\n";
}
cout << "\n";
getch();
} //Display
template <class T>
Boolean Stack<T>::IsEmpty()
{
if (Top == NULL)
return TRUE;
else
return FALSE;
} //IsEmpty
template <class T>
void Stack<T>::Pop ()
{
if (IsEmpty())
Underflow = TRUE;
else
{
Underflow = FALSE;
RemoveFromHead();
Top = Head;
Bottom = Tail;
} //else
} //Pop
template <class T>
void Stack<T>::StackTop (T& x)
{
if (IsEmpty())
Underflow = TRUE;
else
{
Underflow = FALSE;
x = ListHead();
} //else
} //StackTop
#pragma option -Jgd
// phoney instances of above templates to satisfy
// the compilers need for public symbol defs.... Christ in a dumptruck.
typedef Stack<int> Phoney_I;
typedef Stack<float> Phoney_F;
typedef Stack<char> Phoney_C;
typedef LinkedList<int> LinkPhoney_I;
typedef LinkedList<float> LinkPhoney_F;
typedef LinkedList<char> LinkPhoney_C;
// DS23.cpp OOp Stack w/ dynamic indirect storage.
// Programmer : Lars Sorensen
// CUS1151
//
//program DS23.cpp
//An object oriented Stack using dynamic indirect storage.
//The list functions are separated out from the Stack class.
//The list is put into a separate module.
//
//
// Took the easy way out and setup three menu's, one for
// each data type. I'll puzzle out choosing multiple
// data types at runtime when I don't have an 18 credit
// semester.
//
#include <iostream.h> //cout()
#include <process.h> //exit()
#include <conio.h>
#include <iomanip.h>
#include "mystuff.h"
#include "templfnc.h"
#include <new.h> //for handling memory allocation errors
#pragma option -Jgx // So you can define the templates in
// another module. BC++4 reference pg 170.
void Menu1()
{
int Choice;
Stack<int> S;
int val=0;
ErrorMsg E;
int keep_runnin = TRUE;
while (keep_runnin)
{
clrscr();
cout << "Simple Stack Demo\n";
cout << "\n";
cout << "1. Create the stack.\n";
cout << "2. Get data for the stack.\n";
cout << "3. Display the stack.\n";
cout << "4. Pop an element from the stack.\n";
cout << "5. Push an element onto the stack.\n";
cout << "6. Retrieve the element on top of the stack.\n";
cout << "7. Is the Stack empty?\n";
cout << "0. Exit\n";
cout << "\n";
cout << "Enter your choice: ";
cin >> Choice;
switch (Choice)
{
case 1 : // can't construct the object here
// because it will be destroyed next time
// through the loop
S.Display();
break;
case 2 : S.GetDataKB();
S.Display();
break;
case 3 : S.Display();
break;
case 4 : S.Pop ();
if (S.GetUnderflow())
E.Display ("Error: Tried to pop from empty stack.");
else
S.Display();
break;
case 5 : cout << "Enter the element to be pushed: ";
val=0;
cin >> val;
S.Push (val);
if (S.GetOverflow())
E.Display ("Error: Tried to push onto full stack.");
else
S.Display();
break;
case 6 : S.StackTop (val);
if (S.GetUnderflow())
E.Display ("Error: Tried to retrieve from empty stack.");
else
{
cout << "The element on top of the stack is:" << val << "\n";
S.Display();
}
break;
case 7 : if (S.IsEmpty())
{
cout << "The Stack is Empty.";
getch();
}
else
{
cout << "The Stack currently has elements.";
getch();
}
break;
case 0 : keep_runnin = FALSE;
break;
} //Case
} //while
} //Menu1 the one for integers.....
void Menu2()
{
int Choice;
Stack<float> S;
float val=0.0;
ErrorMsg E;
int keep_runnin = TRUE;
while (keep_runnin)
{
clrscr();
cout << "Simple Stack Demo\n";
cout << "\n";
cout << "1. Create the stack.\n";
cout << "2. Get data for the stack.\n";
cout << "3. Display the stack.\n";
cout << "4. Pop an element from the stack.\n";
cout << "5. Push an element onto the stack.\n";
cout << "6. Retrieve the element on top of the stack.\n";
cout << "7. Is the Stack empty?\n";
cout << "0. Exit\n";
cout << "\n";
cout << "Enter your choice: ";
cin >> Choice;
switch (Choice)
{
case 1 : // can't construct the object here
// because it will be destroyed next time
// through the loop
S.Display();
break;
case 2 : S.GetDataKB();
S.Display();
break;
case 3 : S.Display();
break;
case 4 : S.Pop ();
if (S.GetUnderflow())
E.Display ("Error: Tried to pop from empty stack.");
else
S.Display();
break;
case 5 : cout << "Enter the element to be pushed: ";
val=0.0;
cin >> setiosflags(ios::unitbuf) >> val;
S.Push (val);
if (S.GetOverflow())
E.Display ("Error: Tried to push onto full stack.");
else
S.Display();
break;
case 6 : S.StackTop (val);
if (S.GetUnderflow())
E.Display ("Error: Tried to retrieve from empty stack.");
else
{
cout << "The element on top of the stack is:" << val << "\n";
S.Display();
}
break;
case 7 : if (S.IsEmpty())
{
cout << "The Stack is Empty.";
getch();
}
else
{
cout << "The Stack currently has elements.";
getch();
}
break;
case 0 : keep_runnin = FALSE;
break;
} //Case
} //while
} //Menu2 the one for floats......
void Menu3()
{
int Choice;
Stack<char> S;
char val;
ErrorMsg E;
int keep_runnin = TRUE;
while (keep_runnin)
{
clrscr();
cout << "Simple Stack Demo\n";
cout << "\n";
cout << "1. Create the stack.\n";
cout << "2. Get data for the stack.\n";
cout << "3. Display the stack.\n";
cout << "4. Pop an element from the stack.\n";
cout << "5. Push an element onto the stack.\n";
cout << "6. Retrieve the element on top of the stack.\n";
cout << "7. Is the Stack empty?\n";
cout << "0. Exit\n";
cout << "\n";
cout << "Enter your choice: ";
cin >> Choice;
switch (Choice)
{
case 1 : // can't construct the object here
// because it will be destroyed next time
// through the loop
S.Display();
break;
case 2 : S.GetDataKB();
S.Display();
break;
case 3 : S.Display();
break;
case 4 : S.Pop ();
if (S.GetUnderflow())
E.Display ("Error: Tried to pop from empty stack.");
else
S.Display();
break;
case 5 : cout << "Enter the element to be pushed: ";
cin >> val;
S.Push (val);
if (S.GetOverflow())
E.Display ("Error: Tried to push onto full stack.");
else
S.Display();
break;
case 6 : S.StackTop (val);
if (S.GetUnderflow())
E.Display ("Error: Tried to retrieve from empty stack.");
else
{
cout << "The element on top of the stack is:" << val << "\n";
S.Display();
}
break;
case 7 : if (S.IsEmpty())
{
cout << "The Stack is Empty.";
getch();
}
else
{
cout << "The Stack currently has elements.";
getch();
}
break;
case 0 : keep_runnin = FALSE;
break;
} //Case
} //while
} //Menu3 the one for chars
void mem_warn() {
cerr << "\nCan't allocate!";
exit(1);
}
void main(void)
{
int the_choice;
set_new_handler(mem_warn);
while (1)
{
clrscr();
cout << " The Stack from Hell!!!\n";
cout << "\n Choose your data type and beware!!\n\n";
cout << "1. Integers.\n";
cout << "2. Floats.\n";
cout << "3. Chars.\n";
cout << "0. Exit now and spare yourself.\n\n";
cout << "What's your choice: ";
cin >> the_choice;
switch(the_choice)
{
case 1 : Menu1();
break;
case 2 : Menu2();
break;
case 3 : Menu3();
break;
case 0 : set_new_handler(0); // reset to default
exit(0);
} // end of case
} // end of while
} //DS23.cpp ===================================================
![]()
DS24 - I'm sick of Stacks | Top
//Class version of homework 24
//
// Lars G Sorensen... DS24
// Class Library version Stacks w/ objects.
#include <conio.h>
#include <stdio.h>
#include <iostream.h>
#include <graphics.h>
#include <iomanip.h>
#include <process.h>
#include <stdlib.h>
#include <stacks.h> //not stack.h
#include <queues.h>
#include "lds3.h"
void StackMenu()
{ //Menu
int Choice,DataChoice,Go=1;
MyObject *MyObjPtr;
BI_IStackAsVector<MyObject> S;
typedef BI_IStackAsVectorIterator <MyObject> MyIterator;
while (Go)
{ //while
textbackground(BLACK);
clrscr();
cout <<" Class Library Stack Implementation\n";
cout <<"\n";
cout <<"1. Push items onto Stack.\n";
cout <<"2. Display Stack in Text mode.\n";
cout <<"3. Pop an Item off the Stack.\n";
cout <<"4. Display Stack in Graphics mode.\n";
cout <<"5. Is the Stack Empty?\n";
cout <<"6. Display the Top of the Stack.\n";
cout <<"0. Exit\n";
cout <<"\n";
cout << "Enter your choice: ";
cin >> (Choice);
switch (Choice) {
case 1 :
{
clrscr();
cout <<" Pick your Data Type\n";
cout <<"\n";
cout <<"1. Integer.\n";
cout <<"2. Character.\n";
cout <<"3. Float.\n";
cout <<"4. Point.\n";
cout <<"5. Circle.\n";
cout <<"6. Rectangle.\n";
cout <<"\n";
cout << "Enter your choice: ";
cin >> (DataChoice);
switch (DataChoice) {
case 1 :
{
int x;
cout << "\nEnter an Integer Element:";
cin >> x;
Integer* num = new Integer(x);
S.push(num);
break;
}
case 2 :
{
char x;
cout << "\nEnter a Character Element:";
cin >> x;
Character* ch = new Character(x);
S.push(ch);
break;
}
case 3 :
{
float x;
cout << "\nEnter a Float Element:";
cin >> x;
Float* fl = new Float(x);
S.push(fl);
break;
}
case 4 :
{
int x,y,pc;
cout << "Point\n";
cout << "Enter the X Coordinate : ";
cin >> x;
cout << "Enter the Y Coordinate : ";
cin >> y;
cout << "Enter the Color : ";
cin >> pc;
PointObj* pnt = new PointObj(x,y,pc);
S.push(pnt);
break;
}
case 5 :
{
int x,y,pc,r;
cout << "Circle\n";
cout << "Enter the X Coordinate : ";
cin >> x;
cout << "Enter the Y Coordinate : ";
cin >> y;
cout << "Enter the Color : ";
cin >> pc;
cout << "Enter the Radius : ";
cin >> r;
CircleObj* cir = new CircleObj(x,y,pc,r);
S.push(cir);
break;
}
case 6 :
{
int x,y,pc,w,h;
cout << "Rectangle\n";
cout << "Enter the X Coordinate : ";
cin >> x;
cout << "Enter the Y Coordinate : ";
cin >> y;
cout << "Enter the Color : ";
cin >> pc;
cout << "Enter the Width : ";
cin >> w;
cout << "Enter the Height : ";
cin >> h;
RectObj* rec = new RectObj(x,y,pc,w,h);
S.push(rec);
}
} // end of switch......
break;
} // end of case 1 for Push Choice....
case 2 :
{
if ( !S.isEmpty() )
{
MyIterator nextobj(S);
cout << "\nThe Stack from Top up....\n";
while ( nextobj != 0 )
{
nextobj.current()->printOn(cout);
cout << '\n';
++nextobj;
}
cout << '\n';
getch();
}
else
{
cout << " The Stack is Empty.";
getch();
}
break;
}
case 3 :
{
if ( !S.isEmpty() )
{
MyObjPtr = S.pop();
MyObjPtr->printOn(cout);
cout << " has been Popped.";
getch();
delete MyObjPtr;
}
else
{
cout << "The Stack is Empty.";
getch();
}
break;
}
case 4 :
{
MyIterator nextobj(S);
SetupGraphics();
if ( !S.isEmpty() )
{
outtextxy(5,5,"Stack from the Top up... ");
int x=5,y=15;
while ( nextobj != 0 )
{
nextobj.current()->GrPrintOn(x,y);
cout << '\n';
++nextobj; y+=10;
}
cout << '\n';
getch();
}
else
{
outtextxy(5,5,"The Stack is Empty.");
getch();
}
closegraph();
break;
}
case 5 :
{
if ( S.isEmpty() )
cout << "The Stack is Empty.\n";
else
cout << "The Stack has elements.\n";
getch();
break;
}
case 6 :
{
if ( !S.isEmpty() )
{
cout << "The top of the stack is ...\n";
S.top()->printOn(cout);
cout << '\n';
}
else
cout << "The Stack is Empty!!\n";
getch();
break;
}
case 0 : Go=0;
} //switch
} //while
} //StackMenu
void QueueMenu()
{ //Menu
int Choice,DataChoice,Go=1;
MyObject *MyObjPtr; // use this for front and rear
// function return
BI_IQueueAsVector<MyObject> S;
typedef BI_IQueueAsVectorIterator <MyObject> MyIterator;
// Don't ask me why..... but the Iterator won't work
// correctly until an item has been deleted from the
// queue. In lue of picking apart the BIDS to find
// out why, I'm inserting and item now and deleting it
// without anybody looking..... B->
Integer* num = new Integer(88); // my lucky number
S.put(num);
S.get();
delete num;
// All done.......Works great!!
while (Go)
{ //while
textbackground(BLACK);
clrscr();
cout <<" Lars Sorensen CUS1151 ADS Prof Davis\n";
cout <<" Class Library Queue Implementation\n";
cout <<"\n";
cout <<"1. Insert items onto Queue.\n";
cout <<"2. Display Queue in Text mode.\n";
cout <<"3. Delete an Item off the Queue.\n";
cout <<"4. Display Queue in Graphics mode.\n";
cout <<"5. Is the Queue Empty?\n";
cout <<"6. Display the Front of the Queue.\n";
cout <<"7. Display the Rear of the Queue. \n";
cout <<"0. Exit\n";
cout <<"\n";
cout << "Enter your choice: ";
cin >> (Choice);
switch (Choice) {
case 1 :
{
clrscr();
cout <<" Pick your Data Type\n";
cout <<"\n";
cout <<"1. Integer.\n";
cout <<"2. Character.\n";
cout <<"3. Float.\n";
cout <<"4. Point.\n";
cout <<"5. Circle.\n";
cout <<"6. Rectangle.\n";
cout <<"\n";
cout << "Enter your choice: ";
cin >> (DataChoice);
switch (DataChoice) {
case 1 :
{
int x;
cout << "\nEnter an Integer Element:";
cin >> x;
Integer* num = new Integer(x);
S.put(num);
break;
}
case 2 :
{
char x;
cout << "\nEnter a Character Element:";
cin >> x;
Character* ch = new Character(x);
S.put(ch);
break;
}
case 3 :
{
float x;
cout << "\nEnter a Float Element:";
cin >> x;
Float* fl = new Float(x);
S.put(fl);
break;
}
case 4 :
{
int x,y,pc;
cout << "Point\n";
cout << "Enter the X Coordinate : ";
cin >> x;
cout << "Enter the Y Coordinate : ";
cin >> y;
cout << "Enter the Color : ";
cin >> pc;
PointObj* pnt = new PointObj(x,y,pc);
S.put(pnt);
break;
}
case 5 :
{
int x,y,pc,r;
cout << "Circle\n";
cout << "Enter the X Coordinate : ";
cin >> x;
cout << "Enter the Y Coordinate : ";
cin >> y;
cout << "Enter the Color : ";
cin >> pc;
cout << "Enter the Radius : ";
cin >> r;
CircleObj* cir = new CircleObj(x,y,pc,r);
S.put(cir);
break;
}
case 6 :
{
int x,y,pc,w,h;
cout << "Rectangle\n";
cout << "Enter the X Coordinate : ";
cin >> x;
cout << "Enter the Y Coordinate : ";
cin >> y;
cout << "Enter the Color : ";
cin >> pc;
cout << "Enter the Width : ";
cin >> w;
cout << "Enter the Height : ";
cin >> h;
RectObj* rec = new RectObj(x,y,pc,w,h);
S.put(rec);
}
} // end of switch......
break;
} // end of case 1 for Put Choice....
case 2 :
{
if ( !S.isEmpty() )
{
MyIterator nextobj(S);
cout << "\nThe Queue from Rear to the Front...\n";
while ( nextobj != 0 )
{
nextobj.current()->printOn(cout);
cout << '\n';
++nextobj;
}
cout << '\n';
getch();
}
else
{
cout << " The Queue is Empty.";
getch();
}
break;
}
case 3 :
{
if ( !S.isEmpty() )
{
MyObjPtr = S.get();
MyObjPtr->printOn(cout);
cout << " has been deleted.";
getch();
delete MyObjPtr;
}
else
{
cout << "The Queue is Empty.";
getch();
}
break;
}
case 4 :
{
MyIterator nextobj(S);
SetupGraphics();
if ( !S.isEmpty() )
{
outtextxy(5,5,"Queue from the Rear to the Front... ");
int x=5,y=15;
while ( nextobj != 0 )
{
nextobj.current()->GrPrintOn(x,y);
cout << '\n';
++nextobj; y+=10;
}
cout << '\n';
getch();
}
else
{
outtextxy(5,5,"The Queue is Empty.");
getch();
}
closegraph();
break;
}
case 5 :
{
if ( S.isEmpty() )
cout << "The Queue is Empty.\n";
else
cout << "The Queue has elements.\n";
getch();
break;
}
case 6 :
{
if ( !S.isEmpty() )
{
cout << "The Front of the queue is ...\n";
S.peekRight()->printOn(cout);
cout << '\n';
}
else
cout << "The Queue is Empty!!\n";
getch();
break;
}
case 7 :
{
if ( !S.isEmpty() )
{
cout << "The Rear of the queue is ...\n";
S.peekLeft()->printOn(cout);
cout << '\n';
}
else
cout << "The Queue is Empty!!\n";
getch();
break;
}
case 0 : Go=0;
} //switch
} //while
} //Menu
void main(void)
{
StackMenu();
QueueMenu();
getch();
}
![]()
//Class version of homework 25
//
// Lars G Sorensen... DS25
// Class Library version Queues w/ objects.
#include <conio.h>
#include <stdio.h>
#include <iostream.h>
#include <graphics.h>
#include <iomanip.h>
#include <process.h>
#include <stdlib.h>
#include <queues.h>
#include "lds3.h"
void Menu()
{ //Menu
int Choice,DataChoice;
MyObject *MyObjPtr; // use this for front and rear
// function return
BI_IQueueAsVector<MyObject> S;
typedef BI_IQueueAsVectorIterator <MyObject> MyIterator;
// Don't ask me why..... but the Iterator won't work
// correctly until an item has been deleted from the
// queue. In lue of picking apart the BIDS to find
// out why, I'm inserting and item now and deleting it
// without anybody looking..... B->
Integer* num = new Integer(88); // my lucky number
S.put(num);
S.get();
delete num;
// All done.......Works great!!
while (1)
{ //while
textbackground(BLACK);
clrscr();
cout <<" Lars Sorensen CUS1151 ADS Prof Davis\n";
cout <<" Class Library Queue Implementation\n";
cout <<"\n";
cout <<"1. Insert items onto Queue.\n";
cout <<"2. Display Queue in Text mode.\n";
cout <<"3. Delete an Item off the Queue.\n";
cout <<"4. Display Queue in Graphics mode.\n";
cout <<"5. Is the Queue Empty?\n";
cout <<"6. Display the Front of the Queue.\n";
cout <<"7. Display the Rear of the Queue. \n";
cout <<"0. Exit\n";
cout <<"\n";
cout << "Enter your choice: ";
cin >> (Choice);
switch (Choice) {
case 1 :
{
clrscr();
cout <<" Pick your Data Type\n";
cout <<"\n";
cout <<"1. Integer.\n";
cout <<"2. Character.\n";
cout <<"3. Float.\n";
cout <<"4. Point.\n";
cout <<"5. Circle.\n";
cout <<"6. Rectangle.\n";
cout <<"\n";
cout << "Enter your choice: ";
cin >> (DataChoice);
switch (DataChoice) {
case 1 :
{
int x;
cout << "\nEnter an Integer Element:";
cin >> x;
Integer* num = new Integer(x);
S.put(num);
break;
}
case 2 :
{
char x;
cout << "\nEnter a Character Element:";
cin >> x;
Character* ch = new Character(x);
S.put(ch);
break;
}
case 3 :
{
float x;
cout << "\nEnter a Float Element:";
cin >> x;
Float* fl = new Float(x);
S.put(fl);
break;
}
case 4 :
{
int x,y,pc;
cout << "Point\n";
cout << "Enter the X Coordinate : ";
cin >> x;
cout << "Enter the Y Coordinate : ";
cin >> y;
cout << "Enter the Color : ";
cin >> pc;
PointObj* pnt = new PointObj(x,y,pc);
S.put(pnt);
break;
}
case 5 :
{
int x,y,pc,r;
cout << "Circle\n";
cout << "Enter the X Coordinate : ";
cin >> x;
cout << "Enter the Y Coordinate : ";
cin >> y;
cout << "Enter the Color : ";
cin >> pc;
cout << "Enter the Radius : ";
cin >> r;
CircleObj* cir = new CircleObj(x,y,pc,r);
S.put(cir);
break;
}
case 6 :
{
int x,y,pc,w,h;
cout << "Rectangle\n";
cout << "Enter the X Coordinate : ";
cin >> x;
cout << "Enter the Y Coordinate : ";
cin >> y;
cout << "Enter the Color : ";
cin >> pc;
cout << "Enter the Width : ";
cin >> w;
cout << "Enter the Height : ";
cin >> h;
RectObj* rec = new RectObj(x,y,pc,w,h);
S.put(rec);
}
} // end of switch......
break;
} // end of case 1 for Put Choice....
case 2 :
{
if ( !S.isEmpty() )
{
MyIterator nextobj(S);
cout << "\nThe Queue from Rear to the Front...\n";
while ( nextobj != 0 )
{
nextobj.current()->printOn(cout);
cout << '\n';
++nextobj;
}
cout << '\n';
getch();
}
else
{
cout << " The Queue is Empty.";
getch();
}
break;
}
case 3 :
{
if ( !S.isEmpty() )
{
MyObjPtr = S.get();
MyObjPtr->printOn(cout);
cout << " has been deleted.";
getch();
delete MyObjPtr;
}
else
{
cout << "The Queue is Empty.";
getch();
}
break;
}
case 4 :
{
MyIterator nextobj(S);
SetupGraphics();
if ( !S.isEmpty() )
{
outtextxy(5,5,"Queue from the Rear to the Front... ");
int x=5,y=15;
while ( nextobj != 0 )
{
nextobj.current()->GrPrintOn(x,y);
cout << '\n';
++nextobj; y+=10;
}
cout << '\n';
getch();
}
else
{
outtextxy(5,5,"The Queue is Empty.");
getch();
}
closegraph();
break;
}
case 5 :
{
if ( S.isEmpty() )
cout << "The Queue is Empty.\n";
else
cout << "The Queue has elements.\n";
getch();
break;
}
case 6 :
{
if ( !S.isEmpty() )
{
cout << "The Front of the queue is ...\n";
S.peekRight()->printOn(cout);
cout << '\n';
}
else
cout << "The Queue is Empty!!\n";
getch();
break;
}
case 7 :
{
if ( !S.isEmpty() )
{
cout << "The Rear of the queue is ...\n";
S.peekLeft()->printOn(cout);
cout << '\n';
}
else
cout << "The Queue is Empty!!\n";
getch();
break;
}
case 0 : exit(0);
} //switch
} //while
} //Menu
void main(void)
{
Menu();
getch();
}
![]()
//Class version of homework 26
//
// Lars G Sorensen... DS26
// Class Library version Queues w/ Lists.
#include <conio.h>
#include <stdio.h>
#include <iostream.h>
#include <graphics.h>
#include <iomanip.h>
#include <process.h>
#include <stdlib.h>
#include <queues.h>
#include "lds3.h"
void Menu()
{ //Menu
int Choice,DataChoice;
MyObject *MyObjPtr; // use this for front and rear
// function return
BI_IQueueAsVector<MyObject> S;
typedef BI_IQueueAsVectorIterator <MyObject> MyIterator;
// Don't ask me why..... but the Iterator won't work
// correctly until an item has been deleted from the
// queue. In lue of picking apart the BIDS to find
// out why, I'm inserting and item now and deleting it
// without anybody looking..... B->
Integer* num = new Integer(88); // my lucky number
S.put(num);
S.get();
delete num;
// All done.......Works great!!
while (1)
{ //while
textbackground(BLACK);
clrscr();
cout <<" Lars Sorensen CUS1151 ADS Prof Davis\n";
cout <<" Class Library Queue with Lists Implementation\n";
cout <<"\n";
cout <<"1. Insert List onto Queue.\n";
cout <<"2. Display Queue in Text mode.\n";
cout <<"3. Delete an List off the Queue.\n";
cout <<"4. Display Queue in Graphics mode.\n";
cout <<"5. Is the Queue Empty?\n";
cout <<"6. Display the Front of the Queue.\n";
cout <<"7. Display the Rear of the Queue. \n";
cout <<"0. Exit\n";
cout <<"\n";
cout << "Enter your choice: ";
cin >> (Choice);
switch (Choice) {
case 1 :
{
int ListEle,i;
clrscr();
cout << "How Many Elements Does your list have? :";
cin >> ListEle;
AList* lis = new AList(ListEle);
for(i=0;i<ListEle;i++)
{
int x;
cout << "Enter an Integer: ";
cin >> x;
lis->Load(x);
}
S.put(lis);
break;
} // end of case 1 for Put Choice....
case 2 :
{
if ( !S.isEmpty() )
{
MyIterator nextobj(S);
cout << "\nThe Queue from Rear to the Front...\n";
while ( nextobj != 0 )
{
nextobj.current()->printOn(cout);
cout << '\n';
++nextobj;
}
cout << '\n';
getch();
}
else
{
cout << " The Queue is Empty.";
getch();
}
break;
}
case 3 :
{
if ( !S.isEmpty() )
{
MyObjPtr = S.get();
MyObjPtr->printOn(cout);
cout << " has been deleted.";
getch();
delete MyObjPtr;
}
else
{
cout << "The Queue is Empty.";
getch();
}
break;
}
case 4 :
{
MyIterator nextobj(S);
SetupGraphics();
if ( !S.isEmpty() )
{
outtextxy(5,5,"Queue from the Rear to the Front... ");
int x=5,y=15;
while ( nextobj != 0 )
{
nextobj.current()->GrPrintOn(x,y);
cout << '\n';
++nextobj; y+=10;
}
cout << '\n';
getch();
}
else
{
outtextxy(5,5,"The Queue is Empty.");
getch();
}
closegraph();
break;
}
case 5 :
{
if ( S.isEmpty() )
cout << "The Queue is Empty.\n";
else
cout << "The Queue has elements.\n";
getch();
break;
}
case 6 :
{
if ( !S.isEmpty() )
{
cout << "The Front of the queue is ...\n";
S.peekRight()->printOn(cout);
cout << '\n';
}
else
cout << "The Queue is Empty!!\n";
getch();
break;
}
case 7 :
{
if ( !S.isEmpty() )
{
cout << "The Rear of the queue is ...\n";
S.peekLeft()->printOn(cout);
cout << '\n';
}
else
cout << "The Queue is Empty!!\n";
getch();
break;
}
case 0 : exit(0);
} //switch
} //while
} //Menu
void main(void)
{
Menu();
getch();
}
![]()
//
//
// function protos for Binary trees
#include "oobst.h"
#include <values.h>
template <class T> class BTNode
{
N* TheData; // pntr to indirect data
BTNode* <T> Left; // pntr to other B Nodes
BTNode* <T> Right; // pntr to other B Nodes
public:
BTNode();
BTNode(BTNode <T> * inLeft, BTNode <T> * inRight, T inData);
~BTNode();
void Display();
BTNode* GetLeft() {return Left;}
BTNode* GetRight() {return Right;}
T* GetTheData() {return TheData;}
void SetLeft(BTNode <T> * inLeft) {Left = inLeft;}
void SetRight(BTNode <T> * inRight) {Right = inRight;}
void SetTheData(T* inTheData) {TheData = inTheData;}
friend BinaryTree <T>;
};
/*
//===================================================
template <class T> class BinaryTree
{
protected:
BTNode <T> * Root;
char Name[25];
int Size;
int LCount;
int MaxLeafLevel;
int MinLeafLevel;
public:
BinaryTree();
~BinaryTree();
void PreOrderDisplay(BTNode <T> * & Ptr );
void InOrderDisplay(BTNode <T> * & Ptr );
void PostOrderDisplay(BTNode <T> * & Ptr );
void GetDataKB();
void GetDataDF();
void GetDataRandom();
// access functions for class....
BTNode <T> * GetRoot() {return Root;}
// Insert and Delete
void Insert (BTNode <T> * & ptr, T x);
void Delete (float keyval);
void Delitem(BTNode <T> * & DelPtr );
void Leftmost(BTNode <T> * & p, T& getitem);
int Del(BTNode <T> * & p, float keyval);
};
*/
//
//
// Lars Sorensen BST Imps
//
// Implementation code for the binary tree apps.
//
//
template <class T> BTNode<T>::BTNode()
{
Left = NULL;
Right = NULL;
TheData = NULL;
}
template <class T> BTNode<T>::BTNode(BTNode <T> * inLeft,
BTNode <T> * inRight,
T inData )
{
Left = inLeft;
Right = inRight;
TheData = new T (inData);
}
template <class T> BTNode<T>::~BTNode()
{
delete TheData;
}
template <class T> void BTNode<T>::Display()
{
cout << *TheData;
}
#pragma option -Jgd
typedef BTNode<int> Phoney_I;
typedef BTNode<float> P_f;
typedef BTNode<char> P_f;
/*
// The Binary Tree Function Imps
template <class T> BinaryTree<T>::BinaryTree()
{
Size = 0;
strcopy(Name,"asd");
//
//
//
/// recursive functions.......
preorder // visit left right
template <class T> void PreOrderDisplay(............
{
if (Ptr != NULL)
{
Ptr ->Display(); // visit
cout << " ";
PreOrderDisplay(Ptr->Left); // Left
PreOrderDisplay(Ptr->Right); // Right
}
inorder // left visit right
postorder // left right visit
// The Insert......
//pg 538
template <class T> void BinaryTree::Insert (BTNode <T> * & Ptr, T x)
{
if ( Ptr == NULL )
Ptr = new BTNode<T>(NULL,NULL,x);
else
{
if (x > (*(Ptr->GetTheData())))
Insert(Ptr->Right,x);
else
Insert(Ptr->Left,x);
}
}
#pragma option -Jgd
![]()