Exemplo n.º 1
0
/****************postorderPrint function***********************/
void postorderPrint(NodePtr nodePtr){
	NodePtr currentPtr=nodePtr;
	if(currentPtr!=NULL){
		postorderPrint(currentPtr->leftPtr);
		postorderPrint(currentPtr->rightPtr);
		printf("%d\t",currentPtr->number);
	}
	return ;
}
Exemplo n.º 2
0
int main()
{
	Node* cur = NULL;
	int buf = 0;
	FILE* f = fopen("test.txt", "r");
	while (!feof(f))
	{
		char symbol = fgetc(f);
		if (symbol != '(' && symbol != ' ')
		{
			if (isSign(symbol))
				cur = addChar(cur, symbol);
			if (symbol == ')')
				cur = cur->parent;
			if (isDigit(symbol))
			{
				ungetc(symbol, f);
				fscanf(f, "%d", &buf);
				cur = addValue(cur, buf);
			}
		}
	}
	while(cur->parent != NULL)
		cur = cur->parent;
	List *top = new List();
	Stack *stack = new Stack();
	stack->top = top;
	postorderPrint(cur, stack);
	printf("\n");
	printf("result = %d\n", stack->top->next->value);
	clearStack(stack);
}
Exemplo n.º 3
0
/**************new main function*****************/
int main(void)
{
	int element,printChoice;
	NodePtr rootNodePtr=NULL;
	puts("enter a number to insert or enter a non-number to stop insert function\n");
	printf("?:");
	while(scanf("%d",&element)){
		if(rootNodePtr==NULL){
			rootNodePtr=(NodePtr)malloc(sizeof(Node));
			rootNodePtr->number=element;
			rootNodePtr->leftPtr=NULL;
			rootNodePtr->rightPtr=NULL;
		}
		else{
			insert(rootNodePtr,element);	
		}
		printf("?:");
	}
	getchar();					//第77行 scanf("%d",&i);是告诉电脑,你从输入流输入一个东西,
	printInstruction();				//如果他是整形(%d)判断,那么他将传递过来的地址
	scanf("%d",&printChoice);			//(&i是对i取地址)接收,否则将被继续放在缓存中。
							//92行输入非数字就会进入死循环! 因为它一直被放在缓存中
	while(printChoice!=4){
		switch(printChoice){
		case 1:												
			inorderPrint(rootNodePtr);
			break;
		case 2:
			preorderPrint(rootNodePtr);
			break;
		case 3:
			postorderPrint(rootNodePtr);
			break;
		default:
			puts("invalid choice\n");
			//printInstruction();
			break;
		}
		fflush(stdin);					// to clean the date in the cache
		printInstruction();
		scanf("%d",&printChoice);
	}
	puts("function end\n");
	return 0;
}		//end main function