Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.31 KB | None | 0 0
  1. // Linked List CPP
  2. #include<iostream>
  3.  
  4. using namespace std;
  5.  
  6. class Node
  7. {
  8. public:
  9.     int data;
  10.     Node *next;
  11. };
  12.  
  13. class List
  14. {
  15.     Node *head;
  16.     Node *tail;
  17.     Node *temp;
  18.     bool isEmpty()
  19.     {
  20.         return head == NULL;
  21.     }
  22. public:
  23.     List()
  24.     {
  25.         head = NULL;
  26.         tail = NULL;
  27.     }
  28.  
  29.     void insert(int x)
  30.     {
  31.         temp = new Node;
  32.         temp->data = x;
  33.         if(isEmpty())
  34.         {
  35.             temp->next = NULL;
  36.             tail = temp;
  37.         }
  38.         else
  39.             temp->next = head;
  40.         head = temp;
  41.     }
  42.  
  43.     /*void insertAtEnd(int x)
  44.     {
  45.         temp = new Node;
  46.         temp->data = x;
  47.         temp->next = NULL;
  48.         if(isEmpty())
  49.         {
  50.             head = temp;
  51.             tail = temp;
  52.         }
  53.         else
  54.         {
  55.             tail->next = temp;
  56.             tail = temp;
  57.         }
  58.     }*/
  59.  
  60.     void remove(int x)
  61.     {
  62.         temp = head;
  63.         Node *prev;
  64.         while(temp->next != NULL && temp->data != x)
  65.         {
  66.             prev = temp;
  67.             temp = temp->next;
  68.         }
  69.         if(temp->data == x)
  70.         {
  71.             prev->next = temp->next;
  72.             delete temp;
  73.         }
  74.         else if(temp->next == NULL)
  75.         {
  76.             cout << "Error: Number Not found..." << endl;
  77.         }
  78.     }
  79.  
  80.     void find(int x)
  81.     {
  82.         int i;
  83.         for(i=1, temp = head;temp->next != NULL && temp->data != x;temp = temp->next, i++);
  84.         if(temp->data == x)
  85.         {
  86.             cout << "Found at position:" << i << endl;
  87.         }
  88.         else if(temp->next == NULL)
  89.         {
  90.             cout << "Error: Number Not found..." << endl;
  91.         }
  92.     }
  93.  
  94.     void display()
  95.     {
  96.         if(!isEmpty())
  97.         {
  98.             for(temp = head; temp != NULL; temp=temp->next)
  99.                 cout << temp->data << " ";
  100.             cout << endl;
  101.         }
  102.         else
  103.         {
  104.             cout << "List is Empty!" << endl;
  105.         }
  106.     }
  107. };
  108.  
  109. int main()
  110. {
  111.     List l;
  112.     l.display();
  113.  /*   l.insert(15);
  114.     l.display();
  115.     l.insert(25);
  116.     l.display();
  117.     l.insert(35);
  118.     l.display();
  119.     l.insert(45);
  120.     l.display();
  121.     l.find(15);
  122.     l.remove(25);
  123.     l.display();
  124.     l.insertAtEnd(55);
  125.     l.insertAtEnd(65);
  126.     l.display();*/
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement