Untitled


SUBMITTED BY: Guest

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

FORMAT: C++

SIZE: 1.1 kB

HITS: 1397

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

comments powered by Disqus