1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
|
//*******************************************************************
// LinkedList.h
// LinkedList_Project
//
// Created by Karlina Beringer on June 12, 2014.
// This header file contains the LinkedList class declarations.
//*******************************************************************
#ifndef LinkedList_h
#define LinkedList_h
#include <iostream>
#include <string>
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
{
string song;
string artist;
node * next;
};
//*******************************************************************
// LinkedList is a list of singly-linked nodes.
// In this project, it will represent a song playlist.
//*******************************************************************
class LinkedList
{
private:
// Head of the list contains no song data,
// but points to the song playlist.
node * head;
int listLength;
public:
// Default Constructor creates the head node.
LinkedList();
// Setter adds a node to the list at a given position.
// Takes a node and list position as parameters.
// Position must be between 1 and the number of data nodes.
// Returns true if the operation is successful.
bool insertNode( node * newNode, int position );
// Setter removes a node by its given position.
// Returns true if the operation is successful.
bool removeNode( int position );
// Prints each node in the list in consecutive order,
// starting at the head and ending at the tail.
// Prints list data to the console.
void printList();
// Destructor de-allocates memory used by the list.
~LinkedList();
};
#endif
//*******************************************************************
// LinkedList.cpp
// LinkedList_Project
//
// Created by Karlina Beringer on June 12, 2014.
// This source file contains the LinkedList class definitions.
//*******************************************************************
#include "LinkedList.h"
// Default Constructor creates the head node.
LinkedList::LinkedList()
{
head -> song = "head (contains no song data)";
head -> artist = "head (contains no artist data)";
head -> next = NULL;
listLength = 0;
}
// Setter adds a node to the list at a given position.
// Takes a node and list position as parameters.
// Position must be between 1 and the number of data nodes.
// Returns true if the operation is successful.
bool LinkedList::insertNode( node * newNode, int position )
{
if ((position <= 0) || (position > listLength + 1))
{
cout << "nError: the given position is out of range.n";
return false;
}
if (head -> next == NULL)
{
head -> next = newNode;
listLength++;
return true;
}
int count = 0;
node * p = head;
node * q = head;
while (q)
{
if (count == position)
{
p -> next = newNode;
newNode -> next = q;
listLength++;
return true;
}
p = q;
q = p -> next;
count++;
}
if (count == position)
{
p -> next = newNode;
newNode -> next = q;
listLength++;
return true;
}
cout << "nError: node was not added to list.n";
return false;
}
// Setter removes a node by its given position.
// Returns true if the operation is successful.
bool LinkedList::removeNode( int position )
{
if ((position <= 0) || (position > listLength + 1))
{
cout << "nError: the given position is out of range.n";
return false;
}
if (head -> next == NULL)
{
cout << "nError: there is nothing to remove.n";
return false;
}
int count = 0;
node * p = head;
node * q = head;
while (q)
{
if (count == position)
{
p -> next = q -> next;
delete q;
listLength--;
return true;
}
p = q;
q = p -> next;
count++;
}
cout << "nError: nothing was removed from the list.n";
return false;
}
// Prints each node in the list in consecutive order,
// starting at the head and ending at the tail.
// Prints the data to the console.
void LinkedList::printList()
{
node * p = head;
node * q = head;
cout << "n---------------------------n";
cout << "Song Playlist n";
while (q)
{
p = q;
cout << "n-----------------------------n";
cout << "t position: " << count << endl;
cout << "t song: " << p -> song << endl;
cout << "t artist: " << p -> artist << endl;
q = p -> next;
count++;
}
}
// Destructor de-allocates memory used by the list.
LinkedList::~LinkedList()
{
node * p = head;
node * q = head;
while (q)
{
p = q;
q = p -> next;
if (q) delete p;
}
}
//*******************************************************************
// main.cpp
// LinkedList_Project
//
// Created by Karlina Beringer on June 12, 2014.
// This driver implements the LinkedList class.
//*******************************************************************
#include "LinkedList.h"
using namespace std;
int main()
{
// STEP 1: Create some unlinked song nodes.
node * A = new node;
A -> song = "We Are";
A -> artist = "Vertical Horizon";
node * B = new node;
B -> song = "I Stand Alone";
B -> artist = "Godsmack";
node * C = new node;
C -> song = "Heir Apparent";
C -> artist = "Opeth";
node * D = new node;
D -> song = "Fear of the Dark";
D -> artist = "Iron Maiden";
node * E = new node;
E -> song = "Blue Monday";
E -> artist = "New Order";
node * F = new node;
F -> song = "The Moth";
F -> artist = "Aimee Mann";
// STEP 2: Build a list of three song nodes by appending to end of list.
LinkedList myList;
myList.insertNode(A, 1);
myList.insertNode(B, 2);
myList.insertNode(C, 3);
myList.insertNode(D, 4);
myList.printList();
// STEP 3: Insert a node into middle of list.
myList.insertNode(E, 2);
myList.printList();
// STEP 4: Insert node at the front of list.
myList.insertNode(F,1);
myList.printList();
// STEP 5: Remove the last node from the list.
myList.removeNode(6);
myList.printList();
// 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.
myList.removeNode(3);
myList.printList();
return 0;
}
|