Created
May 12, 2017 16:23
-
-
Save majedsiefalnasr/734987860761e3ad422c449e63743b97 to your computer and use it in GitHub Desktop.
Linked List
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
struct Node{ | |
int index; | |
string data; | |
Node *next; | |
}; | |
void st_Node(Node *head, int index, string data){ | |
head->index = index; | |
head->data = data; | |
head->next = NULL; | |
} | |
void insert_Node(Node *head, int index, string data){ | |
Node *new_Node = new Node; | |
new_Node->index = index; | |
new_Node->data = data; | |
new_Node->next = NULL; | |
Node *curr = head; | |
while (curr != NULL) | |
{ | |
if (curr->next == NULL) | |
{ | |
curr->next = new_Node; | |
return; | |
} | |
curr = curr->next; | |
} | |
} | |
Node *search_Node(Node *head, string data) | |
{ | |
Node *curr = head; | |
while (curr != NULL) | |
{ | |
if (data.compare(curr->data) == 0) | |
return curr; | |
else | |
curr = curr->next; | |
} | |
return NULL; | |
} | |
Node *search_Node(Node *head, int index) | |
{ | |
Node *curr = head; | |
while (curr != NULL) | |
{ | |
if (index == curr->index) | |
return curr; | |
else | |
curr = curr->next; | |
} | |
return NULL; | |
} | |
bool delete_Node(Node *head, Node *to_delete){ | |
if (to_delete == NULL) | |
return false; | |
else if (to_delete == head) | |
{ | |
head = to_delete->next; | |
delete to_delete; | |
return true; | |
} | |
else{ | |
Node *curr = head; | |
while (curr) | |
{ | |
if (curr->next == to_delete) | |
{ | |
curr->next = to_delete->next; | |
delete to_delete; | |
return true; | |
} | |
curr = curr->next; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment