Created
November 6, 2016 04:43
-
-
Save shaunlgs/5b30bdd152cf292f2f8b19502c7c23bd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <conio.h> | |
using namespace std; | |
class Node { | |
public: | |
double data; | |
Node* next; | |
}; | |
class List { | |
public: | |
List(void) { head = NULL; } // constructor | |
~List(void); // destructor | |
bool IsEmpty() { return head == NULL; } | |
Node* InsertNode(double x); | |
int FindNode(double x); | |
int DeleteNode(double x); | |
void DisplayList(void); | |
private: | |
Node* head; | |
}; | |
Node* List::InsertNode(double x) { | |
int currIndex = 0; | |
Node* currNode = head; | |
Node* prevNode = NULL; | |
while (currNode && x > currNode->data) | |
{ prevNode = currNode; currNode = currNode->next; currIndex++; } | |
Node* newNode = new Node; | |
newNode->data = x; | |
if (currIndex == 0) { newNode->next = head; head = newNode; } | |
else { newNode->next = prevNode->next; prevNode->next = newNode; } | |
return newNode; | |
} | |
int List::FindNode(double x) { | |
Node* currNode = head; | |
int currIndex = 1; | |
while (currNode && currNode->data != x) { currNode = currNode->next; currIndex++; } | |
if (currNode) | |
return currIndex; | |
else | |
return 0; | |
} | |
void List::DisplayList() { | |
int num = 0; | |
Node* currNode = head; | |
while (currNode != NULL){ cout << currNode->data << endl; currNode = currNode->next; num++; } | |
cout << "Number of nodes in the list: " << num << endl; | |
} | |
int List::DeleteNode(double x) { | |
Node* prevNode = NULL; | |
Node* currNode = head; | |
int currIndex = 1; | |
while (currNode && currNode->data != x) | |
{ | |
prevNode = currNode; | |
currNode = currNode->next; | |
currIndex++; | |
} | |
if (currNode) { | |
if (prevNode) { | |
prevNode->next = currNode->next; | |
delete currNode; | |
} else { head = currNode->next; delete currNode; } | |
return currIndex; | |
} | |
return 0; } | |
List::~List() { | |
Node* currNode = head, | |
*nextNode = NULL; | |
while (currNode != NULL) { | |
nextNode = currNode->next; // destroy the current node | |
delete currNode; | |
currNode = nextNode; | |
} | |
head = NULL; | |
} | |
int main(void) { | |
List list; | |
list.InsertNode(7.0); // successful | |
list.InsertNode(5.0); // successful | |
list.InsertNode(6.0); // successful | |
list.InsertNode(4.0); // successful | |
// print all the elements | |
list.DisplayList(); | |
if(list.FindNode(5) > 0) | |
cout << "5 found" << endl; | |
else | |
cout << "5 not found" << endl; | |
if(list.FindNode(4.5) > 0) | |
cout << "4.5 found" << endl; | |
else | |
cout << "4.5 not found" << endl; | |
list.DeleteNode(7.0); | |
list.DisplayList(); | |
getch(); | |
return 0; } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment