Friday 6 July 2012

Link list by Barsha

Credit: Barsha, Shahrair Robbani



#include<stdio.h>
#include<stdlib.h>
struct node
{
int n;
struct node *p;
};
void insert(struct node *l,int k)
{
while(l->p!=NULL)
l=l->p;
l->p=(struct node *)malloc(sizeof(struct node));
l->p->n=k;
l->p->p=NULL;
}
void display(struct node *l)
{


if(l->p!=NULL)
{
        l=l->p;
while(l->p!=NULL)
{
printf("%d\t",l->n);
l=l->p;
}
printf("%d\t",l->n);
}
else
printf("EMPTY :p");
}
int search(struct node *l,int s)
{
int c=1,i=0;
while(l->p!=NULL)
{
if(l->p->n==s)
return c;
else
            c++;
            l=l->p;
}
return i;
}
void deletion(struct node *l,int it)
{
int i;
i=search(l,it);
if(i==0)
printf("\n\n\tTHE VELUE DOESN'T EXIST");
else
{
    if(l->p!=NULL)
    {
        struct node *t=(struct node *)malloc(sizeof(struct node));
        while(l->p->n!=it)
           l=l->p;
          t->p=l->p;
          l->p=l->p->p;
          free(t->p);
 printf("\n\n\tSUCCESSFULLY DELETED");

    }
    else
    printf("\n\n\tEmpty LIST");
}
}
int count_list(struct node *l)
{
int c=0;
if(l->p!=NULL)
{
while(l->p!=NULL)
{
c++;
l=l->p;
}
}
else
return c;
return c;
}
void insert_sp(struct node *l,int b,int k)
{
int c=count_list(l);
struct node *t;
t=(struct node *)malloc(sizeof(struct node));
int i=0;
if(k>c)
insert(l,b);
else
{
while(l->p!=NULL)
{
l=l->p;
i++;
if(i==k-1)
break;
}
t=l->p;
l->p=(struct node *)malloc(sizeof(struct node));
l->p->n=b;
l->p->p=t;
}
}
int main()
{
int c=1;
int k,b,d;
struct node *l;
l=(struct node *)malloc(sizeof(struct node));
l->n=0;
l->p=NULL;
while(c!=0)
{
printf("\n\n\t1.INSERT\n\t2.DISPLAY\n\t3.DELETE\n\t4.COUNT LIST\n\t5.SEARCH\n\t6.INSERT IN SPECIFIC POSITION\n\t0.QUIT");
printf("\n\n\tChoose any of the case\n\n\t");
scanf("%d",&c);
switch(c)
{
      case 1:
  printf("\n\n\tENTER VALUE\n\n\t");
  scanf("%d",&k);
  insert(l,k);
  break;
  case 2:
  printf("\n\n\tTHE TAKEN VALUES ARE\n\n\t");
  display(l);
  break;
  case 3:
  printf("\n\n\tWHICH VALUE U WANT TO DELETE\n\t");
  scanf("%d",&k);
  deletion(l,k);
  break;
  case 4:
  d=count_list(l);
  if(d>0)
  printf("\n\n\tTOTAL ELEMENT = %d",d);
  else
  printf("\n\n\tEXIST NO LIST");
  break;
  case 5:
  printf("\n\tWANNA SEARCH THE VALUE\t");
  scanf("%d",&b);
  d=search(l,b);
  if(d==0)
  printf("\n\tNOT FOUND.....SORRY :)");
  else
  printf("\n\tFOUND AT POSITION\t%d",d);
  break;
  case 6:
  printf("\n\tINSERT THE VALUE\n\t");
  scanf("%d",&b);
  printf("\n\tINSERT THE POSITION\n\t");
  scanf("%d",&k);
  insert_sp(l,b,k);
  printf("\n\n\tSUCCESSFULLY ADDED......");
  break;
  case 0:
  printf("\n\n\t:D");
  break;
  default:
  printf("\n\n\tWRONG CHOICE\n");
  break;
}
}
return 0;
}

0 comments :

Post a Comment