Advertisement
Eternoseeker

Singly linked list

Nov 23rd, 2022
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.92 KB | Source Code | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. struct node{
  4.     int data;
  5.     node* next;
  6. };
  7. class LIST{
  8. public:
  9.     node* head;
  10.  
  11.     LIST(){
  12.             head = NULL;
  13.     }
  14.     void insertAtTheHead(int value);
  15.     void insertAtTheEnd(int value);
  16.     void insertAtTheMidAfterVal(int value);
  17.     void insertAtTheMidAfterNode(node* prev_node);
  18.     void insertAtTheMidBeforeNode(node* next_node);
  19.     void insertAfterValue(int value, int afterValue);
  20.     void printList(){
  21.         node* temp = head;
  22.         while(temp != NULL){
  23.             cout<<temp->data<<" ->";
  24.             temp=temp->next;
  25.         }
  26.         cout<<endl;
  27.         cout<<"Printing done.."<<endl;
  28.     }
  29.     void deleteAValue(int value){
  30.         cout<<"Welcome to deletevalue: "<<value<<endl;
  31.         node *nodeToDelete = head, *prev = NULL;
  32.  
  33.         if(nodeToDelete == NULL){
  34.             cout<<"Empty List, can't delete"<<endl;
  35.             return;
  36.         }
  37.  
  38.         if(nodeToDelete->data == value){
  39.             head = head->next;
  40.             free(nodeToDelete);
  41.             return;
  42.         }
  43.  
  44.         while(nodeToDelete->data != value && nodeToDelete!=NULL){
  45.             prev = nodeToDelete;
  46.             nodeToDelete = nodeToDelete->next;
  47.         }
  48.         if(nodeToDelete==NULL){
  49.             cout<<"Data not found in Linked List; Can't delete";
  50.             return;
  51.         }
  52.         else{
  53.             prev->next = nodeToDelete->next;
  54.             free(nodeToDelete);
  55.  
  56.         }
  57.  
  58.     }
  59.  
  60. };
  61.  
  62. void LIST::insertAtTheHead(int value){
  63.     node* new_node = (node*)malloc(sizeof(node));
  64.     new_node->data = value;
  65.     new_node->next = head;
  66.     head = new_node;
  67.  
  68. }
  69. void LIST::insertAtTheEnd(int value){
  70.     node *nn = new node;
  71.     nn->data = value;
  72.     nn->next = NULL;
  73.  
  74.     node* temp = head;
  75.     if(head == NULL){
  76.         head=nn;
  77.     }
  78.     else{
  79.         while(temp->next!=NULL){
  80.             temp = temp->next;
  81.         }
  82.         temp->next = nn;
  83.     }
  84. }
  85. void LIST::insertAfterValue(int value, int afterValue){
  86.     node* nn = new node;
  87.     nn->data = value;
  88.     nn->next = NULL;
  89.  
  90.     node* temp = head;
  91.     while(temp->data!=afterValue){
  92.  
  93.         temp = temp->next;
  94.         if(temp==NULL){
  95.             cout<<afterValue<<" not found in the list"<<endl;
  96.         return;
  97.         }
  98.     }
  99.     nn->next = temp->next;
  100.     temp->next = nn;
  101.  
  102. }
  103. int main(){
  104.     cout<<"Welcome to the world of Singly Linked List.."<<endl;
  105.     LIST mylist;
  106.     mylist.insertAtTheHead(5);
  107.     mylist.insertAtTheEnd(10);
  108.     mylist.insertAtTheEnd(15);
  109.     mylist.insertAtTheEnd(25);
  110.     mylist.insertAtTheHead(1);
  111.  
  112.     mylist.printList();
  113.  
  114.     mylist.insertAfterValue(20,15);
  115.     mylist.printList();
  116.  
  117.     mylist.insertAfterValue(2,100);
  118.     mylist.printList();
  119.  
  120.     mylist.insertAfterValue(2,1);
  121.     mylist.printList();
  122.  
  123.     mylist.deleteAValue(15);
  124.     mylist.printList();
  125.  
  126.     mylist.deleteAValue(1);
  127.     mylist.printList();
  128.     return 0;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement