// Singly Linked List
#include <bits/stdc++.h>
using namespace std;
class node
{
public:
int data;
node *next;
node(int val)
{
data = val;
next = NULL;
}
};
void display(node *head)
{
node *temp = head;
while (temp != NULL)
{
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
}
// Insertion
void insertAtHead(node *&head, int val)
{
node *n = new node(val); // created a block
n->next = head; // connecting it with the head
head = n;
}
void insertAtTail(node *&head, int val)
{
node *n = new node(val);
if (head == NULL)
{
cerr << "Head is NULL" << endl;
head = n;
return;
}
node *temp = head;
while (temp->next != NULL)
{
cerr << "Traversing to node with value-> " << temp->data <<endl;
temp = temp->next;
}
cerr << "Creating a node with value-> " << n->data <<"with node of value -> " << temp->data<<endl;
temp->next = n;
}
void insertAtPosition(node* &head, int pos, int val){
if (pos <= 0) {
// Invalid position, nothing to insert
return;
}
if (pos == 1 || head == NULL) {
// Insert at the head
insertAtHead(head, val);
return;
}
int count = 1;
node* temp = head;
while (temp->next != NULL && count + 1 != pos) {
temp = temp->next;
count++;
}
if (pos > count + 1 || temp->next == NULL) {
// Position is greater than the size of the list
// or the position is at the end of the list
return;
}
node* n = new node(val);
n->next = temp->next;
temp->next = n;
}
// Deletion
void deleteAtHead(node* &head){
node* deletingNode = head;
head = head->next;
delete deletingNode;
}
void deleteAtTail(node *&head)
{
if (head == NULL)
{ // if Head is NULL, just return
return;
}
node *temp = head;
if (temp->next == NULL)
{ // if LinkedList is having only one element, delete it
deleteAtHead(head);
return;
}
while (temp->next->next != NULL)
{ // if LL has more than 1 element, traverse to
// last 2nd
temp = temp->next;
}
node *deletingNode = temp->next; // element we need to delete
// because it is pointing at nothing now.
temp->next = NULL; // point the last second node to NULL
delete deletingNode; // way to delete something from the memory
}
void deletePosition(node* &head, int pos){
if(pos <= 0 || head == nullptr){
// Invalid position or empty list, nothing to delete
return;
}
if(pos == 1){
// Deleting the head node
deleteAtHead(head);
return;
}
int count = 1;
node* temp = head;
while(temp->next != nullptr && count + 1 != pos){
temp = temp->next;
count++;
}
if(pos > count + 1 || temp->next == nullptr){
// Position is greater than the size of the list
// or the position is at the end of the list
return;
}
// Delete the node at the given position
node* toDelete = temp->next;
temp->next = toDelete->next;
delete toDelete;
}
// Searching
bool searchInLL(node *head, int target)
{
node *temp = head;
while (temp != NULL)
{
if (temp->data == target)
return true;
temp = temp->next;
}
return false;
}
// Problems
void findPointers(node *head)
{
node *temp = head;
int firstIndex = -1;
int secondIndex = -1;
int count = 1;
while (temp->next != NULL)
{
if (temp->data == 0)
{ // if it's a node with zero value
if (firstIndex == -1)
{ // when it's very first time we saw zero
firstIndex = count;
}
else
{ // if we already saw zero somewhere, probably it's second index
secondIndex = count;
}
}
temp = temp->next; // move the pointer to next node
count++; // count++ to keep a track of indices
}
// once loop is over, we might have got our firstI and secondI
cout << firstIndex << ' ' << secondIndex << endl;
}
int sumLinkList(node *head)
{
int sum = 0;
if (head == NULL)
return sum;
node *temp = head;
while (temp != NULL)
{
sum += (temp->data);
temp = temp->next;
}
return sum;
}
int main()
{
node *head = NULL;
insertAtTail(head, 1);
insertAtTail(head, 2);
insertAtTail(head, 3);
insertAtHead(head, 4);
display(head);
deleteAtTail(head);
display(head);
insertAtHead(head, 5);
display(head);
insertAtHead(head, 6);
display(head);
deleteAtTail(head);
display(head);
deleteAtHead(head);
display(head);
// Create a node
// add at last
// add at first
// remove from last
// remove from first
// Search in LL
}