Last active
March 22, 2022 15:08
-
-
Save shivajichalise/0d029c329e6260e2a643c95c62b3a208 to your computer and use it in GitHub Desktop.
Singly Linked List implementation in C++
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> | |
using namespace std; | |
class Node { | |
public: | |
int data; | |
Node *next; | |
Node() { | |
data = 0; | |
next = NULL; | |
} | |
Node(int d) { | |
data = d; | |
next = NULL; | |
} | |
}; | |
class LinkedList { | |
public: | |
Node *head; | |
LinkedList() { head = NULL; } | |
LinkedList(Node *n) { head = n; } | |
void traverse() { | |
if (head == NULL) { | |
cout << "List is empty!" << endl; | |
return; | |
} else { | |
Node *ptr = head; | |
do { | |
cout << ptr->data << "-->"; | |
ptr = ptr->next; | |
} while (ptr != NULL); | |
cout << endl; | |
} | |
} | |
void appendNode(Node *n) { | |
if (head == NULL) { | |
head = n; | |
} else { | |
Node *ptr = head; | |
while (ptr->next != NULL) { | |
ptr = ptr->next; | |
} | |
ptr->next = n; | |
} | |
} | |
void prependNode(Node *n) { | |
if (head == NULL) { | |
head = n; | |
} else { | |
n->next = head; | |
head = n; | |
} | |
} | |
bool nodeExists(int d) { | |
Node *ptr = head; | |
while (ptr != NULL) { | |
if (ptr->data == d) { | |
return true; | |
} | |
ptr = ptr->next; | |
} | |
return false; | |
} | |
void insertNodeAfter(int d, Node *n) { | |
Node *ptr = head; | |
while (ptr->data != d) { | |
ptr = ptr->next; | |
if (ptr == NULL) { | |
cout << "Data " << d << " does not exist in the list" << endl; | |
return; | |
} | |
} | |
n->next = ptr->next; | |
ptr->next = n; | |
} | |
void insertNodeBefore(int d, Node *n) { | |
Node *prevNode = head; | |
Node *nextNode = head->next; | |
if (prevNode->data == d) { | |
prependNode(n); | |
return; | |
} | |
while (nextNode->data != d) { | |
prevNode = prevNode->next; | |
nextNode = nextNode->next; | |
if (prevNode == NULL) { | |
cout << "Data " << d << " does not exist in the list" << endl; | |
return; | |
} | |
} | |
n->next = prevNode->next; | |
prevNode->next = n; | |
cout << "Node inserted before node with value " << d << endl; | |
} | |
void deleteNodeAfter(int d) { | |
Node *ptr = head; | |
Node *temp = NULL; | |
while (ptr->data != d) { | |
ptr = ptr->next; | |
if (ptr == NULL) { | |
cout << "Data " << d << " does not exist in the list" << endl; | |
return; | |
} | |
} | |
if (ptr->next == NULL) { | |
cout << "Node with data " << d << " is in the last of the list." << endl; | |
return; | |
} | |
temp = ptr->next; | |
ptr->next = temp->next; | |
cout << "Node after node with data " << d << " unlinked" << endl; | |
delete temp; | |
} | |
void deleteNodeBefore(int d) { | |
Node *prevNode = head; | |
Node *midNode = head->next; | |
Node *nextNode = midNode->next; | |
if (prevNode->data == d) { | |
cout << "Data " << d | |
<< " is the first node no other node exist before it." << endl; | |
return; | |
} | |
while (nextNode->data != d) { | |
nextNode = nextNode->next; | |
prevNode = prevNode->next; | |
midNode = midNode->next; | |
if (nextNode == NULL) { | |
cout << "Data " << d << " does not exist in the list" << endl; | |
return; | |
} | |
} | |
prevNode->next = midNode->next; | |
delete midNode; | |
} | |
void searchList(int d) { | |
Node *ptr = head; | |
while (ptr->data != d) { | |
ptr = ptr->next; | |
if (ptr == NULL) { | |
cout << "Data " << d << " does not exist in the list" << endl; | |
return; | |
} | |
} | |
cout << "Data " << d << " found!" << endl; | |
} | |
}; | |
int main() { | |
LinkedList myList; | |
myList.appendNode(new Node(5)); | |
myList.appendNode(new Node(6)); | |
myList.appendNode(new Node(7)); | |
myList.appendNode(new Node(8)); | |
myList.traverse(); | |
myList.prependNode(new Node(9)); | |
myList.traverse(); | |
myList.insertNodeBefore(9, new Node(1)); | |
myList.traverse(); | |
myList.insertNodeAfter(1, new Node(2)); | |
myList.traverse(); | |
myList.deleteNodeBefore(1); | |
myList.deleteNodeAfter(8); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment