Skip to content

Instantly share code, notes, and snippets.

@perryraskin
Created September 3, 2017 19:20
Show Gist options
  • Save perryraskin/698a146aeb61d214949970914a76c17e to your computer and use it in GitHub Desktop.
Save perryraskin/698a146aeb61d214949970914a76c17e to your computer and use it in GitHub Desktop.
main p1
/*
*
* CS 323 Project 1 (C++)
*
* Linked List Class: Insertion Sort
*
* Perry Raskin
* Computer Science, CUNY Queens College
*/
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
// listNode class
class linkedList {
private:
struct listNode {
string data;
listNode* next;
};
listNode* listHead;
listNode* curr;
listNode* temp;
int numElements;
public:
linkedList();
void listInsert(string data);
void printList(ofstream fout);
bool isEmpty();
};
// List Object
linkedList::linkedList() {
listHead = NULL;
curr = NULL;
temp = NULL;
numElements = 0;
}
// INSERT
void linkedList::listInsert(string data) {
listNode* n;
n->next = NULL;
n->data = data;
if (listHead != NULL) {
curr = listHead;
while (curr->next != NULL) {
curr = curr->next;
}
curr->next = n;
}
else {
listHead = n;
}
}
// isEmpty
bool linkedList::isEmpty() {
return (numElements == 0);
}
// PRINT
void linkedList::printList(ofstream fout) {
curr = listHead;
while (curr != NULL) {
fout << curr->data << endl;
curr = curr->next;
}
}
/*
*
* CS 323 Project 1 (C++)
*
* Main
*
* Perry Raskin
* Computer Science, CUNY Queens College
*/
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <array>
#include "LinkedList.h"
using namespace std;
int main(int argc, char *argv[]) {
// Initialize Objects
linkedList list;
string data[100], line, ln, word, t;
char letter;
ifstream input (argv[1]);
ofstream output (argv[2]);
ifstream in (argv[1]);
int i = 0;
// Store words in array while outputting, skipping blank lines
while(in.good()) {
getline (in, ln, ' ');
if (ln != "") {
data[i] = ln;
//cout << data[i] << endl;
i++;
}
}
// Output whole array with spaces between each word
output<<"Input file data:\n";
for (int k = 0; k <= i; k++) {
output << data[k] << " ";
}
if (!input) {
cout << "Error opening file.\n";
return 0;
}
while (input.get(letter)) {
if (letter == ' ' || letter == '\n') {
// if the char is a space or new line, add the word to the list
cout << word << endl;
list.listInsert(word);
word = t;
}
else {
// Otherwise, create the next word in the file
word += letter;
}
}
//listNode *dummy;
list.printList(output);
//delete dummy;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment