Basic Data Structures
Basic Data Structures
a data type that does not describe or belong to any specific data, yet
allows the specification of organization and manipulation of data
a data type whose properties (domain and operations) are specified
independently of any particular implementation
a data type that specifies and can share its logical properties without
giving specifics of the implementation code
a way of thinking about data types, often outside the constraints of a
programming language
actual data type is added later in an implementation
// File StackType.h
#include "ItemType.h"
class StackType
{
public:
StackType( );
// Class constructor.
bool IsFull () const;
// Function: Determines whether the stack is full.
// Pre: Stack has been initialized
// Post: Function value = (stack is full)
// File: StackType.cpp
#include "StackType.h"
#include <iostream>
StackType::StackType( )
{
top = -1;
}
bool StackType::IsEmpty() const
{
return(top = = -1);
}
void StackType::Pop()
{
if( IsEmpty() )
throw EmptyStack();
top--;
}
ItemType StackType::Top()
{
if (IsEmpty())
throw EmptyStack();
return items[top];
}
charStack.Push(letter);
charStack.Push(’C’);
charStack.Push(’S’);
charStack.Pop();
charStack.Push(’K’);
while (!charStack.IsEmpty())
{
letter = charStack.Top();
charStack.Pop()
}
class EmptyQueue
{};
struct NodeType;
#include "ItemType.h"
class QueType
{
public:
QueType();
// Class constructor.
// Because there is a default constructor, the precondition
// that the queue has been initialized is omitted.
QueType(int max);
// Parameterized class constructor.
~QueType();
// Class destructor.
void MakeEmpty();
// Function: Initializes the queue to an empty state.
// Post: Queue is empty.
struct NodeType {
ItemType info;
NodeType* next;
};
void QueType::MakeEmpty()
// Post: Queue is empty; all elements have been deallocated.
{
NodeType* tempPtr;
// Class destructor.
QueType::~QueType()
{
MakeEmpty();
}
tempPtr = front;
item = front->info;
front = front->next;
if (front == NULL)
rear = NULL;
delete tempPtr;
}
}
int main()
{
ItemType item;
QueType queue;
int numCommands;
// Prompt for file names, read file names, and prepare files
cout << "Enter name of input command file; press return." << endl;
cin >> inFileName;
inFile.open(inFileName.c_str());
cout << "Enter name of output file; press return." << endl;
cin >> outFileName;
outFile.open(outFileName.c_str());
cout << "Enter name of test run; press return." << endl;
cin >> outputLabel;
outFile << outputLabel << endl;
catch (EmptyQueue)
{
outFile << "EmtpyQueue exception thrown." << endl;
}
numCommands++;
cout << " Command number " << numCommands << " completed."
<< endl;
inFile >> command;
};
Root
Leaf Node
Node Leaf
B C
D E
F G
50
40 70
60 80
55 65