Last active
March 7, 2016 07:34
-
-
Save SohanChy/d4e2eb8f8b93842cdec7 to your computer and use it in GitHub Desktop.
Simple link list implementation using struct and functions
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> | |
| using namespace std; | |
| struct ListNode | |
| { | |
| int data; | |
| ListNode *next; | |
| }; | |
| ListNode* GetNewNode(int userData = 0,bool userInput = false) | |
| { | |
| ListNode *Node = new ListNode; | |
| if(userInput == true) | |
| { | |
| cin>>Node->data; | |
| } | |
| else | |
| { | |
| Node->data = userData; | |
| } | |
| Node->next = NULL; | |
| return Node; | |
| } | |
| void DeleteNode(ListNode*& Head,int position) | |
| { | |
| ListNode* tmp = Head; | |
| if(position > 0) | |
| { | |
| for(int i=0; i<(position-1) && tmp != NULL; i++) | |
| { | |
| tmp = tmp->next; | |
| } | |
| ListNode* toDelete = tmp->next; | |
| tmp->next = toDelete->next; | |
| delete toDelete; | |
| } | |
| else | |
| { | |
| Head = tmp->next; | |
| delete tmp; | |
| } | |
| } | |
| void printList(ListNode *Head) | |
| { | |
| ListNode *tmp = Head; | |
| cout<<endl; | |
| while(tmp != NULL) | |
| { | |
| cout<<tmp->data<<" "; | |
| tmp = tmp->next; | |
| } | |
| cout<<endl; | |
| } | |
| int SearchNode(ListNode* Head,int needle) | |
| { | |
| ListNode *tmp = Head; | |
| for(int i = 0; tmp != NULL ; i++) | |
| { | |
| if(tmp->data == needle) | |
| { | |
| return i; | |
| } | |
| tmp = tmp->next; | |
| } | |
| return -1; | |
| } | |
| void insertBeg(ListNode*& Head,int value) | |
| { | |
| ListNode* newNode = GetNewNode(value); | |
| newNode->next = Head; | |
| Head = newNode; | |
| } | |
| void insertAfter(ListNode* Head,int afterVal, int value) | |
| { | |
| ListNode * tmp = Head; | |
| bool found = false; | |
| while(tmp != NULL) | |
| { | |
| if(tmp->data == afterVal) | |
| { | |
| ListNode* newNode = GetNewNode(value); | |
| newNode->next = tmp->next; | |
| tmp->next = newNode; | |
| found = true; | |
| } | |
| tmp = tmp->next; | |
| if(found== false && tmp->next == NULL) | |
| { | |
| break; | |
| } | |
| } | |
| if(found == false) | |
| { | |
| ListNode* newNode = GetNewNode(value); | |
| tmp->next = newNode; | |
| } | |
| } | |
| int main() | |
| { | |
| cout<<"How many link list items do you want?"<<endl; | |
| int sz = 0; | |
| cin>>sz; | |
| ListNode *Head; | |
| ListNode *Node = GetNewNode(0,true); | |
| Head = Node; | |
| for(int i = 1; i<sz; i++) | |
| { | |
| Node->next = GetNewNode(i,true); | |
| Node = Node->next; | |
| } | |
| printList(Head); | |
| cout<<"Want to delete a item? enter position:"<<endl; | |
| int pos; | |
| cin>>pos; | |
| DeleteNode(Head,pos); | |
| printList(Head); | |
| { | |
| int needle; | |
| cout<<"Search item: "; | |
| cin>>needle; | |
| int x = SearchNode(Head,needle); | |
| if(x>=0) | |
| { | |
| cout<<"FOUND at "<<x<<endl; | |
| } | |
| else cout<<"NOT FOUND"<<endl; | |
| } | |
| printList(Head); | |
| { | |
| int needle,val; | |
| cout<<"Insert after item: "; | |
| cin>>needle; | |
| cout<<"Insert item value: "; | |
| cin>>val; | |
| insertAfter(Head,needle,val); | |
| } | |
| printList(Head); | |
| { | |
| int val; | |
| cout<<"Insert at start? "; | |
| cin>>val; | |
| cout<<endl; | |
| insertBeg(Head,val); | |
| } | |
| printList(Head); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment