#include <vector>
#include <iostream>
#include <fstream>
using namespace std;
class Node
{
public:
Node *prev;
Node *next;
int key;
Node(int k, Node *p, Node *n)
{
prev = p;
next = n;
key = k;
}
void print() // print the contents of this node
{
//cout << "Node: key = " << key << ", prev = " << ((prev) ? prev->key : 0) << ", next = " << ((next) ? next->key : 0) << '\n';
printf("Node: key = %d, prev = %d, next = %d\n", key, ((prev) ? prev->key : 0),((next) ? next->key : 0));
}
void printAll() // when printing all nodes in array form, print this node's
{ // key, and then call this function for the next node
//cout << "node printall" << '\n';
if (next)
{
//cout << key << ", ";
printf("%d, ",key);
next->printAll();
}
else
//cout << key;
printf("%d",key);
}
};
class DLL
{
public:
Node *head;
Node *tail;
int size;
DLL()
{
size = 0;
}
void updateTail()
{
Node *temp = head;
while (temp->next)
{
temp = temp->next;
}
tail = temp;
}
void insert(int k) // insert node with key k at head
{
Node *temp = head;
while (temp) // check for duplicate key
{
if (temp->key == k)
{
//cout << "rejecting - duplicate key " << k << '\n';
printf("rejecting - duplicate key %d\n", k);
return;
}
temp = temp->next;
}
Node *n = new Node(k, nullptr, nullptr);
n->next = head;
if (head)
head->prev = n;
head = n;
updateTail();
size++;
}
void insert(int k, int i) // insert node with key k after node with key i
{
Node *temp = head;
while (temp) // check for duplicate key
{
if (temp->key == k)
{
//cout << "rejecting - duplicate key " << k << '\n';
printf("rejecting - duplicate key %d\n", k);
return;
}
temp = temp->next;
}
Node *n = new Node(k, nullptr, nullptr);
temp = head;
while (temp)
{
if (temp->key == i)
{
n->next = temp->next;
n->prev = temp;
if (temp->next)
temp->next->prev = n;
temp->next = n;
size++;
updateTail();
return;
}
temp = temp->next;
}
//cout << "rejecting - key " << i << " not found" << '\n'; // reject invalid key
printf("rejecting - key %d not found\n", i);
}
void remove(int k) // remove node which contains key k
{
Node *temp = head;
while (temp)
{
if (temp->key == k)
{
temp->prev->next = temp->next;
if (temp->next)
temp->next->prev = temp->prev;
size--;
updateTail();
return;
}
temp = temp->next;
}
//cout << "cannot delete - key " << k << " not found" << '\n';
printf("cannot delete - key %d not found\n", k);
}
bool search(int k) // determine if DLL contains key k
{
Node *temp = head;
while (temp)
{
if (temp->key == k)
return true;
temp = temp->next;
}
return false;
}
void print() // print all nodes' keys in array format
{
//cout << "[ ";
printf("[ ");
if (head) head->printAll();
//cout << " ]" << '\n';
printf(" ]\n");
}
void print(int i) // print node at index i
{
Node *temp = head;
for (int j = 0; j < i; j++)
{
temp = temp->next;
}
temp->print();
}
void printAll() // print the contents of all nodes in DLL
{
for (int i = 0; i < size; i++)
{
print(i);
}
}
};
/* SPLIT STRING FUNCTION */
void split(const string &s, char c, vector<string> &v)
{
string::size_type i = 0;
string::size_type j = s.find(c);
while (j != string::npos)
{
v.push_back(s.substr(i, j - i));
i = ++j;
j = s.find(c, j);
if (j == string::npos)
v.push_back(s.substr(i, s.length()));
}
}
int main(int argc, const char **argv)
{
DLL *list = new DLL();
/* GET FILE NAME */
//cout << "Enter input file name:" << '\n';
printf("Enter input file name:\n");
string fileName;
getline(cin, fileName);
//cout << '\n';
/* READ FIRST LINE OF FILE */
string input;
ifstream inFile(fileName.c_str());
if (inFile.is_open())
{
getline(inFile, input);
inFile.close();
}
else
{
//cout << "unable to open input file " << fileName.c_str() << '\n';
//cout << "Default ";
printf("unable to open input file %s\nDefault ", fileName.c_str());
/* DEFAULT LIST OF COMMANDS */
input = string("1.in 3.in 5.in 3.del 7.in 9.in 11.in 12.in 2.in 1.sch 15.in_7");
}
//cout << "Input: " << input.c_str() << '\n';
printf("Input: %s\n", input.c_str());
list->print();
/* PARSE AND EXECUTE COMMANDS */
vector<string> commands;
split(input, ' ', commands);
int key;
for (int i = 0; i < commands.size(); i++)
{
string command = commands[i];
vector<string> pieces;
split(command, '.', pieces);
key = stoi(pieces[0]);
if (pieces[1].length() > 3)
{
vector<string> todos;
split(pieces[1].c_str(), '_', todos);
int location = stoi(todos[1]);
list->insert(key, location);
}
else
{
if (pieces[1] == "in")
{
list->insert(key);
}
else if (pieces[1] == "del")
{
list->remove(key);
}
else if (pieces[1] == "sch")
{
//cout << key << " " << ((list->search(key)) ? "found" : "not found") << '\n';
printf("%d %s\n", key, ((list->search(key)) ? "found" : "not found"));
}
}
/* PRINT LIST CONTENTS AFTER EACH COMMAND */
list->print();
}
//cout << '\n';
//cout << "Detailed final list contents:" << '\n';
//cout << '\n';
printf("\nDetailed final list contents:\n\n");
list->printAll();
return 0;
}