Miti059

insert first (doubly)

Oct 13th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.83 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct Node
  4. {
  5.   int a;
  6.   struct Node *next,*prev;
  7.   }node;
  8. node*head=NULL,*tail=NULL;
  9.  
  10. void insert_first (int an)
  11. {
  12. node*N=(node*)malloc(sizeof(node));
  13. N->a=an;
  14.  
  15. N->next=NULL;
  16. N->prev=NULL;
  17.  
  18. if(head==NULL){
  19.    head=N;
  20.    tail=N;
  21. }
  22. else{
  23.    head->prev=N;
  24.    N->next=head;
  25.    head=N;
  26. }
  27. }
  28. void display_first()
  29. {
  30.     node*list=head;
  31.     if(head==NULL)
  32.     {
  33.         printf("No Data Available\n");
  34.  
  35.  
  36.     }
  37.     else
  38.     {
  39.         while(list!=NULL)
  40.         {
  41.             printf("%d\n",list->a);
  42.             list=list->next;
  43.         }
  44.     }
  45.     }
  46. int main(){
  47. int n,x,i;
  48.     head=NULL;
  49.     printf("Input the Number of Node:");
  50.     scanf("%d",&n);
  51.     for(i=0;i<n;i++){
  52.         scanf("%d",&x);
  53.         insert_first(x);
  54.  
  55.     }
  56. display_first();
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment