Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2014
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define N 5
  4.  
  5. typedef struct node
  6. {
  7. int type;
  8. void *dataPtr;
  9. struct node *next;
  10. }tNode;
  11.  
  12. typedef struct nodeInfo
  13. {
  14. int count;
  15. tNode *head;
  16. }tStack;
  17.  
  18. typedef struct Name
  19. {
  20. int type; //type 0
  21. char name[16];
  22. int location; // record the used space number
  23. bool used;
  24. }tTypeName;
  25.  
  26. typedef struct Score
  27. {
  28. int type; //type 1
  29. int recordNumber;
  30. int location; // record the used space number
  31. bool used;
  32. }tTypeScore;
  33.  
  34. tTypeName nameBuf[N];
  35. tTypeScore scoreBuf[N];
  36.  
  37. tStack *createStack(void); //Create a stack
  38.  
  39. void pushStack (tStack *pStack, void *dataPtr); //push a dataPtr to a specific stack
  40. void *handlePopOperation (tStack *pStack); //pop a specific stack and return a void* dataPtr
  41. void printStackContentFromBottom(tStack *pStack); //print all content in stack
  42. int getNameSpace (tTypeName **ppName); //get the space from nameBuf, return the allocated number
  43. int getScoreSpace (tTypeScore **ppScore); //get the space from scoreBuf, return the allocated number
  44. void returnNameSpace (int location); //return the space of the used nameBuf
  45. void returnScoreSpace (int location); //return the space of the used scoreBuf
  46.  
  47. tStack* createStack(void)
  48. {
  49. tStack *pstack;
  50. pstack = (tStack *)malloc(sizeof(tStack));
  51. return pstack;
  52. }
  53.  
  54. void handlePushOperation(tStack *pStack)
  55. {
  56. int datatype=-1;
  57. tTypeName *pName;
  58. tTypeScore *pScore;
  59. int location;
  60. tNode *newnode;
  61.  
  62. bool key=true;
  63. while(key)
  64. {
  65. printf("Which data (0 or 1) you are going to push?");
  66. scanf("%d", &datatype);
  67.  
  68. if(datatype==0 && nameBuf[4].used==false) key=false;
  69. else if(datatype==1 && scoreBuf[4].used==false) key=false;
  70. else printf("Error Input!!\n");
  71.  
  72. }
  73.  
  74. switch(datatype)
  75. {
  76. case 0:
  77. printf("Please enter a string:");
  78. location = getNameSpace(&pName);
  79.  
  80. newnode = (tNode *)malloc(sizeof(tNode));
  81.  
  82. newnode->dataPtr=(void *)pName;
  83.  
  84. newnode->next = pStack->head;
  85. pStack->head = newnode;
  86. if(pStack->count==0) pStack->head = NULL;
  87. pStack->count++;
  88. break;
  89.  
  90. case 1:
  91. printf("Please enter a number:");
  92. location = getScoreSpace(&pScore);
  93. // xx
  94. break;
  95.  
  96. default:
  97. printf("Error Input!!\n");
  98. break;
  99. }
  100. }
  101.  
  102. int getNameSpace (tTypeName **ppName)
  103. {
  104. int i;
  105. for(i=0;i<5;i++) if(nameBuf[i].used==false) break;
  106.  
  107. *ppName=&nameBuf[i];
  108. scanf("%s",nameBuf[i].name);
  109. nameBuf[i].type = 0;
  110. nameBuf[i].location = i;
  111. nameBuf[i].used = true;
  112.  
  113. return i;
  114. }
  115.  
  116. int getScoreSpace (tTypeScore **ppScore)
  117. {
  118. int i;
  119. for(i=0;i<5;i++) if(scoreBuf[i].used==false) break;
  120.  
  121. *ppScore=&scoreBuf[i];
  122. scoreBuf[i].type = 1;
  123.  
  124. scanf("%d",&scoreBuf[i].recordNumber);
  125. scoreBuf[i].location = i;
  126. scoreBuf[i].used = true;
  127.  
  128. return i;
  129. }
  130. /*
  131. void handlePopOperation(tStack *pStack)
  132. {
  133. //if 0
  134. returnNameSpace(loc);
  135.  
  136. //if 1
  137. returnScoreSpace(loc);
  138.  
  139. }
  140. */
  141. void printStackContentFromBottom(tStack *pStack)
  142. {
  143. tNode *p = pStack->head;
  144. tTypeName *pName;
  145. tTypeScore *pScore;
  146. while (p!= NULL)
  147. {
  148. if(p->type==0)
  149.  
  150. {
  151. pName=(tTypeName *)p->dataPtr;
  152. puts(pName->name);
  153. }
  154. if(p->type==1) {
  155. pScore=(tTypeScore *)p->dataPtr;
  156. printf("%d\n",pScore->recordNumber);
  157. }
  158.  
  159. p = p->next;
  160. }
  161. }
  162.  
  163.  
  164. int main (void)
  165. {
  166. tStack *pStack;
  167. int op;
  168. pStack = createStack();
  169.  
  170. while (1)
  171. {
  172.  
  173. //menu
  174. printf("\n1. push a item to the stack\n");
  175. printf("2. pop a item to the stack\n");
  176. scanf("%d",&op);
  177. if(op==0){
  178. handlePushOperation(pStack);
  179. }
  180. else if(op==1){
  181. handlePopOperation(pStack);
  182. }
  183.  
  184. else {
  185. printf("Error Input!!\n");
  186. }
  187.  
  188.  
  189. printStackContentFromBottom(pStack);
  190. }
  191.  
  192.  
  193. return 0;
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement