Esempio n. 1
0
/*******************************************************
 * Que 1. Discuss how stacks can be used for balancing 
 *        of symbols
 * 
 * Parameters : Expression (char array)
 * 		size of array
 * returns    : bool (true/false)
 *******************************************************/
bool IsExpressionBalanced(char A[], int size)
{
   LinkedListStack stack;
   printf("Expression : %s\n",A);
   int i;
   char a,temp;
   for(i=0;i<size;i++)
   {
      a = A[i];
      switch(a)
      {
	case '(': case '{': case '[':
	  stack.Push(a);
	  break;
	
	case ')': case '}' : case ']':
	  temp = stack.Pop();
	  if(temp == -1)
	    return false;
	  else
	  {
	    if((temp == '(' && a != ')') || 
	       (temp == '{' && a != '}') ||
	       (temp == '[' && a != ']') 
	    )
	      return false;
	  }
	  break;
      }
   }
   if(stack.Pop() != -1)
      return false;
   else
      return true;
} /* END QUE 1 */
Esempio n. 2
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);
}
Esempio n. 3
0
void AddElementAtBottom(LinkedListStack &stack, int data)
{
   if(stack.IsEmpty())
   {
      stack.Push(data);
      return;
   }
   int temp = stack.Pop();
   AddElementAtBottom(stack,data);
   stack.Push(temp);
}
Esempio n. 4
0
int main(){
	LinkedListStack<int> stack = LinkedListStack<int>();
	stack.push(1);
	stack.push(2);
	stack.push(3);
	stack.push(4);
	stack.push(5);
	stack.push(6);
	stack.push(7);
	Invierte(stack, 3);

	return 0;
}
Esempio n. 5
0
void Invierte(LinkedListStack <T> stack, const int &positions){ //tengo que poner el type aqui ya????? Se puede hacer con listas??

	T *intermedia = new T[positions];
	int i = 0;
	while (!stack.empty() && i<positions){
		intermedia[i] = stack.top();
		stack.pop();
		i++;
	}
	for (int j = 0 ; j <i; j++){
		stack.push(intermedia[j]);
	}
	delete[] intermedia;
}
Esempio n. 6
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;
}
Esempio n. 7
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();   
}
Esempio n. 8
0
// Test input:
// to be or not to - be - - that - - - is
// run with: cat data/tobe.txt | ./bin/stack
int main(int argc, char *argv[])
{
  LinkedListStack<std::string> stackList;
  ResizingArrayStack<std::string> stackArray;
  string lineInput;

  while (cin >> lineInput)
  {
    string *item = new string(lineInput);
    if(item->compare("-"))
    {
      stackList.push(item);
      stackArray.push(item);
    }
    else if(!stackList.isEmpty())
    {
      cout << *stackList.pop() << " ";
      cout << *stackArray.pop() << " ";
    }
  }
  cout << endl << "(" << stackList.size() << " left on stackList)" << endl;
  cout << "(" << stackArray.size() << " left on stackArray)" << endl;
  return 0;
}
Esempio n. 9
0
/*************************************************************
* Que 5. Use stack to find minimum element for all stack 
* elements
* 
* Comment    : Creating min stack to get current index minimum
* 	       Element for all elements.
* 
*    1. Improved space complexity by pushing in stack
*       Only if there is more less value available &
*       and popped only when the current stack top value
*	is equal to array value.
* 
* Parameters : integer array
* Retuns     : None
**************************************************************/
void  GetMinimum(int A[],int size)
{
   if(size == 0) return;
   LinkedListStack stack;
   stack.Push(A[0]);
   for(int i=1; i<size; i++)
   {
      if(stack.Top() > A[i])
	stack.Push(A[i]);
       //else
       //stack.Push(stack.Top());
   }
   for(int i=size - 1;i>=0;i--)
      printf("%d ",A[i]);
   printf("\n");
   
   int c = stack.GetSize();
   for(int i=0;i<c;i++)
      printf("%d ",stack.Pop());
   printf("\n");
}
Esempio n. 10
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;
}