LinkedList Implementation in C++/C

·

3 min read

Linked List Implementaion in C++

What is a LinkedList? LinkedList is a Data Structure model which used to make the accessibiity of data more easy .Each element in a LinkedList is know as a node which links or points to another node. The address of the LinkedList is stored in the HEAD node ,which makes the traversing of the LinkedList possilbe. The END node or the a Last node points to NULL to mark the end of the List.

Here is visual Representation of a Simple LinkedList

linked-list.png

Advantages of LinkedList over array

  • It makes the insertion of any element(node) in the beginning,end and middle easy (time complexity O(1) )
  • It makes the deletion of element(node) from the beginning,end and middle easy(time complexity O(1)
  • Inserting an element in the middle is comparatively less costly in LinkedList than Arrays ,Cause in array's you have to shift the whole array after the particular index or position but in LinkedList you have to traverse only till that index at which you wish to insert any element.
  • Length of the LInkedList can by increased or decreased at any time of the program , unlike Arrays in which length of the array are fixed.

Let us see a simple implementaion of LinkedList in C++/C

Don't get intimidated by the number of lines in this implementaion, i have made it so long so that you can get the concept of Pointers and working with pointers, and then finally using it to implement a LinkedList

#include <iostream>
using namespace std;

int sizes;

struct LinkedList{
  int data;
  LinkedList* next;
}; 

LinkedList* Head;

void print();
void search(int item);
void removeFirst();
void removeAt(int position);
void removeLast();
void insertAtLast(int x);
void insertAtBeginning(int x);
void insertAt(int position,int num);

int main(){
  Head=NULL;
  int x=45,y=56,z=58,v=99,u=69;
  insertAtLast(x);
  insertAtLast(y);
  insertAtLast(z);
  insertAtLast(v);
  insertAtLast(u);
  print();
  cout<<"The length of the LinkedList is :"<<sizes<<endl;
  insertAtBeginning(999);
  search(69);
  insertAt(1,1000);
  cout<<"\n";
  removeFirst();
  removeAt(3);
  removeLast();
  print();
  cout<<"The length of the LinkedList is :"<<sizes;
  return 0;

}


void insertAtLast(int x){

  LinkedList* temp=new LinkedList;
  LinkedList* temp2=new LinkedList;

    if(Head==NULL){
    temp->next=NULL;
    temp->data=x;
    Head=temp;
    return;
    }
    else{
    temp=Head;
    while(temp->next!=NULL){
      temp=temp->next;
    }
    temp->next=temp2;
    temp2->data=x;
    temp2->next=NULL;

    }

}


void print(){
  sizes=0;
   LinkedList* temp=new LinkedList;
   temp=Head;
   if(temp==NULL){
     cout<<"There are no elements in this linked list please enter some elements and try again :) "<<endl;
     return;
   }
   while(temp->next!=NULL){
    cout<<"Printintg the data "<<temp->data<<endl;
    sizes++;
   temp=temp->next;
   }
    cout<<"Printintg the data "<<temp->data<<endl;
    sizes++;
    return;

}

void search(int item){
  int count=0;
  int index=0;
  LinkedList* temp=new LinkedList;
  temp=Head;
  while(temp->next!=NULL){
    if(temp->data==item){
     count++;
     break;
    }
    temp=temp->next;
    index++;
  }
    if(temp->data==item){
      count++;
    }
  (count>0)?cout<<"Item found at "<<index <<"\n":cout<<"Item not found \n";

}

void removeFirst(){
  LinkedList* temp=new LinkedList;
  temp=Head;
  Head=temp->next;

}
void removeLast(){
  LinkedList* temp=new LinkedList;
  LinkedList* temp2= new LinkedList;
  temp=Head;
  while(temp->next!=NULL){
    temp=temp->next;
    temp2=temp->next;
    if(temp2->next==NULL){
      temp->next=NULL;
      break;
    }
  }


}
void removeAt(int position){
  int i=0;
  LinkedList* temp=new LinkedList;
  LinkedList* temp2=new LinkedList;
  temp=Head;
  while(i!=position-1){
    temp=temp->next;
    i++;
    }
    temp2=temp->next;
    temp->next=temp2->next;


  }

void insertAt(int position,int num){
  int i=0;
  LinkedList* temp=new LinkedList;
  LinkedList* temp2=new LinkedList;
  temp=Head;
while(i!=position-1){
temp=temp->next;
i++;
}
temp2->next=temp->next;
temp2->data=num;
temp->next=temp2;

}
void insertAtBeginning(int x){
   LinkedList* temp=new LinkedList;
   temp->next=Head;
   temp->data=x;
   Head=temp;
}

Thanks for reading and investing your time ,i hope you might have got some idea about LinkeList implementaion if not all.