void PostOrderNonRecursive(struct BinaryTreeNode *root){
    TreeStack *S = CreateStack(10);
    while(1) {
        //自分をスタックにpushして左に行けるところまで行く。
        if(root) {
            Push(S, root);
            root = root->left;
        }
        else {
            if(IsEmptyStack(S)) {
                printf("Stack is Empty\n");
                return;
            }
            //右にも行けなくなったらpopして1つ前のノードを取り出す(1つ戻る)
            else if(Top(S)->right == NULL) {
                root = Pop(S);
                printf("%d", root->data);
                //
            }
            while(root == Top(S)->right) {
                    printf("%d", Top(S)->data);
                    root = Pop(S);
            }
            
            if(!IsEmptyStack(S)){
                    root = Top(S)->right;
            }else{
                root = NULL;
            }
        }
    }
    DeleteStack(S);
}
示例#2
0
文件: KOM.c 项目: punktniklas/NiKom
void KomLoop(void) {
  int defaultCmd;
  g_unreadRepliesStack = CreateStack();

  for(;;) {
    if(StackSize(g_unreadRepliesStack) > 0) {
      defaultCmd = CMD_NEXTREPLY;
    } else if(hasUnreadInConf(mote2)) {
      defaultCmd = CMD_NEXTTEXT;
    } else if(HasUnreadMail()) {
      defaultCmd = CMD_GOMAIL;
    } else if(FindNextUnreadConf(mote2) >= 0) {
      defaultCmd = CMD_NEXTCONF;
    } else {
      defaultCmd = CMD_SEETIME;
    }

    displayPrompt(defaultCmd);
    if(shouldLogout()) {
      break;
    }
  }

  DeleteStack(g_unreadRepliesStack);
}
示例#3
0
文件: Stack.c 项目: Adesh/cPrograms
int main(){
	int Option=1,
			Value,
			i;	       
	
	while(Option){
		printf("1) PUSH Stack\n2) POP Stack\n3) Print Stack\n4) Peep\n5) Change Stack\n0) Exit program\n"); 
		scanf("%d", &Option);  
		switch(Option){
		 case 1:
		   do{
				printf("Enter a +ve value: ");
 		    scanf("%d", &Value);
			 }while(Value<0);       
			 Push(Value);	/* allow possitive values */		   
			 printf("Stack PUSHed\n\n");
		   break;
		 case 2:
		   Pop();
		   printf("Stack POPed\n\n");
		   break;
		 case 3:
		   StackPrint();
		   break;
		 case 4:
			 printf("Enter a valid index: ");
		   scanf("%d", &Value);
		   Value = Peep(Value);
			 if(Value)
			 	printf("Value at asked index: %d\n\n", Value);
		   else
				printf("wrong index\n\n");
			 break;
		 case 5:
			 printf("Enter a valid index & value: ");
		   scanf("%d %d", &i, &Value);
			 Change(i,Value);
			 printf("Value changed\n\n");
		   break;
		 case 0:
		   DeleteStack();
		   break;
		 default:
			 printf("Enter a valid choice\n\n");
			 break;
		}
	}
  return 1;
}
示例#4
0
void Preorder_without_Recursion(struct tree * root1)
{
  CreateStack();
    while(1) {
    while(root1) {
      printf("%d ",root1->data);
      Push(root1);
      root1=root1->left;
    }
    if(IsStackEmpty())
      break;
    root1 = Pop();
    root1 = root1->right;
  }
  DeleteStack();
}
int main(int argc, char *argv[])
{
	Stack stack;
	Element item;
	memset(&stack, 0x00, sizeof(Stack));

	item.key = 1;
	printf("Add 1st element into the stack. \n");
	AddStack(&stack, item);
	PrintStack(&stack);

	item.key = 2;
	printf("Add 2nd element into the stack. \n");
	AddStack(&stack, item);
	PrintStack(&stack);

	item.key = 3;
	printf("Add 3rd element into the stack. \n");
	AddStack(&stack, item);
	PrintStack(&stack);

	item.key = 4;
	printf("Add 4th element into the stack. \n");
	AddStack(&stack, item);
	PrintStack(&stack);

	printf("Delete the top element from the stack. \n");
	item = DeleteStack(&stack);
	printf("Delete %d \n", item.key);

	PrintStack(&stack);

	printf("Destroy the stack. \n");
	DestroyStack(&stack);

	
	return 0;
}
示例#6
0
int main()
{
	int i,r;
	printf("Testing Queue\n");
	Queue* queue = CreateQueue();

	printf("Inserting values\n");
	for(i = 0; i < 5 ; i++)
	{	
		printf("%d\n",i);
		int result = QueueEnqueue(queue, i);
		if(result == 0)
		{
			printf("%d\n",i);
		}
		else
		{
			printf("Error in enqueuing queue\n");
		}
	}

	printf("Removing values\n");
	for(i = 0; i < 5 ; i++)
	{
		int result =QueueDequeue(queue, &r);
		if(result == 0)
		{
			printf("%d\n",r);
		}
		else
		{
			printf("Error in dequeuing queue\n");
		}
	}

	DeleteQueue(queue);

	printf("\nTesting Stack\n");

	Stack* stack = CreateStack();
	for(i = 0; i < 5 ; i++)
	{
		int result = StackPush(stack, i);
		if(result == 0)
		{
			printf("%d\n",i);
		}
		else
		{
			printf("Error in pushing to stack");
		}
	}

	for(i = 0; i < 5 ; i++)
	{
		int result = StackPop(stack, &r);
		if(result == 0)
		{
			printf("%d\n",r);
		}
		else
		{
			printf("Error in popping values");
		}
	}
	
	DeleteStack(stack); 
	return 0;
}
示例#7
0
int main()
{
	struct timeval t1, t2;
	double elapsedTime;
	srand(time(NULL));
	int i;
	int r;
	int result; 

	printf("Testing Queue\n");
	// start timer
	gettimeofday(&t1, NULL);
	Queue* queue = CreateQueue();

	for(i = 0; i < NO_OF_ELEMENTS ; i++)
	{
		// Generate a random value and put that to stack
		r = rand();	
		result = QueueEnqueue(queue, r);
		if(result != 0)
		{
			printf("Error in enqueuing queue\n");
		}
	}

	for(i = 0; i < NO_OF_ELEMENTS ; i++)
	{
		result =QueueDequeue(queue, &r);
		if(result != 0)
		{
			printf("Error in dequeuing queue\n");
		}
	}

	DeleteQueue(queue);

	// Time calculation
	gettimeofday(&t2, NULL);
	elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0;
	elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0;
	printf("Elapsed time %f\nQueue testing completed...\n", elapsedTime);
	
	printf("\nTesting Stack\n");
	// start timer
	gettimeofday(&t1, NULL);

	Stack* stack = CreateStack();
	for(i = 0; i < NO_OF_ELEMENTS ; i++)
	{
		// Generate a random value and put that to stack
		r = rand();	
		result = StackPush(stack, r);
		if(result != 0)
		{
			printf("Error in pushing to stack");
		}
	}

	for(i = 0; i < NO_OF_ELEMENTS ; i++)
	{
		result = StackPop(stack, &r);
		if(result != 0)
		{
			printf("Error in poping stack");
		}
	}
	
	DeleteStack(stack); 

	// Time calculation
	gettimeofday(&t2, NULL);
	elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0;
	elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0;

	printf("Elapsed time %f\nStack testing completed...\n", elapsedTime);

	return 0;
}
示例#8
0
int main()
{
  printf("############ WELCOME TO STACK OPERATIONS ############# \n \n");
  int i=0;
  int size=0,data=0;
  stack *S=(stack*)malloc(sizeof(stack));
  while(1)
  {
  printf("\n 1)Create a Stack(Press 1)\n 2)Insert an element(Press 2)\n 3)Delete an element(Press 3)\n 4)Show the stack(Press 4)\n 5)Delete a Stack(Press 5)\n 6)Exit(Press 6 to exit) \n");
  scanf("%d",&i);
  switch(i)
  {
    case 1:

        printf("\nEnter the size of the stack= ");
        scanf("%d",&size);
       *CreateStack(S,size);
       printf("Enter the values of the element (Press -1  to stop)= \n");
       while(1)
       {

          scanf("%d",&data);
          if(data==-1)
              break;
          else
              Push(S,data);
       }
       break;

    case 2:
          printf("\nEnter the value of the element= ");
          scanf("%d",&data);
          if(data==-1)
              break;
          else
              Push(S,data);
        break;

    case 3:
          printf("The element deleted is =%d\n",Pop(S));
          break;
    case 4:
       if (flag==1)
       {
        if(!isEmptyStack(S))
        {
          printf("\nThe elements of the stack are: ");
          for(i=0;i<=S->top;i++)
            printf("%d ",S->array[i]);
          printf("\n");
        }
        else
            printf("Stack is empty !!!! \n");
       }
      else
        printf("Stack is not created !!! \n");
          break;
    case 5:
          DeleteStack(S);
          printf("Stack is deleted !!! \n");
          break;
    case 6:
          exit(0);
    default:
          printf("Invalid option \n");
          break;
   }
  }

 return 0;
}