Created
October 29, 2020 15:15
-
-
Save DARK-art108/55f313ccaf2a31a3f760aa3c675506dc 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<bits/stdc++.h> | |
#include <ext/pb_ds/assoc_container.hpp> | |
using namespace __gnu_pbds; | |
using namespace std; | |
#define ff first | |
#define ss second | |
#define in cin | |
#define out cout | |
#define int long long | |
#define pb push_back | |
#define mp make_pair | |
#define pii pair<int,int> | |
#define vi vector<int> | |
#define mii map<int,int> | |
#define pqb priority_queue<int> | |
#define pqs priority_queue<int,vi,greater<int> > | |
#define setbits(x) __builtin_popcountll(x) | |
#define zrobits(x) __builtin_ctzll(x) | |
#define rep(i, n) for(int i = 0; i < n; i++) | |
#define REP(i,a,b) for(int i=a;i<b;i++) | |
#define rep1(i,b) for(int i=1;i<=b;i++) | |
#define mod 1000000007 | |
#define inf 1e18 | |
#define ps(x,y) fixed<<setprecision(y)<<x | |
#define mk(arr,n,type) type *arr=new type[n]; | |
#define w(x) int x; cin>>x; while(x--) | |
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); | |
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; | |
void c_p_c() | |
{ | |
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); | |
#ifndef ONLINE_JUDGE | |
freopen("input.txt", "r", stdin); | |
freopen("output.txt", "w", stdout); | |
#endif | |
} | |
struct Node | |
{ | |
int data; | |
struct Node * next; | |
}; | |
void Linked_List_Traverse(struct Node *ptr) | |
{ | |
while (ptr != NULL) | |
{ | |
cout << "Elements in Linked List: " << ptr -> data << "\n"; | |
ptr = ptr -> next; | |
} | |
} | |
struct Node * insertAtFirst(struct Node *head, int data) | |
{ | |
struct Node * ptr = (struct Node *) malloc(sizeof(struct Node)); | |
ptr -> next = head; //ptr.next ptr.data ptr | |
ptr -> data = data; | |
return ptr; | |
} | |
struct Node * insertAtIndex(struct Node *head, int data, int index) | |
{ | |
struct Node * ptr = (struct Node *) malloc(sizeof(struct Node)); | |
struct Node * p = head; | |
int i = 0; | |
while (i != index - 1) | |
{ | |
p = p -> next; //p.next | |
i++; | |
} | |
ptr -> data = data; | |
ptr -> next = p -> next; | |
p -> next = ptr; | |
return head; | |
}; | |
struct Node * insertAtEnd(struct Node *head, int data) | |
{ | |
struct Node *ptr = (struct Node*) malloc(sizeof(struct Node)); | |
struct Node *p = head; | |
ptr -> data = data; | |
while (p -> next != NULL) | |
{ | |
p = p -> next; | |
} | |
p -> next = ptr; | |
ptr -> next = NULL; | |
return head; | |
}; | |
struct Node * insertAfterNode(struct Node *head, struct Node *prevnode, int data) | |
{ | |
struct Node *ptr = (struct Node *) malloc(sizeof(struct Node)); | |
ptr -> data = data; | |
ptr -> next = prevnode -> next; | |
prevnode -> next = ptr; | |
return head; | |
}; | |
/*Deletion in a linked list*/ | |
struct Node * del_FirstNode(struct Node *head) | |
{ | |
struct Node * ptr = head; // ptr = head | |
head = head -> next; | |
free(ptr); | |
return head; | |
}; | |
struct Node * del_AtIndex(struct Node *head, int index) | |
{ | |
struct Node * p = head; | |
struct Node * q = head -> next; | |
for (int i = 0; i < index - 1; i++) | |
{ | |
/* code */ | |
p = p -> next; | |
q = q -> next; | |
} | |
p -> next = q -> next; | |
free(q); | |
return head; | |
}; | |
struct Node * del_AtLast(struct Node *head) | |
{ | |
struct Node * p = head; | |
struct Node * q = head -> next; | |
while (q -> next != NULL) | |
{ | |
/* code */ | |
p = p -> next; | |
q = q -> next; | |
} | |
p ->next = NULL; | |
free(q); | |
return head; | |
}; | |
struct Node * del_thevalue(struct Node *head, int value) | |
{ | |
struct Node * p = head; | |
struct Node * q = head -> next; | |
while (q -> data != value && q -> next != NULL) | |
{ | |
/* code */ | |
p = p -> next; | |
q = q -> next; | |
} | |
if (q -> data == value) | |
{ | |
/* code */ | |
p -> next = q -> next; | |
free(q); | |
} | |
return head; | |
}; | |
//int *ptr; | |
int32_t main() | |
{ | |
c_p_c(); | |
struct Node * head; | |
struct Node * second; | |
struct Node * third; | |
struct Node * fourth; | |
/*Allocating memory in heap*/ | |
head = (struct Node *) malloc(sizeof(struct Node)); | |
second = (struct Node *) malloc(sizeof(struct Node)); | |
third = (struct Node *) malloc(sizeof(struct Node)); | |
fourth = (struct Node *) malloc(sizeof(struct Node)); | |
/*Link first and second node*/ | |
head -> data = 7; | |
head -> next = second; | |
/*Link second and third*/ | |
second -> data = 5; | |
second -> next = third; | |
third -> data = 11; | |
third -> next = fourth; | |
fourth -> data = 16; | |
fourth -> next = NULL; | |
cout << "\nLinked list before Insertion:\n\n"; | |
Linked_List_Traverse(head); | |
cout << "\n<-------------------------------->\n"; | |
cout << "\nLinked list after Insertion at first:\n\n"; | |
head = insertAtFirst(head, 3); | |
Linked_List_Traverse(head); | |
cout << "\n<-------------------------------->\n"; | |
cout << "\nLinked list after Insertion at Index:\n\n"; | |
head = insertAtIndex(head , 56, 3); | |
Linked_List_Traverse(head); | |
cout << "\n<-------------------------------->\n"; | |
cout << "\nLinked list after Insertion at End:\n\n"; | |
head = insertAtEnd(head, 122); | |
Linked_List_Traverse(head); | |
cout << "\n<-------------------------------->\n"; | |
cout << "\nLinked list after Insertion after a Node:\n\n"; | |
head = insertAfterNode(head, second, 199); | |
Linked_List_Traverse(head); | |
cout << "\n<-------------------------------->\n"; | |
cout << "\nLinked list before deletion:\n\n"; | |
Linked_List_Traverse(head); | |
cout << "\n<-------------------------------->\n"; | |
cout << "\nLinked List after deletion of first Node:\n\n"; | |
head = del_FirstNode(head); | |
Linked_List_Traverse(head); | |
cout << "\n<-------------------------------->\n"; | |
cout << "\nLinked List after deletion of Index Node:\n\n"; | |
head = del_AtIndex(head, 3); | |
Linked_List_Traverse(head); | |
cout << "\n<-------------------------------->\n"; | |
cout << "\nLinked List after deletion of Last Node:\n\n"; | |
head = del_AtLast(head); | |
Linked_List_Traverse(head); | |
cout << "\n<-------------------------------->\n"; | |
cout << "\nLinked List after deletion of value:\n\n"; | |
head = del_thevalue(head, 11); | |
Linked_List_Traverse(head); | |
return 0; | |
} |
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
Linked list before Insertion: | |
Elements in Linked List: 7 | |
Elements in Linked List: 5 | |
Elements in Linked List: 11 | |
Elements in Linked List: 16 | |
<--------------------------------> | |
Linked list after Insertion at first: | |
Elements in Linked List: 3 | |
Elements in Linked List: 7 | |
Elements in Linked List: 5 | |
Elements in Linked List: 11 | |
Elements in Linked List: 16 | |
<--------------------------------> | |
Linked list after Insertion at Index: | |
Elements in Linked List: 3 | |
Elements in Linked List: 7 | |
Elements in Linked List: 5 | |
Elements in Linked List: 56 | |
Elements in Linked List: 11 | |
Elements in Linked List: 16 | |
<--------------------------------> | |
Linked list after Insertion at End: | |
Elements in Linked List: 3 | |
Elements in Linked List: 7 | |
Elements in Linked List: 5 | |
Elements in Linked List: 56 | |
Elements in Linked List: 11 | |
Elements in Linked List: 16 | |
Elements in Linked List: 122 | |
<--------------------------------> | |
Linked list after Insertion after a Node: | |
Elements in Linked List: 3 | |
Elements in Linked List: 7 | |
Elements in Linked List: 5 | |
Elements in Linked List: 199 | |
Elements in Linked List: 56 | |
Elements in Linked List: 11 | |
Elements in Linked List: 16 | |
Elements in Linked List: 122 | |
<--------------------------------> | |
Linked list before deletion: | |
Elements in Linked List: 3 | |
Elements in Linked List: 7 | |
Elements in Linked List: 5 | |
Elements in Linked List: 199 | |
Elements in Linked List: 56 | |
Elements in Linked List: 11 | |
Elements in Linked List: 16 | |
Elements in Linked List: 122 | |
<--------------------------------> | |
Linked List after deletion of first Node: | |
Elements in Linked List: 7 | |
Elements in Linked List: 5 | |
Elements in Linked List: 199 | |
Elements in Linked List: 56 | |
Elements in Linked List: 11 | |
Elements in Linked List: 16 | |
Elements in Linked List: 122 | |
<--------------------------------> | |
Linked List after deletion of Index Node: | |
Elements in Linked List: 7 | |
Elements in Linked List: 5 | |
Elements in Linked List: 199 | |
Elements in Linked List: 11 | |
Elements in Linked List: 16 | |
Elements in Linked List: 122 | |
<--------------------------------> | |
Linked List after deletion of Last Node: | |
Elements in Linked List: 7 | |
Elements in Linked List: 5 | |
Elements in Linked List: 199 | |
Elements in Linked List: 11 | |
Elements in Linked List: 16 | |
<--------------------------------> | |
Linked List after deletion of value: | |
Elements in Linked List: 7 | |
Elements in Linked List: 5 | |
Elements in Linked List: 199 | |
Elements in Linked List: 16 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment