Advertisement
Guest User

Untitled

a guest
May 29th, 2015
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.49 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define INPUT int
  5.  
  6. struct element;
  7. struct stack;
  8.  
  9. typedef struct element * POINTER;
  10. typedef struct stack * LIST;
  11.  
  12. typedef struct element
  13. {
  14. INPUT data;
  15. POINTER next;
  16. } ELEMENT;
  17.  
  18. typedef struct stack
  19. {
  20. POINTER head;
  21. POINTER size;
  22. };
  23.  
  24. void insert (LIST list, INPUT input)
  25. {
  26.  
  27. POINTER newElement = malloc(sizeof(ELEMENT));
  28. newElement->data = input;
  29. newElement->next = NULL;
  30.  
  31. if (list->head==NULL)
  32. {
  33. list->head = newElement;
  34. list->size = newElement;
  35. } else
  36. {
  37. list->size->next = newElement;
  38. list->size = newElement;
  39. }
  40. }
  41.  
  42. void showList(LIST list, int o){
  43.  
  44.  
  45. POINTER pom = list->head;
  46. int i = 1;
  47. printf("=============================\n");
  48. printf("Prikaz elemenata liste\n");
  49. printf("=============================\n");
  50.  
  51. while (pom!=NULL)
  52. {
  53. if (o == 0)
  54. printf("\t%d.element liste je %d\n",i, pom->data);
  55. else
  56. printf("\t%d.element liste je %c\n",i, pom->data);
  57. i++;
  58. pom = pom->next;
  59. }
  60.  
  61. printf("=============================\n");
  62. }
  63.  
  64. int getNumber(int input, int number)
  65. {
  66. number = number * 10 + (input - 48);
  67. return number;
  68. }
  69.  
  70. int popFirst (LIST list)
  71. {
  72. POINTER current;
  73.  
  74. if (list->head==NULL)
  75. {
  76. printf("NE MOZE SE IZBACITI ELEMENT IZ LISTE ZATO STO JE LISTA PRAZNA");
  77. return 0;
  78. }
  79.  
  80. printf("\nFIRSTIzbacuje se element %d\n", list->head->data);
  81. current = list->head;
  82. if (list->head == list->size)
  83. {
  84. list->head = NULL;
  85. list->size = NULL;
  86.  
  87. return 0;
  88. }
  89. else
  90. {
  91. list->head = list->head->next;
  92. }
  93.  
  94. free(current);
  95.  
  96. return 1;
  97.  
  98. }
  99.  
  100. int popLast (LIST list)
  101. {
  102.  
  103. POINTER current;
  104.  
  105. if (list->head==NULL)
  106. {
  107. printf("NE MOZE SE IZBACITI ELEMENT IZ LISTE ZATO STO JE LISTA PRAZNA");
  108. return 0;
  109. }
  110.  
  111. printf("\nLastIzbacuje se element %d\n", list->size->data);
  112. current = list->head;
  113. if (list->head == list->size)
  114. {
  115. list->head = NULL;
  116. list->size = NULL;
  117. }
  118. else
  119. {
  120.  
  121. while(current->next != list->size)
  122. {
  123. current = current->next;
  124. }
  125.  
  126. list->size = current;
  127. list->size->next = NULL;
  128. current = current->next;
  129. }
  130.  
  131. free(current);
  132.  
  133. return 1;
  134.  
  135. }
  136.  
  137. int popN(LIST list, POINTER kick, POINTER past){
  138.  
  139. //POINTER current = list->head;
  140. //POINTER past = NULL;
  141.  
  142. /*while(current!=NULL && current->data!=zadataVrednost)
  143. {
  144. past = current;
  145. current = current->next;
  146. }*/
  147.  
  148. printf("usao 1 \n");
  149. if (kick==list->head)
  150. {
  151. printf("usao 2 \n");
  152. int checker;
  153. checker = popFirst (list);
  154. if (checker == 0) return 0;
  155.  
  156. }
  157. else
  158. {
  159. printf("usao 3 \n");
  160. if (kick==list->size)
  161. {
  162. printf("usao 4 \n");
  163. return popLast (list);
  164. }
  165. else
  166. {
  167. printf("usao 5 \n");
  168. past->next = kick->next;
  169. free(kick);
  170. }
  171. }
  172.  
  173. return 1;
  174. }
  175.  
  176. void searchPriority (LIST numbers, LIST operators)
  177. {
  178. POINTER currentNumber, currentOperator, pastNumber, pastOperator;
  179.  
  180. currentNumber = numbers->head;
  181. currentOperator = operators->head;
  182.  
  183. pastNumber = numbers->head;
  184. pastOperator = operators->head;
  185.  
  186. while(currentOperator!=NULL)
  187. {
  188. if (currentOperator->data == 42)
  189. {
  190. POINTER pastNumberCopy, pastOperatorCopy, currentNumberCopy, currentOperatorCopy;
  191.  
  192. currentNumber->next->data = currentNumber->data * currentNumber->next->data;
  193.  
  194. showList(numbers, 0);
  195. printf("aaaa\n");
  196. showList(operators, 1);
  197.  
  198. if (currentNumber != numbers->head)
  199. pastNumberCopy = currentNumber;
  200. else
  201. {
  202. pastNumberCopy = currentNumber->next;
  203. numbers->head = currentNumber->next;
  204. }
  205.  
  206. currentNumberCopy = currentNumber->next;
  207. printf("NUMBER >> prije izbacivanja: trenutni %d, prethodni %d\n", currentNumber->data, pastNumber->data);
  208. popN(numbers, currentNumber, pastNumber);
  209. //printf("poslije izbacivanja: trenutni %d, prethodni %d\n", currentNumber->data, pastNumber->data);
  210.  
  211. pastNumber = pastNumberCopy;
  212. currentNumber = currentNumberCopy;
  213. printf("NUMBER >> poslije izbacivanja: trenutni %d, prethodni %d\n", currentNumber->data, pastNumber->data);
  214.  
  215. if (currentOperator != operators->head)
  216. pastOperatorCopy = currentOperator;
  217. else
  218. {
  219. pastOperatorCopy = currentOperator->next;
  220. operators->head = currentOperator->next;
  221. }
  222.  
  223. currentOperatorCopy = currentOperator->next;
  224. printf("OPERATOR >> prije izbacivanja: trenutni %d, prethodni %d\n", currentOperator->data, pastOperator->data);
  225. popN(operators, currentOperator, pastOperator);
  226. pastOperator = pastOperatorCopy;
  227. pastNumber = pastNumberCopy;
  228.  
  229. printf("OPERATOR >> prije izbacivanja: trenutni %d, prethodni %d\n", currentOperator->data, pastOperator->data);
  230.  
  231. printf("OPERATOR: \nglava %d\nrep %d", operators->head->data, operators->size->data);
  232. continue;
  233. }
  234.  
  235. pastNumber = currentNumber;
  236. currentNumber = currentNumber->next;
  237.  
  238. pastOperator = currentOperator;
  239. currentOperator = currentOperator->next;
  240. }
  241. }
  242. int main(void)
  243. {
  244. int number = 0;
  245. INPUT input;
  246. LIST numbers, operators;
  247.  
  248. numbers = malloc (sizeof(LIST));
  249. numbers->head = NULL;
  250. numbers->size = NULL;
  251.  
  252. operators = malloc (sizeof(LIST));
  253. operators->head = NULL;
  254. operators->size = NULL;
  255. do
  256. {
  257. //fflush(stdin);
  258. input = getchar();
  259.  
  260. if (input == '\n')
  261. {
  262. insert (numbers, number);
  263. break;
  264. }
  265.  
  266. if (input >=42 && input <= 47)
  267. {
  268. if (number != 0)
  269. {
  270. insert (numbers, number);
  271. number = 0;
  272. }
  273.  
  274. insert (operators, input);
  275. }
  276. else
  277. {
  278. if (number == 0 && input == 48)
  279. insert (numbers, number);
  280.  
  281. number = getNumber (input, number);
  282. }
  283.  
  284.  
  285. } while (input!='\n');
  286.  
  287. showList(numbers, 0);
  288. showList(operators, 1);
  289.  
  290. searchPriority (numbers, operators);
  291.  
  292. showList(numbers, 0);
  293. showList(operators, 1);
  294.  
  295. return 0;
  296. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement