Monday 25 July 2016

Single Link List Through C

#include<stdio.h>
#include<conio.h>
/* *****************************
 Girfa Student Help
 Programmer Mrityunjoy Sengupta
 for more DS Code : Visit :http://girfahelp.blogspot.com/p/data.html

   ******************************
*/
typedef struct n
{
 int data;
 struct n *next;
}node;
/* Function Prototype */
node *start=NULL;/* Global Variable point first node*/
node* create(int);/* Create dynamic space for Node */
void addbegin(int);/* Add an Item to starting position of list  */
void addlast(int);/* Add an Item to last position of list  */
void addbefore(int);/* Add item before a number */
void addafter(int); /* Add item after a number */
void del(int);
void print();
void main()
{
 int n,s,opt;
 do
 {
  clrscr();
 printf("\n1. Print\n2. Add First\n3. Add Last\n4. Add Before\n5. Add After\n6. Delete\n0. Exit\n\nEnter Your Choice>> ");
  scanf("%d",&opt);

  switch(opt)

  {
   case 1:
    print();
    getch();
    break;
   case 2:
     printf("Enter Number for add begin>> ");
    scanf("%d",&n);
    for(opt=1;opt<=n;opt++)
    {
 printf("Enter %d'st number>> ",opt);
 scanf("%d",&s);
 addbegin(s);
    };
    break;
   case 3:
        printf("Enter Number for add last>> ");
      scanf("%d",&n);
     for(opt=1;opt<=n;opt++)
     {
  printf("Enter %d'st number>> ",opt);
  scanf("%d",&s);
  addlast(s);
     }

     break;
    break;
   case 4:
    if(start==NULL)
    {
     printf("\nList is empty add some data");
     getch();
    }
    else
    {
     printf("Enter Number for Search>> ");
     scanf("%d",&s);
     addbefore(s);
    }
    break;
   case 5:
    if(start==NULL)
    {
     printf("\nList is empty add some data");
     getch();
    }
    else
    {
     printf("Enter Number for Search>> ");
     scanf("%d",&s);
     addafter(s);
    }
    break;
   case 6:
    if(start==NULL)
    {
     printf("\nList is empty add some data");
     getch();
    }
    else
    {
     printf("Enter Number for Delete>> ");
     scanf("%d",&s);
     del(s);
    }
    break;
   case 0:
    break;
   default:
    printf("Invalid Choice");
    getch();
  }
 }while(opt!=0);
}
node* create(int n)
{
 node *nw;
 nw=(node*) malloc(sizeof(node));
 nw->data=n;
 nw->next=NULL;
 return nw;
}
void addbegin(int n)
{
 node *nw=create(n);
 if(start==NULL)
  start=nw;
 else
 {
  nw->next=start;
  start=nw;
 }
}
void print()
{
 node *pt;
 for(pt=start;pt!=NULL;pt=pt->next)
  printf("[%d]",pt->data);
}
void addlast(int n)
{
 node *pt,*nw=create(n);
 if(start==NULL)
  start=nw;
 else
 {
  for(pt=start;pt->next!=NULL;pt=pt->next);
  pt->next=nw;
 }
}
void addbefore(int s)
{
 int n;
 node *pt,*pre,*nw;
 for(pt=start;pt!=NULL;pre=pt,pt=pt->next)
 {
  if(pt->data==s)
   break;
 }
 if(pt==NULL)
 {
  printf("Search Number not found");
  getch();
 }
 else
 {
  printf("Enter Number for Add Before>> ");
  scanf("%d",&n);
  nw=create(n);
  if(pt==start)/* add before first node */
  {
   nw->next=start;
   start=nw;
  }
  else
  {
   nw->next=pt;
   pre->next=nw;
  }
 }
}
void addafter(int s)
{
 int n;
 node *pt,*nw;
 for(pt=start;pt!=NULL;pt=pt->next)
 {
  if(pt->data==s)
   break;
 }
 if(pt==NULL)
 {
  printf("Search Number not found");
  getch();
 }
 else
 {
  printf("Enter Number for Add after>> ");
  scanf("%d",&n);
  nw=create(n);
  nw->next=pt->next;
  pt->next=nw;
 }
}
void del(int n)
{
 node *pt,*pre,*ptr;
 for(pt=start;pt!=NULL;pre=pt,pt=pt->next)
  if(pt->data==n)
   break;
 if(pt==NULL)
 {
  printf("\nSearch Value Not Found");
  getch();
 }
 else
 {
   if(start==NULL)
   {
    printf("\nList is Empty");
    getch();
   }
   else
   {
  ptr=pt;
  //if only one node exist
  if(start->next==NULL)
   start=NULL;
  else
  {
   //If first node has to delete
   if(pt==start)
    start=start->next;
   else
   {
    pre->next=pt->next;
   }
  }
  free(ptr);
 }
 }
}

Next Program

No comments:

Post a Comment