#include <cstddef>
#include <new>
template <class ItemType>
struct NodeType;
template<class ItemType>
class UnsortedType {
public:
UnsortedType();
~UnsortedType();
void MakeEmpty();
void InsertItem(ItemType item);
void DeleteItem(ItemType item);
bool IsFull();
int LengthIs();
void RetrieveItem(ItemType& item, bool& found);
void ResetList();
void GetNextItem(ItemType& item);
private:
int length;
NodeType<ItemType>* listData;
NodeType<ItemType>* currentPos;
};
template<class ItemType>
struct NodeType {
ItemType info;
NodeType<ItemType>* next;
};
template<class ItemType>
UnsortedType<ItemType>::UnsortedType() {
length = 0;
listData = NULL;
}
template<class ItemType>
UnsortedType<ItemType>::~UnsortedType() {
MakeEmpty();
}
template<class ItemType>
void UnsortedType<ItemType>::MakeEmpty() {
NodeType<ItemType>* tempPtr;
while (listData != NULL) {
tempPtr = listData;
listData = listData->next;
delete tempPtr;
}
length = 0;
}
template<class ItemType>
bool UnsortedType<ItemType>::IsFull() {
NodeType<ItemType>* location;
try {
location = new NodeType<ItemType>;
delete location;
return false;
}
catch (std::bad_alloc exception) {
return true;
}
}
template<class ItemType>
int UnsortedType<ItemType>::LengthIs() {
return length;
}
template<class ItemType>
void UnsortedType<ItemType>::InsertItem(ItemType item) {
NodeType<ItemType>* location;
location = new NodeType<ItemType>;
location->info = item;
location->next = listData;
listData = location;
length++;
}
template<class ItemType>
void UnsortedType<ItemType>::DeleteItem(ItemType item) {
NodeType<ItemType>* location;
NodeType<ItemType>* tempLocation;
location = listData;
if (location->info == item) { // 처음에 item 존재
tempLocation = location;
listData = location->next;
}
else {// 중간에 item 존재
while ((location->next)->info != item)
location = location->next;
tempLocation = location->next;
location->next = (location->next)->next;
}
delete tempLocation;
length--;
}
template<class ItemType>
void UnsortedType<ItemType>::ResetList() {
currentPos = NULL;
}
template<class ItemType>
void UnsortedType<ItemType>::GetNextItem(ItemType& item) {
if (currentPos == NULL) {
currentPos = listData;
}
else {
currentPos = currentPos->next;
}
item = currentPos->info;
}
template<class ItemType>
void UnsortedType<ItemType>::RetrieveItem(ItemType& item, bool& found) {
NodeType<ItemType>* location = listData;
bool moreToSearch = (location != NULL);
found = false;
while (moreToSearch && !found) {
if (location->info == item) {
found = true;
item = location->info;
}
else {
location = location->next;
moreToSearch = (location != NULL);
}
}
}
Operation | Array Implementaion | Linked-list Implementation |
---|---|---|
Constructor | O(1) | O(1) |
MakeEmpty | O(1) | O(N) |
InsertItem | O(1) | O(1) |
DeleteItem | O(N) | O(N) |
IsFull | O(1) | O(1) |
LengthIs | O(1) | O(1) |
RetrieveItem | O(N) | O(N) |
ResetList | O(1) | O(1) |
GetNextItem | O(1) | O(1) |
Destructor | O(1) | O(N) |
#include <cstddef>
#include <new>
#include "ItemType.h"
#define MAX_ITEMS 100
template<class ItemType>
class SortedType {
public:
SortedType();
~SortedType();
void MakeEmpty();
void InsertItem(ItemType item);
void DeleteItem(ItemType item);
bool IsFull();
int LengthIs();
void RetrieveItem(ItemType& item, bool& found);
void ResetList();
void GetNextItem(ItemType& item);
private:
int length;
NodeType<ItemType>* listData;
NodeType<ItemType>* currentPos;
};
template<class ItemType>
struct NodeType {
ItemType info;
ItemType* next;
};
template<class ItemType>
SortedType<ItemType>::SortedType() {
length = 0;
listData = NULL;
}
template<class ItemType>
SortedType<ItemType>::~SortedType() {
MakeEmpty();
}
template<class ItemType>
void SortedType<ItemType>::MakeEmpty() {
NodeType<ItemType>* tempPtr;
while (listData != NULL) {
tempPtr = listData;
listData = listData->next;
delete tempPtr;
}
length = 0;
}
template<class ItemType>
bool SortedType<ItemType>::IsFull() {
NodeType<ItemType>* location;
try {
location = new NodeType<ItemType>;
delete location;
return false;
}
catch (std::bad_alloc exception) {
return true;
}
}
template<class ItemType>
int SortedType<ItemType>::LengthIs() {
return length;
}
template<class ItemType>
void SortedType<ItemType>::InsertItem(ItemType item) {
NodeType<ItemType>* location;
NodeType<ItemType>* preLoc;
NodeType<ItemType>* newNode;
location = listData;
preLoc = NULL;
bool moreToSearch = (location != NULL);
while (mroeToSearch) {
if (location->info < item) {
preLoc = location;
location = location->next;
moreToSearch = (location != NULL);
}
else
moreToSearch = false;
}
newNode = new NodeType<ItemType>;
newNode->info = item;
if (preLoc == NULL) {
newNode->next = location;
listData = newNode;
}
else {
newNode->next = location;
preLoc->next = newNode;
}
length++;
}
template<class ItemType>
void SortedType<ItemType>::DeleteItem(ItemType item) {
NodeType<ItemType>* location;
NodeType<ItemType>* tempLocation;
location = listData;
if (location->info == item) { // 처음에 item 존재
tempLocaton = location;
listData = location->nextl
}
else {// 중간에 item 존재
while ((location->next)->infp != item)
locaiton = location->next;
tempLocation = location->next;
location->next = (location->next)->next;
}
delete tempLocation;
length--;
}
template<class ItemType>
void SortedType<ItemType>::ResetList() {
currentPos = NULL;
}
template<class ItemType>
void SortedType<ItemType>::GetNextItem(ItemType& item) {
if (currentPos == NULL) {
currentPos = listData;
}
else {
currentPos = currentPos->next;
}
item = currentPos->info;
}
template<class ItemType>
void SortedType<ItemType>::RetrieveItem(ItemType& item, bool& found) {
NodeType<ItemType>* location = listData;
bool moreToSearch = (location != NULL);
found = false;
while (moreToSearch && !found) {
if (location->info == item) {
found = true;
item = location->info;
}
else {
location = location->next;
moreToSearch = (location != NULL);
}
}
}
Operation | Array Implementaion | Linked-list Implementation |
---|---|---|
Constructor | O(1) | O(1) |
MakeEmpty | O(1) | O(N) |
InsertItem | O(1) | O(1) |
DeleteItem | O(N) | O(N) |
IsFull | O(1) | O(1) |
LengthIs | O(1) | O(1) |
RetrieveItem | O(N) | O(N) |
ResetList | O(1) | O(1) |
GetNextItem | O(1) | O(1) |
Destructor | O(1) | O(N) |
#include <cstddef>
#include <new>
template <class ItemType>
struct NodeType;
class FullStack {
};
class EmptyStack {
};
template <class ItemType>
class StackType {
public:
StackType();
~StackType();
bool IsFull() const;
bool IsEmpty() const;
void Push(ItemType item);
void Pop();
ItemType Top();
private:
NodeType<ItemType>* topPtr;
};
template <class ItemType>
struct NodeType {
ItemType info;
NodeType* next;
};
template <class ItemType>
StackType<ItemType>::StackType() {
topPtr = NULL;
}
template <class ItemType>
StackType<ItemType>::~StackType() {
NodeType<ItemType>* tempPtr;
while (topPtr != NULL) {
tempPtr = topPtr;
topPtr = topPtr->next;
delete tempPtr;
}
}
template <class ItemType>
bool StackType<ItemType>::IsFull() const {
NodeType<ItemType>* location;
try {
location = new NodeType<ItemType>;
delete location;
return false;
}
catch (std::bad_alloc exception) {
return true;
}
}
template <class ItemType>
bool StackType<ItemType>::IsEmpty() const {
return (topPtr == NULL);
}
template <class ItemType>
ItemType StackType<ItemType>::Top() {
if (topPtr == NULL)
throw EmptyStack();
else
return topPtr->info;
}
template <class ItemType>
void StackType<ItemType>::Push(ItemType item) {
if (IsFull())
throw FullStack();
else {
NodeType<ItemType>* location;
location = new NodeType<ItemType>;
location->info = item;
location->next = topPtr;
topPtr = location;
}
}
template <class ItemType>
void StackType<ItemType>::Pop() {
if (IsEmpty())
throw EmptyStack();
else {
NodeType<ItemType>* tempPtr;
tempPtr = topPtr;
topPtr = topPtr->next;
delete tempPtr;
}
}
Operation | Array Implementaion | Linked-list Implementation |
---|---|---|
Constructor | O(1) | O(1) |
MakeEmpty | O(1) | O(N) |
Push | O(1) | O(1) |
Pop | O(1) | O(1) |
IsFull | O(1) | O(1) |
IsEmpty | O(1) | O(1) |
Top | O(1) | O(1) |
Destructor | O(1) | O(N) |
#include <cstddef>
#include <new>
#include <cstddef>
#include <new>
template <class ItemType>
struct NodeType;
class FullQueue {
};
class EmptyQueue {
};
template<class ItemType>
class QueueType {
public:
QueueType();
~QueueType();
void MakeEmpty();
bool IsFull() const;
bool IsEmpty() const;
void Enqueue(ItemType newItem);
void Dequeue(ItemType& item);
private:
NodeType<ItemType>* front;
NodeType<ItemType>* rear;
};
template<class ItemType>
struct NodeType {
ItemType info;
NodeType<ItemType>* next;
};
template<class ItemType>
QueueType<ItemType>::QueueType() {
front = NULL;
rear = NULL;
}
template<class ItemType>
QueueType<ItemType>::~QueueType() {
MakeEmpty();
}
template<class ItemType>
void QueueType<ItemType>::MakeEmpty() {
NodeType<ItemType>* tempPtr;
while (front != NULL) {
tempPtr = front;
front = front->next;
delete tempPtr;
}
rear = NULL;
}
template<class ItemType>
bool QueueType<ItemType>::IsEmpty() const {
return (front == NULL);
}
template<class ItemType>
bool QueueType<ItemType>::IsFull() const {
NodeType<ItemType>* location;
try {
location = new NodeType<ItemType>;
delete location;
return false;
}
catch (std::bad_alloc exception) {
return true;
}
}
template<class ItemType>
void QueueType<ItemType>::Enqueue(ItemType newItem) {
if (IsFull())
throw FullQueue();
else {
NodeType<ItemType>* location;
location = new NodeType<ItemType>;
location->info = newItem;
if (rear == NULL)
front = location;
else
rear->next = location;
rear = location;
}
}
template<class ItemType>
void QueueType<ItemType>::Dequeue(ItemType& item) {
if (IsEmpty())
throw EmptyQueue();
else {
NodeType<ItemType>* tempPtr;
tempPtr = front;
item = front->info;
front = front->next;
if (front == NULL)
rear = NULL;
delete tempPtr;
}
}
Operation | Array Implementaion | Linked-list Implementation |
---|---|---|
Constructor | O(1) | O(1) |
MakeEmpty | O(1) | O(N) |
Enqueue | O(1) | O(1) |
Dequeue | O(1) | O(1) |
IsFull | O(1) | O(1) |
IsEmpty | O(1) | O(1) |
|Destructor |O(1) |O(N) |