Advertisement
Guest User

Untitled

a guest
Jul 12th, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct node
  5. {
  6. int data;
  7. struct node* next;
  8. };
  9.  
  10. void printList (struct node* head)
  11. {
  12. struct node* current;
  13. for (current = head; current != NULL; current = current->next)
  14. printf ("%dn",current->data);
  15. }
  16.  
  17. void appendNode (struct node** headRef, int data)
  18. {
  19. struct node* newNode = malloc(sizeof(struct node));
  20.  
  21. newNode->data = data;
  22. newNode->next = *headRef;
  23. *headRef = newNode;
  24. }
  25.  
  26. void reverseList (struct node** head)
  27. {
  28. struct node* past = NULL;
  29. struct node* present;
  30. struct node* future;
  31.  
  32. for (present = *head; present != NULL; )
  33. {
  34. future = present->next;
  35. present->next = past;
  36. past = present;
  37. present = future;
  38. }
  39. *head = past;
  40. }
  41.  
  42. void removeGreater (struct node** head)
  43. {
  44. struct node* current;
  45. struct node* prev;
  46. int max;
  47.  
  48. if (*head != NULL)
  49. {
  50. max = (*head)->data;
  51. prev = *head;
  52. for (current = (*head)->next; current != NULL;)
  53. {
  54.  
  55. if (current->data >= max)
  56. {
  57. max = current->data;
  58. prev = current;
  59. current = current->next;
  60. }
  61. else
  62. {
  63. if (current->next != NULL)
  64. {
  65. struct node* temp = current->next;
  66. current->data = temp->data;
  67. current->next = temp->next;
  68. free (temp);
  69. }
  70. else
  71. {
  72. prev->next = NULL;
  73. free (current);
  74. break;
  75. }
  76. }
  77. }
  78. }
  79. }
  80.  
  81. int main(void)
  82. {
  83.  
  84. struct node* head = NULL;
  85. struct node* tail;
  86. struct node* current;
  87.  
  88. int input,num,num1;
  89.  
  90. printf ("Enter no. of inputs n");
  91. scanf ("%d",&num);
  92. num1 = num;
  93. printf ("n");
  94. printf ("Enter the numbersn");
  95.  
  96. do
  97. {
  98. scanf("%d",&input);
  99. if (head == NULL)
  100. {
  101. appendNode (&head, input);
  102. tail = head;
  103. }
  104. else
  105. {
  106. appendNode (&tail->next, input);
  107. tail = tail->next;
  108. }
  109. num--;
  110. } while (num > 0);
  111.  
  112. printf ("n Before Reversing :n");
  113. printList (head);
  114.  
  115. reverseList(&head);
  116.  
  117. printf ("n After Reversing :n");
  118. printList (head);
  119.  
  120. removeGreater (&head);
  121. printf ("n After Removing :n");
  122. printList (head);
  123.  
  124. reverseList(&head);
  125. printf ("n Final Output :n");
  126. printList (head);
  127.  
  128.  
  129. return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement