SLL, DLL, CLL Operations(class work)

 /*Syntax or Node structure of Single linked list*/

struct node

{

int data;


struct node *link;

};

struct node *root=NULL; (here root value intially NULL)


/*Creating nodes in single linked list*/

void create()

{

 struct node *second,*third,*fourth,*fifth;

 clrscr();

 root=malloc(sizeof(struct node));

 second=malloc(sizeof(struct node));

 third=malloc(sizeof(struct node));

 fourth=malloc(sizeof(struct node));

 fifth=malloc(sizeof(struct node));

 root->data=10;

 second->data=20;

 third->data=30;

 fourth->data=40;

 fifth->data=50;

 root->link=second;

 second->link=third;

 third->link=fourth;

fourth->link=fifth;

fifth->link=NULL;

}




/*insert at an end in SLL*/


void append()

{

struct node *temp;


    temp=(struct node*)malloc(sizeof(struct node));

    printf("enter node data:");

    scanf("%d",&temp->data);

    temp->link=NULL;

    if(root==NULL)

{

root=temp;

}

   else

   {

   struct node *p;

   p=root;

    while(p->link!=NULL)

      {

        p=p->link;

       }

   p->link=temp;

   }

  printf("\nOne node inserted!!!\n");

}



/*inserting a newnode at middle/position into a single linked list*/

void addafter()

{

    struct node *temp;

    int loc,len,i=1,*p;

    printf("enter location:");

    scanf("%d",&loc);

    len=length();

  if(loc>len)

   {

    printf("invalid location"); 

    printf("currently list is having %d nodes",len); 

   }

   else

   {

      p=root;

      while(i<loc)

     {

         p=p->link;

         i++;

      }

temp=(struct node *)malloc(sizeof(struct node));

  printf("enter node data:");

  scanf("%d",&temp->data);

  temp->link=NULL;

  temp->link=p->link;  /*right*/

  p->link=temp;       /*left */

}




/*add at beginning of a list in SLL*/


void addatbegin()

{

struct node *temp;

temp=(struct node *)malloc(sizeof(struct node));

    printf("enter node data:");

    scanf("%d",&temp->data);

    temp->link=NULL;

if(root==NULL)

{

root=temp;

}

else

{

temp->link=root;  (right-1)

root=temp;         (left-2)

}

}


/*displaying elements data in SLL*/


void display()

{

     struct node *temp;

     temp=root;

    if(temp==NULL)

    {

printf("No nodes in the list");

    }

    else

    {

while(temp!=NULL)

{

        printf("%d",temp->data);

        temp=temp->link;

        }

    }

}


/*finding a length of a list in SLL*/


int length()

{

int count=0;

struct node *temp;

temp=root;

  while(temp!=NULL)

  {

  count++;

  temp=temp->link;

  }

return count;

}




/*delete at begin in SLL*/


void deleteatbegin()

{

 printf("enter location to delete:");

 scanf("%d",&loc);

 if(loc>length)

   {

    printf("invalid location"); 

   }

 else if(loc==1)

 {

  temp=root;

  root=temp->link;

  temp->link=NULL;

  free(temp);

 }


/*delete at middle in SLL*/

void deleteatmiddle()

{

 printf("enter location to delete:");

 scanf("%d",&loc);

 if(loc>length)

   {

    printf("invalid location"); 

   }

else

{

struct node *p=root,*q;

int i=1;

 while(i<loc-1)

 {

  p=p->link;

  i++;

 }

q=p->link;

p->link=q->link;

q->link=NULL;

free(q);

}


/*delete at end is also same as above*/

consider location =4 and perform delete at middle operation.


void deleteatend()

{

 printf("enter location to delete:");

 scanf("%d",&loc);

 if(loc>length)

   {

    printf("invalid location"); 

   }

else

{

struct node *p=root,*q;

int i=1;

 while(i<loc-1)

 {

  p=p->link;

  i++;

 }

q=p->link;

p->link=q->link;

q->link=NULL;

free(q);

}


/* Syntax or Node structure for circular linked list*/

struct node

{

 int data;

 struct node *link;

};

struct node *root=NULL;



/* Creating a nodes in circular linked list*/

void create()

{

struct node *second,*third,*fourth,*fifth;

clrscr();

root=malloc(sizeof(struct node));

second=malloc(sizeof(struct node));

third=malloc(sizeof(struct node));

fourth=malloc(sizeof(struct node));

fifth=malloc(sizeof(struct node));

root->data=10;

second->data=20;

third->data=30;

fourth->data=40;

fifth->data=50;

root->link=second;

second->link=third;

third->link=fourth;

fourth->link=fifth;

fifth->link=root;

}

/*inserting newnode at beginning of the circular linked list*/ 

void addFirst(int val)

{

struct node *temp=malloc(sizeof(struct node));

temp->data=val;

if(root==NULL)

{

temp->link=temp;

root=temp;

}

else

{

struct node *p;

p=root;

while(p->link!=root)

{

p=p->link;

}

p->next=temp;

temp->link=root;

root=temp;

}

}


/*inserting a newnode at end of the circular linked list*/ 

void addatEnd(int val)

{

struct node *temp=malloc(sizeof(struct node));

temp->data=val;

if(root==NULL)

{

temp->link=temp;

root=temp;

}

else

 {

 struct node *p;

 p=root;

  while(p->link!=root)

  {

  p=p->link;

  }

 p->link=temp;

 temp->link=root;

 }  

}


/*inserting a newnode at "middle/position" of the circular linked list*/ 


void addafter(int val)

{

int pos,i=1;

struct node *temp,*p;

temp=malloc(sizeof(struct node));

temp->data=val;

temp->link=NULL;

printf("enter the location");

scanf("%d",&loc);

p=root;

  while(i<loc)

  {

  p=p->link;

  i++;

  }

temp->link=p->link;

p->link=temp;

}


/* Displaying nodes data in CLL*/


void display()

{

struct node *temp;

temp=root;

do

{

printf("%d--",temp->data);

temp=temp->link;

}while(temp!=root);

}


/*delete at begin in CLL*/


void deleteatbegin()

{

 printf("enter location to delete:");

 scanf("%d",&loc);

 if(loc>length)

   {

    printf("invalid location"); 

   }

 else if(loc==1)

 {

  temp=root;

  root=temp->link;

  temp->link=NULL;

  free(temp);

 }


/*delete at middle in CLL*/

void deleteatmiddle()

{

 printf("enter location to delete:");

 scanf("%d",&loc);

 if(loc>length)

   {

    printf("invalid location"); 

   }

else

{

struct node *p=root,*q;

int i=1;

 while(i<loc-1)

 {

  p=p->link;

  i++;

 }

q=p->link;

p->link=q->link;

q->link=NULL;

free(q);

}


/*delete at end is also same as above*/

consider location =4 and perform delete at middle operation(consider list is having 4 nodes).


void deleteatend()

{

 printf("enter location to delete:");

 scanf("%d",&loc);

 if(loc>length)

   {

    printf("invalid location"); 

   }

else

{

struct node *p=root,*q;

int i=1;

 while(i<loc-1)

 {

  p=p->link;

  i++;

 }

q=p->link;

p->link=q->link;

q->link=root;

free(q);

}


/*DOUBLE Linked List*/

/* Node structure*/

struct node

{

int data;

struct node *left;

struct node *right;

};

struct node *root=NULL; (here root value intially NULL)



/* Creating a nodes in DOUBLE Linked List*/

void create()

{

 struct node *second,*third,*fourth,*fifth;

 clrscr();

 root=malloc(sizeof(struct node));

 second=malloc(sizeof(struct node));

 third=malloc(sizeof(struct node));

 fourth=malloc(sizeof(struct node));

 fifth=malloc(sizeof(struct node));

 root->data=10;

 second->data=20;

 third->data=30;

 fourth->data=40;

 fifth->data=50;

root->left=NULL;

root->right=second;

second->left=root;

second->right=third;

third->left=second;

third->right=fourth;

fourth->left=third;

fourth->right=fifth;

fifth->left=fourth;

fifth->right=NULL;

}


/*adding a newnode at end of the DLL*/

void append()

{

struct node *temp;


    temp=(struct node *)malloc(sizeof(struct node));

    printf("enter node data:");

    scanf("%d",&temp->data);

    temp->left=NULL;

    temp->right=NULL;

    if(root==NULL)

{

root=temp;

}

   else

   {

  struct node *p;

p=root;

    while(p->right!=NULL)

      {

        p=p->right;

       }

    p->right=temp;

    temp->left=p;

    }

  printf("\nOne node inserted!!!\n");

}




/*add at beginning of a list*/


void addatbegin()

{

struct node *temp;

temp=(struct node*)malloc(sizeof(struct node));

    printf("enter node data:");

    scanf("%d",&temp->data);

    temp->left=NULL;

    temp->right=NULL;

 if(root==NULL)

 {

 root=temp;

 }

  else

   {

   temp->right=root;  (right-1)

   root->left=temp;

   root=temp;        

}

/*Displaying elements data in DLL*/

void display()

{

struct node *temp;

temp=root;

while(temp!=NULL)

{

printf("%d--",temp->data);

temp=temp->right;

}

}

/* DELETE AT BEGINING IN DLL*/

void delete_at_begin()

{

temp=root;

root=root->right;

temp->right=NULL;

root->left=NULL;

        free(temp);

/* delete at ending in DLL*/

void delete_at_end()

{

temp=p;

p=p->left;

temp->left=NULL;

p->right=NULL;

}

/* delete at middle/position in DLL*/

void delete_at_pos()

{

printf("\n enter position\n");

scanf("%d",&loc);

temp=root;

for(i=0;i<loc-1;i++)

temp=temp->right;

temp->right=temp->right->right;

temp->right->left=temp;

}

/* reversing a list in DLL*/

void  reverse()

{

temp=p;

while(temp!=NULL)

{

printf("%d<->",temp->data);

temp=temp->left;

}

}
































Comments

Popular posts from this blog

Conversion of Infix to Postfix Notation lab program

Unit-2 chapter-1 STACKS AND ITS APPLICTIONS

Data Structures UNIT-1 Chapter-1