Skip to content

Instantly share code, notes, and snippets.

@mudassaralichouhan
Created February 4, 2024 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mudassaralichouhan/e04480138fab488c91eb12903d3a607e to your computer and use it in GitHub Desktop.
Save mudassaralichouhan/e04480138fab488c91eb12903d3a607e to your computer and use it in GitHub Desktop.
Single link list
#include "iostream"
using namespace std;
struct Node {
unsigned int no;
string name;
Node *next;
Node(unsigned int data, string name) : no(data), name(name), next(nullptr) {};
};
class SLL {
public:
void insert(Node *&head, unsigned int no, string name) {
Node *node = new Node(no, name);
if (head == nullptr) {
head = node;
} else {
Node *tmp = head;
while (tmp->next) {
tmp = tmp->next;
}
tmp->next = node;
}
}
void print(Node *head) {
Node *next = head;
while (next != nullptr) {
cout << next->no << ", " << next->name << endl;
next = next->next;
}
}
void insertAtBegin(Node *&head, int no, string name) {
Node *tmp = head;
head = new Node(no, name);
head->next = tmp;
}
int insertAt(Node *&head, int needle, int no, string name) {
Node *newNode = new Node(no, name);
if (head == nullptr) {
head = newNode;
return 1;
} else {
Node *tmp = head;
while (tmp) {
if (tmp->no == needle) {
Node *t = tmp->next;
tmp->next = newNode;
newNode->next = t;
return 1;
}
tmp = tmp->next;
}
}
return 0;
}
int deleteFrom(Node *&head, unsigned int needle) {
if (head == nullptr)
return 0;
Node *tmp = head;
Node *prv = nullptr;
while (tmp) {
if (tmp->no == needle) {
if (prv == nullptr) {
head = tmp->next;
return 1;
}
prv->next = tmp->next;
return 1;
}
prv = tmp;
tmp = tmp->next;
}
return 0;
}
Node *search(Node *head, unsigned int needle) {
if (head == nullptr)
return NULL;
Node *tmp = head;
while (tmp) {
if (tmp->no == needle) {
return tmp;
}
tmp = tmp->next;
}
return NULL;
}
Node *search(Node *head, string needle) {
if (head == nullptr)
return NULL;
Node *tmp = head;
while (tmp) {
if (tmp->name == needle) {
return tmp;
}
tmp = tmp->next;
}
return NULL;
}
void sort(Node *&head) {
if (!head || !head->next)
return;
Node *sorted = nullptr;
Node *current = head;
while (current) {
Node *nextNode = current->next;
if (sorted == nullptr || current->no < sorted->no) {
current->next = sorted;
sorted = current;
} else {
Node *temp = sorted;
while (temp->next != nullptr && temp->next->no < current->no) {
temp = temp->next;
}
current->next = temp->next;
temp->next = current;
}
current = nextNode;
}
cout << endl;
head = sorted;
}
};
int main() {
SLL sll;
Node *node = nullptr;
sll.insert(node, 10, "Ali");
sll.insert(node, 1, "Ali");
sll.insert(node, 2, "Husain");
sll.insert(node, 4, "Ben");
sll.insert(node, 3, "Jose");
sll.insert(node, 5, "Julia");
sll.insert(node, 11, "Chauhan");
sll.insert(node, 6, "Jon");
sll.insert(node, 0, "Jon");
sll.insertAtBegin(node, 10, "Raza");
sll.insertAt(node, 2, 5, "Jon Rambo");
sll.deleteFrom(node, 3);
sll.print(node);
Node *search = sll.search(node, 10);
if (search != NULL)
cout << "search: " << search->no << ", " << search->name << endl;
search = sll.search(node, "Husain");
if (search != NULL)
cout << "search: " << search->no << ", " << search->name << endl;
cout << endl << "After sort: " << endl;
sll.sort(node);
sll.print(node);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment