Sunday 5 July 2015

Singly Linked List Operation


                                  SINGLY LINKED LIST OPERATION
#include<stdio.h>
#include<conio.h>


// Syntax for Creating Node
struct node
{
int data;                           // Type of data store in nodes
struct node *link;           // Link pointer for joining nodes
};
struct node *head=NULL;    // head pointer for pointing the first node of linked list
int item;                                // Integer temporary variable for inserting the data in linked list

void main()
{
 int choice;
 clrscr();
 printf("\nEnter 1:ADDEND \n 2:ADDBEG \n 3:ADDAFTER \n 4:DELBEG \n 5:DELEND \n 6:DELAFTER \n 7:DISPLAY \n 8:COUNT \n 9:EXIT");
  while(1)
  {
   printf("\nEnter Choice ");
   scanf("%d",&choice);
    switch(choice)
    {
    case 1:
    AddEnd();
    break;
    case 2:
    AddBeg();
    break;
    case 3:
    AddAfter();
    break;
    case 4:
    DelBeg();
    break;
    case 5:
    DelEnd();
    break;
    case 6:
    DelAfter();
    break;
    case 7:
    Display();
    break;
    case 8:
    Count();
    break;
    case 9:
    exit();
   }
 }
}

// Function for adding node in the last of the Linked List

int AddEnd()
{
 struct node *temp,*q;
  if(head==NULL)
  {
   temp=(struct node *)malloc(sizeof(struct node));
   printf("\nEnter Item ");
   scanf("%d",&item);
   temp->data=item;
   temp->link=NULL;
   head=temp;
  }
  else
  {
   q=head;
    while(q->link!=NULL)
    {
     q=q->link;
    }
   temp=(struct node *)malloc(sizeof(struct node));
   printf("\nEnter Item ");
   scanf("%d",&item);
   temp->data=item;
   temp->link=NULL;
   q->link=temp;
  }
 return 0;
}

// Function for adding node in the beginning of the Linked List

AddBeg()
{
 struct node *temp,*q;
  if(head==NULL)
  {
   temp=(struct node *)malloc(sizeof(struct node));
   printf("\nEnter Item ");
   scanf("%d",&item);
   temp->data=item;
   head=temp;
  }
  else
  {
   temp=(struct node *)malloc(sizeof(struct node));
   printf("\nEnter Item ");
   scanf("%d",&item);
   temp->data=item;
   temp->link=head;
   head=temp;
  }
 return 0;
}

// Function for adding node after some nodes(in the middle) of the Linked List

AddAfter()
{
 int pos,n;
 printf("\nEnter position to Insert new node");
 scanf("%d",&pos);
 n=Count();
  if(pos>n)
   printf("\nNo Available node are less");
  else if(pos==n)
   AddEnd();
  else if(pos==0)
   AddBeg();
  else if(pos>=1 && pos<n)
  {
   int i;
   struct node *q,*temp,*r;
   temp=(struct node *)malloc(sizeof(struct node));
   printf("\nEnter Item ");
   scanf("%d",&item);
   temp->data=item;
   q=head;
    for(i=1;i<pos;i++)
    {
     q=q->link;
      //r=q->link->link;
    }
   temp->link=q->link;
   q->link=temp;
  }
 return 0;
}

// Function for deleting node from beginning of the Linked List

DelBeg()
{
 struct node *q;
  if(head==NULL)
   printf("\nNo Node to Delete");
  else
  {
   printf("\n%d Data has Deleted",head->data);
   q=head;
   q=q->link;
   free(head);
   head=q;
  }
 return 0;
}

// Function for deleting node from last of the Linked List

DelEnd()
{
 struct node *q;
 q=head;
  if(head==NULL)
   printf("\nNo node to Delete");
  else
   {
    if(head->link==NULL)
     {
     printf("\n%d Data has Deleted",head->data);
     free(head);
     head=NULL;
     }
    else
    {
     while(q->link->link!=NULL)
      {
       q=q->link;
      }
     printf("\n%d Data has Deleted",q->link->data);
     free(q->link);
     q->link=NULL;
    }
  }
 return 0;
}

// Function for deleting node after some nodes(in the middle) Linked List

DelAfter()
{
 int pos,n,i;
 struct node *q,*r;
 printf("\nEnter position which is to be Delete ");
 scanf("%d",&pos);
 n=Count();
  if(head==NULL)
   printf("\nNo Available Node to Delete");
  else if(head->link==NULL)
  {
   printf("\n%d Data is Deleted",head->data);
   head=NULL;
   free(head);
  }
 if(pos==0)
 {
  printf("\nDELBEG Function Calling");
  DelBeg();
 }
 else if(pos==n)
 {
  printf("\nDELEND Function Calling");
  DelEnd();
 }
 else if(pos>=1 && pos<n)
 {
  q=head;
  r=head->link;
  for(i=1;i<pos;i++)
   {
    q=q->link;
    r=r->link;
   }
  printf("\n%d Data is Deleted",r->data);
  q->link=r->link;
  free(r);
 }
 return 0;
}

// Function for Counting nodes in Linked List

int Count()
{
 int i,counter=1;
 struct node *q;
 q=head;
  if(head==NULL)
   printf("\nTotal No. of Node is : %d",0);
  else
   {
    while(q->link!=NULL)
     {
      counter++;
      q=q->link;
     }
    printf("\nTotal no. of Node is : %d",counter);
    return(counter);
   }
 return 0;
}

// Function for displaying the Linked List

Display()
{
 struct node *q;
  if(head==NULL)
   printf("\nNo Linked List Available to Show");
  else
   {
    q=head;
     while(q!=NULL)
      {
       printf(" %d",q->data);
       q=q->link;
      }
   }
 return 0;
}