Untitled


SUBMITTED BY: Guest

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

FORMAT: C++

SIZE: 2.9 kB

HITS: 1242

  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. typedef struct stringType
  10. {
  11. char string[80];
  12. struct stringType *next;
  13. }stringRecord, *stringPtr;
  14. void main()
  15. {
  16. node *head, *pos;
  17. int done = 1;
  18. int temp;
  19. node *p, *q;
  20. stringPtr headS, posS, r,s;
  21. int i,m=0,n=0;
  22. head = (node*)malloc(sizeof(node));
  23. head->num = 2;
  24. pos = (node*)malloc(sizeof(node));
  25. head->next = pos;
  26. pos->num = 4;
  27. pos->next = (node*)malloc(sizeof(node));
  28. pos->next->num = 7;
  29. pos = pos->next;
  30. pos->next = (node*)malloc(sizeof(node));
  31. pos->next->num = 1;
  32. pos->next->next = head;
  33. head = pos->next;
  34. headS = (stringPtr)malloc(sizeof(stringRecord));
  35. strcpy(headS->string,"abc");
  36. posS = (stringPtr)malloc(sizeof(stringRecord));
  37. headS->next = posS;
  38. strcpy(posS->string,"hello");
  39. posS->next = (stringPtr)malloc(sizeof(stringRecord));
  40. posS = posS->next;
  41. posS->next = (stringPtr)malloc(sizeof(stringRecord));
  42. strcpy(posS->string,"the");
  43. posS = posS->next;
  44. posS->next = (stringPtr)malloc(sizeof(stringRecord));
  45. strcpy(posS->string,"Hi");
  46. posS = posS->next;
  47. posS->next = (stringPtr)malloc(sizeof(stringRecord));
  48. strcpy(posS->string,"surviving");
  49. posS = posS->next;
  50. posS->next = (stringPtr)malloc(sizeof(stringRecord));
  51. strcpy(posS->string,"string");
  52. posS = posS->next;
  53. posS->next = (stringPtr)malloc(sizeof(stringRecord));
  54. strcpy(posS->string,"bye");
  55. posS = posS->next;
  56. posS->next = headS;
  57. headS = posS;
  58. s = headS;
  59. posS = headS->next;
  60. printf("%s ", posS->string);
  61. posS = posS->next;
  62. while (posS != headS)
  63. {
  64. printf("%s ", posS->string);
  65. posS = posS->next;
  66. }
  67. do
  68. {
  69. p = head->next;
  70. done = 1;
  71. while (/*1*/p!=head)
  72. {
  73. q = p->next;
  74. if (p->num > q->num)
  75. {
  76. temp = p->num;
  77. p->num = q->num;
  78. q->num = temp;
  79. done = 0;
  80. }
  81. p = p->next;
  82. }
  83. }
  84. while (/*2*/done!=1);
  85. //print list
  86. pos = head->next;
  87. printf("%d ", pos->num);
  88. pos = pos->next;
  89. while (pos != head->next)
  90. {
  91. printf("%d ", pos->num);
  92. pos = pos->next;
  93. }
  94. if (head)
  95. {
  96. s = headS;
  97. do
  98. {
  99. p = p->next;
  100. n = p->num;
  101. for (i = m; i<n-1; i++) s = s->next;
  102. r = s->next;
  103. /*3*/;
  104. free(r);
  105. m = /*4*/;
  106. }
  107. while (p !=head);
  108. }
  109. posS = headS->next;
  110. printf("%s ", posS->string);
  111. posS = posS->next;
  112. while (posS != headS->next)
  113. {
  114. printf("%s ", posS->string);
  115. posS = posS->next;
  116. }
  117. getch();
  118. }

comments powered by Disqus