Saleh127

Odd Even

Feb 28th, 2020
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct node
  4. {
  5. int data;
  6. struct node *next;
  7. };
  8. int main()
  9. {
  10. struct node *prev,*head,*p;
  11. int n,i;
  12. printf("Input the number of nodes ");
  13. scanf("%d",&n);
  14. head=NULL;
  15. for(i=1;i<=n;i++)
  16. {
  17. p=(struct node*)malloc(sizeof(struct node));
  18. printf("Input data for node %d: ",i);
  19. scanf("%d",&p->data);
  20. p->next=NULL;
  21. if(head==NULL){
  22. head=p;
  23. }
  24. else{
  25. prev->next=p;
  26. }
  27. prev=p;
  28. }
  29. struct node* ptr;
  30. ptr=head;
  31. int e=0,o=0;
  32. while(ptr!=NULL)
  33. {
  34. if(ptr->data%2==0) e++;
  35. else o++;
  36. ptr=ptr->next;
  37. }
  38. if(e==0)
  39. {
  40. printf("Even list is empty\n");
  41. }
  42. else
  43. {
  44. printf("Even list is :\n");
  45. ptr=head;
  46. while(ptr!=NULL)
  47. {
  48. if(ptr->data%2==0)
  49. {
  50. printf("%d ",ptr->data);
  51. }
  52. ptr=ptr->next;
  53. }
  54. printf("\n");
  55. }
  56. if(o==0)
  57. {
  58. printf("Odd list is empty\n");
  59. }
  60. else
  61. {
  62. printf("Odd list is :\n");
  63. ptr=head;
  64. while(ptr!=NULL)
  65. {
  66. if(ptr->data%2==1)
  67. {
  68. printf("%d ",ptr->data);
  69. }
  70. ptr=ptr->next;
  71. }
  72. printf("\n");
  73. }
  74. return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment