Untitled


SUBMITTED BY: Guest

DATE: Nov. 26, 2014, 12:46 p.m.

FORMAT: C++

SIZE: 1.1 kB

HITS: 1199

  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 = NULL;
  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. do
  28. {
  29. p = head->next;
  30. done = 1;
  31. while (/*1*/)
  32. {
  33. q = p->next;
  34. if (p->num > q->num)
  35. {
  36. temp = p->num;
  37. p->num = q->num;
  38. q->num = temp;
  39. done = 0;
  40. }
  41. p = p->next;
  42. }
  43. }
  44. while (/*2*/);
  45. //print list
  46. pos = head;
  47. printf("%d ", pos->num);
  48. pos = pos->next;
  49. while (pos != head)
  50. {
  51. printf("%d ", pos->num);
  52. pos = pos->next;
  53. }
  54. getch();
  55. }

comments powered by Disqus