-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathLinkedList.cpp
More file actions
148 lines (141 loc) · 4.16 KB
/
LinkedList.cpp
File metadata and controls
148 lines (141 loc) · 4.16 KB
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
//***********************************************************************************
// 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()
{
cout << "\nEntering Constructor ..." << endl;
head = new node;
head -> song = "head (contains no song data)";
head -> artist = "head (contains no artist data)";
head -> next = NULL;
listLength = 0;
cout << "Success: head node created. listLength set to 0." << endl;
}
// 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 )
{
cout << "\nEntering insertNode ..." << endl;
if ( (position <= 0) || (position > listLength + 1) )
{
cout << "Error: the given position is out of range." << endl;
return false;
}
if (!head -> next)
{
head -> next = newNode;
listLength++;
cout << "Success: added '" << newNode -> song << "' to position " << position << ".\n";
cout << "listLength = " << listLength << endl;
return true;
}
int count = 0;
node * p = head;
node * q = head;
while (q)
{
if (count == position)
{
p -> next = newNode;
newNode -> next = q;
listLength++;
cout << "Success: added '" << newNode -> song << "' to position " << position << ".\n";
cout << "listLength = " << listLength << endl;
return true;
}
p = q;
q = p -> next;
count++;
}
if (count == position)
{
p -> next = newNode;
newNode -> next = q;
listLength++;
cout << "Success: added '" << newNode -> song << "' to position " << position << ".\n";
cout << "listLength = " << listLength << endl;
return true;
}
cout << "Error: song node was not added to list." << endl;
return false;
}
// Setter removes a node by its given position.
// Returns true if the operation is successful.
bool LinkedList::removeNode( int position )
{
cout << "\nEntering removeNode..." << endl;
if ( (position <= 0) || (position > listLength + 1) )
{
cout << "Error: the given position is out of range." << endl;
return false;
}
if (!head -> next)
{
cout << "Error: there is nothing to remove." << endl;
return false;
}
int count = 0;
node * p = head;
node * q = head;
while (q)
{
if (count == position)
{
p -> next = q -> next;
delete q;
listLength--;
cout << "Success: node at position " << position << " was deleted." << endl;
cout << "listLength = " << listLength << endl;
return true;
}
p = q;
q = p -> next;
count++;
}
cout << "Error: nothing was removed from the list." << endl;
return false;
}
// 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 LinkedList::printList()
{
cout << "\nEntered printList..." << endl;
int count = 0;
node * p = head;
node * q = head;
cout << "\n---------------------\n";
cout << " Song Playlist\n";
while (q)
{
p = q;
cout << "---------------------\n";
cout << "\t position: " << count << "\n";
cout << "\t song: " << p -> song << "\n";
cout << "\t artist: " << p -> artist << "\n";
q = p -> next;
count++;
}
}
// Destructor de-allocates memory used by the list.
LinkedList::~LinkedList()
{
cout << "\nEntering Destructor..." << endl;
node * p = head;
node * q = head;
while (q)
{
p = q;
q = p -> next;
delete p;
}
cout << "Success: list is deleted." << endl;
}