Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions LinkedList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)";
Expand All @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions LinkedList.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
22 changes: 15 additions & 7 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
//***********************************************************************************

#include "LinkedList.h"
#include <cstring>
#include <iostream>
using namespace std;

int main()
Expand All @@ -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";
Expand All @@ -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);
Expand All @@ -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"<<endl;
return 0;
}