Thursday, April 19, 2012

Linked list


////Node class in C++
#include <iostream>;

class Node {
public:
double value;
Node* next;

public:
Node(){ value = 0; next = NULL; }

Node(double x){ value = x; next = NULL;}
};

////Single linked list in C++

#include "Node.h";
#include <stdio.h>;
using namespace std;

class SingleListedList{
public:
Node* head;
SingleListedList(){ head = NULL; }
SingleListedList(Node* node){ node = head; }

void Insert(int position, Node* newNode);
void InsertBehindFirst(int value, Node* newNode);
void Delete(int position);
void DeleteFirstValue(int value);
private:
void insertNodeBehind(Node* current, Node* newNode);
};

void SingleListedList::Insert(int position, Node* newNode){
//determine if valid paramter
if (position < 1){
cout << "The input parameters are not valid.";
return;
}
if(newNode == NULL){
cout << "The node to be inserted is NULL." << endl;
return;
}

//boundary - beginning
if (head == NULL){
if(position > 1){
cout << "The linked list does not exist and the inserted position is not from the beginning." << endl;
return;
}
else{
head = newNode;
return;
}
}

//boundary case -- head
if(head != NULL && position == 1){
newNode->next = head;
head = newNode;
return;
}

int pos = 1;
Node* current = head;

while ((pos < (position-1)) && current != NULL){
pos++;
current = current -> next;
}

//boudary - terminated
if (current == NULL){
cout << "The linked list is not long enough, and the position is beyond the index." << endl;
return;
}

//happy path
insertNodeBehind(current, newNode);

return;
}

void SingleListedList::InsertBehindFirst(int value, Node* newNode){
//check input parameter
if(newNode == NULL){
cout << "The new node is NULL." << endl;
return;
}
if(head == NULL){
cout << "The linked list does not exist." << endl;
return;
}

if (head->value == value){
insertNodeBehind(head, newNode);
return;
}

Node* current = head;

do{
current = current->next;
} while(current->value != value && current->next != NULL);

if (current->value == value){
insertNodeBehind(current, newNode);
} else {
cout << "The value is not found." << endl;
}

return;
}

void SingleListedList::Delete(int position){
//check condition
//check input variable
if (position < 1){
cout << "Can node delete node with invalid position index." << endl;
return;
}
//check external condition
if(head == NULL) {
cout << "Empty list and no node to delete" << endl;
return;
}

//boundary case
if (position == 1){
head = head -> next;
return;
}

int currPos = 2;
Node* prep = head;
Node* curr = head->next;

//find location
while (currPos != position && curr != NULL){
currPos++;
prep = curr;
curr = curr->next;
}

//delete the node
if (curr != NULL) {
prep->next = curr->next;
} else {
cout << "The position does not exist" << endl;
}

return;
}

void SingleListedList::DeleteFirstValue(int value){
//check condition
if (head == NULL) {
cout << "empty list" << endl;
return;
}

//boundary case - head
if (head->value == value){
head = head->next;
return;
}

//happy path
Node* prep = head;
Node* curr = head->next;

while (curr->value != value && curr->next != NULL){
prep = curr;
curr = curr->next;
}

if (curr->value == value){
prep->next = curr->next;
} else {
cout << "cannot find the node." << endl;
}

return;
}

void SingleListedList::insertNodeBehind(Node* current, Node* newNode){
if(current == NULL){
cout << "The node is invalid, and we cannot insert another node behind." <<endl;
return;
}

Node* temp = current;
current = current -> next;
temp -> next = newNode;
newNode -> next = current;
}

////Linked list test


#include "LinkedList.h";

void printLinkedList(SingleListedList* singleList);
void printRevertLinkedList(Node* node);
void printRevertedLinkedListSimp(Node* node);

void LikedListTest(){
//test Node
Node node1;
double x = 8;
Node* node8 = new Node(x);
node1.next = node8;
cout << "output the nodes in order" << endl;
cout << node1.value << "->" << node1.next->value << endl;

//test Insert in SingleLinkedList
SingleListedList* singleList = new SingleListedList();
Node* newNode = new Node(-1);
//boudary case
singleList->Insert(2, newNode);
//happy path
singleList->head = node8;
for(int i=0;i<5;i++){
newNode = new Node(i);
singleList->Insert(1, newNode);
}
newNode = new Node(5);
singleList->Insert(2,newNode);

    newNode = new Node(9);
singleList->InsertBehindFirst(9, newNode);
singleList->InsertBehindFirst(8, newNode);

//test happy path for insert
printLinkedList(singleList);
printRevertLinkedList(singleList->head);
cout << "end" << endl;
printRevertedLinkedListSimp(singleList->head);
cout << "end" << endl;

//test happy path for delete
singleList->Delete(1);
printLinkedList(singleList);
singleList->DeleteFirstValue(3);
printLinkedList(singleList);
}

void printLinkedList(SingleListedList* singleList){
    Node* current = singleList->head;

if(current == NULL){
cout << "Empty List" << endl;
return;
}

cout << current->value << "->";
while(current->next != NULL){
current = current->next;
cout << current->value << "->";
}
cout << "end" << endl;

return;
}



No comments:

Post a Comment