Untitled
unknown
plain_text
4 years ago
11 kB
10
Indexable
#include <iostream>
#include <fstream>
#include <string>
#include "SortedLinkedList.h"
using namespace std;
int main(int argc, char** argv) {
SortedLinkedList list;
int value;
int length;
ifstream inputFile(argv[1], ios::in);
if (!inputFile) {
cout << "File cannot be opened...Terminating" << endl;
exit(1);
}
// reads the numbers from the input file
while (inputFile >> value) {
ItemType newItem;
newItem.initialize(value);
list.insertItem(newItem);
}
length = list.length();
string userInput;
/* */
SortedLinkedList newList;
int length2;
int listValues;
// Asking length of list
cout << "Length of list to merge: ";
cin >> length2;
while (cin.fail()) {
cout << "Please enter a numerical value: ";
cin.clear();
cin.ignore();
cin >> length2;
}
// Asking for elements
bool amount = false;
int counter = 0;
cout << "List elements separated by spaces in order: ";
// for loop
/*
while (cin >> listValues && amount == false) {
counter++;
ItemType a;
a.initialize(listValues);
newList.insertItem(a);
if (counter == length2) {
amount = true;
break;
}
}
*/
// Reading input values for newList
for (int i = 0; i < length2; i++) {
cin >> listValues;
ItemType a;
a.initialize(listValues);
newList.insertItem(a);
}
// original list
cout << "List 1: ";
list.resetList();
for (int i = 0; i < list.length(); i++) {
ItemType a = list.getNextItem();
cout << a.getValue() << " ";
}
cout << endl;
list.resetList();
// user defined list
cout << "List 2: ";
newList.resetList();
for (int i = 0; i < newList.length(); i++) {
ItemType a = newList.getNextItem();
cout << a.getValue() << " ";
}
cout << endl;
newList.resetList();
list.commonList(newList);
cout << "Made it to end" << endl;
/*
bool start = true;
cout << "Commands:" << endl;
cout << "(i) - Insert Value" << endl
<< "(d) - Delete Value" << endl
<< "(s) - Search Value" << endl
<< "(n) - Print next iterator value" << endl
<< "(r) - Reset Iterator" << endl
<< "(a) - Delete alternate nodes" << endl
<< "(m) - Merge two lists" << endl
<< "(t) - Intersection" << endl
<< "(p) - Print list" << endl
<< "(l) - Print length" << endl
<< "(q) - Quit Program" << endl;
while (start) {
cout << "Enter a Command: ";
cin >> userInput;
if (userInput.compare("i") == 0) { // Insert Value
list.resetList();
// print out values
int number;
// print out list
for (int i = 0; i < list.length(); i++) {
ItemType a = list.getNextItem();
cout << a.getValue() << " ";
}
// ask for number
cout << endl;
cout << "Enter a number: ";
cin >> number;
while (cin.fail()) {
cout << "Please enter a numerical value: ";
cin.clear();
cin.ignore();
cin >> number;
}
// insert item
ItemType insertItem;
insertItem.initialize(number);
list.insertItem(insertItem);
list.resetList();
// print out list again
length = list.length();
for (int i = 0; i < list.length(); i++) {
ItemType a = list.getNextItem();
cout << a.getValue() << " ";
}
// reset list for next command
list.resetList();
cout << endl;
} else if (userInput.compare("d") == 0) { // Delete Value
int number;
list.resetList();
// print out list
for (int i = 0; i < list.length(); i++) {
ItemType a = list.getNextItem();
cout << a.getValue() << " ";
}
// ask for number to delete
cout << endl;
cout << "Enter a number: ";
cin >> number;
while (cin.fail()) {
cout << "Please enter a numerical value: ";
cin.clear();
cin.ignore();
cin >> number;
}
int counter = 0;
ItemType b;
b.initialize(number);
list.resetList();
for (int i = 0; i < list.length(); i++) {
ItemType a = list.getNextItem();
if (a.compareTo(b) == EQUAL) {
counter++;
}
}
if (counter != 0) {
// delete item
ItemType deletedItem;
deletedItem.initialize(number);
list.deleteItem(deletedItem);
list.resetList();
} else {
cout << "Item not found!" << endl;
}
// print out new list
list.resetList();
length = list.length();
for (int i = 0; i < list.length(); i++) {
ItemType a = list.getNextItem();
cout << a.getValue() << " ";
}
// reset list for next command
list.resetList();
cout << endl;
} else if (userInput.compare("s") == 0) { // Search Value
int number;
int index;
list.resetList();
// print out list
for (int i = 0; i < list.length(); i++) {
ItemType a = list.getNextItem();
cout << a.getValue() << " ";
}
cout << endl;
cout << "Enter a value to search: ";
cin >> number;
while (cin.fail()) {
cout << "Please enter a numerical value: ";
cin.clear();
cin.ignore();
cin >> number;
}
ItemType a;
a.initialize(number);
index = list.searchItem(a);
if (index >= 0) {
cout << "Index: " << index << endl;
} else {
cout << "Item is not found." << endl;
}
list.resetList();
} else if (userInput.compare("n") == 0) { // Print next iterator value
ItemType a;
a = list.getNextItem();
cout << a.getValue() << endl;
} else if (userInput.compare("r") == 0) { // Reset Iterator
list.resetList();
} else if (userInput.compare("a") == 0) { // Delete alternate nodes
// alternate deleting when list has one node doesnt do anything
cout << "List before alternate delete: ";
list.resetList();
for (int i = 0; i < list.length(); i++) {
ItemType a;
a = list.getNextItem();
cout << a.getValue() << " ";
}
list.resetList();
cout << endl;
list.deleteAlternateNodes();
cout << "List after alternate delete: ";
list.resetList();
for (int i = 0; i < list.length(); i++) {
ItemType a;
a = list.getNextItem();
cout << a.getValue() << " ";
}
list.resetList();
cout << endl;
} else if (userInput.compare("m") == 0) { // Merge two lists
// merging a second time doesn't work
SortedLinkedList newList;
int length2;
int listValues;
// Asking length of list
cout << "Length of list to merge: ";
cin >> length2;
while (cin.fail()) {
cout << "Please enter a numerical value: ";
cin.clear();
cin.ignore();
cin >> length2;
}
// Asking for elements
bool amount = false;
int counter = 0;
cout << "List elements separated by spaces in order: ";
while (cin >> listValues && amount == false) {
counter++;
ItemType a;
a.initialize(listValues);
newList.insertItem(a);
if (counter == length2) {
amount = true;
break;
}
}
// merge list
list.mergeList(newList);
newList.resetList();
list.resetList();
// print list 1
for (int i = 0; i < list.length(); i++) {
ItemType a = list.getNextItem();
cout << a.getValue() << " ";
}
list.resetList();
newList.resetList();
cout << endl;
} else if (userInput.compare("t") == 0) { // Intersection
SortedLinkedList newList;
int length2;
int listValues;
// Asking length of list
cout << "Length of list to merge: ";
cin >> length2;
while (cin.fail()) {
cout << "Please enter a numerical value: ";
cin.clear();
cin.ignore();
cin >> length2;
}
// Asking for elements
bool amount = false;
int counter = 0;
cout << "List elements separated by spaces in order: ";
while (cin >> listValues && amount == false) {
counter++;
ItemType a;
a.initialize(listValues);
newList.insertItem(a);
if (counter == length2) {
amount = true;
break;
}
}
cout << "List 1: ";
list.resetList();
for (int i = 0; i < list.length(); i++) {
ItemType a = list.getNextItem();
cout << a.getValue() << " ";
}
cout << endl;
list.resetList();
cout << "List 2: ";
newList.resetList();
for (int i = 0; i < newList.length(); i++) {
ItemType a = newList.getNextItem();
cout << a.getValue() << " ";
}
cout << endl;
newList.resetList();
list.commonList(newList);
} else if (userInput.compare("p") == 0) { // Print List
list.resetList();
for (int i = 0; i < list.length(); i++) {
ItemType a;
a = list.getNextItem();
cout << a.getValue() << " ";
}
list.resetList();
cout << endl;
} else if (userInput.compare("l") == 0) { // Print length
cout << "List length is: " << list.length() << endl;
} else if (userInput.compare("q") == 0) { // Quit Program
cout << "Exiting program..." << endl;
exit(1);
} else { // Anything else
cout << "Invalid Command. Please try again!" << endl;
}
}
*/
}
Editor is loading...