Created
February 25, 2014 21:25
-
-
Save bitwiser/9218149 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <string> | |
struct SLinkList | |
{ | |
std::string data; | |
SLinkList *next; | |
SLinkList() : data(""), next(NULL) { } | |
SLinkList(const char *value) : data(value), next(NULL) { } | |
SLinkList* InsertNext(const char *data) | |
{ | |
SLinkList *node = new SLinkList(data); | |
if(this->next == NULL) | |
{ | |
// Easy to handle | |
node->next = NULL; // already set in constructor | |
this->next = node; | |
} | |
else | |
{ | |
// Insert in the middle | |
SLinkList *temp = this->next; | |
node->next = temp; | |
this->next = node; | |
} | |
return node; | |
} | |
bool DeleteNext() | |
{ | |
if(this->next == NULL) | |
return false; | |
SLinkList *pNode = this->next; | |
this->next = this->next->next; // can be NULL here | |
delete pNode; | |
} | |
void Traverse(SLinkList *node = NULL) | |
{ | |
if(node == NULL) | |
node = this; | |
std::cout << "\n\nTraversing in Forward Direction\n\n"; | |
while(node != NULL) | |
{ | |
std::cout << node->data; | |
std::cout << "\n"; | |
node = node->next; | |
} | |
} | |
}; | |
int main() | |
{ | |
SLinkList *node1 = new SLinkList("ONE"); | |
SLinkList *node2 = node1->InsertNext("TWO"); | |
SLinkList *node3 = node2->InsertNext("THREE"); | |
SLinkList *node4 = node3->InsertNext("FOUR"); | |
SLinkList *node5 = node4->InsertNext("FIVE"); | |
node1->Traverse(); | |
node3->DeleteNext(); // delete the node "FOUR" | |
node2->Traverse(); | |
std::cout << "\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment