Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. #include<iostream>
  2. #include<string.h>
  3. #include<vector>
  4. using namespace std;
  5.  
  6. struct LNode //tagnodelist
  7. {
  8. char key;
  9. LNode *pNext;
  10. };
  11.  
  12. struct TNode
  13. {
  14. char key;
  15. TNode *pLeft;
  16. TNode *pRight;
  17. TNode *pNext;
  18. };
  19.  
  20. struct Stack
  21. {
  22. LNode *pHead;
  23. LNode *pTail;
  24. };
  25.  
  26. struct Queue
  27. {
  28. LNode *pHead;
  29. LNode *pTail;
  30. };
  31.  
  32. struct StackTree
  33. {
  34. TNode *pHead;
  35. TNode *pTail;
  36. };
  37.  
  38. LNode* CreateNode(char x)
  39. {
  40. LNode *p= new LNode;
  41. if (p == NULL);
  42. else
  43. {
  44. p->key = x;
  45. p->pNext = NULL;
  46. }
  47. return p;
  48. }
  49.  
  50. TNode* CreateTNode(char x)
  51. {
  52. TNode *p = new TNode;
  53. p->key = x;
  54. p->pLeft = p->pRight = NULL;
  55. p->pNext = NULL;
  56. return p;
  57. }
  58.  
  59. void CreateStack(Stack &S)
  60. {
  61. S.pHead = NULL;
  62. S.pTail = NULL;
  63. }
  64.  
  65. void CreateQueue(Queue &Q)
  66. {
  67. Q.pHead = NULL;
  68. Q.pTail = NULL;
  69. }
  70.  
  71. void CreateStackTree(StackTree &ST)
  72. {
  73. ST.pHead = NULL;
  74. ST.pTail = NULL;
  75. }
  76.  
  77. void PushStack(Stack &S, char x)
  78. {
  79. LNode *p = CreateNode(x);
  80. if (S.pHead == NULL)
  81. {
  82. S.pHead = p;
  83. S.pTail = S.pHead;
  84. }
  85. else
  86. {
  87. p->pNext = S.pHead;
  88. S.pHead = p;
  89. }
  90. }
  91.  
  92. char PopStack(Stack &S)
  93. {
  94. char a = S.pHead->key;
  95. LNode *p = S.pHead;
  96. if (S.pHead == S.pTail)
  97. {
  98. delete p;
  99. p = NULL;
  100. S.pHead = S.pTail = NULL;
  101. }
  102. else
  103. {
  104. S.pHead = S.pHead->pNext;
  105. delete p;
  106. p = NULL;
  107. }
  108. return a;
  109. }
  110.  
  111. void EnQueue(Queue &Q, char x)
  112. {
  113. LNode *p = CreateNode(x);
  114. if (Q.pHead == NULL)
  115. {
  116. Q.pHead = p;
  117. Q.pTail = Q.pHead;
  118. }
  119. else
  120. {
  121. Q.pTail->pNext = p;
  122. Q.pTail = p;
  123. }
  124. }
  125.  
  126. char DeQueue(Queue &Q)
  127. {
  128. char a = Q.pHead->key;
  129. LNode *p = Q.pHead;
  130. if (Q.pHead == Q.pTail)
  131. {
  132. delete p;
  133. p = NULL;
  134. Q.pHead = Q.pTail = NULL;
  135. }
  136. else
  137. {
  138. Q.pHead = Q.pHead->pNext;
  139. delete p;
  140. p = NULL;
  141. }
  142. return a;
  143. }
  144.  
  145. int Pritoty(char x)
  146. {
  147. if (x == '*' || x == '/')
  148. return 2;
  149. else if (x == '+' || x == '-')
  150. return 1;
  151. else
  152. return 0;
  153. }
  154.  
  155. int Operator(char x)
  156. {
  157.  
  158. if (Pritoty(x) == 0)
  159. {
  160. if (x == '(' || x == ')')
  161. return 0;
  162. else
  163. return 1;
  164. }
  165. else
  166. return 2;
  167. }
  168.  
  169. void InFixToPostFix(vector<char> a, Queue &Q)
  170. {
  171. Stack S;
  172. CreateStack(S);
  173. for (int i = 0; i < a.size(); i++)
  174. {
  175. if (Operator(a[i]) == 1)
  176. {
  177. EnQueue(Q, a[i]);
  178. }
  179. else if (Operator(a[i]) == 2)
  180. {
  181. if (S.pHead != NULL)
  182. {
  183. while (Pritoty(a[i]) <= Pritoty(S.pHead->key))
  184. {
  185. char t = PopStack(S);
  186. EnQueue(Q, t);
  187. if (S.pHead == NULL)
  188. break;
  189. }
  190. }
  191. PushStack(S, a[i]);
  192. }
  193. else if (Operator(a[i]) == 0)
  194. {
  195. if (a[i] == '(')
  196. PushStack(S, a[i]);
  197. else
  198. {
  199. while (S.pHead->key != '(')
  200. {
  201. char t = PopStack(S);
  202. EnQueue(Q, t);
  203. }
  204. PopStack(S);
  205. }
  206. }
  207.  
  208. }
  209. while (S.pHead != NULL)
  210. {
  211. char t = PopStack(S);
  212. EnQueue(Q, t);
  213. }
  214. }
  215.  
  216. void PushStackTree(StackTree &ST, TNode *TN)
  217. {
  218. if (ST.pHead == NULL)
  219. {
  220. ST.pHead = TN;
  221. ST.pTail = ST.pHead;
  222. }
  223. else
  224. {
  225. TN->pNext = ST.pHead;
  226. ST.pHead = TN;
  227. }
  228. }
  229.  
  230. TNode* PopStackTree(StackTree &ST)
  231. {
  232. if (ST.pHead == NULL)
  233. {
  234. return NULL;
  235. }
  236. else if (ST.pHead == ST.pTail)
  237. {
  238. return ST.pHead;
  239. }
  240.  
  241. TNode *p = ST.pHead;
  242. ST.pHead = p->pNext;
  243. return p;
  244. }
  245.  
  246. void CreateTree(StackTree &ST, Queue Q)
  247. {
  248. LNode *p = Q.pHead;
  249. while (p != NULL)
  250. {
  251. if (Operator(p->key) == 1)
  252. {
  253. PushStackTree(ST, CreateTNode(p->key));
  254. }
  255. else if (Operator(p->key) == 2)
  256. {
  257. TNode *A = CreateTNode(p->key);
  258. A->pRight = ST.pHead;
  259. PopStackTree(ST);
  260. A->pLeft = ST.pHead;
  261. PopStackTree(ST);
  262. PushStackTree(ST, A);
  263. }
  264. p = p->pNext;
  265. }
  266. }
  267.  
  268. void LRN(TNode *T)
  269. {
  270. if (T != NULL)
  271. {
  272. LRN(T->pLeft);
  273. LRN(T->pRight);
  274. cout << T->key;
  275. }
  276. }
  277.  
  278. void NLR(TNode *T)
  279. {
  280. if (T != NULL)
  281. {
  282. cout << T->key;
  283. NLR(T->pLeft);
  284. NLR(T->pRight);
  285. }
  286. }
  287.  
  288. void NhapBT(vector<char> &a)
  289. {
  290. char x;
  291. cin >> x;
  292. while (x != '#')
  293. {
  294. a.push_back(x);
  295. cin >> x;
  296. }
  297. }
  298.  
  299. int main()
  300. {
  301. Queue Q;
  302. StackTree ST;
  303. vector<char> A;
  304.  
  305. CreateQueue(Q);
  306. CreateStackTree(ST);
  307.  
  308. NhapBT(A);
  309.  
  310. InFixToPostFix(A, Q);
  311.  
  312. CreateTree(ST, Q);
  313.  
  314. TNode *a = ST.pHead;
  315. NLR(a);
  316. cout << endl;
  317. LRN(a);
  318. return 0;
  319. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement