Example #1
0
int main(int argc,char** argv)
{
	LList* l = new LList();
	l->add(1)->p()->add(2)->p()->add(4)->p()->add(10)->p()->add(80)->p();
	l->reverse()->p();
	l->remove(44)->p()->remove(2)->p()->remove(10)->p()->remove(80)->p()->remove(1)->p()->remove(4)->p()->remove(99)->p();


	StackL* s = new StackL();
	s->push(1)->p()->push(2)->p()->push(5)->p()->push(3)->p();
	s->pop();
	s->p();
	s->pop();
	s->p();
	s->pop();
	s->p();
	s->pop();
	s->p();

	QueueL* q = new QueueL();
	q->enq(1)->p()->enq(2)->p()->enq(5)->p();
	q->dq()->p()->dq()->p()->dq()->p();

	return 0;
}
void reverse(char* exp) {

	StackL<char> *stack = new StackL<char>();

	for(int i = 0; exp[i]; i++) {
		stack->push(exp[i]);
	}

	for(int i = 0; exp[i]; i++) {
		exp[i] = stack->pop();
	}

	delete stack;
}
int evaluatePostfix(char* exp) {
	StackL<int> *stack = new StackL<int>();

	int interRes, val1, val2;

	for(int i = 0; exp[i]; i++) {
		interRes = 0;
		if(isOperand(exp[i])) {
			stack->push((int)exp[i] - '0');
		} else {
				val1 = stack->pop();
				val2 = stack->pop();

				switch(exp[i]) {
					case '+': 
							interRes = val2 + val1;
							break;	
					case '-': 
							interRes = val2 - val1;
							break;			
					case '*': 
							interRes = val2 * val1;
							break;	
					case '/': 
							interRes = val2 / val1;
							break;	
					case '^': 
							interRes = val2 ^ val1;
							break;	
				}

				stack->push(interRes);
			}

		}

	int res = stack->pop();
	delete stack;
	return res;
}
int main(void) {

	StackL<int> *testObj = new StackL<int>();

	for(int i = 0; i< 100000; i++) {
		testObj->push(rand() * 100 + 1);	
	}
	
	testObj->printStack();

	cout<<testObj->pop()<<endl;

	testObj->printStack();
	cout<<testObj->peek()<<endl;
	testObj->printStack();

	delete testObj;

	return 0;
}
Example #5
0
//#define _CRTDBG_MAP_ALLOC
int main(int argc, const char * argv[]) {
    //_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
    StackL<int> stack;
    cout<<"Pushing 10000 elements"<<endl;
    timepoint start = std::chrono::high_resolution_clock::now();
    for (int i = 0; i<STACK_SIZE; i++)
    {
        stack.Push(i);
    }
    timepoint stop = std::chrono::high_resolution_clock::now();
    double duration = std::chrono::duration_cast<std::chrono::microseconds>(stop-start).count();
    cout<<"Is empty?"<<endl;
    cout<<stack.isEmpty()<<endl;
    
  
    
    cout<<"Popping 10000 elements"<<endl;
    
    start = std::chrono::high_resolution_clock::now();
    for (int i = 0; i<STACK_SIZE; i++)
    {
        stack.Pop();
    }
    stop = std::chrono::high_resolution_clock::now();
    duration += std::chrono::duration_cast<std::chrono::microseconds>(stop-start).count();
    cout<<"Is empty?"<<endl;
    cout<<stack.isEmpty()<<endl;
    
    cout<<"Popping 1 more element"<<endl;
    try
    {
        stack.Pop();
    } catch (Exception e)
    {
        cout<<e.getMessage()<<endl;
    }
    
    cout<<duration/1000<<" milliseconds required to push and pop 10000 elements"<<endl;
    cout<<"Pushing 400 elements"<<endl;
    for (int i = 0; i<400; i++)
    {
        stack.Push(i);
    }
    cout<<"Is empty?"<<endl;
    cout<<stack.isEmpty()<<endl;
 
    cout<<"Peeking at top of stack\nTop of stack: "<<stack.Peek()<<endl;
    cout<<"Size of stack: "<<stack.Size()<<endl;
    
    StackL<int> stack2(stack);
    stack2.Push(100);
    cout<<stack2.Size()<<endl;
    stack = stack2;
    cout<<stack.Size()<<endl;
    
    return 0;
}