Example #1
0
/***************************************************************
 * Que 7. Given an array A, the apan S[i] of A[i] is the 
 *        maximum number of consecutive elements A[j]
 *        elements A[j] and such that A[j] <= A[i]
 * 
 * Commenst   :
 * 
 * Parameters : integer arrays.
 * Retuns     : longest spans
 * 
 **************************************************************/
int *FindingSpans(int A[],int size)
{
   LinkedListStack stack;
   int p;
   int * s = new int[size];
   
   for(int i=0;i<size;i++)
   {
      while(!stack.IsEmpty())
      {
	if(A[i] > A[stack.Top()])
	  stack.Pop();
	else
	  break;
      }
   
      if(stack.IsEmpty())
	p = -1;
      else
	p = stack.Top();
      
      s[i] = i-p;
      stack.Push(i);
   }
   return s;
}
Example #2
0
/*************************************************************
 * Que 3. Post fix expression evaluation
 * 
 * Comment    : 1) prepare stack for holding operands only.
 *              2) Push when numbers are read.
 *              3) Pop two numbers when ever operand is encountered.
 * 
 * Parameters : expression array
 * Returns    : int (eval result)
 *************************************************************/
int  PostFixEval(char A[])
{
   LinkedListStack stack;
   int tokenType = EMPTY,num,i=0;;
   char token;

   while((tokenType = ReadToken(A,i,token,num)) != EMPTY)
   {
      if(tokenType == OPERAND) //Numbers
      {
	 stack.Push(token - '0');
      }
      else if(tokenType == OPERATOR) //Mathematical operator
      {
	int A = stack.Pop();
	int B = stack.Pop();
	stack.Push(Operate(A,B,token));
      }
      else 
      {
	//Should not reach here
	assert(1 == 0);
      }
  }
  if(stack.IsEmpty())
    return -1;
  return stack.Pop();   
}
Example #3
0
/*************************************************************
 * Que 6. Reverse stack element using recursion. without using
 * 	  Another stack.
 * 
 * Comment    : 
 * 
 * Parameters : Stack
 * Returns    : None
 *************************************************************/
void ReveseStack(LinkedListStack &stack)
{
    if(stack.IsEmpty())
      return;
  int data = stack.Pop();
  ReveseStack(stack);
  AddElementAtBottom(stack,data);
}
Example #4
0
void AddElementAtBottom(LinkedListStack &stack, int data)
{
   if(stack.IsEmpty())
   {
      stack.Push(data);
      return;
   }
   int temp = stack.Pop();
   AddElementAtBottom(stack,data);
   stack.Push(temp);
}
Example #5
0
/*******************************************************
 * Que 2. Infix to Postfix conversion.
 * 
 * Parameters : Expression
 * Retuns     : None
 * 
 * Time Complexity : O(N)
 *******************************************************/
char * InfixToPostFix(char A[])
{
  LinkedListStack stack;
  char * output = new char[1024];
  int iOutput = 0;
  int tokenType = EMPTY;
  int i=0,num;
  char token;
  char topStack = '\0';
  
  while((tokenType = ReadToken(A,i,token,num)) != EMPTY)
  {
      if(tokenType == OPERAND) //Numbers
      {
	//Add simply in output string
	output[iOutput++] = token; //num - '0';
      }
      else if(tokenType == OPERATOR) //Mathematical operator
      {
	if(!stack.IsEmpty()) topStack = stack.Top();
			else topStack = '\0';
	
	if(topStack == '\0' || topStack == '(' || 
	   topStack == '{' || topStack == '[')
	{
	    stack.Push(token);
	}
	else
	{
	   if(CompairPrecedence(topStack,token))
	     stack.Push(token);
	   else
	   {
	      while( topStack != -1   && !CompairPrecedence(topStack,token) && 
		     topStack != '\0' && 
		     topStack != '('  && 
		     topStack != '{'  && 
		     topStack != '[')
	      {
		 output[iOutput++] = stack.Pop();
		 topStack = stack.Top();
	      }
	      stack.Push(token);
	   }
	}
      }
      else if(tokenType == RIGHTP)   //Right parenthesis
      {
	 char top = stack.Top();
	 while(top != '(' ) //&& topStack != '{'  && topStack != '[')
	 {
	     output[iOutput++] = stack.Pop();
	     top = stack.Top();
	 }
	 stack.Pop();
      }
      else if(tokenType == LEFTP)    //Left parenthesis
      {
	stack.Push(token);
      }
  }
  while(!stack.IsEmpty())
    output[iOutput++] = stack.Pop();
  output[iOutput] = '\0';
  return output;
}