Saturday, October 13, 2018

Write A Program in C to Insert , Search , Delete , Traverse Double Linked List

C code to insert , search , delete , traverse in Double Linked List .



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

struct node
{
  int num;
  struct node *pre,*next;
};
struct node *head=NULL;

void create();
void delete(int);
int search(int);
void traverse();

//this function is for checking whether a list is empty or not
int checklist();

void main()
{
  int choice,item;
  while(1)
  {
  printf("1:Create 2:Delete 3:Search 4:Traverse 5:Exit \n");
  scanf("%d",&choice);
  switch (choice) {
    case /* value */1:  create();break;
    case 2:

    if(checklist()!=0)
    {
      printf("Enter item to be search \n");
      scanf("%d",&item);
      if(search(item)==1)
      delete(item);
    }
    break;

    case 3:

    if(checklist()!=0)
    {
      printf("Enter item to be search \n");
      scanf("%d",&item);
      search(item);
    }
    break;
    case 4: if(checklist()!=0)traverse();break;
    default: exit(4);
  }
}
}

void create()
{
struct node *temp,*trav;

//creating a temporary structure
temp=(struct node*)malloc(sizeof(struct node));

//checking wheter memory is allocated or not
if(temp==NULL)
{
  printf("Memory Allocation Failed \n");
}
else
{
  temp->pre=temp->next=NULL;
  printf("Enter the item to be entered \n");
  scanf("%d",&temp->num);

  if(head==NULL)
  {
    head=temp;
  }
  else
  {
    trav=head;
    while(trav->next!=NULL)
    {
      trav=trav->next;
    }
    trav->next=temp;
    temp->pre=trav;
  }
}

}

int search(int item)
{
struct node *trav;
int flag=0;
trav=head;


while(trav!=NULL)
{
  if(item==trav->num)
  {
    printf("Item is in list \n");
    flag=1;
    break;
  }
  trav=trav->next;
}

if(flag==0)
printf("Item is not in list \n");

printf("\n");
return flag;
}


void delete(int item)
{

//we are using three pointer --> pretrav trav postrav

struct node *trav,*pretrav,*postrav;

//pointing to head
trav=head;

//in deletion we have three cases
// delete first element
//delete mid element
//delete last element

// checking there is only one element , if yes then delete it and made head null
if(head->next==NULL && head->num==item)
{
  head=NULL;
  free(trav);
}

// checking if we need to delete firrst element and list having other elements too
else if(head->num==item&&head->next!=NULL)
{
//then simply moving head to next pointer
head=head->next;
head->pre=NULL;
}
//now below cases handle delete from mid and last
else
{
//now we are using two pointer
  pretrav=trav;
  while(trav!=NULL)
{
  //pretrav pointing to previous element  and trav will point to be deleted elemnt
  pretrav=trav;
  trav=trav->next;


  if(trav->num==item)
  {
    break;
  }
}

//if we need to delete element is last ones

 if(trav->next==NULL)
{
  //we just point previous pretrav next pointer to null and free trav pointer
  pretrav->next=NULL;
  free(trav);
}
else
{
//here we are changing the pointers
postrav=trav->next;
pretrav->next=postrav;
postrav->pre=pretrav;

free(trav);
}
}

  printf("Item deleted =%d\n",item);
  printf("\n");
}


void traverse()
{

struct node *trav;

//pointing to head
trav=head;

//traversing till last element
while(trav!=NULL)
{
  printf("%d ",trav->num);
  trav=trav->next;
}
printf("\n");
}

//below function is telling whether a list is empty or not
int checklist()
{
  if(head==NULL)
  {
  printf("List is empty \n");
  return 0;
  }
  else
  return 1;
}

No comments:

Post a Comment

Behavior Recognition System Based on Convolutional Neural Network

Our this article is on this  research paper .  Credit : Bo YU What we will do ? We build a set of human behavior recognition syste...