Hello friend, This is Darshan and here I have perform the simply linked list program in easy method so enjoy this.......
simply linked list

  • Singly Linked List

 
#include<stdio.h>
#include<stdlib.h>

struct node
{
 int data;
 struct node *next;
}*first=NULL,*temp=NULL,*newnode=NULL,*t=NULL;

struct node* createnode()
{
 int x;
 struct node*temp;
 temp=(struct node*)malloc(sizeof(struct node));
 printf("\nEnter value:");
 scanf("%d",&x);
 if(temp!=NULL)
 {
  temp->data=x;
  temp->next=NULL;
  return temp;
 }
 else
 {
  printf("\nMemory not allocated");
  return 0;
 }
}

void display()
{
 if(first!=NULL)
 {
  for(temp=first;temp->next!=NULL;temp=temp->next)
  printf("%d->",temp->data);
  printf("%d->NULL",temp->data);
 }
 else
  printf("\nLinked list is empty");
}
void insf()
{
 newnode=createnode();
 if(newnode==NULL)
  printf("\nMemory not allocated.");
 else
 {
  if(first==NULL)
   first=newnode;
  else
  {
   newnode->next=first;
   first=newnode;
  }
 }
 display();
}

void insl()
{
 newnode=createnode();
 if(first==NULL)
 {
  first=newnode;
 }
 else
 {
  for(temp=first;temp->next!=NULL;temp=temp->next);
  temp->next=newnode;
  newnode->next=NULL;
 }
 display();
}

int countnode()
{
 int i=0;
 for(temp=first;temp->next!=NULL;temp=temp->next,i++);
 i++;
 return i;
}

void inssp()
{
 int n,i,pos;
 printf("\nENter position to add:");
 scanf("%d",&pos);
 n=countnode();
 if(pos>n)
  printf("\nEnter valid position");
 else
 {
  for(temp=first,i=1;i<pos-1;i++,temp=temp->next);
  newnode=createnode();
  newnode->next=temp->next;
  temp->next=newnode;
 } 
 display();
}

void insord()
{
 newnode=createnode();
 if(first==NULL)
  first=newnode;
 else
 {
  if(newnode->data<=first->data)
  {
   newnode->next=first;
   first=newnode;
  }
  else
  {
   for(temp=first;temp->next!=NULL&&newnode->data>=temp->next->data;temp=temp->next);
   newnode->next=temp->next;
   temp->next=newnode; 
  }
 }
 display();
}

void delf()
{
 if(first!=NULL)
 {
  struct node *t;
  t=first;
  temp=t->next;
  first=temp;
  free(t);
 }
 display();
}

void delasp()
{
 int pos,i=1;
 printf("\nEnter position");
 scanf("%d",&pos);
 if(pos>=countnode())
 {
  printf("\nEnter valid position\n");
 }
 else
 {
  for(temp=first;i<pos;i++,temp=temp->next);
  t=temp->next;
  temp->next=t->next;
  free(t);
 }
 display();
}

void delbsp()
{
 int pos,i=1;
 printf("\nEnter position");
 scanf("%d",&pos);
 if(pos>countnode())
 {
  printf("\nEnter valid position\n");
 }
 else
 {
  if(pos==1)
   printf("\nEnter valid number");
  else if(pos==2)
  {
   temp=first;
   first=temp->next;
   free(temp);
  }
  else
  {
   for(temp=first;i<pos-2;i++,temp=temp->next);
   t=temp->next;
   temp->next=t->next;
   free(t);
  } 
 }
 display();
}

main()
{
 int choice;
 while(1)
 {
  printf("\nMENU\n1.insert at first\n2.insert at last\n3.insert in order\n4.delete at first\n5.delete after specific position\n6.delete before specific position\n7.display linked list\n8.EXIT\n");
  scanf("%d",&choice);
  switch(choice)
  {
   case 1:
    insf();
    break;
   case 2:
    insl();
    break;
      case 3:
    insord();
    break;
   case 4:
    delf();
    break;
   case 5:
    delasp();
    break;
   case 6:
    delbsp();
    break;  
   case 7:
    display();
    break;
   case 8:
    exit(0);
   default :
    printf("\nenter valid choice");
  }
 }
}


happy coding✌😎

techsayright

Post a Comment

Hii

أحدث أقدم