CREATE A NODE PROGRAM : (a) insert in an empty list (b)insert at beginning (c)insert at end (d) insert N-position (e) display

hello friends....

TAKE THE FIRST STEP TO KNOWLEDGE FRIENDS BECAUSE KNOWLEDGE IS FREE.

/* CREATE A  NODE PROGRAM TO IMPLEMENT FOLLOWING  SEPTS (a) insert in an empty list (b)insert at beginning (c)insert at end (d) insert N-position (e) display*/ 

#include<stdio.h>
#include<conio.h>
struct node
{
    int info;
    struct node *link;
};

struct node *emplist(struct node *start, int data);
struct node *beg(struct node *start, int data);
struct node *end(struct node *start, int data);
struct node *nposition(struct node *start, int data, int position);
void disp(struct node *start);

void main()
{
   struct node *start=NULL;
   int ch,data,position;
   clrscr();

   while(1)
   {
     printf("1-insert in an empty list\n");
     printf("2-insert at beginning\n");
     printf("3-insert at end\n");
     printf("4-insert N-position\n");
     printf("5-display\n");
     printf("6-end programm\n");
     printf("=======================\n");
     printf("ENTER CHOICE : ");
     scanf("%d",&ch);

     printf("\n");
     printf("=================================\n");
     printf("\n");

     switch(ch)
     {
case 1:
  if(start!=NULL)
  {
     printf("List is Not Empty !\n");
     break;
  }
  printf("enter element : ");
  scanf("%d",&data);
  start=emplist(start,data);
  break;

case 2:
  printf("enter element : ");
  scanf("%d",&data);
  start=beg(start,data);
  break;

case 3:
  printf("enter elements : ");
  scanf("%d",&data);
  start=end(start,data);
  break;

case 4:
  printf("enter elements :");
  scanf("%d",&data);
  printf("enter position : ");
  scanf("%d",&position);
  start=nposition(start,data,position);
  break;

case 5:
  disp(start);
  break;

case 6:
  exit(0);

default:
  printf("InValid choice\n");
     }  // switch close
   } // while close
   getch();
} //main close

struct node *emplist(struct node *start, int data)
{
   struct node *tmp;
   tmp=(struct node *)malloc(sizeof(struct node));

   tmp->info=data;
   tmp->link=start;
   start=tmp;
   return start;
}

struct node *beg(struct node *start,int data)
{
   struct node *tmp;
   tmp=(struct node *)malloc(sizeof(struct node));

   tmp->info=data;
   tmp->link=start;
   start=tmp;
   return start;
}

struct node *end(struct node *start, int data)
{
   struct node *p,*tmp;
   tmp=(struct node *)malloc(sizeof(struct node));

   tmp->info=data;
   p=start;
   while(p->link!=NULL)
   {
     p = p->link;
   }
   p->link=tmp;
   tmp->link=NULL;
   return start;
}

struct node *nposition(struct node *start,int data,int position)
{
  int i;
  struct node *tmp,*p;
  tmp = (struct node *)malloc(sizeof(struct node));

  tmp->info=data;
  if(position==1)
  {
      tmp->link=start;
      start=tmp;
      return start;
  }
  p=start;
    for(i=1; i<position-1 && p!=NULL; i++)
    {
       p=p->link;
    }
      if(p==NULL)
      {
  printf("**********************************\n");
  printf("* There are less then %d element *\n",position);
  printf("**********************************\n");
      }
      else
      {
tmp->link= p->link;
p->link = tmp;
      }
   return start;
}

void disp(struct node *start)
{
   struct node *p;
   if(start==NULL)
   {
      printf("List is Empty !!!\n");
   }
   else
   {
      p=start;
      printf("List : \n");
      while(p!=NULL)
      {
printf("%d\t",p->info);
p=p->link;
      }

      printf("\n");
   }

}

         OUTPUT
=================


* * * * * * * * * * * * * *

>> IF YOU LIKE THIS BLOG, PLEASE SHARE AND SUBSCRIBE. ALSO COMMENT  FOR THIS BLOG.
>> IF YOU HAVE ANY QUESTIONS PLEASE ASK IN COMMENT.
>> IF YOU WANT TO LEARN HTML WITH OUTPUT, SO VISIT THIS BLOG
      https://dnpwebdeveloper.blogspot.com/
>> IF YOU WANT TO LEARN C++ PROGRAM WITH OUTPUT, SO VISIT THIS BLOG
      https://cplusplusdnpdeveloper.blogspot.com/
>> IF YOU WANT TO LEARN C PROGRAM WITH OUTPUT, SO VISIT THIS BLOG
      https://dnpdeveloper.blogspot.com/
>> MY INSTAGRAM ID : dnp176


Comments