Last active
August 29, 2015 14:14
-
-
Save martynchamberlin/ff0320cd2e2cd5228375 to your computer and use it in GitHub Desktop.
LinkedList example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// LinkedList.c | |
// linked_lists | |
// | |
// Created by Martyn Chamberlin on 2/5/15. | |
// Copyright (c) 2015 Martyn Chamberlin. All rights reserved. | |
// | |
#include "LinkedList.h" | |
#include <iostream> | |
#include <fstream> | |
using namespace std; | |
// Have to initialize static object properties | |
LinkedList * LinkedList::head = new LinkedList(); | |
LinkedList::LinkedList() { | |
} | |
LinkedList::LinkedList( int num ) { | |
this->num = num; | |
} | |
bool LinkedList::number_already_exists( int num ) { | |
LinkedList * link = head; | |
while ( link != 0 ) { | |
if ( link->num == num ) { | |
ofstream myfile; | |
myfile.open ("/users/martynchamberlin/desktop/duplicates.txt", fstream::app|fstream::out); | |
myfile << num << endl; | |
myfile.close(); | |
return true; | |
} | |
link = link->next; | |
} | |
return false; | |
} | |
void LinkedList::put_new_item_in_proper_order(int num ) { | |
if ( number_already_exists( num ) ) return; | |
LinkedList * current = LinkedList::head; | |
LinkedList * newItem = new LinkedList( num ); | |
// First, check if we need to insert at the beginning | |
if ( LinkedList::head->num > num ) { | |
// Insert newItem at the front | |
newItem->next = LinkedList::head; | |
LinkedList::head = newItem; | |
return; | |
} | |
// Check if we need to insert it in the middle | |
while( current->next != 0 ) { | |
// This condition is only satsified if the linked list contains more than | |
// one item and the num contains a value that's not at either extreme | |
if ( current->next->num > num ) { | |
// Insert between current and next | |
newItem->next = current->next; | |
current->next = newItem; | |
return; | |
} | |
current = current->next; | |
} | |
// If we haven't inserted it yet, that means we need to insert newItem at the end | |
newItem->next = 0; | |
current->next = newItem; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// test.h | |
// linked_lists | |
// | |
// Created by Martyn Chamberlin on 2/5/15. | |
// Copyright (c) 2015 Martyn Chamberlin. All rights reserved. | |
// | |
#ifndef linked_lists_test_h | |
#define linked_lists_test_h | |
class LinkedList { | |
public: | |
LinkedList(); | |
LinkedList( int num ); | |
bool static number_already_exists(int num); | |
void static put_new_item_in_proper_order(int num ); | |
void static print_duplicates(); | |
LinkedList static * head; | |
LinkedList * next; | |
int num; | |
}; | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.cpp | |
// linked_lists | |
// | |
// Created by Martyn Chamberlin on 2/5/15. | |
// Copyright (c) 2015 Martyn Chamberlin. All rights reserved. | |
// | |
#include <iostream> | |
#include "LinkedList.h" | |
using namespace std; | |
int main(int argc, const char * argv[]) { | |
const int length = 20; | |
int randomArray[length]; | |
// This gives us more random numbers. Without this we are getting the | |
// same set of random numbers every time | |
srand(time(NULL)); | |
// Initially generate 20 random numbers and store them in a generic array | |
cout << "These are the random numbers as they appeared in the array" << endl; | |
for ( int i = 0; i < length; i++ ) { | |
randomArray[i] = rand() % 58 + 1; | |
cout << randomArray[ i ] << endl; | |
} | |
cout << endl; | |
// Copy the 20 random numbers and put them into the linked list. Don't store | |
// any duplicates and put them in their proper sequence | |
LinkedList::head->num = randomArray[ 0 ]; | |
LinkedList::head->next = 0; | |
for ( int i = 1; i < length; i++ ) { | |
LinkedList::put_new_item_in_proper_order(randomArray[i]); | |
} | |
LinkedList * link = LinkedList::head; | |
// Let's print out the linked list | |
cout << endl << "These are the random numbers as they appeared in the linked list" << endl; | |
while ( link != 0 ) { | |
cout << link->num << endl; | |
link = link->next; | |
} | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment