Sunday, February 16, 2020

13. Complete Program for Singly Linked List

// This program is summation of all the sub function we have covered from 1-12

/*****************************************************************************
                    
@Mukesh Mann dated 27-01-2020.
*******************************************************************************/
#include <stdio.h>
/* Lets create structure of the node*/
             struct node
            {
                int data;
                struct node *link;
            }*head; // Creating a global head pointer of struct type
/*  lets declare a function to create nodes*/
 void createnode(int n);
 void inseratbegning();
 void inseratend();
 void traverselist();
 void inseratanyindex();
 void deletnodefrombegning();
 void deletelastnode();
 void deleteatanyindex();
 void movelastnodetofront();
 struct node * Reverseiterative(struct  node *);
void ReverseusingRecursion(struct  node *,struct node *);

  //void struct node *ReverseusingRecurssion(struct node);
/* lets call main function */

 int main()
 {
        int n;
        printf("Enter number of node\n\n");
        scanf("%d", &n);
        createnode(n);
        printf("Inserting a new node with value 40 at the begning of  list\n\n");
        inseratbegning();
        printf("Inserting a new node with value 70 at the end of list\n\n");
        inseratend();
        printf("\nTraversing the created list\n");
        traverselist();
        printf("Inserting a new node with value 90 after node 2 in the linked list\n\n");
        inseratanyindex();
        printf("After inserting current list is as under\n\n ");
        traverselist();
        printf("Deleteing firt node from list\n");
        deletnodefrombegning();
        printf("After deleteing current list values are\n\n ");
       traverselist();
       printf("----------------------------------Delete node from last--------------------\n");
       deletelastnode();
       printf("After deleteing last node the  current list values are\n\n ");
       traverselist();
       printf("----------------------------------Delete node WITH VALUE =3 IN THE LIST--------------------\n");
       deleteatanyindex();
       printf("After deleteing node WITH VALUE =3;  the  current list values are\n\n ");
       traverselist();
       printf("Move last node to front of node\n\n ");
       movelastnodetofront();
       printf("----------------------AfterMoving last node to front the list is --------------\n\n ");
       traverselist();
     printf("----------------------An Iterative verison to reverse data in linked list --------------\n\n ");
     //*Reverseiterative function will return a pointer and takes a pointer
     head= (*Reverseiterative)(head); 
      traverselist();
     
     printf("----------------------A Recursive verison to reverse data in linked list --------------\n\n ");
     //*Reverseiterative function will return a pointer and takes a pointer
     ReverseusingRecursion(NULL,head); 
      traverselist();
       return 0;
 }
 void createnode(int n)
 {
    
     int data ;
     struct node *temp;
    /* lets create first node using malloc */
    head=(struct node*)malloc(sizeof(struct node));
    
                 if(head == NULL)
                 {
                     printf("Memory not allocated ");
                     return;
                    
                 }
     printf("Enter first node data ");
     scanf("%d", &data);
     temp=head;     
     head-> data = data;
     head->link=NULL;
     
     
                  for(int i=2; i<=n; i++)
                  {
                     
                   struct node  *NewNode=(struct node *)malloc(sizeof(struct node));
                          if(NewNode== NULL)
                            {
                               printf("Memory not allocated ");
                               break;
                             }
                         printf("Enter  node data ");
                         scanf("%d", &data);
                         NewNode->data=data;
                         NewNode->link=NULL;
                         temp->link=NewNode;         // linking current node with NewNode
                         temp = temp->link;   // Moving temp node to NewNode location
                  }
        printf("List created successfuly!\n");
                
      }
   
/*****
traverselist function
*****/ 
void traverselist()
{
    struct node *temp=head;
    int i=1;
           while(temp)
               {
               printf("Printing %d Element of Linked List %d\n",i,temp->data);
                   i++;
                   temp= temp->link;
               }
}
/*****
inseratbegning function
*****/     
void inseratbegning()
{
          printf("\nLets insert a node at the begninig\n");
          struct node *mynode;
          mynode=(struct node *)malloc(sizeof(struct node));
         
          mynode->link=head;
          mynode->data=40;
          head=mynode;
        printf("New Node Inserted Successfuly at the begning!\n");
}
         
/*****
inseratEnd function
*****/     
void inseratend()
{
          printf("\nLets insert a node at the End\n");
          struct node *mynode;
          struct node *temp=head;
          mynode=(struct node *)malloc(sizeof(struct node));
         
          while(temp->link!=NULL)
          {
             
             temp=temp->link;
             
          }
          temp->link=mynode;
          mynode->link=NULL;
          mynode->data=70;
          printf("New Node Inserted Successfuly at the End!\n");
}
    
/*****
inseratanyindex function
*****/     
void inseratanyindex()
{
printf("\nLets insert a node at the End\n");
 struct node *mynode;
 struct node *temp=head;
 mynode=(struct node *)malloc(sizeof(struct node));
         
          while(temp->data!=2)
          {
             temp=temp->link;
             
          }
          mynode->link=temp->link;
          temp->link=mynode;
          mynode->data=90;
         printf("New Node Inserted Successfuly at the after node 2!\n"); 
}
/*****
deletnodefrombegning()
*****/
deletnodefrombegning()
{
 struct node *temp=head;
 if(head== NULL)
 {
    printf("THERE IS NO NODE TO DELETE\n");  
 }
 if(head->link==NULL) // there is only one node
 {
     free(head);
     head=NULL;
 }
 head=head->link;
 free(temp);
}

   /*****
deletelastnode()
*****/
deletelastnode()
{
 struct node *temp=head;
 while(temp->link->link!=NULL)
 {
   temp=temp->link;  
 }
 temp->link->link=NULL;
 free(temp->link);
 temp->link=NULL;
}
/*****
deleteatanyindex()
*****/
deleteatanyindex()
{
  struct node *temp=head;
  while(temp->link->data!=3)
  {
     temp=temp->link;  
  }
  struct node *temp1=temp->link;
  temp->link=temp1->link->link;
  free(temp1);
  temp1= NULL;
}
 /*****
Moving Last node to the front of linked list
movelastnodetofront()
*****/
// Lets have two pointer (p,q) one will be one step ahead of other;
// the moment one pointer (p) reaches last node ; point its link to head,
// and put null into q ;
movelastnodetofront()
{

 struct node *p,*q;
 p=head;
  while(p->link != NULL)
{
   
    q=p;
    p=p->link;
}
 q->link= NULL;
 p->link=head;
 head=p;
}
/*****
Reverseing content of linked list using Iterative version
Reverseiterative() is a function pointer which returns a pointer to a structure
and it takes head as its initial pointer as parameter
*****/
 struct node *Reverseiterative(struct  node *curr)
{
  
struct node *prev= NULL;
   struct node *nextnode= NULL;
    while(curr)
    {
     nextnode =curr->link;
     curr->link= prev;
     prev=curr;
     curr=nextnode;
    }
    head=prev;
      printf("value of current node is%d",head);
   return prev;
   }
  
 /*****
Reverseing content of linked list using Recursion
ReverseusingRecursion() is a function  which returns a pointer to a structure
and it takes NULL in prev node and head in curr.
*****/
void ReverseusingRecursion(struct node *prev, struct node *curr)
{
    if(curr)
    {
     ReverseusingRecursion(curr,curr->link);
     curr->link=prev;
    }
    else
    head=prev;
}

No comments:

Post a Comment

9_Regular Expressions

Regular expressions- Sometimes in HTML, the developer defines more than one class name ( that’s class input has more than one name Here ...