Love Babbar's Linked List notes (class instead of structs were used) 02. Linked List
Abdul Sir's Linked List in C++ using Class : 11. Linked List > C++ Class Code for a Linked List
Problem with array : Fixed Size
We might not be sure of how many elements we'd want to store in an array. Taking an array of a fixed size might lead to problems where the size might be either too large or too small for our requirements.
We can either create an array inside the stack or inside the heap.
int a[5]; // to create the array inside the stack
int *p = new int[5] // to create an array inside the heap
If we want a variable size data structure then we can't opt for arrays.
linked list is a collection of nodes where each node contains data and pointer to the next node.
Linked list is always created inside heap memory.
Element Space and the Pointer Space together are called node.
struct Node *next;
: here next is a pointer of struct Node type.In any compiler if integer is taking 4 or 2 bytes inside a structure then the pointer inside that structure will also take 4 or 2 bytes respectively.
For creating a node we need a pointer because nodes are to be created inside the heap.
struct Node
{
int data;
struct Node *next;
};
struct Node *p; // this pointer will be created inside the stack
// for clang : we use malloc
p = (struct Node *)malloc(sizeof(struct Node));
// for c++ :
p = new Node;
p -> data = 10; //assigning data to the dataspace
p -> next = 0; // zero which means NULL is stored here, so it is not pointing anywhere
p
is pointing to the address of dataspace in the nodep
is a pointer to a structure therefore we'll use arrow to access the members of the structure.q=p;
: means q=200 so q will also point to the address 200q=p->next;
: q will get the value 210, therefore q is pointing on the next node. It will be read as let q point on next node of p
.p=p->next;
: p will now be pointing to the value stored at address 210true
then that means the current node is the last node in the linked list.true
then that means the current node is NOT the last node in the linked list.p=0;
#include<iostream>
using namespace std;
struct Node
{
int data;
struct Node *next;
}*first=NULL; // first is a global pointer
/* -------- creating a linked list using an array ------- */
void create(int A[], int n) // n = no. of elements in the array
{
int i;
struct Node *t, *last; //'t' will help us in creating a new node, 'last' will be pointing on the last node and will help us in adding a new node in the linked list
first = new Node; // first is a pointer which is pointing to a node created in heap.
first -> data = A[0];
first -> next = NULL;
last=first;
// for the rest of the nodes we'll create them using for loop
for (i=1; i<n; i++) // we are starting from 1 as 0th element has already been created
{
t = new Node;
t -> data = A[i];
t->next=NULL;
last->next=t;
last=t;
}
}
void Display ( struct Node *p)
{
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
}
int main()
{
int A[]={3,5,7,10,15};
create(A, 5);
Display(first);
return 0;
}
#include<iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
}*first=NULL;
void create(int A[], int n) // n = no. of elements in the array
{
int i;
Node *t, *last; //'t' will help us in creating a new node, 'last' will be pointing on the last node and will help us in adding a new node in the linked list
first = new Node; // first is a pointer which is pointing to a node created in heap.
first -> data = A[0];
first -> next = NULL;
last=first;
// for the rest of the nodes we'll create them using for loop
for (i=1; i<n; i++) // we are starting from 1 as 0th element has already been created
{
t = new Node;
t -> data = A[i];
t->next=NULL;
last->next=t;
last=t;
}
}
void Display (Node *p)
{
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
}
int main()
{
int A[]={3,5,7,10,15};
create(A, 5);
Display(first);
return 0;
}
another method :
int findMax(struct Node *p)
{
int max = INT_MIN;
while(p)
{
if(p->data > max)
max = p->data;
p=p->next;
}
return max;
}
int findMax(struct Node *p)
{
int x = INT_MIN;
if(p==0)
return INT_MIN;
else
{
x=findMax(p->next);
if (x > p->data)
return x;
else
return p->data;
}
}
Same as method 2 but with ternary operator
int findMax(struct Node *p)
{
int x = INT_MIN;
if(p==0)
return INT_MIN;
x = findMax(p->next);
return x > p->data ? x : p->data;
}
See the search
function :
#include<iostream>
using namespace std;
struct Node
{
int data;
struct Node *next;
}*first=NULL; // first is a global pointer
/* -------- creating a linked list using an array ------- */
void create(int A[], int n) // n = no. of elements in the array
{
int i;
struct Node *t, *last; //'t' will help us in creating a new node, 'last' will be pointing on the last node and will help us in adding a new node in the linked list
first = new Node; // first is a pointer which is pointing to a node created in heap.
first -> data = A[0];
first -> next = NULL;
last=first;
// for the rest of the nodes we'll create them using for loop
for (i=1; i<n; i++) // we are starting from 1 as 0th element has already been created
{
t = new Node;
t -> data = A[i];
t->next=NULL;
last->next=t;
last=t;
}
}
void Display ( struct Node *p)
{
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
}
Node *search(struct Node *p, int key)
{
while (p != NULL)
{
if (p->data == key)
return p; // return the address of the node
else
p = p->next;
}
return NULL;
}
int main()
{
int A[]={3,5,7,10,15};
create(A, 5);
Display(first);
printf("%d", search(first, 7));
return 0;
}
#include<iostream>
using namespace std;
struct Node
{
int data;
struct Node *next;
}*first=NULL; // first is a global pointer
/* -------- creating a linked list using an array ------- */
void create(int A[], int n) // n = no. of elements in the array
{
int i;
struct Node *t, *last; //'t' will help us in creating a new node, 'last' will be pointing on the last node and will help us in adding a new node in the linked list
first = new Node; // first is a pointer which is pointing to a node created in heap.
first -> data = A[0];
first -> next = NULL;
last=first;
// for the rest of the nodes we'll create them using for loop
for (i=1; i<n; i++) // we are starting from 1 as 0th element has already been created
{
t = new Node;
t -> data = A[i];
t->next=NULL;
last->next=t;
last=t;
}
}
void Display ( struct Node *p)
{
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
}
Node *search(Node *p, int key)
{
if (p == NULL)
return NULL;
else if (key == p->data)
return p;
else
return search(p->next, key);
}
int main()
{
int A[]={3,5,7,10,15};
create(A, 5);
Display(first);
printf("%d", search(first, 7));
return 0;
}
We've already seen two methods to improve linear search in arrays :
#include<iostream>
using namespace std;
struct Node
{
int data;
struct Node *next;
}*first=NULL; // first is a global pointer
/* -------- creating a linked list using an array ------- */
void create(int A[], int n) // n = no. of elements in the array
{
int i;
struct Node *t, *last; //'t' will help us in creating a new node, 'last' will be pointing on the last node and will help us in adding a new node in the linked list
first = new Node; // first is a pointer which is pointing to a node created in heap.
first -> data = A[0];
first -> next = NULL;
last=first;
// for the rest of the nodes we'll create them using for loop
for (i=1; i<n; i++) // we are starting from 1 as 0th element has already been created
{
t = new Node;
t -> data = A[i];
t->next=NULL;
last->next=t;
last=t;
}
}
void Display ( struct Node *p)
{
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
}
Node *searchNormal(Node *p, int key)
{
while(p != NULL)
{
if (p->data == NULL)
return NULL;
else if ( p->data == key)
return p;
else
p = p-> next;
}
return NULL;
}
Node *searchOptimized(Node *p, int key)
{
Node *q = NULL;
while(p != NULL)
{
if (key == p-> data)
{
q->next = p->next;
p->next = first;
first = p;
return p;
}
else
{
q=p;
p=p->next;
}
}
return NULL;
}
int main()
{
int A[] = {3,5,7,10,15};
create(A, 5);
Display(first);
printf("\n%d", searchOptimized(first, 7));
printf("\n%d", searchNormal(first, 7));
return 0;
}
#include<iostream>
using namespace std;
struct Node
{
int data;
struct Node *next;
}*first=NULL; // first is a global pointer
/* -------- creating a linked list using an array ------- */
void create(int A[], int n) // n = no. of elements in the array
{
int i;
struct Node *t, *last; //'t' will help us in creating a new node, 'last' will be pointing on the last node and will help us in adding a new node in the linked list
first = new Node; // first is a pointer which is pointing to a node created in heap.
first -> data = A[0];
first -> next = NULL;
last=first;
// for the rest of the nodes we'll create them using for loop
for (i=1; i<n; i++) // we are starting from 1 as 0th element has already been created
{
t = new Node;
t -> data = A[i];
t->next=NULL;
last->next=t;
last=t;
}
}
void Display ( struct Node *p)
{
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
cout << endl;
}
void insert(struct Node *p, int pos, int x )
{
Node *t;
if(pos == 0) // inserting before 'first'
{
t = new Node;
t->data = x;
t->next = first;
first = t;
}
else if (pos > 0)
{
for (int i = 0; i<pos-1 && p; i++) //as for inserting after pos 4 we'll have to move the p pointer 3 times as initially the p pointer is pointing at first node. Also, we did '&& p' to check if p exists or not
{
p = p->next;
}
if(p)
{
t = new Node;
t->data = x;
t->next = p->next;
p->next = t;
}
}
}
int main()
{
int A[] = {3,5,7,10,15};
create(A, 5);
Display(first);
insert(first, 3, 12);
Display(first);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
} *first = NULL;
void create(int A[], int n)
{
int i;
struct Node *t, *last;
first = (struct Node *)malloc(sizeof(struct Node));
first->data = A[0];
first->next = NULL;
last = first;
for (i = 1; i < n; i++)
{
t = (struct Node *)malloc(sizeof(struct Node));
t->data = A[i];
t->next = NULL;
last->next = t;
last = t;
}
}
int count(Node *p)
{
if(p==NULL)
return 0;
else
return count(p->next) + 1;
}
void Display(struct Node *p)
{
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
}
void Insert(struct Node *p, int index, int x)
{
struct Node *t;
int i;
if (index < 0 || index > count(p))
return;
t = (struct Node *)malloc(sizeof(struct Node));
t->data = x;
if (index == 0)
{
t->next = first;
first = t;
}
else
{
for (i = 0; i < index - 1; i++)
p = p->next;
t->next = p->next;
p->next = t;
}
}
int main()
{
int A[] = {10, 20, 30, 40, 50};
create(A, 5);
Insert(first, 0, 5);
Display(first);
return 0;
}
#include<iostream>
using namespace std;
struct Node
{
int data;
struct Node *next;
}*first=NULL; // first is a global pointer
/* -------- creating a linked list using an array ------- */
void create(int A[], int n) // n = no. of elements in the array
{
int i;
struct Node *t, *last; //'t' will help us in creating a new node, 'last' will be pointing on the last node and will help us in adding a new node in the linked list
first = new Node; // first is a pointer which is pointing to a node created in heap.
first -> data = A[0];
first -> next = NULL;
last=first;
// for the rest of the nodes we'll create them using for loop
for (i=1; i<n; i++) // we are starting from 1 as 0th element has already been created
{
t = new Node;
t -> data = A[i];
t->next=NULL;
last->next=t;
last=t;
}
}
void Display ( struct Node *p)
{
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
cout << endl;
}
void insert(struct Node *p, int pos, int x )
{
Node *t;
if(pos == 0) // inserting before 'first'
{
t = new Node;
t->data = x;
t->next = first;
first = t;
}
else if (pos > 0)
{
for (int i = 0; i<pos-1 && p; i++) //as for inserting after pos 4 we'll have to move the p pointer 3 times as initially the p pointer is pointing at first node. Also, we did '&& p' to check if p exists or not
{
p = p->next;
}
if(p)
{
t = new Node;
t->data = x;
t->next = p->next;
p->next = t;
}
}
}
int main()
{
// int A[] = {3,5,7,10,15};
// create(A, 5);
// Display(first);
insert(first, 0, 1);
insert(first, 1, 2);
insert(first, 3, 4);
insert(first, 2, 3);
Display(first);
// → 1 2 3 // notice that 4 is not printed because for it p had become NULL as we had jumped a node
return 0;
}
last
which will always point at the last node, this way we'll able to save a lot of time.first
pointer is pointing to that node then the last
pointer must also point to that first node, this is a special case that we need to take care of. 3 Different ways :
void insertLast(int x)
:#include<iostream>
using namespace std;
struct Node
{
int data;
struct Node *next;
}*first=NULL; // first is a global pointer
/* -------- creating a linked list using an array ------- */
void create(int A[], int n) // n = no. of elements in the array
{
int i;
struct Node *t, *last; //'t' will help us in creating a new node, 'last' will be pointing on the last node and will help us in adding a new node in the linked list
first = new Node; // first is a pointer which is pointing to a node created in heap.
first -> data = A[0];
first -> next = NULL;
last=first;
// for the rest of the nodes we'll create them using for loop
for (i=1; i<n; i++) // we are starting from 1 as 0th element has already been created
{
t = new Node;
t -> data = A[i];
t->next=NULL;
last->next=t;
last=t;
}
}
void Display ( struct Node *p)
{
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
cout << endl;
}
void insertLast(int x )
{
Node *last = new Node;
Node *t = new Node;
t->data = x;
t->next = NULL;
if(first == NULL) // means there are no nodes in the linked list
first = last = t;
else
{
last = first;
while(last->next)
last = last->next;
last->next = t;
last = t;
}
}
int main()
{
Display(first);
insertLast(2);
insertLast(3);
insertLast(4);
Display(first); // → 2 3 4
return 0;
}
void insertLast(struct Node *&p, int x)
:#include <iostream>
using namespace std;
struct Node
{
int data;
struct Node *next;
} *first = NULL; // first is a global pointer
/* -------- creating a linked list using an array ------- */
void create(int A[], int n) // n = no. of elements in the array
{
int i;
struct Node *t, *last; //'t' will help us in creating a new node, 'last' will be pointing on the last node and will help us in adding a new node in the linked list
first = new Node; // first is a pointer which is pointing to a node created in heap.
first->data = A[0];
first->next = NULL;
last = first;
// for the rest of the nodes we'll create them using for loop
for (i = 1; i < n; i++) // we are starting from 1 as 0th element has already been created
{
t = new Node;
t->data = A[i];
t->next = NULL;
last->next = t;
last = t;
}
}
void Display(struct Node *p)
{
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
cout << endl;
}
void insertLast(struct Node *&p, int x)
{
Node *last = new Node;
Node *t = new Node;
t->data = x;
t->next = NULL;
if (p == NULL) // means there are no nodes in the linked list
p = last = t;
else
{
last = p;
while (last->next)
last = last->next;
last->next = t;
last = t;
}
}
int main()
{
Display(first);
insertLast(first, 2);
insertLast(first, 3);
insertLast(first, 4);
Display(first); // → 2 3 4
return 0;
}
struct Node *insertLast(struct Node *p, int x)
:#include <iostream>
using namespace std;
struct Node
{
int data;
struct Node *next;
} *first = NULL; // first is a global pointer
/* -------- creating a linked list using an array ------- */
void create(int A[], int n) // n = no. of elements in the array
{
int i;
struct Node *t, *last; //'t' will help us in creating a new node, 'last' will be pointing on the last node and will help us in adding a new node in the linked list
first = new Node; // first is a pointer which is pointing to a node created in heap.
first->data = A[0];
first->next = NULL;
last = first;
// for the rest of the nodes we'll create them using for loop
for (i = 1; i < n; i++) // we are starting from 1 as 0th element has already been created
{
t = new Node;
t->data = A[i];
t->next = NULL;
last->next = t;
last = t;
}
}
void Display(struct Node *p)
{
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
cout << endl;
}
struct Node *insertLast(struct Node *p, int x)
{
Node *last = new Node;
Node *t = new Node;
t->data = x;
t->next = NULL;
if (p == NULL)
{
p = last = t;
}
else
{
last = p;
while (last->next)
last = last->next;
last->next = t;
last = t;
}
return p; // Return the updated pointer
}
int main()
{
Display(first);
first = insertLast(first, 2);
first = insertLast(first, 3);
first = insertLast(first, 4);
Display(first);
return 0;
}
#include<iostream>
using namespace std;
struct Node
{
int data;
struct Node *next;
}*first=NULL; // first is a global pointer
/* -------- creating a linked list using an array ------- */
void create(int A[], int n) // n = no. of elements in the array
{
int i;
struct Node *t, *last; //'t' will help us in creating a new node, 'last' will be pointing on the last node and will help us in adding a new node in the linked list
first = new Node; // first is a pointer which is pointing to a node created in heap.
first -> data = A[0];
first -> next = NULL;
last=first;
// for the rest of the nodes we'll create them using for loop
for (i=1; i<n; i++) // we are starting from 1 as 0th element has already been created
{
t = new Node;
t -> data = A[i];
t->next=NULL;
last->next=t;
last=t;
}
}
void Display ( struct Node *p)
{
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
cout << endl;
}
void change(Node *p)
{
for(int i=0; i<2; i++)
p = p->next;
p->data = 0;
}
int main()
{
int A[] = {3,5,7,10,15};
create(A, 5);
Display(first);
change(first);
Display(first); // → 2 3 4
return 0;
}
#include<iostream>
using namespace std;
struct Node
{
int data;
struct Node *next;
}*first=NULL; // first is a global pointer
/* -------- creating a linked list using an array ------- */
void create(int A[], int n) // n = no. of elements in the array
{
int i;
struct Node *t, *last; //'t' will help us in creating a new node, 'last' will be pointing on the last node and will help us in adding a new node in the linked list
first = new Node; // first is a pointer which is pointing to a node created in heap.
first -> data = A[0];
first -> next = NULL;
last=first;
// for the rest of the nodes we'll create them using for loop
for (i=1; i<n; i++) // we are starting from 1 as 0th element has already been created
{
t = new Node;
t -> data = A[i];
t->next=NULL;
last->next=t;
last=t;
}
}
void Display ( struct Node *p)
{
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
}
void insertSorted(Node *p, int x)
{
Node *t = new Node;
Node *q = new Node;
t->data = x;
t->next = NULL;
if(first == NULL) // if there are no nodes created yet
first = t;
else
{
while( p && p->data <= x) // do not interchange these conditions around the && operator. We've to check whether p exists or not and if it exists then only check if p-> data <=x.
{
q=p;
p=p->next;
}
if (p==first) // if we've to insert before first node
{
t->next=first;
first=t;
}
else
{
t->next=q->next;
q->next=t;
}
}
}
int main()
{
int A[]={3,5,7,10,15};
create(A, 5);
insertSorted(first, 23);
Display(first); // -> 3 5 7 10 15 23
return 0;
}
#include <iostream>
using namespace std;
struct Node
{
int data;
struct Node *next;
} *first = NULL; // first is a global pointer
/* -------- creating a linked list using an array ------- */
void create(int A[], int n) // n = no. of elements in the array
{
int i;
struct Node *t, *last; //'t' will help us in creating a new node, 'last' will be pointing on the last node and will help us in adding a new node in the linked list
first = new Node; // first is a pointer which is pointing to a node created in heap.
first->data = A[0];
first->next = NULL;
last = first;
// for the rest of the nodes we'll create them using for loop
for (i = 1; i < n; i++) // we are starting from 1 as 0th element has already been created
{
t = new Node;
t->data = A[i];
t->next = NULL;
last->next = t;
last = t;
}
}
void Display(struct Node *p)
{
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
}
int count(Node *p)
{
if (p == NULL)
return 0;
else
return count(p->next) + 1;
}
void Delete(Node *p, int pos)
{
int x = -1;
Node *q = NULL;
if (pos == 1)
{
x = first->data;
p = first;
first = first->next;
delete (p);
}
else
{
for (int i = 0; i < pos - 1; i++)
{
q = p;
p = p->next;
}
q->next = p->next;
x = p->data; // storing the value of deleted node's 'data' if we'd like to use it in the future.
free(p);
}
}
int main()
{
int A[] = {3, 5, 7, 10, 15};
create(A, 5);
Delete(first, 5);
Display(first);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
} *first = NULL;
void create(int A[], int n)
{
int i;
struct Node *t, *last;
first = (struct Node *)malloc(sizeof(struct Node));
first->data = A[0];
first->next = NULL;
last = first;
for (i = 1; i < n; i++)
{
t = (struct Node *)malloc(sizeof(struct Node));
t->data = A[i];
t->next = NULL;
last->next = t;
last = t;
}
}
void Display(struct Node *p)
{
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
}
int count(Node *p)
{
if(p==NULL)
return 0;
else
return count(p->next) + 1;
}
void RDisplay(struct Node *p)
{
if (p != NULL)
{
RDisplay(p->next);
printf("%d ", p->data);
}
}
int Delete(struct Node *p, int index)
{
struct Node *q = NULL;
int x = -1, i;
if (index < 1 || index > count(p))
return -1;
if (index == 1)
{
q = first;
x = first->data;
first = first->next;
free(q);
return x;
}
else
{
for (i = 0; i < index - 1; i++)
{
q = p;
p = p->next;
}
q->next = p->next;
x = p->data;
free(p);
return x;
}
}
int main()
{
int A[] = {10, 20, 30, 40, 50};
create(A, 5);
printf("%d\n",Delete(first,2));
Display(first);
return 0;
}
(ignore this method)
void checkSorted(Node *p) {
if(p)
{
int x, flag=0;
while (p->next != NULL)
{
x = p->data;
p = p->next;
if(x>p->data)
{
flag++;
break;
}
}
if(flag>0) printf("UnSorted");
else printf("Sorted");
}
}
// can be simplified by using return statements.
bool checkSorted(Node *p) {
int x = INT_MIN;
while (p != NULL)
{
if (x > p->data)
{
return false;
}
x=p->data;
p=p->next;
}
return true;
}
p
and q
, p
is the trailing pointer) on consecutive nodes and check if the data
is same or not.p
point to node after q
and then delete the node q
.q
point to node after p
;Code :
#include <iostream>
using namespace std;
struct Node
{
int data;
struct Node *next;
} *first = NULL; // first is a global pointer
/* -------- creating a linked list using an array ------- */
void create(int A[], int n) // n = no. of elements in the array
{
int i;
struct Node *t, *last; //'t' will help us in creating a new node, 'last' will be pointing on the last node and will help us in adding a new node in the linked list
first = new Node; // first is a pointer which is pointing to a node created in heap.
first->data = A[0];
first->next = NULL;
last = first;
// for the rest of the nodes we'll create them using for loop
for (i = 1; i < n; i++) // we are starting from 1 as 0th element has already been created
{
t = new Node;
t->data = A[i];
t->next = NULL;
last->next = t;
last = t;
}
}
void Display(struct Node *p)
{
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
cout << endl;
}
int count(Node *p)
{
if (p == NULL)
return 0;
else
return count(p->next) + 1;
}
void removeDuplicateInSorted (Node *p)
{
Node *q=p->next;
while (q != NULL)
{
if(p->data != q->data)
{
p=q;
q=q->next;
}
else
{
p->next = q->next;
free (q);
q=p->next;
}
}
}
int main()
{
int A[] = {3, 5, 7, 10, 10, 10, 15};
create(A, 7); // → 3 5 7 10 10 10 15
Display(first);
removeDuplicateInSorted(first);
Display(first); // → 3 5 7 10 15
return 0;
}
2
and 8
will be interchanged but the nodes will remain the same.300
should be pointing to address 250
, 250
should be pointing to address 210
and 210
should be pointing to address 200
.300
node should become the first
node.In a linked list we prefer Reversing Links rather than Reversing Elements. Because data
in a linked list need not be an integer always. It can be a record too, for e.g. a node can have roll no.
, name
, class
, etc... therefore it is easy to Reverse Links instead of reversing elements.
#include <iostream>
using namespace std;
struct Node
{
int data;
struct Node *next;
} *first = NULL; // first is a global pointer
/* -------- creating a linked list using an array ------- */
void create(int A[], int n) // n = no. of elements in the array
{
int i;
struct Node *t, *last; //'t' will help us in creating a new node, 'last' will be pointing on the last node and will help us in adding a new node in the linked list
first = new Node; // first is a pointer which is pointing to a node created in heap.
first->data = A[0];
first->next = NULL;
last = first;
// for the rest of the nodes we'll create them using for loop
for (i = 1; i < n; i++) // we are starting from 1 as 0th element has already been created
{
t = new Node;
t->data = A[i];
t->next = NULL;
last->next = t;
last = t;
}
}
void Display(struct Node *p)
{
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
cout << endl;
}
int count(Node *p)
{
if (p == NULL)
return 0;
else
return count(p->next) + 1;
}
void reverseElements(Node *p)
{
int i =0;
int *A = new int[count(p)];
Node *q = p;
while(q!=NULL)
{
A[i] = q->data;
q=q->next;
i++;
}
q=p;
i--;
while(q!=NULL)
{
q->data = A[i];
q=q->next;
i--;
}
}
int main()
{
int A[] = {2,4,6,8};
create(A, 4);
Display(first); // → 2 4 6 8
reverseElements(first);
Display(first); // → 8 6 4 2
return 0;
}
q
will point to will have it's next
changed so that it starts pointing to r
, then p q r
will shift ahead and again q
's next will be made to point r
. The cycle continues...Code :
#include <iostream>
using namespace std;
struct Node
{
int data;
struct Node *next;
} *first = NULL; // first is a global pointer
/* -------- creating a linked list using an array ------- */
void create(int A[], int n) // n = no. of elements in the array
{
int i;
struct Node *t, *last; //'t' will help us in creating a new node, 'last' will be pointing on the last node and will help us in adding a new node in the linked list
first = new Node; // first is a pointer which is pointing to a node created in heap.
first->data = A[0];
first->next = NULL;
last = first;
// for the rest of the nodes we'll create them using for loop
for (i = 1; i < n; i++) // we are starting from 1 as 0th element has already been created
{
t = new Node;
t->data = A[i];
t->next = NULL;
last->next = t;
last = t;
}
}
void Display(struct Node *p)
{
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
cout << endl;
}
int count(Node *p)
{
if (p == NULL)
return 0;
else
return count(p->next) + 1;
}
void reverseLinks(Node *p)
{
Node *q, *r;
q = r = NULL;
while (p!=NULL)
{
r=q;
q=p;
p=p->next;
q->next = r;
}
first = q;
}
int main()
{
int A[] = {2,4,6,8};
create(A, 4);
Display(first); // → 2 4 6 8
reverseLinks(first);
Display(first); // → 8 6 4 2
return 0;
}
#include <iostream>
using namespace std;
struct Node
{
int data;
struct Node *next;
} *first = NULL; // first is a global pointer
/* -------- creating a linked list using an array ------- */
void create(int A[], int n) // n = no. of elements in the array
{
int i;
struct Node *t, *last; //'t' will help us in creating a new node, 'last' will be pointing on the last node and will help us in adding a new node in the linked list
first = new Node; // first is a pointer which is pointing to a node created in heap.
first->data = A[0];
first->next = NULL;
last = first;
// for the rest of the nodes we'll create them using for loop
for (i = 1; i < n; i++) // we are starting from 1 as 0th element has already been created
{
t = new Node;
t->data = A[i];
t->next = NULL;
last->next = t;
last = t;
}
}
void Display(struct Node *p)
{
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
cout << endl;
}
int count(Node *p)
{
if (p == NULL)
return 0;
else
return count(p->next) + 1;
}
void reverseRecursive(Node *q, Node *p)
{
if(p != NULL)
{
reverseRecursive(p, p->next);
p->next = q;
}
else
{
first = q;
}
}
int main()
{
int A[] = {2,4,6,8};
create(A, 4);
Display(first); // → 2 4 6 8
reverseRecursive(NULL, first);
Display(first); // → 8 6 4 2
return 0;
}
Procedure : We have to reach the end of the Linked List and make the last node point to the 2nd Linked List.
#include <iostream>
using namespace std;
struct Node
{
int data;
struct Node *next;
} *first = NULL, *second = NULL, *third=NULL; // first is a global pointer
/* -------- creating a linked list using an array ------- */
void create(int A[], int n) // n = no. of elements in the array
{
int i;
struct Node *t, *last; //'t' will help us in creating a new node, 'last' will be pointing on the last node and will help us in adding a new node in the linked list
first = new Node; // first is a pointer which is pointing to a node created in heap.
first->data = A[0];
first->next = NULL;
last = first;
// for the rest of the nodes we'll create them using for loop
for (i = 1; i < n; i++) // we are starting from 1 as 0th element has already been created
{
t = new Node;
t->data = A[i];
t->next = NULL;
last->next = t;
last = t;
}
}
void create2(int A[], int n) // n = no. of elements in the array
{
int i;
struct Node *t, *last; //'t' will help us in creating a new node, 'last' will be pointing on the last node and will help us in adding a new node in the linked list
second = new Node; // second is a pointer which is pointing to a node created in heap.
second->data = A[0];
second->next = NULL;
last = second;
// for the rest of the nodes we'll create them using for loop
for (i = 1; i < n; i++) // we are starting from 1 as 0th element has already been created
{
t = new Node;
t->data = A[i];
t->next = NULL;
last->next = t;
last = t;
}
}
void Display(struct Node *p)
{
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
cout << endl;
}
int count(Node *p)
{
if (p == NULL)
return 0;
else
return count(p->next) + 1;
}
void concat(Node *p, Node *q)
{
third = p;
while (p->next != NULL)
p=p->next;
p->next = q;
}
int main()
{
int A[] = {50,40,10,30,20};
int B[] = {1,2,3,4,5};
create(A, 5);
create2(B, 5);
Display(first); // → 50 40 10 30 20
Display(second); // → 1 2 3 4 5
concat(first, second);
Display(first); // → 50 40 10 30 20 1 2 3 4 5
Display(third);// → 50 40 10 30 20 1 2 3 4 5
return 0;
}
Remember that we learned about Merging in Arrays as well, there we had to make use of the third new array as well but here in Linked List we do not require any extra LL or Array.
However we do require a extra pointers (other than first and second pointers).
We'll take two extra Pointers third
& last
. third
will point to the starting of the merged LL and last
will point to the end of the merged LL.
third
and last
upon it.first
ahead then make last->next = NULL;
(remember that third is pointing to 2
now)first
& second
and make last->next
point to whichever is smaller. (here 4 < 8 so last-> next = second
)second
ahead and then last->next = NULL
Now,
Final Code :
#include <iostream>
using namespace std;
struct Node
{
int data;
struct Node *next;
} *first = NULL, *second = NULL, *third = NULL; // first is a global pointer
/* -------- creating a linked list using an array ------- */
void create(int A[], int n) // n = no. of elements in the array
{
int i;
struct Node *t, *last; //'t' will help us in creating a new node, 'last' will be pointing on the last node and will help us in adding a new node in the linked list
first = new Node; // first is a pointer which is pointing to a node created in heap.
first->data = A[0];
first->next = NULL;
last = first;
// for the rest of the nodes we'll create them using for loop
for (i = 1; i < n; i++) // we are starting from 1 as 0th element has already been created
{
t = new Node;
t->data = A[i];
t->next = NULL;
last->next = t;
last = t;
}
}
void create2(int A[], int n) // n = no. of elements in the array
{
int i;
struct Node *t, *last; //'t' will help us in creating a new node, 'last' will be pointing on the last node and will help us in adding a new node in the linked list
second = new Node; // second is a pointer which is pointing to a node created in heap.
second->data = A[0];
second->next = NULL;
last = second;
// for the rest of the nodes we'll create them using for loop
for (i = 1; i < n; i++) // we are starting from 1 as 0th element has already been created
{
t = new Node;
t->data = A[i];
t->next = NULL;
last->next = t;
last = t;
}
}
void Display(struct Node *p)
{
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
cout << endl;
}
int count(Node *p)
{
if (p == NULL)
return 0;
else
return count(p->next) + 1;
}
void merge(Node *p, Node *q)
{
Node *last = NULL;
if (p->data < q->data)
{
third = last = p;
p = p->next;
third->next = NULL;
}
else
{
third = last = q;
q = q->next;
last->next = NULL;
}
while (p && q)
{
if (p->data < q->data)
{
last->next=p;
last=p;
p=p->next;
last->next = NULL;
}
else
{
last ->next = q;
last = q;
q=q->next;
last->next=NULL;
}
}
if(p) last->next = p;
if(q) last->next = q;
}
int main()
{
int A[] = {50, 40, 10, 30, 20};
int B[] = {1, 2, 3, 5, 4};
create(A, 5);
create2(B, 5);
Display(first);
Display(second);
merge(first, second);
Display(third); // → 1 2 3 5 4 50 40 10 30 20
return 0;
}
p
and q
.p
will move by 1 step and q
will move by two steps.p
& q
meet again then a loop exists.q
will become null first as it is moving faster) then the LL is linear.Suppose we have two cars with different speeds then, on a Linear Track these cars will never meet again but on a Circular Track they will meet again. Same concept is being used here.
while
statement).#include <iostream>
using namespace std;
struct Node
{
int data;
struct Node *next;
} *first = NULL; // first is a global pointer
/* -------- creating a linked list using an array ------- */
void create(int A[], int n) // n = no. of elements in the array
{
int i;
struct Node *t, *last; //'t' will help us in creating a new node, 'last' will be pointing on the last node and will help us in adding a new node in the linked list
first = new Node; // first is a pointer which is pointing to a node created in heap.
first->data = A[0];
first->next = NULL;
last = first;
// for the rest of the nodes we'll create them using for loop
for (i = 1; i < n; i++) // we are starting from 1 as 0th element has already been created
{
t = new Node;
t->data = A[i];
t->next = NULL;
last->next = t;
last = t;
}
}
void Display(struct Node *p)
{
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
cout << endl;
}
int count(Node *p)
{
if (p == NULL)
return 0;
else
return count(p->next) + 1;
}
int isLoop (Node *f)
{
Node *p, *q;
p = q = f;
do
{
p=p->next;
q=q->next;
q=q? q->next : q;
}while(p && q && p!=q);
if(p==q)
return 1;
else
return 0;
}
int main()
{
int A[] = {10, 20, 30, 40, 50};
create(A, 5);
Display(first);
/* ------------------- to form a loop: ------------------ */
Node *t1, *t2;
t1=first->next->next; // t1 is now pointing at 30
t2=first->next->next->next->next; // t2 is now pointing at 50
t2->next = t1;
/* ------------------------------------------------------ */
cout << isLoop(first); // → 1
return 0;
}
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node *next;
};
class LinkedList
{
private:
Node *first;
public:
// constructor function
LinkedList()
{
first = NULL;
}
// constructor for creating a Linked List using an array
LinkedList(int A[], int n);
~LinkedList(); // Destructor function
void Display();
void Insert(int index, int x);
int Delete(int index);
int Length();
};
LinkedList::LinkedList(int A[], int n)
{
Node *last, *t;
int i = 0;
first = new Node;
first->data = A[0];
first->next = NULL;
last = first;
for (i = 1; i < n; i++)
{
t = new Node;
t->data = A[i];
t->next = NULL;
last->next = t;
last = t;
}
}
LinkedList::~LinkedList()
{
Node *p = first;
while (first)
{
first = first->next;
delete p;
p = first;
}
}
void LinkedList::Display()
{
Node *p = first;
while (p)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
int LinkedList::Length()
{
Node *p = first;
int len = 0;
while (p)
{
len++;
p = p->next;
}
return len;
}
void LinkedList::Insert(int index, int x)
{
Node *t, *p = first;
if (index < 0 || index > Length())
return;
t = new Node;
t->data = x;
t->next = NULL;
if (index == 0)
{
t->next = first;
first = t;
}
else
{
for (int i = 0; i < index - 1; i++)
p = p->next;
t->next = p->next;
p->next = t;
}
}
int LinkedList::Delete(int index)
{
Node *p, *q = NULL;
int x = -1;
if (index < 1 || index > Length())
return -1;
if (index == 1)
{
p = first;
first = first->next;
x = p->data;
delete p;
}
else
{
p = first;
for (int i = 0; i < index - 1; i++)
{
q = p;
p = p->next;
}
q->next = p->next;
x = p->data;
delete p;
}
return x;
}
int main()
{
int A[] = {1, 2, 3, 4, 5};
LinkedList l(A, 5);
l.Insert(3, 10);
l.Display();
return 0;
}
Linked List where the last node points at the first node is called a circular Linked List.
Here since the LL is circular so there is no first node.
So we name the pointer head
instead of first
.
A Circular LL can't be NULL even if it is empty.
Another representation of Circular LL :
head
node is not used for storing data.#include <iostream>
using namespace std;
struct Node
{
int data;
Node *next;
} *head;
// creating a Circular Linked List
void create(int A[], int n)
{
int i;
struct Node *t, *last;
head = new Node;
head->data = A[0];
head->next = head;
last = head;
for (int i = 1; i < n; i++)
{
t = new Node;
t->data = A[i];
t->next = last->next;
last->next = t;
last = t;
}
}
void display(Node *h)
{
do
{
printf("%d ", h->data);
h=h->next;
} while (h != head);
}
int main()
{
int A[] = {2, 3, 4, 5, 6};
create(A, 5);
display(head);
return 0;
}
#include <iostream>
using namespace std;
struct Node
{
int data;
Node *next;
} *head;
// creating a Circular Linked List
void create(int A[], int n)
{
int i;
struct Node *t, *last;
head = new Node;
head->data = A[0];
head->next = head;
last = head;
for (int i = 1; i < n; i++)
{
t = new Node;
t->data = A[i];
t->next = last->next;
last->next = t;
last = t;
}
}
void recursiveDisplay(Node *h)
{
static int flag = 0;
if (h != head || flag == 0)
{
flag = 1;
printf("%d ", h->data);
recursiveDisplay(h->next);
}
}
int main()
{
int A[] = {2, 3, 4, 5, 6};
create(A, 5);
recursiveDisplay(head);
return 0;
}
5
(see image above) so the node will be added after index 5
which will technically be before head
. But we'll see another method for it as well where we won't have to specify the index : head
in a Circular Linked List :See the NOTE above as well please.
t
which we desire to insert before the head
node.t->next = head;
next
point to t
t
) as head
? Logically you don't have to.#include <iostream>
using namespace std;
struct Node
{
int data;
Node *next;
} *head;
// creating a Circular Linked List
void create(int A[], int n)
{
int i;
struct Node *t, *last;
head = new Node;
head->data = A[0];
head->next = head;
last = head;
for (int i = 1; i < n; i++)
{
t = new Node;
t->data = A[i];
t->next = last->next;
last->next = t;
last = t;
}
}
void display(Node *h)
{
do
{
printf("%d ", h->data);
h=h->next;
} while (h != head);
}
int length(Node *p)
{
int len = 0;
do
{
len++;
p=p->next;
}while(p != head);
return len;
}
void insert (Node *p, int index, int x)
{
if(index < 0 || index > length(head)) return;
Node *t;
int i;
if(index == 0)
{
t = new Node;
t->data = x;
if(head == NULL)
{
head = t;
head->next = NULL;
}
else
{
t->next = head;
while (p->next != head) p=p->next;
p->next = t;
head = t;
}
}
else
{
t = new Node;
t->data = x;
for (int i=0; i < index-1; i++)
p=p->next;
t->next = p->next;
p->next = t;
}
}
int main()
{
int A[] = {2, 3, 4, 5, 6};
create(A, 5);
// cout << length(head);
insert(head, 2, 1); // inserting value 1 after value 3
display(head); // → 2 3 1 4 5 6
return 0;
}
#include <iostream>
using namespace std;
struct Node
{
int data;
Node *next;
} *head;
// creating a Circular Linked List
void create(int A[], int n)
{
int i;
struct Node *t, *last;
head = new Node;
head->data = A[0];
head->next = head;
last = head;
for (int i = 1; i < n; i++)
{
t = new Node;
t->data = A[i];
t->next = last->next;
last->next = t;
last = t;
}
}
void display(Node *h)
{
do
{
printf("%d ", h->data);
h=h->next;
} while (h != head);
}
int length(Node *p)
{
int len = 0;
do
{
len++;
p=p->next;
}while(p != head);
return len;
}
int Delete ( Node*p, int index)
{
Node *q;
int i, x;
if(index < 0 || index > length(head))
return -1;
if(index==0)
{
while(p->next != head) p=p->next;
x= head->data;
if(p==head)
{
free(head);
head=NULL;
}
else
{
p->next=head->next;
free(head);
head=p->next;
}
}
else
{
for (i=0 ; i<index-1; i++)
p=p->next;
q=p->next;
p->next = q->next;
x = q->data;
free(q);
}
return x;
}
int main()
{
int A[] = {2, 3, 4, 5, 6};
create(A, 5);
// display(head);
cout << Delete(head, 1); // → 3
return 0;
}
#include<iostream>
using namespace std;
struct Node
{
struct Node *prev;
int data;
struct Node *next;
}* first = NULL;
void create (int A[], int n)
{
struct Node *t, *last;
int i;
first = new Node;
first -> data = A[0];
first->prev = first->next=NULL;
last = first;
for(i=1; i<n; i++)
{
t= new Node;
t->data=A[i];
t->next=last->next; // i.e NULL or Simply take NULL instead of last->next
t->prev=last;
last->next=t;
last = t;
}
}
void Display ( Node *p)
{
while(p)
{
printf("%d ", p->data);
p=p->next;
}
cout << endl;
}
int Length(Node *p)
{
int len = 0;
while (p)
{
len++;
p=p->next;
}
return len;
}
int main()
{
int A[] = {10, 20, 30, 40, 50};
create(A, 5);
Display(first);
cout << Length(first);
return 0;
}
Two Cases :
Complete Code :
#include<iostream>
using namespace std;
struct Node
{
struct Node *prev;
int data;
struct Node *next;
}* first = NULL;
void create (int A[], int n)
{
struct Node *t, *last;
int i;
first = new Node;
first -> data = A[0];
first->prev = first->next=NULL;
last = first;
for(i=1; i<n; i++)
{
t= new Node;
t->data=A[i];
t->next=last->next;
t->prev=last;
last->next=t;
last = t;
}
}
void Display ( Node *p)
{
while(p)
{
printf("%d ", p->data);
p=p->next;
}
cout << endl;
}
int Length(Node *p)
{
int len = 0;
while (p)
{
len++;
p=p->next;
}
return len;
}
void Insert(Node *p, int index, int x)
{
Node *t;
int i;
if(index<0 || index>Length(p))
return;
if(index == 0)
{
t = new Node;
t->data = x;
t->prev=NULL;
t->next=first;
first->prev = t;
first = t;
}
else
{
for (int i = 0; i<index-1; i++)
p=p->next;
t=new Node;
t->data = x;
t->prev = p;
t->next = p->next;
if(p->next) p->next->prev = t;
p->next = t;
}
}
int main()
{
int A[] = {10, 20, 30, 40, 50};
create(A, 5);
Insert(first, 0, 23);
Display(first); // → 23 10 20 30 40 50
return 0;
}
#include<iostream>
using namespace std;
struct Node
{
struct Node *prev;
int data;
struct Node *next;
}* first = NULL;
void create (int A[], int n)
{
struct Node *t, *last;
int i;
first = new Node;
first -> data = A[0];
first->prev = first->next=NULL;
last = first;
for(i=1; i<n; i++)
{
t= new Node;
t->data=A[i];
t->next=last->next;
t->prev=last;
last->next=t;
last = t;
}
}
void Display ( Node *p)
{
while(p)
{
printf("%d ", p->data);
p=p->next;
}
cout << endl;
}
int Length(Node *p)
{
int len = 0;
while (p)
{
len++;
p=p->next;
}
return len;
}
int Delete (Node *p, int index)
{
Node *q;
int x = -1, i;
if(index < 1 || index > Length(p))
return -1;
if(index == 1)
{
first = first -> next;
if(first) first->prev = NULL;
x=p->data;
free(p);
}
else
{
for (i=0; i<index-1; i++)
p=p->next;
p->prev->next = p->next;
if(p->next)
p->next->prev = p->prev;
x=p->data;
free(p);
}
return x;
}
int main()
{
int A[] = {10, 20, 30, 40, 50};
create(A, 5);
Delete(first, 1); // → 20 30 40 50
Display(first);
return 0;
}
#include<iostream>
using namespace std;
struct Node
{
struct Node *prev;
int data;
struct Node *next;
}* first = NULL;
void create (int A[], int n)
{
struct Node *t, *last;
int i;
first = new Node;
first -> data = A[0];
first->prev = first->next=NULL;
last = first;
for(i=1; i<n; i++)
{
t= new Node;
t->data=A[i];
t->next=last->next;
t->prev=last;
last->next=t;
last = t;
}
}
void Display ( Node *p)
{
while(p)
{
printf("%d ", p->data);
p=p->next;
}
cout << endl;
}
int Length(Node *p)
{
int len = 0;
while (p)
{
len++;
p=p->next;
}
return len;
}
void Reverse(Node *p)
{
Node *temp;
while(p!=NULL)
{
temp = p->next;
p->next = p->prev;
p->prev = temp;
p=p->prev;
if(p!= NULL && p->next==NULL)
first=p;
}
}
int main()
{
int A[] = {10, 20, 30, 40, 50};
create(A, 5);
Reverse(first);
Display(first); // → 50 40 30 20 10
return 0;
}