nebulafire
Tổng số bài gửi : 25 Join date : 06/02/2010 Age : 37
| Tiêu đề: Queue - Chuyen Hau to sang trung to Thu Feb 25, 2010 7:51 am | |
| - Code:
-
#include <stdlib.h> #include <string.h> #include<stdio.h> #include<conio.h> #define elem char typedef struct nodet { elem data; struct nodet *next; } node; typedef node *nodeptr; typedef struct { nodeptr front, rear; } queue; void createqueue(queue &q) { q.front = NULL; }
int emptyqueue(queue q) { return q.front == NULL; }
void addqueue(queue &q, elem &x) { nodeptr newp = new node; memcpy(&newp->data, &x, sizeof(elem)); newp->next = NULL; if (q.front == NULL) q.front = newp; else q.rear->next = newp; q.rear = newp; }
void removequeue(queue &q, elem &x) { nodeptr t = q.front; if (q.front == NULL) exit(0); q.front = t->next; memcpy(&x, &t->data, sizeof(elem)); delete t; } int laKytu(char kytu){ return ( kytu=='+' || kytu=='-' || kytu=='*' || kytu=='/' || kytu=='(' || kytu==')'); }
// 23+4*5/ = (2+3) *4/5 void PostfixToInfix(char *bt){ queue qso,qkyso,qkq; int len = strlen(bt); int i; char infixstr[100]; char temp,pushval; createqueue(qso); createqueue(qkyso); createqueue(qkq); for (i = 0; i <len; ++i){ if (!laKytu(bt[i])){ addqueue(qso, bt[i]); } else{ addqueue(qkyso, bt[i]); } } while (!emptyqueue(qso)){ removequeue(qso,temp); printf("%c",temp); if (!emptyqueue(qkyso)){ removequeue(qkyso,temp); printf("%c",temp); } } }
void main() { char bt[100]; printf("\nNhap mot bieu thuc hau to hop le (trong de thoat): "); gets(bt); if (strlen(bt) > 0) PostfixToInfix(bt); getch();
}
| |
|