Advertisement
dsdeep

Assign27Nov-1

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