Advertisement
dsdeep

Answer-Of-3

Dec 12th, 2020
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.30 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. void createList();
  4. void printList();
  5. void insertAtLast();
  6. int countNode();
  7. void deleteFromMid();
  8.  
  9. typedef struct Database{
  10. int d;
  11. struct Database *n;
  12. }node;
  13.  
  14. node *h;
  15.  
  16. int main(void) {
  17.   createList();
  18.   printList();
  19.   insertAtLast();
  20.   printList();
  21.   deleteFromMid();
  22.   printList();
  23.   printf("%d",countNode());
  24.   return 0;
  25. }
  26.  
  27. void insertAtLast(){
  28.   printf("Enter Data : ");
  29.   int a;
  30.   scanf("%d",&a);
  31.   node *list=h;
  32.   while(list->n){
  33.     list=list->n;
  34.   }
  35.   node *tmp=(node*)malloc(sizeof(node));
  36.   tmp->d=a;
  37.   tmp->n=NULL;
  38.   list->n=tmp;
  39. }
  40.  
  41. void deleteFromMid(){
  42.   node *list=h;
  43. int c=countNode();
  44. c= (c%2==0)?c/2:(c/2)+1;
  45. int i=1;
  46. while(i<(c-1)){
  47.   list=list->n;
  48.   i++;
  49. }
  50. node *tmp=list->n;
  51. list->n=list->n->n;
  52. free(tmp);
  53. }
  54.  
  55. int countNode(){
  56.   node *list=h;
  57.   int i=0;
  58.   while(list){
  59.     i++;
  60.     list=list->n;
  61.   }
  62.   return i;
  63. }
  64.  
  65. void createList(){
  66.  
  67. node *n1=(node*)malloc(sizeof(node));
  68. node *n2=(node*)malloc(sizeof(node));
  69. node *n3=(node*)malloc(sizeof(node));
  70. node *n4=(node*)malloc(sizeof(node));
  71. n1->d=1;
  72. n1->n=n2;
  73. n2->d=2;
  74. n2->n=n3;
  75. n3->d=3;
  76. n3->n=n4;
  77. n4->d=4;
  78. n4->n=NULL;
  79. h=n1;
  80. }
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement