Advertisement
dsdeep

Assign27Nov-3

Nov 27th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.07 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. typedef struct Database{
  5.     int d;
  6.     struct Database *n;
  7. }node;
  8. node *h;
  9.  
  10. int main(){
  11. createList();
  12. printList();
  13. int c=countNode();
  14. printf("Total Node = %d\n",c);
  15. deleteNode();
  16. printList();
  17. findNode();
  18. printList();
  19.  
  20. }
  21. void deleteNode(){
  22. printf("Enter N : ");
  23. int n;
  24. scanf("%d",&n);
  25. int c=countNode();
  26. if(n<2||n>(c+1)){
  27.     printf("%d No Node Not Available\n",n-1);
  28.     return;
  29. }
  30. if(n==2){
  31.     printf("Deleting %d No Node\n",n-1);
  32.     node *tmp=h;
  33.     h=h->n;
  34.     free(tmp);
  35.     return;
  36. }
  37.     node *list=h;
  38.     int i=0;
  39.     while(list){
  40.             i++;
  41.         if(i==(n-2)){
  42.             node *tmp=list->n;
  43.             list->n=list->n->n;
  44.             free(tmp);
  45.             return;
  46.         }
  47.         list=list->n;
  48.     }
  49. }
  50. void findNode(){
  51. printf("Enter The Data : ");
  52. int x;
  53. scanf("%d",&x);
  54. if(x==h->d){
  55.     printf("Node Position = 1\n");
  56.     node *tmp=h;
  57.     h=h->n;
  58.     free(tmp);
  59.     tmp=(node*)malloc(sizeof(node));
  60.         printf("Enter Data In New Node : ");
  61.         int n;
  62.         scanf("%d",&n);
  63.         tmp->d=n;
  64.         tmp->n=h->n;
  65.         h=tmp;
  66.         return;
  67. }
  68. node *list=h;
  69. int i=0;
  70. while(list->n){
  71.         i++;
  72.     if(list->n->d==x){
  73.         printf("Node Position = %d\n",i+1);
  74.         node *tmp=list->n;
  75.         list->n=list->n->n;
  76.         free(tmp);
  77.         tmp=(node*)malloc(sizeof(node));
  78.         printf("Enter Data In New Node : ");
  79.         int n;
  80.         scanf("%d",&n);
  81.         tmp->d=n;
  82.         tmp->n=list->n;
  83.         list->n=tmp;
  84.         return;
  85.     }
  86.     list=list->n;
  87. }
  88. }
  89.  
  90. int countNode(){
  91. node *list=h;
  92. int i=0;
  93. while(list){
  94.     i++;
  95.     list=list->n;
  96. }
  97. return i;
  98. }
  99. void createList(){
  100. node *n1,*n2,*n3,*n4;
  101. n1=(node*)malloc(sizeof(node));
  102. n2=(node*)malloc(sizeof(node));
  103. n3=(node*)malloc(sizeof(node));
  104. n4=(node*)malloc(sizeof(node));
  105. n1->d=1;
  106. n1->n=n2;
  107. n2->d=2;
  108. n2->n=n3;
  109. n3->d=4;
  110. n3->n=n4;
  111. n4->d=5;
  112. n4->n=NULL;
  113. h=n1;
  114. }
  115. void printList(){
  116. node *list=h;
  117. while(list){
  118.     printf("%d ",list->d);
  119.     list=list->n;
  120. }
  121. printf("\n");
  122. }
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement