Skip to content

Instantly share code, notes, and snippets.

Created September 23, 2014 23:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/09b480545fe8b6b329a1 to your computer and use it in GitHub Desktop.
Save anonymous/09b480545fe8b6b329a1 to your computer and use it in GitHub Desktop.
Read input.txt
Delete Microsoft
Print backward
Print forward
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct wordNode{
string word;
wordNode *nextLink;
wordNode *prevLink;
};
void performPrint(wordNode *w, string s2);
void performDelete(wordNode *f, wordNode *l, string s1);
void performInsert(wordNode *f, wordNode *l, string s1, string s2);
void main(){//int arc, char *argv[]){
ifstream comFile;
string command;
string pass1;
string pass2;
wordNode *first, *last, *newNode;
string fromFile;
bool firstComm = true;
comFile.open("commands.txt");//argv[1]);
if(comFile.fail())
exit(1);
while(!comFile.eof()){
comFile>>command;
comFile>>pass1;
if(command == "Read" && firstComm){
ifstream mainFile;
mainFile.open(pass1);
if (mainFile.fail()){
cout << "Failed to open text file.\n";
exit(1);
}
first = NULL;
last = NULL;
while(!mainFile.eof()){
mainFile>>fromFile;
newNode = new wordNode;
newNode->word = fromFile; //new word from file is assigned as the word of the new node
newNode->nextLink = NULL; //assigns NULL to the new nodes link
newNode->prevLink = NULL;
if (first == NULL){ //if list is empty, the new node is the first && the last entry
first = newNode;
first->prevLink = NULL;
last = newNode;
}
else{ //if list is not empty, assign the new node as the last entry and the last->link as the link of the new node aka NULL
newNode->prevLink = last;
last->nextLink = newNode;
last = newNode;
}
}
firstComm = false;
}
else{
if(command =="Print"){
if(pass1 == "backward")
performPrint(last, pass1);
else
performPrint(first, pass1);
}
else if(command =="Delete"){
performDelete(first, last, pass1);
}
else if(command =="Insert"){
comFile>>pass2;
performInsert(first, last, pass1, pass2);
}
}
}
comFile.close();
system("pause");
}
void performPrint(wordNode *position, string pass1){
if(pass1 == "forward"){
wordNode *current;
current = position;
while(current != NULL){
cout << current->word << endl;
current = current->nextLink;
}
}
else if(pass1 == "backward"){
wordNode *current;
current = position;
while(current != NULL){
cout << current->word << endl;
current = current->prevLink;
}
}
}
//end of Print command
void performDelete(wordNode *head, wordNode *tail, string pass1){ //deletes a word as well as the occupied memory
bool found = false;
wordNode *current;
current = new wordNode;
if(head == NULL && tail == NULL) //checks for empty list
cout<<"This list is empty. Nothing to delete.\n";
else if(head->word == pass1){ //checks to see if the first word is the target word
current = head;
head = head->nextLink;
head->prevLink = NULL;
current->nextLink = NULL;
delete current;
}
/*else if(head != NULL){ //if head is not empty, change the prevLink to NULL
head->prevLink = NULL;
current->nextLink = NULL;
current->prevLink = NULL;
delete current;
}
else if(current != NULL && !found){
}*/
} //end of Delete command
void performInsert(wordNode *head, wordNode *tail, string pass1, string pass2){ //insert pass1 after pass2
/* wordNode *current, *newNode;
newNode = new wordNode;
newNode->word = pass1;
newNode->nextLink = NULL;
newNode->prevLink = NULL;
bool found = false;
current = head;
while(current != NULL && !found){
if(current->word == pass2){
found = true;
}
else{
current = current->nextLink;
}
}
if(current == head){
newNode->nextLink = head->nextLink;
head = newNode;
}
else{
if(current != NULL){
newNode->prevLink = current;
newNode->nextLink = current->nextLink;
current->nextLink->prevLink = newNode;
current->nextLink = newNode;
}
}
*/}
Microsoft has many many products,
such as Windows
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment