Untitled


SUBMITTED BY: Guest

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

FORMAT: C++

SIZE: 1.1 kB

HITS: 1358

  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. //create the list
  12. node *head, *pos; //nlist = head
  13. int done = 1;
  14. int temp;
  15. node *p, *q;
  16. head = (node*)malloc(sizeof(node));
  17. head->num = 2;
  18. pos = (node*)malloc(sizeof(node));
  19. head->next = pos;
  20. pos->num = 1;
  21. pos->next = (node*)malloc(sizeof(node));
  22. pos->next->num = 4;
  23. pos = pos->next;
  24. pos->next = (node*)malloc(sizeof(node));
  25. pos->next->num = 3;
  26. pos->next->next = head;
  27. head = pos->next;
  28. //do the question
  29. do
  30. {
  31. p = head->next;
  32. done = 1;
  33. while (/*1*/)
  34. {
  35. q = p->next;
  36. if (p->num > q->num)
  37. {
  38. temp = p->num;
  39. p->num = q->num;
  40. q->num = temp;
  41. done = 0;
  42. }
  43. p = p->next;
  44. }
  45. }
  46. while (/*2*/);
  47. //print list
  48. pos = head;
  49. printf("%d ", pos->num);
  50. pos = pos->next;
  51. while (pos != head)
  52. {
  53. printf("%d ", pos->num);
  54. pos = pos->next;
  55. }
  56. getch();
  57. }

comments powered by Disqus