Implementation of linked list.

Implementation of linked list/ 



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

struct node
{
 int data;
 struct node *link;
};

void insertbegin();
void display();
void insertend();
int length();
void insertatpos();
void deletebegin();
void deleteEnd();
void deletepos();

struct node *root=NULL;

int main()
{
 int choice;


 while(1)
 {
  printf("\nPress 1. for Insert node at begaining\n");
  printf("Press 2. for Insert node at End\n");
  printf("Press 3. for Insert node at At given position\n");
  printf("Press 4. for Delete node from begaining\n");
  printf("Press 5. for Delete node from End\n");
  printf("Press 6. for Delete Node from given position\n");
  printf("Press 7. for Display Linked List\n");
  printf("Press 8. for Find Length of Linked List\n");
  printf("Press 9. for Quit\n");
  printf("Enter your Choice : ");
  scanf("%d",&choice);  

   switch(choice)
  {
   case 1 : insertbegin();
      break;     


    case 2 : insertend();
      break;    

    case 3 : insertatpos();
      break;     

    case 4 : deletebegin();
      break;     

    case 5 : deleteEnd();
      break;     

    case 6 : deletepos();
      break;     

    case 7 : display();
      break;     

    case 8 : printf("%d",length());
      break;     

    case 9 : exit(0);    

    defaultprintf("Invalide Input please check : \n");

  }
 } 
 return 0;
}

void insertbegin()
{
 int data;
 struct node *temp;
 temp=(struct node *)malloc(sizeof(struct node));
 printf("Enter Data To insert : ");
 scanf("%d",&data);
 temp->data=data;
 temp->link=NULL

  if(root==NULL)
 {
  root=temp;
  printf("Node is Inserted Success Fully \n");
 }
 else

 {
  temp->link=root;
  root=temp;  
 }
}

void display()
{
 struct node *temp;
 temp=root

  while(temp!=NULL)
 {
  printf("%d-->",temp->data);
  temp=temp->link;
 }
 printf("\n\n");
}

void insertend()
{
 int data,i=0;
 int len=length();
 struct node *temp, *p;

 temp=(struct node *)malloc(sizeof(struct node));
 printf("Enter Data To insert : ");
 scanf("%d",&data);
 temp->data=data;
 temp->link=NULL;
 p=root

  if(root==NULL)
 {
  root=temp;
  printf("Node is Inserted Success Fully \n");
 }
 else
 {
  while(i<len-1)
  {
   p=p->link;
   i++;
  }
  p->link=temp;
  printf("Node inserted successfully\n");
 }
 
}


int length()
{
 int length=0;
 struct node *temp;
 temp=root;
 
 while(temp!=NULL)
 {
  length++;
  temp=temp->link;
 }
 return length;
 printf("\n"); 
}

void insertatpos()
{
 int data,loc,i=1;
 struct node *temp,*p;
 printf("Enter position where you want to ensert data\n");\
 scanf("%d",&loc);
 temp=(struct node *)malloc(sizeof(struct node));

 printf("Enter Data To insert : ");
 scanf("%d",&data);
 temp->data=data;
 temp->link=NULL;
 p=root

  if(loc>length())
 {
  printf("Invalid Location\n");
 }
 else
 {
  while(i<loc-1)
  {
   p=p->link;
   i++;
  }
  temp->link=p->link;
  p->link=temp;
 }
}

void deletebegin()
{

 struct node *temp;
 temp=root;

 if(root==NULL)
 {
  printf("No Element present in Linked List\n");
 }
 else
 {
  root=temp->link;
  temp->link=NULL;
  printf("First Node of Linked List has been deleted \n");
 }
}

void deleteEnd()
{
 int len=1;
 struct node *temp;
 temp=root;

 if(root==NULL)
 {

  printf("No Element present in Linked List \n");
 }
 else if(length()==1)
 {
  root=NULL;
  printf("Last Node has been deleted \n");
 }
 else
 {
  while(len<length()-1)
  {
   temp=temp->link;
   len++;
  }
  temp->link=NULL;
  printf("Last Node has been deleted \n");
 }
}

void deletepos()
{
 struct node *temp,*p;
 int pos,len=1;
 temp=root;

 printf("Enter the position of Node which do you want to delete \n");
 scanf("%d",&pos);
 if(pos>length())
 {
  printf("Invlide Position \n");
 }
 else
 {
  while(len<pos-1)
  {
   temp=temp->link;
   len++;
  }
  p=temp->link;
  temp->link=p->link;
  p->link=NULL;
  printf("Node of Location %d has been deleted \n",pos);  
 }
}







 




Post a Comment

If you have furthur QUERIES please let us know

Previous Post Next Post