diff --git a/LinkedList.cpp b/LinkedList.cpp index 44375d9..237b741 100644 --- a/LinkedList.cpp +++ b/LinkedList.cpp @@ -11,6 +11,8 @@ // Default Constructor creates the head node. LinkedList::LinkedList() { + + // Initialize the head node cout << "\nEntering Constructor ..." << endl; head = new node; head -> song = "head (contains no song data)"; @@ -32,14 +34,20 @@ bool LinkedList::insertNode( node * newNode, int position ) cout << "Error: the given position is out of range." << endl; return false; } + + + // The first node after the head node. + // head->next is NULL. if (!head -> next) { head -> next = newNode; + newNode->next = NULL; listLength++; - cout << "Success: added '" << newNode -> song << "' to position " << position << ".\n"; + cout << "Success: added'" << newNode -> song << "' to position " << position << ".\n"; cout << "listLength = " << listLength << endl; return true; } + int count = 0; node * p = head; node * q = head; @@ -51,23 +59,26 @@ bool LinkedList::insertNode( node * newNode, int position ) newNode -> next = q; listLength++; cout << "Success: added '" << newNode -> song << "' to position " << position << ".\n"; - cout << "listLength = " << listLength << endl; + cout << "listLength = = " << listLength << endl; return true; } p = q; q = p -> next; count++; } + + + // Insert at the last place of the list (the tail node.) if (count == position) { p -> next = newNode; - newNode -> next = q; + newNode -> next = NULL; listLength++; - cout << "Success: added '" << newNode -> song << "' to position " << position << ".\n"; - cout << "listLength = " << listLength << endl; + cout << "Success: added boo'" << newNode -> song << "' to position " << position << ".\n"; + cout << "listLength = = = " << listLength << endl; return true; } - cout << "Error: song node was not added to list." << endl; + // cout << "Error: song node was not added to list." << endl; return false; } @@ -118,8 +129,7 @@ void LinkedList::printList() int count = 0; node * p = head; node * q = head; - cout << "\n---------------------\n"; - cout << " Song Playlist\n"; + while (q) { p = q; diff --git a/LinkedList.h b/LinkedList.h index 75c84b9..b979127 100644 --- a/LinkedList.h +++ b/LinkedList.h @@ -17,17 +17,17 @@ using namespace std; // Node structs contain data and a pointer to the next node. // In this project, it will represent a song/artist combination. //*********************************************************************************** -struct node -{ + +struct node{ string song; string artist; node * next; }; -//*********************************************************************************** -// LinkedList is a list of singly-linked nodes. -// In this project, it will represent a song playlist. -//*********************************************************************************** +// //*********************************************************************************** +// // LinkedList is a list of singly-linked nodes. +// // In this project, it will represent a song playlist. +// //*********************************************************************************** class LinkedList { private: diff --git a/main.cpp b/main.cpp index 5c79120..79428c1 100644 --- a/main.cpp +++ b/main.cpp @@ -7,6 +7,8 @@ //*********************************************************************************** #include "LinkedList.h" +#include +#include using namespace std; int main() @@ -19,7 +21,8 @@ int main() node * B = new node; B -> song = "I Stand Alone"; B -> artist = "Godsmack"; - + + node * C = new node; C -> song = "Heir Apparent"; C -> artist = "Opeth"; @@ -31,11 +34,16 @@ int main() node * E = new node; E -> song = "Blue Monday"; E -> artist = "New Order"; - + node * F = new node; F -> song = "The Moth"; F -> artist = "Aimee Mann"; + + node * G = new node; + G -> song = "G String"; + G -> artist = "Notre Dame"; + // STEP 2: Build a list of three song nodes by appending to end of list. LinkedList myList; myList.insertNode(A, 1); @@ -48,22 +56,22 @@ int main() myList.insertNode(E, 2); myList.printList(); - // STEP 4: Insert node at the front of list. + // // STEP 4: Insert node at the front of list. myList.insertNode(F,1); myList.printList(); - // STEP 5: Remove the last node from the list. + // // STEP 5: Remove the last node from the list. myList.removeNode(6); myList.printList(); - // STEP 6: Remove the first node from the list. + // // STEP 6: Remove the first node from the list. myList.removeNode(1); myList.printList(); - // STEP 7: Remove a node from the middle of the list. + // // STEP 7: Remove a node from the middle of the list. myList.removeNode(3); myList.printList(); - + cout<<"oi"<