Advertisement
fuadnafiz98

Single Linked list

Apr 5th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <string>
  4. #include <string.h>
  5.  
  6. using namespace std;
  7.  
  8. struct Node{
  9.     int val;
  10.     Node *next;
  11. };
  12. struct linkedlist
  13. {
  14.   private:
  15.     Node *head = NULL;
  16.   public:
  17.     void push_front(int v){
  18.         Node *temp = new Node;
  19.         temp->val = v;
  20.         temp->next = NULL;
  21.         if (head == NULL){
  22.             head = temp;
  23.         }
  24.         else{
  25.             temp->next = head;
  26.             head = temp;
  27.         }
  28.     }
  29.     void display()
  30.     {
  31.         if (head == NULL){
  32.             cout << "no element." << endl;
  33.         }
  34.         else{
  35.             Node *temp = head;
  36.             while (temp != NULL){
  37.                 cout << temp->val << " ";
  38.                 temp = temp->next;
  39.             }
  40.             cout << endl;
  41.         }
  42.     }
  43.     void dispose(){
  44.         Node *temp;
  45.         while (head != NULL){
  46.             temp = head;
  47.             head = head->next;
  48.             delete temp;
  49.         }
  50.         cout << "all elements are cleared." << endl;
  51.     }
  52.     int pop_front(){
  53.         if (head == NULL){
  54.             cout << "list is empty." << endl;
  55.             return -1;
  56.         }
  57.         else{
  58.             Node *temp = head;
  59.             head = temp->next;
  60.             int res = temp->val;
  61.             delete temp;
  62.             return res;
  63.         }
  64.     }
  65.     int get_the_first_element(){
  66.         if (head == NULL){
  67.             cout << "no elements. " << endl;
  68.             return -1;
  69.         }
  70.         else{
  71.             return head->val;
  72.         }
  73.     }
  74.     Node *search(int v){
  75.         Node *temp = head;
  76.         int count = 0;
  77.         while (temp != NULL)
  78.         {
  79.             if (temp->val == v)
  80.             {
  81.                 return temp;
  82.             }
  83.             else
  84.             {
  85.                 temp = temp->next;
  86.                 count++;
  87.             }
  88.         }
  89.         return NULL;
  90.     }
  91.     bool deleted(int v){
  92.         if (head == NULL){
  93.             return false;
  94.         }
  95.         else if (head->val == v){
  96.             pop_front();
  97.             return true;
  98.         }
  99.         else{
  100.             Node *temp = head;
  101.             while (temp->next != NULL){
  102.                 if (temp->next->val == v){
  103.                     Node *temp2 = temp->next;
  104.                     temp->next = temp->next->next;
  105.                     delete temp2;
  106.                     return true;
  107.                 }
  108.                 temp = temp->next;
  109.             }
  110.         }
  111.     }
  112.     Node *getelement(int pos)
  113.     {
  114.         Node *temp = head;
  115.         int count = 0;
  116.         while (temp != NULL)
  117.         {
  118.             if (pos == count)
  119.             {
  120.                 return temp;
  121.             }
  122.             else
  123.             {
  124.                 count++;
  125.                 temp = temp->next;
  126.             }
  127.         }
  128.         return NULL;
  129.     }
  130.     bool deleteat(int pos)
  131.     {
  132.         int count = 1;
  133.         if (head == NULL)
  134.         {
  135.             cout << "no eleement is here!!!!!!" << endl;
  136.             return false;
  137.         }
  138.         else if (pos == 1)
  139.         {
  140.             pop_front();
  141.             return true;
  142.         }
  143.         else
  144.         {
  145.             Node *temp = head;
  146.             while (temp->next != NULL)
  147.             {
  148.                 if (pos == count)
  149.                 {
  150.                     Node *temp2 = temp->next;
  151.                     temp->next = temp->next->next;
  152.                     delete temp2;
  153.                     return true;
  154.                 }
  155.                 count++;
  156.                 temp = temp->next;
  157.             }
  158.         }
  159.     }
  160. };
  161.  
  162. int main()
  163. {
  164.     linkedlist l1;
  165.     cout << "Choose one of the option: " << endl;
  166.     cout << "1. Insert element at the 1st position:  " << endl;
  167.     cout << "2. print all element:  " << endl;
  168.     cout << "3. delete an element at first position:  " << endl;
  169.     cout << "4. search by value :  " << endl;
  170.     cout << "5. get element at given position:  " << endl;
  171.     cout << "6. Delete  element at given index:  " << endl;
  172.     cout << "7. delete element of given value:  " << endl;
  173.     cout << "8. Quit  " << endl;
  174.     int po, r = 0, element;
  175.     while (r < 9)
  176.     {
  177.         cout << "enter option:";
  178.         cin >> r;
  179.         if (r == 1)
  180.         {
  181.             cout << "enter element:";
  182.             cin >> element;
  183.             l1.push_front(element);
  184.         }
  185.         else if (r == 2)
  186.         {
  187.             l1.display();
  188.         }
  189.         else if (r == 3)
  190.         {
  191.             l1.pop_front();
  192.         }
  193.         else if (r == 4)
  194.         {
  195.             cout << "enter element:";
  196.             cin >> element;
  197.             struct Node *m = l1.search(element);
  198.             if (m == NULL)
  199.                 cout << "element not found" << endl;
  200.             else
  201.                 cout << "element found" << endl;
  202.         }
  203.         else if (r == 5)
  204.         {
  205.             cout << "enter position:";
  206.             cin >> po;
  207.             struct Node *m = l1.getelement(--po);
  208.             if (m == NULL)
  209.                 cout << "element not found" << endl;
  210.             else
  211.                 cout << m -> val << endl;
  212.         }
  213.         else if (r == 6)
  214.         {
  215.             cout << "enter position:";
  216.             cin >> po;
  217.             if (l1.deleteat(po) == true)
  218.             {
  219.                 cout << "element deleted" << endl;
  220.             }
  221.             else
  222.             {
  223.                 cout << "element not found" << endl;
  224.             }
  225.         }
  226.         else if (r == 7)
  227.         {
  228.             cout << "enter element:";
  229.             cin >> element;
  230.             if (l1.deleted(element) == true)
  231.             {
  232.                 cout << "element deleted" << endl;
  233.             }
  234.             else
  235.             {
  236.                 cout << "element not found" << endl;
  237.             }
  238.         }
  239.         else if (r == 8)
  240.         {
  241.             l1.dispose();
  242.             return 0;
  243.         }
  244.     }
  245.     return 0;
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement