Sunday, February 16, 2020

15.Doubly Linked List :2- Create and traverse in doubly Linked List

/*****************************************************************************
Doubly linked list creation !               
@Mukesh Mann dated 10-02-2020.
*******************************************************************************/
#include <stdio.h>
/* Lets create structure of the node*/
             struct node
            {
                int data;
                struct node *prev;
                struct node *next;
            }*head; // Creating a global head pointer of struct type
/*  lets declare a function to create nodes*/
void createnode();
void traverselist();
int main()
 {
        int n;
        printf("Enter number of node\n\n");
        scanf("%d", &n);
        createnode(n);
        printf("\nTraversing the created list\n");
        traverselist();
 }
 void createnode(int n)
 {
     struct node *temp;
     int x ;
     //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", &x);
     temp=head;
     temp->prev=NULL;
     temp->next=NULL;
     temp->data = x;
     int k;
                       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", &k);
                         NewNode->data=k;
                         NewNode->next=NULL;
                         NewNode->prev=temp;
                         temp->next=NewNode;
                         //head->next->prev=head;
                         temp=temp->next;
                        
//printf("Printing Element of Linked List %d\n",head->data);
                  }
        printf("List created successfuly!\n");
       
 }
   
/*****
traverselist function
*****/ 
void traverselist()
{
   struct node *temp;
   temp=head;
   // printf("Value in temp is %d and temp points to %d", temp,temp->data);
    int i=1;
           while(temp!=NULL)
               {
               printf("Printing %d Element of Linked List - %d\n",i,temp->data);
                   i++;
                   temp=temp->next;
               }
               return;
}

1 comment:

  1. This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here keep up the good work linkedin ads tips

    ReplyDelete

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 ...