Browse Source

"19-05-12"

master
jcstill 3 years ago
parent
commit
00503b4ac0
  1. BIN
      List/Driver
  2. 19
      List/Driver.cpp
  3. 8
      List/List.h
  4. BIN
      Queue/Driver
  5. 98
      Queue/Driver.cpp
  6. 235
      Queue/Queue.h
  7. BIN
      Stack/Driver
  8. 97
      Stack/Driver.cpp
  9. 180
      Stack/Stack.h
  10. BIN
      Vector/Driver
  11. 15
      Vector/Driver.cpp
  12. 7
      Vector/Vector.h

BIN
List/Driver

19
List/Driver.cpp

@ -7,7 +7,7 @@ int main(){
cout << TMP.isEmpty() << "\n\n";
cout << "push_back" << '\n';
for(int i=0; i<5; i++){
for(int i=0; i<6; i++){
TMP.push_back(i);
}
TMP.print();
@ -99,10 +99,17 @@ int main(){
TMP2 = TMP;
TMP2.print();
cout << '\n';
for(int i=0; i<6; i++){
TMP.push_back(i);
}
TMP.print();
cout << '\n';
cout << "reverse" << '\n';
TMP.reverse();
TMP.print();
cout << '\n';
return 0;
}
// void reverse();
// const List<T>& operator=(const List<T>&);
}

8
List/List.h

@ -94,7 +94,7 @@ template<class T>List<T>::~List(){
template<class T>void List<T>::resize(int x){
if(x > count){
T tmp{};
for(int i=x-count; i<x; i++){
for(int i=count-x; i<0; i++){
push_back(tmp);
}
}else if(x < count){
@ -104,7 +104,7 @@ template<class T>void List<T>::resize(int x){
}
}
template<class T>void List<T>::swap(int x, int y){
if(x >= 0 && x < count && y >= 0 && y < count){
if(x >= 0 && x < count && y >= 0 && y < count && first != nullptr){
node<T> *current = first;
node<T> *current2 = first;
for(int i=0; i<x; i++){
@ -261,7 +261,9 @@ template<class T>int List<T>::search(const T& x){
return -1;
}
template<class T>void List<T>::reverse(){
for(int i=0; i<(count/2); i++){
swap(i, count-i-1);
}
}
template<class T>const List<T>& List<T>::operator=(const List<T>& x){
clear(0, count - 1);

BIN
Queue/Driver

98
Queue/Driver.cpp

@ -0,0 +1,98 @@
#include "Queue.h"
int main(){
Queue<int> TMP;
cout << "isEmpty" << '\n';
cout << TMP.isEmpty() << "\n\n";
cout << "push" << '\n';
for(int i=0; i<5; i++){
TMP.push(i);
}
TMP.print();
cout << '\n';
cout << "sizeOf" << '\n';
cout << TMP.sizeOf() << "\n\n";
cout << "pop" << '\n';
TMP.pop();
TMP.print();
cout << '\n';
cout << "swap" << '\n';
TMP.swap(0, 2);
TMP.print();
cout << '\n';
cout << "isEmpty" << '\n';
cout << TMP.isEmpty() << "\n\n";
cout << "front\n" << TMP.front() << "\n\n";
cout << "back\n" << TMP.back() << "\n\n";
cout << "resize (larger)" << '\n';
TMP.resize(8);
TMP.print();
cout << '\n';
cout << "resize (smaller)" << '\n';
TMP.resize(6);
TMP.print();
cout << '\n';
cout << "assign" << '\n';
TMP.assign(15, 4);
TMP.assign(32, 5);
TMP.print();
cout << '\n';
cout << "clear" << '\n';
TMP.clear(1);
TMP.print();
cout << '\n';
cout << "search" << '\n';
cout << TMP.search(7) << '\n';
cout << TMP.search(15) << '\n';
cout << '\n';
cout << "Create new queue 15 long\n";
Queue<int> TMP2(15);
TMP2.print();
cout << '\n';
cout << "Copy Constructor\n";
Queue<int> TMP3(TMP);
cout << "TMP3\n";
TMP3.print();
cout << '\n';
cout << "TMP\n";
TMP.print();
cout << '\n';
TMP3.~Queue();
cout << "Overloaded assignment operator\n";
TMP2 = TMP;
cout << "TMP2\n";
TMP2.print();
cout << '\n';
cout << "TMP\n";
TMP.print();
cout << '\n';
TMP2.~Queue();
for(int i=0; i<5; i++){
TMP.push(i);
}
TMP.print();
cout << '\n';
cout << "reverse" << '\n';
TMP.reverse();
TMP.print();
cout << '\n';
return 0;
}

235
Queue/Queue.h

@ -0,0 +1,235 @@
#ifndef __QUEUE_H__
#define __QUEUE_H__
#include <iostream>
using namespace std;
template<class T>struct node{
T data; // Stores data
node<T> *next; // Pointer to next node
};
template<class T>class Queue{
private:
node<T> *first; // Pointer to first node
node<T> *last; // Pointer to last node
int count; // Current size of the Queue
void copy(Queue<T> &); // Deep copy the Queue
node<T> *tFst; // Temporary pointer for copy ops
void hold(Queue<T> &); // Create a tmp reversed stack
public:
Queue(); // Default Constructor
Queue(int); // Non-default Constructor
Queue(Queue<T>&); // Copy Constructor
~Queue(); // Destructor
bool isEmpty(); // Checks if Queue is empty
int sizeOf() const; // Returns the current size of the Queue
const T& front() const; // Return element at front of Queue
const T& back() const; // Return element at back of Queue
void push(const T&); // Add element at the back
void pop(); // Delete first element
void swap(int, int); // Swap elements
void print(); // Prints entire Queue
void resize(int); // Make Queue X elements long adding to back or removing from front
void clear(int); // Erase content through index (start at front moving backwards)
void assign(const T&, int); // Set value at index
int search(const T&); // Returns index of element if in Queue else returns -1
void reverse(); // Reverses the whole Queue
const Queue<T>& operator=(Queue<T>&); // Overloaded assignment operator
/*
Functions for iterator:
begin Return iterator to beginning (public member function )
end Return iterator to end (public member function)
rbegin Return reverse iterator to reverse beginning (public member function )
rend Return reverse iterator to reverse end (public member function )
cbegin Return const_iterator to beginning (public member function )
cend Return const_iterator to end (public member function )
crbegin Return const_reverse_iterator to reverse beginning (public member function )
crend Return const_reverse_iterator to reverse end (public member function )
data Access data (public member function )
get_allocator Get allocator (public member function )
*/
};
template<class T>Queue<T>::Queue(){
first = nullptr;
last = nullptr;
tFst = nullptr;
count = 0;
}
template<class T>Queue<T>::Queue(int x){
first = nullptr;
last = nullptr;
tFst = nullptr;
count = 0;
T tmp{};
for(int i=0; i<x; i++){
push(tmp);
}
}
template<class T>Queue<T>::Queue(Queue<T>& x){
first = nullptr;
last = nullptr;
tFst = nullptr;
count = 0;
copy(x);
}
template<class T>Queue<T>::~Queue(){
node<T> *tmp;
while(first != nullptr){
tmp = first;
first = first -> next;
delete tmp;
}
last = nullptr;
count = 0;
}
template<class T>void Queue<T>::hold(Queue<T> & x){
int tsize = x.sizeOf();
for(int i=0; i<tsize; i++){
node<T> *newnode = nullptr;
newnode = new node<T>;
newnode -> data = x.front();
newnode -> next = nullptr;
if(tFst == nullptr){
tFst = newnode;
}else{
newnode -> next = tFst;
tFst = newnode;
}
x.pop();
}
}
template<class T>void Queue<T>::copy(Queue<T> & x){
int tsize = x.sizeOf();
hold(x);
for(int i=0; i<tsize; i++){
push(tFst -> data);
x.push(tFst -> data);
if(tFst != nullptr){
node<T> *current = tFst;
tFst = tFst -> next;
current -> next = nullptr;
delete current;
}
}
}
template<class T>bool Queue<T>::isEmpty(){
return count == 0;
}
template<class T>int Queue<T>::sizeOf() const{
return count;
}
template<class T>const T& Queue<T>::front() const{
return first -> data;
}
template<class T>const T& Queue<T>::back() const{
return last -> data;
}
template<class T>void Queue<T>::push(const T& x){
node<T> *current = first;
node<T> *newnode = nullptr;
newnode = new node<T>;
newnode -> data = x;
newnode -> next = nullptr;
if(first == nullptr){
first = newnode;
last = newnode;
}else{
last = newnode;
for(int i=0; i<count-1; i++){
current = current -> next;
}
current -> next = newnode;
}
count++;
}
template<class T>void Queue<T>::pop(){
if(first != nullptr){
node<T> *current = first;
first = first -> next;
current -> next = nullptr;
delete current;
count--;
}
}
template<class T>void Queue<T>::swap(int x, int y){
if(x >= 0 && x < count && y >= 0 && y < count && first != nullptr){
node<T> *current = first;
node<T> *current2 = first;
for(int i=0; i<x; i++){
current = current -> next;
}
for(int i=0; i<y; i++){
current2 = current2 -> next;
}
T tmp = current -> data;
T tmp2 = current2 -> data;
current2 -> data = tmp;
current -> data = tmp2;
}
}
template<class T>void Queue<T>::print(){
node<T> *current = first;
for(int i=0; i<count; i++){
if(current != nullptr){
cout << "Index [" << i << "] is: " << current -> data << '\n';
}
current = current -> next;
}
}
template<class T>void Queue<T>::resize(int x){
if(x > count){
T tmp{};
for(int i=count-x; i<0; i++){
push(tmp);
}
}else if(x < count){
for(int i=x; i<=count; i++){
pop();
}
}
}
template<class T>void Queue<T>::assign(const T& x, int y){
node<T> *current = first;
for(int i=0; i<y; i++){
current = current -> next;
}
if(current != nullptr){
current -> data = x;
}
}
template<class T>void Queue<T>::clear(int x){
for(int i=0; i<=x; i++){
pop();
}
}
template<class T>int Queue<T>::search(const T& x){
node<T> *current = first;
for(int i=0; i<count; i++){
if(current -> data == x){
return i;
}
current = current -> next;
}
return -1;
}
template<class T>void Queue<T>::reverse(){
for(int i=0; i<(count/2); i++){
swap(i, count-i-1);
}
}
template<class T>const Queue<T>& Queue<T>::operator=(Queue<T>& x){
clear(count);
first = nullptr;
count = 0;
if(this != &x){
copy(x);
}
return *this;
}
#endif

BIN
Stack/Driver

97
Stack/Driver.cpp

@ -0,0 +1,97 @@
#include "Stack.h"
int main(){
Stack<int> TMP;
cout << "isEmpty" << '\n';
cout << TMP.isEmpty() << "\n\n";
cout << "push" << '\n';
for(int i=0; i<6; i++){
TMP.push(i);
}
TMP.print();
cout << '\n';
cout << "sizeOf" << '\n';
cout << TMP.sizeOf() << "\n\n";
cout << "pop" << '\n';
TMP.pop();
TMP.print();
cout << '\n';
cout << "swap" << '\n';
TMP.swap(0, 2);
TMP.print();
cout << '\n';
cout << "isEmpty" << '\n';
cout << TMP.isEmpty() << "\n\n";
cout << "top\n" << TMP.top() << "\n\n";
cout << "resize (larger)" << '\n';
TMP.resize(8);
TMP.print();
cout << '\n';
cout << "resize (smaller)" << '\n';
TMP.resize(6);
TMP.print();
cout << '\n';
cout << "assign" << '\n';
TMP.assign(15, 4);
TMP.assign(32, 5);
TMP.print();
cout << '\n';
cout << "clear" << '\n';
TMP.clear(4);
TMP.print();
cout << '\n';
cout << "search" << '\n';
cout << TMP.search(7) << '\n';
cout << TMP.search(0) << '\n';
cout << '\n';
cout << "Create new stack 15 long\n";
Stack<int> TMP2(15);
TMP2.print();
cout << '\n';
cout << "Copy Constructor\n";
Stack<int> TMP3(TMP);
cout << "TMP3\n";
TMP3.print();
cout << '\n';
cout << "TMP\n";
TMP.print();
cout << '\n';
TMP3.~Stack();
cout << "Overloaded assignment operator\n";
TMP2 = TMP;
cout << "TMP2\n";
TMP2.print();
cout << '\n';
cout << "TMP\n";
TMP.print();
cout << '\n';
TMP2.~Stack();
for(int i=0; i<6; i++){
TMP.push(i);
}
TMP.print();
cout << '\n';
cout << "reverse" << '\n';
TMP.reverse();
TMP.print();
cout << '\n';
return 0;
}

180
Stack/Stack.h

@ -12,12 +12,14 @@ template<class T>class Stack{
private:
node<T> *first; // Pointer to first node
int count; // Current size of the Stack
void copy(const Stack<T> &); // Deep copy the Stack
// void hold(); // Create a holding stack
void copy(Stack<T> &); // Deep copy the Stack
node<T> *tFst; // Temporary pointer for copy ops
void hold(Stack<T> &); // Create a tmp reversed stack
public:
Stack(); // Default Constructor
Stack(int); // Non-default Constructor
Stack(const Stack<T>&); // Copy Constructor
Stack(Stack<T>&); // Copy Constructor
~Stack(); // Destructor
bool isEmpty(); // Checks if Stack is empty
@ -26,16 +28,14 @@ template<class T>class Stack{
void push(const T&); // Insert element at the front of the Stack
void pop(); // Delete first element
void swap(int, int); // Swap elements
void print(); // Prints entire Stack
void resize(int); // Make Stack X elements long adding or removing at the top
void erase(int); // Erase element at index
void clear(int, int); // Erase content range (first, last)
void insert(const T&, int); // Insert element at position
void clear(int); // Erase content through index
void assign(const T&, int); // Set value at index
void print(); // Prints entire Stack
int search(const T&); // Returns index of element if in Stack else returns -1
void reverse(); // Reverses the whole Stack
const Stack<T>& operator=(const Stack<T>&); // Overloaded assignment operator
const Stack<T>& operator=(Stack<T>&); // Overloaded assignment operator
/*
Functions for iterator:
@ -51,21 +51,23 @@ Functions for iterator:
get_allocator Get allocator (public member function )
*/
};
template<class T>Stack<T>::Stack(){
first = nullptr;
tFst = nullptr;
count = 0;
}
template<class T>Stack<T>::Stack(int x){
first = nullptr;
tFst = nullptr;
count = 0;
T tmp{};
for(int i=0; i<x; i++){
push(tmp);
}
}
template<class T>Stack<T>::Stack(const Stack<T>& x){
template<class T>Stack<T>::Stack(Stack<T>& x){
first = nullptr;
tFst = nullptr;
count = 0;
copy(x);
}
@ -78,6 +80,35 @@ template<class T>Stack<T>::~Stack(){
}
count = 0;
}
template<class T>void Stack<T>::hold(Stack<T> & x){
int tsize = x.sizeOf();
for(int i=0; i<tsize; i++){
node<T> *newnode = nullptr;
newnode = new node<T>;
newnode -> data = x.top();
newnode -> next = nullptr;
if(tFst == nullptr){
tFst = newnode;
}else{
newnode -> next = tFst;
tFst = newnode;
}
x.pop();
}
}
template<class T>void Stack<T>::copy(Stack<T> & x){
int tsize = x.sizeOf();
hold(x);
for(int i=0; i<tsize; i++){
push(tFst -> data);
x.push(tFst -> data);
if(tFst != nullptr){
node<T> *current = tFst;
tFst = tFst -> next;
delete current;
}
}
}
template<class T>bool Stack<T>::isEmpty(){
return count == 0;
}
@ -87,75 +118,104 @@ template<class T>int Stack<T>::sizeOf() const{
template<class T>const T& Stack<T>::top() const{
return first -> data;
}
template<class T>void Stack<T>::push(const T& x){
node<T> *current = nullptr;
node<T> *curback = nullptr;
node<T> *newnode = nullptr;
newnode = new node<T>;
newnode -> data = x;
newnode -> next = nullptr;
newnode -> prev = nullptr;
if(first == nullptr){
first = newnode;
last = newnode;
count++;
}else{
current = first;
if(y == 0){
first -> prev = newnode;
newnode -> next = first;
first = newnode;
count++;
}else if(y <= count){
for(int i=0; i<y; i++){
curback = current;
current = current -> next;
}
if(current != nullptr){
current -> prev = newnode;
newnode -> next = current;
newnode -> prev = curback;
curback -> next = newnode;
}else{
curback -> next = newnode;
newnode -> prev = curback;
last = newnode;
}
count++;
}
newnode -> next = first;
first = newnode;
}
count++;
}
template<class T>void Stack<T>::pop(){
node<T> *current = first;
node<T> *curback = nullptr;
if(x == 0){
if(first != nullptr){
node<T> *current = first;
first = first -> next;
if(first != nullptr){
first -> prev = nullptr;
}else{
last = nullptr;
}
delete current;
count--;
}else if(x<count){
for(int i=0; i<x; i++){
}
}
template<class T>void Stack<T>::swap(int x, int y){
if(x >= 0 && x < count && y >= 0 && y < count && first != nullptr){
node<T> *current = first;
node<T> *current2 = first;
for(int i=count-1; i>x; i--){
current = current -> next;
}
curback = current -> prev;
curback -> next = current -> next;
if(current -> next != nullptr){
current -> next -> prev = curback;
for(int i=count-1; i>y; i--){
current2 = current2 -> next;
}
T tmp = current -> data;
T tmp2 = current2 -> data;
current2 -> data = tmp;
current -> data = tmp2;
}
}
template<class T>void Stack<T>::print(){
node<T> *current = first;
for(int i=0; i<count; i++){
if(current != nullptr){
cout << "Index [" << count-i-1 << "] is: " << current -> data << '\n';
}
current = current -> next;
}
}
template<class T>void Stack<T>::resize(int x){
if(x > count){
T tmp{};
for(int i=count-x; i<0; i++){
push(tmp);
}
if(current == last){
last = curback;
}else if(x < count){
for(int i=x; i<=count; i++){
pop();
}
count--;
delete current;
}
}
template<class T>void Stack<T>::clear(int x){
for(int i=count-1; i>=x; i--){
pop();
}
}
template<class T>void Stack<T>::assign(const T& x, int y){
node<T> *current = first;
for(int i=count-1; i>y; i--){
current = current -> next;
}
if(current != nullptr){
current -> data = x;
}
}
template<class T>int Stack<T>::search(const T& x){
node<T> *current = first;
for(int i=0; i<count; i++){
if(current -> data == x){
return count-i-1;
}
current = current -> next;
}
return -1;
}
template<class T>void Stack<T>::reverse(){
for(int i=0; i<(count/2); i++){
swap(i, count-i-1);
}
}
template<class T>const Stack<T>& Stack<T>::operator=(Stack<T>& x){
clear(count);
first = nullptr;
count = 0;
if(this != &x){
copy(x);
}
return *this;
}
#endif

BIN
Vector/Driver

15
Vector/Driver.cpp

@ -7,7 +7,7 @@ int main(){
cout << TMP.isEmpty() << "\n\n";
cout << "push_back" << '\n';
for(int i=0; i<5; i++){
for(int i=0; i<6; i++){
TMP.push_back(i);
}
TMP.print();
@ -78,7 +78,7 @@ int main(){
cout << TMP.search(7) << '\n';
TMP.print();
cout << '\n';
cout << "Front: " << TMP.retFront() << '\n';
cout << "End: " << TMP.retEnd() << '\n';
cout << "At 2: " << TMP.retAt(2) << "\n\n";
@ -100,5 +100,16 @@ int main(){
TMP2.print();
cout << '\n';
for(int i=0; i<5; i++){
TMP.push_back(i);
}
TMP.print();
cout << '\n';
cout << "reverse" << '\n';
TMP.reverse();
TMP.print();
cout << '\n';
return 0;
}

7
Vector/Vector.h

@ -41,6 +41,7 @@ template<class T>class Vector{
void shrink_to_fit(); // Shrink array to fit data
void resize(int); // Make array X elements long (shortening list will destroy remaining elements)
const T& retAt(int) const; // Return element at index
void reverse(); // Reverses the whole array
const Vector<T>& operator=(const Vector<T>&); // Overloaded assignment operator
const T& operator[](int) const; // Overloaded bracket operator
@ -289,6 +290,10 @@ template<class T>const T& Vector<T>::retAt(int x) const{
cout << "\nERROR! Vector<T>::retAt(int) out of bounds\n";
return array[0];
}
template<class T>void Vector<T>::reverse(){
for(int i=0; i<(size/2); i++){
swap(i, size-i-1);
}
}
#endif
Loading…
Cancel
Save