Posts

Reverse a singly Linked List

  #include<bits/stdc++.h> using namespace std; struct Node {     int data;     struct Node *Next; }; int main() {     int arr[]= {1,2,3,4,5};     struct Node *temp,*Head=NULL,*current=NULL;     //insert data from array     for(int i=0; i<5; i++)     {         temp= (struct Node *)malloc(sizeof(struct Node));         temp->data=arr[i];         temp->Next=NULL;         if(Head==NULL)         {             Head=temp;             current=temp;         }         else         {             //current=temp;             current->Next=temp;             current=current->Next;         }     }     //Reverse a singly Linked List     struct Node *crnt, *prvs, *nxt;     crnt=Head;     prvs=NULL;     while(crnt!=NULL)     {         cout<<crnt->data<<endl;         nxt=crnt->Next;         crnt-> Next=prvs;         prvs=crnt;         crnt=nxt;     }     //print link list     Head=prvs;     while(Head!=NULL)     {         cout<<Head->data<<endl;    

Search an element item in a Linked List

#include<bits/stdc++.h> using namespace std; struct Node {     int data;     struct Node *Next; }; int main() {     int arr[]= {1,2,3,4,5};     struct Node *temp,*Head=NULL,*current=NULL;     //insert data from array     for(int i=0; i<5; i++)     {         temp= (struct Node *)malloc(sizeof(struct Node));         temp->data=arr[i];         temp->Next=NULL;         if(Head==NULL)         {             Head=temp;             current=temp;         }         else         {             //current=temp;             current->Next=temp;             current=current->Next;         }     }     int sitem;     // input search item     cin>>sitem;     int index=0;     //searching happened here     while(Head!=NULL)     {         if(sitem==Head->data){             cout<<"data:"<<Head->data<<endl;             cout<<"index:"<<index<<endl;             break;         }         Head=Head->Next;         index++;    

Create a Linked List from an Array

 #include<bits/stdc++.h> using namespace std; struct Node {     int data;     struct Node *Next; }; int main() {     int arr[]= {1,2,3,4,5};     struct Node *temp,*Head=NULL,*current=NULL;     //insert data from array     for(int i=0; i<5; i++)     {         temp= (struct Node *)malloc(sizeof(struct Node));         temp->data=arr[i];         temp->Next=NULL;         if(Head==NULL)         {             Head=temp;             current=temp;         }         else         {             //current=temp;             current->Next=temp;             current=current->Next;         }     }     //print link list     while(Head!=NULL)     {         cout<<Head->data<<endl;         Head=Head->Next;     } }

Linked List Create and Traversal

 #include<bits/stdc++.h> using namespace std; struct Node{     int data;     struct Node *Next; }; int main() {     struct Node *a;     struct Node *b;     struct Node *c;     a=(struct Node *)malloc(sizeof(struct Node));     b=(struct Node *)malloc(sizeof(struct Node));     c=(struct Node *)malloc(sizeof(struct Node));     a->data=10;     b->data=20;     c->data=30;     a->Next=b;     b->Next=c;     c->Next=NULL;     while(a!=NULL){         cout<<a->data<<endl;         a=a->Next;     } }