Our Feeds

Wednesday 23 December 2015

AJITH KP

Singly Linked List (SLL) Basic Operations Implementation in C++

Hello GuyZ,
     Singly Linked List is important data structure which is used for several purposes like storing sparse matrix, sparse polynomial, etc. Also it is used to create circular queues, stacks, etc. The basic operations performed on SLL are,

  1. Insert data
  2. Delete data
     Data can be inserted at any position in Linked List, also the deletion can be take place at any position.







Source Code

#include <iostream>
using namespace std;
struct node
{
 int data;
 node *link = NULL;
};
class llops
{
public:
 node *root;
 llops(){
  root = NULL;
 }
 void menu(){
  int op = 0;
  while(op!=8){
   cout<<"\n\t\t\tMENU\n1. Add @ Beg\n2. Add @ End\n3. Add at i th node\n4. Del from Beg\n5. Del from End\n6. Del ith node\n7. Traversal\n8. Exit\nOption: ";
   cin>>op;
   switch(op){
    case 1:
    addbeg();
    break;
    case 2:
    addend();
    break;
    case 3:
    addith();
    break;
    case 4:
    delbeg();
    break;
    case 5:
    delend();
    break;
    case 6:
    delith();
    break;
    case 7:
    show();
    break;
    case 8:
    break;
    default:
    cout<<"Wrong option... Try again...";
    break;
   }
  }
 }
 void addbeg(){
  node *nn;
  nn = new node;
  cout<<"Enter data: ";
  cin>>nn->data;
  nn->link = root;
  root = nn;
 }
 void addend(){
  node *nn, *tmp = root;
  cout<<"Enter data: ";
  nn = new node;
  cin>>nn->data;
  if(root==NULL){
   root = nn;
  }
  else{
   while(tmp->link!=NULL){
    tmp=tmp->link;
   }
   tmp->link = nn;
  }
 }
 void addith(){
  int i, j=0;
  node *nn, *pn, *tmp = root;
  cout<<"Enter i th position: ";
  cin>>i;
  i--;
  if(i==0){
   nn = new node;
   cout<<"Enter data: ";
   cin>>nn->data;
   nn->link = root;
   root = nn;
   return;
  }
  while(j<i && tmp!=NULL){
   pn = tmp;
   tmp=tmp->link;
   j++;
  }
  if(i==j){
   nn = new node;
   cout<<"Enter data: ";
   cin>>nn->data;
   nn->link = pn->link;
   pn->link = nn;
  }
  else{
   cout<<"Error: Cannot add item at position "<<i+1<<"\n";
  }
 }
 void delbeg(){
  node *cn;
  if(root){
   cn = root;
   root = root->link;
   cout<<cn->data<<" Deleted from beginning...\n";
   delete(cn);
  }
  else cout<<"Error: List is empty...\n";
 }
 void delend(){
  node *pn=NULL, *tmp = root;
  if(!root){
   cout<<"Error: List is empty...\n";
  }
  else{
   if(root->link==NULL){
    cout<<root->data<<" Deleted from end...\n";
    delete(root);
    root=NULL;
   }
   else{ 
    while(tmp->link){
     pn = tmp;
     tmp=tmp->link;
    }
    pn->link = NULL;
    cout<<tmp->data<<" Deleted from end...\n";
    delete(tmp);
   }
  }
 }
 void delith(){
  int i, j=0;
  node *nn, *pn, *tmp=root;
  cout<<"Enter i th position: ";
  cin>>i;
  i--;
  if(i==0 && root){
   cout<<root->data<<" Deleted from position "<<i+1<<"...\n";
   root = root->link;
  }
  else{
   while(j<i && tmp){
    pn = tmp;
    tmp=tmp->link;
    j++;
   }
   if(j==i && tmp){
    pn->link=tmp->link;
    cout<<tmp->data<<" Deleted from position "<<i+1<<"...\n";
    delete(tmp);
   }
   else{
    cout<<"Error: No nodes at position "<<i+1<<"...\n";
   }
  }
 }
 void show(){
  node *tmp = root;
  if(root){
   cout<<"\nList: ";
   while(tmp){
    cout<<tmp->data<<" ";
    tmp=tmp->link;
   }
   cout<<endl<<endl;
  }
  else{
   cout<<"\nList Empty\n\n";
  }
 }
};
int main(){
 llops l;
 l.menu();
 return 0;
}