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
|
#include "LinkedList.h"
using namespace std;
LinkedList::LinkedList(){ //constructor
head = NULL;
last = NULL;
count = 0;
}
void LinkedList:: insertNodeAtStart(int height, int distanceFromHome, bool climbed, int gridReference){
Node *nextNode;
nextNode = new Node;
nextNode->height = height;
nextNode->distanceFromHome= distanceFromHome;
nextNode->climbed= climbed;
nextNode->gridReference= gridReference;
nextNode->link = head;
head = nextNode;
count++;
if(last == NULL){
last = nextNode;
}
}
void LinkedList:: insertNodeAtEnd(int height, int distanceFromHome, bool climbed, int gridReference ){
Node *lastNode;
lastNode= new Node;
lastNode->link= NULL;
lastNode->height = height;
lastNode->distanceFromHome= distanceFromHome;
lastNode->climbed= climbed;
lastNode->gridReference= gridReference;
count++;
if(head==NULL){
head= lastNode; //if this is the only node, make it first and last.
last= lastNode;
}
else{
last->link= lastNode; //make the link of the last node point to our new node
last= lastNode; //make last or newly created node.
}
}
int LinkedList::getNodeCount(){
return count;
}
Node* LinkedList::getFirst(){
return head;
}
void LinkedList::deleteFirstNode(){
if(count > 0){
Node *temp = head;
head = head->link;
delete temp;
count--;
}
else{
cout<<"There are no nodes to delete."<<endl;
}
}
/*bool LinkedList::findNumber(int number){
int noOfNodes=getNodeCount();
Node *current;
current= getFirst();
for(int i=0; i<=noOfNodes-1; i++){
if(number==current->data){
cout<<"found"<<endl;
return true;
}
else{
current=current->link;
}
}
cout<<"Number is not in the Linked List"<<endl;
return false;
}*/
/*void LinkedList::deleteNode(int number){
Node *current, *next; //two pointer to help maipulate position
current= head; //set current to the head of the list
if(current->data== number) //if the number is first in the list
{
head= current->link; //make head point to the one after
delete current; //delete the node which used to be head
count--; //reduce count by one
return;
}
next=current; //make next = the new head node (current)
while(next!=NULL) //iterate through until no more nodes
{
if(current->data==number) //if the data in current is the number
{
next->link= current->link; //make the link next points to = to what current is pointing to
delete current; //delete the current node
count--;
return;
}
next=current; //keep iterating until the number is found
current=current->link;
}
cout<<"Element not Found!"<<endl; //if not in the list display suitable.
}*/
void LinkedList:: deleteAllNodes(){
int noOfNodes= getNodeCount();
Node *current;
current= head;
for(int i=0; i<noOfNodes;i++){
current->link=current;
delete current;
count--;
}
}
void LinkedList::printNode(){
int noOfNodes= getNodeCount();
Node *current;
current= head;
for(int i=0; i<noOfNodes; i++){
cout<<"Height: "<<current->height<<endl;
cout<<"Distance from Home: "<<current->distanceFromHome<<endl;
cout<<"Climbed: "<<current->climbed<<endl;
cout<<"Grid Ref: "<<current->gridReference<<endl;
cout<<"-------------------"<<endl;
current= current->link;
}
}
void LinkedList::insertNodet(int height, int distanceFromHome, bool climbed, int gridReference){
Node* current= head;
Node* previous= NULL;
Node* temp= new Node();
count++;
temp->height= height;
temp->distanceFromHome= distanceFromHome;
temp->climbed= climbed;
temp->gridReference= gridReference;
temp->link= NULL;
if(count==1)
{
head= temp;
last= temp;
return;
}
do{
if(current->height<temp->height)
{
temp->link= current;
if(current== head)
{
head= temp;
return;
}
else
{
previous->link= temp;
return;
}
if(current==last)
{
current->link=temp;
last= temp;
return;
}
}
previous=current;
current= current->link;
}
while(current!=NULL);
}
void LinkedList::swap(Node *current, Node *next){
Node *swap;
Node *swapLink;
swapLink= current->link;
current->link= next->link;
next->link= swapLink;
swap= current;
current=next;
next=swap;
};
void LinkedList::sort();
{
{
int noOfNodes= getNodeCount();
Node *current;
Node *nextNode;
current= head;
nextNode= current->link;
for(int i=0;i<noOfNodes;i++){
if(current=NULL){
cout<<"No hills in list"<<endl;
return;
}
if(current->height>nextNode->height){
swap(current, nextNode);
}
}
/*void LinkedList::printNode()
{
int noOfNodes= getNodeCount();
Node *current;
current= head;
cout << "Number of Nodes == " << noOfNodes << endl;
for(int i=0; i<noOfNodes && current != NULL; i++)
{
cout<<"Height: "<<current->height<<endl;
cout<<"Distance from Home: "<<current->distanceFromHome<<endl;
cout<<"Climbed: "<<current->climbed<<endl;
cout<<"Grid Ref: "<<current->gridReference<<endl;
cout<<"-------------------"<<endl;
current= current->link;
}*/
|