dsdeep

Assign27Nov-2

Nov 26th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.70 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. findNode();
  14. printList();
  15. findNode();
  16. printList();
  17.  
  18. }
  19. void findNode(){
  20. printf("Enter The Position : ");
  21. int p;
  22. scanf("%d",&p);
  23. int c=countNode();
  24. if(p<1||p>c){
  25.     printf("**%d Position Not Available**\n",p);
  26.     return;
  27. }
  28. if(p==1){
  29.     printf("Node Data = %d\n",h->d);
  30.     printf("Deleting This Node\n");
  31.     node *tmp=h;
  32.     h=h->n;
  33.     free(tmp);
  34.     printf("Adding Node In Position : %d\n",p);
  35.     int x;
  36.     printf("Enter The Data : ");
  37.     scanf("%d",&x);
  38.     node *tmp1=(node*)malloc(sizeof(node));
  39.     tmp->d=x;
  40.     tmp->n=h;
  41.     h=tmp;
  42.     return;
  43. }
  44. node *list=h;
  45. int i=0;
  46. while(list){
  47.     i++;
  48.     if(i==(p-1)){
  49.         printf("Node Data = %d\n",list->n->d);
  50.             printf("Deleting This Node\n");
  51.         node *tmp=list->n;
  52.         list->n=list->n->n;
  53.     free(tmp);
  54.     printf("Adding Node In Position : %d\n",p);
  55.     int x;
  56.     printf("Enter The Data : ");
  57.     scanf("%d",&x);
  58.     node *tmp1=(node*)malloc(sizeof(node));
  59.     tmp->d=x;
  60.     tmp->n=list->n;
  61.     list->n=tmp;
  62.     return;
  63.     }
  64.     list=list->n;
  65. }
  66.  
  67. }
  68.  
  69. int countNode(){
  70. node *list=h;
  71. int i=0;
  72. while(list){
  73.     i++;
  74.     list=list->n;
  75. }
  76. return i;
  77. }
  78. void createList(){
  79. node *n1,*n2,*n3,*n4;
  80. n1=(node*)malloc(sizeof(node));
  81. n2=(node*)malloc(sizeof(node));
  82. n3=(node*)malloc(sizeof(node));
  83. n4=(node*)malloc(sizeof(node));
  84. n1->d=1;
  85. n1->n=n2;
  86. n2->d=2;
  87. n2->n=n3;
  88. n3->d=4;
  89. n3->n=n4;
  90. n4->d=5;
  91. n4->n=NULL;
  92. h=n1;
  93. }
  94. void printList(){
  95. node *list=h;
  96. while(list){
  97.     printf("%d ",list->d);
  98.     list=list->n;
  99. }
  100. printf("\n");
  101. }
  102.  
Add Comment
Please, Sign In to add comment