Last active
August 29, 2015 14:07
-
-
Save masious/c693e2d656f90fb0a0f5 to your computer and use it in GitHub Desktop.
DS Questions - Question 1 - C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
template<class T> | |
class MyStack | |
{ | |
private: | |
T * array; | |
int maxSize; | |
public: | |
int front; | |
//constructor | |
MyStack(int a=20); | |
//destructor | |
~MyStack(void); | |
//pushing elements to the stack | |
void push(T a); | |
//poping elements from stack | |
T pop(); | |
//getting last element without poping from the stack | |
T peek(); | |
//checks if the stack is empty of not | |
bool isEmpty(); | |
//checks that if the stack is full | |
bool isFull(); | |
}; | |
template<class T> | |
MyStack<T>::MyStack(int a){ | |
array = new T[a]; | |
maxSize = a; | |
front = 0; | |
} | |
template<class T> | |
MyStack<T>::~MyStack(void) | |
{ | |
} | |
template<class T> | |
void MyStack<T>::push(T a){ | |
array[front++] = a; | |
} | |
template<class T> | |
T MyStack<T>::pop(){ | |
if(isEmpty()) | |
return 0; | |
return array[front--]; | |
} | |
template<class T> | |
T MyStack<T>::peek(){ | |
if(isEmpty()) | |
return 0; | |
return array[front-1]; | |
} | |
template<class T> | |
bool MyStack<T>::isEmpty(){ | |
return !front; | |
} | |
template<class T> | |
bool MyStack<T>::isFull(){ | |
return maxSize == front+1; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <sstream> //for the getline() function | |
#include "MyStack.h" | |
using namespace std; | |
int main(){ | |
MyStack <char> foundSigns; | |
string str; | |
bool isWrong = false; | |
getline(cin,str); | |
for(unsigned int i=0;i<str.length();i++){ | |
char c = str[i]; | |
if(c == '[' || c == '(' || c == '{'){ | |
foundSigns.push(c); | |
} else if(c == ']' || c == ')' || c == '}'){ | |
char c2 = foundSigns.peek(); | |
foundSigns.pop(); | |
if( c == ']' && c2 !='[') | |
isWrong = true; | |
else if( c == ')' && c2 !='(') | |
isWrong = true; | |
else if( c == '}' && c2 !='{') | |
isWrong = true; | |
if(isWrong) | |
break; | |
} | |
} | |
if(!foundSigns.isEmpty()) | |
isWrong = true; | |
if(isWrong) | |
cout<<"Wrong!"; | |
else | |
cout<<"Right!"; | |
getchar(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment