anchor.diff:  // :hp-tags: HubPress, Blog, Open_Source,
   // :hp-alt-title: My English Title
   == linked list is a data structure that having a list of elements and each element has a reference pointing to the next element in the list.
  -===== First thing we have to know about linked list is we have a head pointer that points to a node that then has some date and points to another node and so on till a node that dose not point to any farther.
  -And node contains 2 elements ,the value or the key (number like 7) and a pointer that points to the next node that contain (another number like 10 for example)
  -
  -
  -. item
  -. item
  -. item
  -* item
  -* 
  -* item
  +==== First thing we have to know about linked list is we have a head pointer that points to a node that then has some date and points to another node and so on till a node that dose not point to any farther. 
  +* So the node contains 2 elements ,the value or the key (number like 7) and a pointer that points to the next node that contain (another number like 10 for example)
   
   
   = 7 → 10
  
positive.code: = All operations and algorithms on single linked Lists in C++
  
  // See https://hubpress.gitbooks.io/hubpress-knowledgebase/content/ for information about the parameters.
  // :hp-image: /covers/cover.png
  
negative.code: * Then update our tail pointer to point to that (previous last element)
  * Then update the tail pointer to be null, this means tail pointer points to the previous last element which it became the tail element
  * Then we can remove the last element
  * And the final run time will be O(n) Because we have got to walk all the way down
  
  == Single Linked list algorithm
  
  * *PushFront*
  ----
  node <- new node 
  node.next <- head 
  head <- node 
  if tail = nil: //an empty list 
   tail <- head`
  ----
  
  * *PopFront*
  ----
  if head = nil:
   ERROR: empty list
  head <- head.next
  if head = nil:
   tail <- nil
  ----
  
  * *PushBack*
  ----
  node <- new node 
   node.key <- key 
   node.next = nil 
   if tail = nil; head <- tail <- node
   else: tail.next <- node, tail <- node
  ----
   
  * *PopBack*
  ----
  if head = nil : 
   ERROR: empty list
  if head = tail
   head <- tail <- nil
  else 
   p <- head
   while p.next.next != nil:
   p <- p.next
  p.next <- nil;
  tail <- p
  ----
  
  * *Add after a node: AddAfter(node,key)*
  ----
  node <- new node
   node.key <- key
   node.next = node.next
   node.next = node
   if tail = node: //one element in list and this is the same node that we want to add key after that
   tail <- node
  ----
   
  *Adding before have the same problem we had with Pop back that we don’t have a link back to the previous element so AddBefore would be an O(n)*
  
  == Here you can find all main operations that we can do on single linked list:
  
  ----
  #include<iostream>
  using namespace std;
  //built node .... node = (data and pointer)
  struct node     
  {
      int data;   //data item
      node* next; //pointer to next node
  };
  //built linked list
  class linkedlist
  {
      private:
          node* head;
  public:
          linkedlist()    //constructor
          {
              head=NULL;  //head pointer points to null 
          }
  void addElementFirst(int d);           
          void addElementEnd(int d);         
          void addElementAfter(int d,int b);    
          void deleteElement(int d);
          void display();                    
  };
  //Push front code
  void linkedlist::addElementFirst(int d)
  {
      node* newNode=new node;        
      newNode->data=d;               
      newNode->next=head;            
      head=newNode;                   
  }
  //Push back code
  void linkedlist::addElementEnd(int x)
  {
      node* newNode=new node;    
      node* temp=new node;        
      temp=head;                 
      newNode->data=x;            
      if(temp==NULL)             
      {
          newNode->next=NULL;
          head=newNode;
          return;             
      }
  while(temp->next!=NULL)
      {
          temp=temp->next;      
      }
      newNode->next=NULL;       
      temp->next=newNode;        
  }
  //head->10->5->8->NULL
  //if d=5,key=2
  //head->10->5->2->8->NULL
  void linkedlist::addElementAfter(int d,int key)    
  {
      node* search=new node;   
      search=head;              
      while(search!=NULL)      
      {
          if(search->data==d)  
          {
              node* newNode=new node; 
              newNode->data=key;      
              newNode->next=search->next;   
              search->next=newNode;         
              return;
          }
          search=search->next;
      }
      
      if(search==NULL)  
          cout<<"The link not inserted because there is not found the node "<<d<<" in the LinkedList. "<<endl;
  }
  void linkedlist::deleteElement(int d)
  {
      node* del;  
      del=head;  
      if(del==NULL)  
      {
          cout<<"Linked list is empty"<<endl;
          return;   
      }
  if(del->data==d)    
      {
          head=del->next; 
          return;
      }
  if(del->next==NULL) 
      {
          cout<<"Is not here, So not deleted."<<endl;
          return;
      }
  //if here more one nodes...one node points to another node ... bigger than 2 nodes .. at least 2 nodes
      while(del->next!=NULL)
      {
          if(del->next->data==d)
          {
              del->next=del->next->next;
              return;
  }
         
          del=del->next;
       }
       cout<<"Is not here, So not deleted."<<endl;
  }
  //void linkedlist::display(node *head)
  void linkedlist::display()
  {
      int n=0;             //counter for number of node
      node* current=head;  //current is pointer points to where head point
  if (current==NULL)
          cout<<"This is empty linked list."<<endl;
  while(current!=NULL) //until current reach to null(last element)
      {
          cout<<"The node data number "<<++n<<" is "<<current->data<<endl;  
          current=current->next;
      }
  cout<<endl;
  }
  int main()
  {
      linkedlist li;      //li is object from linkedlist class
  li.display();       //empty list
  li.addElementFirst(25);  //head->25->NULL
      li.addElementFirst(36);  //head->36->25->NULL
      li.addElementFirst(49);  //head->49->36->25->NULL
      li.addElementFirst(64);  //head->64->49->36->25->NULL
      cout<<"After adding in the first of linked list"<<endl;
      li.display();
          //64
          //49
          //36
          //25
  cout<<endl;
      //head->64->49->36->25->NULL //current linked list from prev addElementFirst method
  cout<<"After adding in the end of linked list"<<endl;
      li.addElementEnd(25);  //head->25->NULL
      li.addElementEnd(36);  //head->25->36->NULL
      li.addElementEnd(49);  //head->25->36->49->NULL
      li.addElementEnd(64);  //head->25->36->49->64->NULL
      li.display();
      //head->64->49->36->25->25->36->49->64->NULL
  cout<<endl;
  //head->64->49->36->25->25->36->49->64->NULL
  cout<<"linked list after adding 10 after node that has data = 49"<<endl;
      li.addElementAfter(49,10);
      li.display();
      //head->64->49->10->36->25->25->36->49->64->NULL
  //head->64->49->10->36->25->25->36->49->64->NULL
      cout<<"linked list after adding deleting 49"<<endl;
      li.deleteElement(49);
      li.display();
      //head->64->10->36->25->25->36->49->64->NULL
      //Notice :delete the first 49 ... not permission for duplicates
  return 0;
  }
  ----
  
  == Last thing to say, Thanks for reading and I hope it would help and don’t hesitate to send your feedback and suggestions :))
  
  === Source code in c++ https://github.com/mohamedkhaledyousef/Crash-courses/blob/master/LinkedLists/Single%20Linked%20Lists/main.cpp[here]
  
  
  
  
  
  