Untitled


SUBMITTED BY: Guest

DATE: Nov. 26, 2014, 1:03 p.m.

FORMAT: C++

SIZE: 2.2 kB

HITS: 1240

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. typedef struct numType
  5. {
  6. int num;
  7. struct numType *next;
  8. }node;
  9. void main()
  10. {
  11. node *head, *pos;
  12. int done = 1;
  13. int temp;
  14. node *p, *q;
  15. head = (node*)malloc(sizeof(node));
  16. head->num = 2;
  17. pos = (node*)malloc(sizeof(node));
  18. head->next = pos;
  19. pos->num = 1;
  20. pos->next = (node*)malloc(sizeof(node));
  21. pos->next->num = 4;
  22. pos = pos->next;
  23. pos->next = (node*)malloc(sizeof(node));
  24. pos->next->num = 3;
  25. pos->next->next = head;
  26. head = pos->next;
  27. //print list before sorting
  28. pos = head->next;
  29. printf("Before sorting:");
  30. printf("%d ", pos->num);
  31. pos = pos->next;
  32. while (pos != head->next)
  33. {
  34. printf("%d ", pos->num);
  35. pos = pos->next;
  36. }
  37. printf("\n");
  38. do
  39. {
  40. p = head->next;
  41. done = 1;
  42. while (done)
  43. {
  44. q = p->next;
  45. if (p->num > q->num)
  46. {
  47. temp = p->num;
  48. p->num = q->num;
  49. q->num = temp;
  50. done = 0;
  51. }
  52. p = p->next;
  53. }
  54. }while (p!=head);
  55. //print list
  56. pos = head->next;
  57. printf("After sorting:");
  58. printf("%d ", pos->num);
  59. pos = pos->next;
  60. while (pos != head->next)
  61. {
  62. printf("%d ", pos->num);
  63. pos = pos->next;
  64. }
  65. getch();
  66. }

comments powered by Disqus