added old projects
This commit is contained in:
BIN
Binary file not shown.
|
After Width: | Height: | Size: 507 KiB |
@@ -0,0 +1,98 @@
|
||||
//
|
||||
// doublylinkedlist.cpp
|
||||
// DoublyLinkedList
|
||||
//
|
||||
// Created by Austin Bennett on 7/18/22.
|
||||
//
|
||||
/*
|
||||
Constructor
|
||||
Destructor
|
||||
Insert function
|
||||
Remove function
|
||||
Accessor function
|
||||
Mutator function
|
||||
*/
|
||||
#include <iostream>
|
||||
|
||||
#include "doublylinkedlist.hpp"
|
||||
|
||||
using namespace::std;
|
||||
|
||||
DoublyLinkedList::DoublyLinkedList() {
|
||||
front = NULL;
|
||||
back = NULL;
|
||||
}
|
||||
|
||||
void DoublyLinkedList::Insert(double data) {
|
||||
Node* newNode = new Node(data);
|
||||
|
||||
bool isEmpty = (front == NULL);
|
||||
|
||||
if (isEmpty) {
|
||||
front = newNode;
|
||||
back = newNode;
|
||||
newNode -> prev = NULL;
|
||||
newNode -> next = NULL;
|
||||
} else {
|
||||
back -> next = newNode;
|
||||
newNode -> prev = back;
|
||||
newNode -> next = NULL;
|
||||
back = newNode;
|
||||
}
|
||||
}
|
||||
|
||||
void DoublyLinkedList::Remove(int index) {
|
||||
Node* temp = front;
|
||||
|
||||
int count = 0;
|
||||
|
||||
for (int i = 0; temp != NULL && i < index; i++) {
|
||||
temp = temp -> next;
|
||||
}
|
||||
|
||||
if (temp == front) {
|
||||
temp -> next = front;
|
||||
front -> prev = NULL;
|
||||
} else if (temp == back) {
|
||||
temp -> prev = back;
|
||||
back -> next = NULL;
|
||||
} else {
|
||||
Node* a = temp -> prev;
|
||||
Node* b = temp -> next;
|
||||
b -> prev = a;
|
||||
a -> next = b;
|
||||
}
|
||||
|
||||
free(temp);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void DoublyLinkedList::DisplayFB() {
|
||||
Node* temp = front;
|
||||
|
||||
while (temp != NULL) {
|
||||
cout << temp -> data << " -> ";
|
||||
temp = temp -> next;
|
||||
}
|
||||
cout << "NULL" << endl;
|
||||
}
|
||||
|
||||
void DoublyLinkedList::DisplayBF() {
|
||||
Node* temp = back;
|
||||
|
||||
while (temp != NULL) {
|
||||
cout << temp -> data << " -> ";
|
||||
temp = temp -> prev;
|
||||
}
|
||||
cout << "NULL" << endl;
|
||||
}
|
||||
|
||||
void DoublyLinkedList::Mutate(int index, double value) {
|
||||
Node* temp = front;
|
||||
|
||||
for (int i = 0; temp != NULL && i < index; i++) {
|
||||
temp = temp->next;
|
||||
}
|
||||
temp -> data = value;
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
//
|
||||
// doublylinkedlist.hpp
|
||||
// DoublyLinkedList
|
||||
//
|
||||
// Created by Austin Bennett on 7/18/22.
|
||||
//
|
||||
/*
|
||||
Constructor
|
||||
Destructor
|
||||
Insert function
|
||||
Remove function
|
||||
Accessor function
|
||||
Mutator function
|
||||
*/
|
||||
|
||||
#ifndef doublylinkedlist_hpp
|
||||
#define doublylinkedlist_hpp
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "node.hpp"
|
||||
|
||||
class DoublyLinkedList {
|
||||
private:
|
||||
Node* front;
|
||||
Node* back;
|
||||
public:
|
||||
DoublyLinkedList();
|
||||
//~DoublyLinkedList();
|
||||
void Insert(double data);
|
||||
void Remove(int index);
|
||||
void DisplayFB();
|
||||
void DisplayBF();
|
||||
void Mutate(int index, double value);
|
||||
};
|
||||
|
||||
#endif /* doublylinkedlist_hpp */
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Header file for ReadValue
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <climits>
|
||||
#include <string>
|
||||
|
||||
template<typename inputType>
|
||||
inputType ReadValue(std::string prompt)
|
||||
{
|
||||
inputType returnValue=0;
|
||||
std::cout << prompt;
|
||||
std::cin >> returnValue;
|
||||
while (std::cin.fail()) {
|
||||
std::cout << "Error! Cannot read input.\n";
|
||||
std::cin.clear();
|
||||
std::cin.ignore(INT_MAX,'\n');
|
||||
std::cout << prompt;
|
||||
std::cin >> returnValue;
|
||||
}
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
|
||||
template<typename inputType>
|
||||
inputType ReadValue(std::string prompt, inputType minValue)
|
||||
{
|
||||
inputType returnValue=0;
|
||||
returnValue=ReadValue<inputType>(prompt);
|
||||
while (returnValue < minValue) {
|
||||
std::cout << "Error! Value must be >= " << minValue << std::endl;
|
||||
returnValue=ReadValue<inputType>(prompt);
|
||||
}
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
|
||||
template<typename inputType>
|
||||
inputType ReadValue(std::string prompt, inputType minValue, inputType maxValue)
|
||||
{
|
||||
inputType returnValue=0;
|
||||
returnValue=ReadValue<inputType>(prompt,minValue);
|
||||
while (returnValue > maxValue) {
|
||||
std::cout << "Error! Value must be <= " << maxValue << std::endl;
|
||||
returnValue=ReadValue<inputType>(prompt,minValue);
|
||||
}
|
||||
return returnValue;
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "doublylinkedlist.hpp"
|
||||
#include "input.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
DoublyLinkedList dll = DoublyLinkedList();
|
||||
|
||||
cout << "1. Add Value" << endl;
|
||||
cout << "2. Delete Value" << endl;
|
||||
cout << "3. Display List (forward)" << endl;
|
||||
cout << "4. Display List (backward)" << endl;
|
||||
cout << "5. Quit" << endl;
|
||||
|
||||
int choice = ReadValue<int>("Enter Your Choice: ");
|
||||
|
||||
do {
|
||||
if (choice == 1) {
|
||||
dll.Insert(ReadValue<double>("Enter Your Data: "));
|
||||
} else if (choice == 2) {
|
||||
dll.Remove(ReadValue<int>("Enter The Index: "));
|
||||
} else if (choice == 3) {
|
||||
dll.DisplayFB();
|
||||
} else if (choice == 4) {
|
||||
dll.DisplayBF();
|
||||
} else if (choice == 5) {
|
||||
break;
|
||||
} else {
|
||||
cout << "Try Again" << endl;
|
||||
}
|
||||
// cout << "1. Add Value" << endl;
|
||||
// cout << "2. Delete Value" << endl;
|
||||
// cout << "3. Display List (forward)" << endl;
|
||||
// cout << "4. Display List (backward)" << endl;
|
||||
// cout << "5. Quit" << endl;
|
||||
|
||||
choice = ReadValue<int>("Enter Your Choice: ");
|
||||
|
||||
} while (choice != 5);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
//
|
||||
// node.cpp
|
||||
// DoublyLinkedList
|
||||
//
|
||||
// Created by Austin Bennett on 7/18/22.
|
||||
//
|
||||
|
||||
#include "node.hpp"
|
||||
|
||||
Node::Node(double value) {
|
||||
data = value;
|
||||
}
|
||||
|
||||
Node::~Node() {
|
||||
delete this;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
//
|
||||
// node.hpp
|
||||
// DoublyLinkedList
|
||||
//
|
||||
// Created by Austin Bennett on 7/18/22.
|
||||
//
|
||||
|
||||
#ifndef node_hpp
|
||||
#define node_hpp
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
struct Node {
|
||||
public:
|
||||
double data;
|
||||
struct Node *prev;
|
||||
struct Node *next;
|
||||
Node(double value);
|
||||
~Node();
|
||||
};
|
||||
|
||||
#endif /* node_hpp */
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 507 KiB |
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,98 @@
|
||||
//
|
||||
// doublylinkedlist.cpp
|
||||
// DoublyLinkedList
|
||||
//
|
||||
// Created by Austin Bennett on 7/18/22.
|
||||
//
|
||||
/*
|
||||
Constructor
|
||||
Destructor
|
||||
Insert function
|
||||
Remove function
|
||||
Accessor function
|
||||
Mutator function
|
||||
*/
|
||||
#include <iostream>
|
||||
|
||||
#include "doublylinkedlist.hpp"
|
||||
|
||||
using namespace::std;
|
||||
|
||||
DoublyLinkedList::DoublyLinkedList() {
|
||||
front = NULL;
|
||||
back = NULL;
|
||||
}
|
||||
|
||||
void DoublyLinkedList::Insert(double data) {
|
||||
Node* newNode = new Node(data);
|
||||
|
||||
bool isEmpty = (front == NULL);
|
||||
|
||||
if (isEmpty) {
|
||||
front = newNode;
|
||||
back = newNode;
|
||||
newNode -> prev = NULL;
|
||||
newNode -> next = NULL;
|
||||
} else {
|
||||
back -> next = newNode;
|
||||
newNode -> prev = back;
|
||||
newNode -> next = NULL;
|
||||
back = newNode;
|
||||
}
|
||||
}
|
||||
|
||||
void DoublyLinkedList::Remove(int index) {
|
||||
Node* temp = front;
|
||||
|
||||
int count = 0;
|
||||
|
||||
for (int i = 0; temp != NULL && i < index; i++) {
|
||||
temp = temp -> next;
|
||||
}
|
||||
|
||||
if (temp == front) {
|
||||
temp -> next = front;
|
||||
front -> prev = NULL;
|
||||
} else if (temp == back) {
|
||||
temp -> prev = back;
|
||||
back -> next = NULL;
|
||||
} else {
|
||||
Node* a = temp -> prev;
|
||||
Node* b = temp -> next;
|
||||
b -> prev = a;
|
||||
a -> next = b;
|
||||
}
|
||||
|
||||
free(temp);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void DoublyLinkedList::DisplayFB() {
|
||||
Node* temp = front;
|
||||
|
||||
while (temp != NULL) {
|
||||
cout << temp -> data << " -> ";
|
||||
temp = temp -> next;
|
||||
}
|
||||
cout << "NULL" << endl;
|
||||
}
|
||||
|
||||
void DoublyLinkedList::DisplayBF() {
|
||||
Node* temp = back;
|
||||
|
||||
while (temp != NULL) {
|
||||
cout << temp -> data << " -> ";
|
||||
temp = temp -> prev;
|
||||
}
|
||||
cout << "NULL" << endl;
|
||||
}
|
||||
|
||||
void DoublyLinkedList::Mutate(int index, double value) {
|
||||
Node* temp = front;
|
||||
|
||||
for (int i = 0; temp != NULL && i < index; i++) {
|
||||
temp = temp->next;
|
||||
}
|
||||
temp -> data = value;
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
//
|
||||
// doublylinkedlist.hpp
|
||||
// DoublyLinkedList
|
||||
//
|
||||
// Created by Austin Bennett on 7/18/22.
|
||||
//
|
||||
/*
|
||||
Constructor
|
||||
Destructor
|
||||
Insert function
|
||||
Remove function
|
||||
Accessor function
|
||||
Mutator function
|
||||
*/
|
||||
|
||||
#ifndef doublylinkedlist_hpp
|
||||
#define doublylinkedlist_hpp
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "node.hpp"
|
||||
|
||||
class DoublyLinkedList {
|
||||
private:
|
||||
Node* front;
|
||||
Node* back;
|
||||
public:
|
||||
DoublyLinkedList();
|
||||
//~DoublyLinkedList();
|
||||
void Insert(double data);
|
||||
void Remove(int index);
|
||||
void DisplayFB();
|
||||
void DisplayBF();
|
||||
void Mutate(int index, double value);
|
||||
};
|
||||
|
||||
#endif /* doublylinkedlist_hpp */
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Header file for ReadValue
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <climits>
|
||||
#include <string>
|
||||
|
||||
template<typename inputType>
|
||||
inputType ReadValue(std::string prompt)
|
||||
{
|
||||
inputType returnValue=0;
|
||||
std::cout << prompt;
|
||||
std::cin >> returnValue;
|
||||
while (std::cin.fail()) {
|
||||
std::cout << "Error! Cannot read input.\n";
|
||||
std::cin.clear();
|
||||
std::cin.ignore(INT_MAX,'\n');
|
||||
std::cout << prompt;
|
||||
std::cin >> returnValue;
|
||||
}
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
|
||||
template<typename inputType>
|
||||
inputType ReadValue(std::string prompt, inputType minValue)
|
||||
{
|
||||
inputType returnValue=0;
|
||||
returnValue=ReadValue<inputType>(prompt);
|
||||
while (returnValue < minValue) {
|
||||
std::cout << "Error! Value must be >= " << minValue << std::endl;
|
||||
returnValue=ReadValue<inputType>(prompt);
|
||||
}
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
|
||||
template<typename inputType>
|
||||
inputType ReadValue(std::string prompt, inputType minValue, inputType maxValue)
|
||||
{
|
||||
inputType returnValue=0;
|
||||
returnValue=ReadValue<inputType>(prompt,minValue);
|
||||
while (returnValue > maxValue) {
|
||||
std::cout << "Error! Value must be <= " << maxValue << std::endl;
|
||||
returnValue=ReadValue<inputType>(prompt,minValue);
|
||||
}
|
||||
return returnValue;
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "doublylinkedlist.hpp"
|
||||
#include "input.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
DoublyLinkedList dll = DoublyLinkedList();
|
||||
|
||||
cout << "1. Add Value" << endl;
|
||||
cout << "2. Delete Value" << endl;
|
||||
cout << "3. Display List (forward)" << endl;
|
||||
cout << "4. Display List (backward)" << endl;
|
||||
cout << "5. Quit" << endl;
|
||||
|
||||
int choice = ReadValue<int>("Enter Your Choice: ");
|
||||
|
||||
do {
|
||||
if (choice == 1) {
|
||||
dll.Insert(ReadValue<double>("Enter Your Data: "));
|
||||
} else if (choice == 2) {
|
||||
dll.Remove(ReadValue<int>("Enter The Index: "));
|
||||
} else if (choice == 3) {
|
||||
dll.DisplayFB();
|
||||
} else if (choice == 4) {
|
||||
dll.DisplayBF();
|
||||
} else if (choice == 5) {
|
||||
break;
|
||||
} else {
|
||||
cout << "Try Again" << endl;
|
||||
}
|
||||
// cout << "1. Add Value" << endl;
|
||||
// cout << "2. Delete Value" << endl;
|
||||
// cout << "3. Display List (forward)" << endl;
|
||||
// cout << "4. Display List (backward)" << endl;
|
||||
// cout << "5. Quit" << endl;
|
||||
|
||||
choice = ReadValue<int>("Enter Your Choice: ");
|
||||
|
||||
} while (choice != 5);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
//
|
||||
// node.cpp
|
||||
// DoublyLinkedList
|
||||
//
|
||||
// Created by Austin Bennett on 7/18/22.
|
||||
//
|
||||
|
||||
#include "node.hpp"
|
||||
|
||||
Node::Node(double value) {
|
||||
data = value;
|
||||
}
|
||||
|
||||
Node::~Node() {
|
||||
delete this;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
//
|
||||
// node.hpp
|
||||
// DoublyLinkedList
|
||||
//
|
||||
// Created by Austin Bennett on 7/18/22.
|
||||
//
|
||||
|
||||
#ifndef node_hpp
|
||||
#define node_hpp
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
struct Node {
|
||||
public:
|
||||
double data;
|
||||
struct Node *prev;
|
||||
struct Node *next;
|
||||
Node(double value);
|
||||
~Node();
|
||||
};
|
||||
|
||||
#endif /* node_hpp */
|
||||
Reference in New Issue
Block a user