Search
 
SCRIPT & CODE EXAMPLE
 

CPP

stack implementation using class in c++

#include<iostream>
using namespace std;
#define Size 5

class Stack
{
private:
	int Array[Size];
	int top;
public:
	Stack()
	{
		top = -1;
	}
	void Push(int x)
	{
		if (top == Size - 1)
		{
			cout << "Error, stack overFlow!" << endl;
			return;
		}

		Array[++top] = x;
	}
	void Pop()
	{
		if (top == -1)
		{
			cout << "Error, stack is Empty!" << endl;
			return;
		}
		top--;
	}

	int Top()
	{
		return Array[top];
	}
	bool IsEmpty()
	{
		if (top == -1)
			return 1;
		return 0;
	}
	void print()
	{
		cout << "Stack: ";
		for (int i = 0; i <= top; i++)
		{
			cout << Array[i] << " ";
		}
		cout << "
";
	}

};
int main()
{
	Stack s;
	s.Push(1);
	s.Push(2);
	s.print();

	return 0;
}
Comment

stack data structure c++

#include<iostream>
#include<stack>
using namespace std;
bool isValid(string s) {
	stack<char> st;
	for (int i = 0; i < s.length(); i++)
	{
		if (s[i] == '(' || s[i] == '{' || s[i] == '[')
			st.push(s[i]);
		if (!st.empty())
		{
			if (s[i] == ')')
			{
				if (st.top() == '(')
				{
					st.pop();
					continue;
				}
				else
					break;
			}
			//
			if (s[i] == '}')
			{
				if (st.top() == '{')
				{
					st.pop();
					continue;
				}
				else
					break;
			}
			//
			if (s[i] == ']')
			{
				if (st.top() == '[')
				{
					st.pop();
					continue;
				}
				else
					break;
			}
		}
		else
			return false;
	}
	return st.empty() ? true : false;
}
int main()
{
	string s;
	cin >> s;
	cout << isValid(s) << "
";
	return 0;
}
Comment

stack in c++ data structure

10 pushed into stack
20 pushed into stack
30 pushed into stack
30 Popped from stack
Top element is : 20
Elements present in stack : 20 10
Comment

stack using cpp

#include <iostream>
#include <stack>
using namespace std;
int main() {
    stack<int> stack;
    stack.push(21);
    stack.push(22);
    stack.push(24);
    stack.push(25);
       
         stack.pop();
    stack.pop();
   
    while (!stack.empty()) {
        cout << stack.top() <<" ";
        stack.pop();
    }
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: how to create a struct in c++ 
Cpp :: ex: cpp 
Cpp :: abs c++ 
Cpp :: pow c++ 
Cpp :: A Program to check if strings are rotations of each other or not 
Cpp :: c++ last element of array 
Cpp :: vector size c++ 
Cpp :: c++ get index of map element 
Cpp :: max and min function in c++ 
Cpp :: c++ constructor 
Cpp :: remove duplicates from sorted list solution in c++ 
Cpp :: C++ programming code to remove all characters from string except alphabets 
Cpp :: fname from FString 
Cpp :: why exceptions can lead to memory leaks 
Cpp :: vector insert to end 
Cpp :: faster solutions 
Cpp :: memset array bool 
Cpp :: 3. The method indexOf, part of the List interface, returns the index of the first occurrence of an object in a List. What does the following code fragment do? 
Cpp :: stricmp CPP 
Cpp :: setFontSize QT 
Cpp :: std::is_standard_layout 
Cpp :: https://www.cplusplus.com/doc/tutorial/pointers/ 
Cpp :: convert datatype of field db browser from text to timedate db browser 
Cpp :: c++ program to convert celsius to fahrenheit 
Cpp :: variadic template constructor matches better than copy constructor 
Cpp :: preorder to postorder converter online 
Cpp :: beecrowd problem 1003 solution in c++ 
Cpp :: c++ merging algorithm 
Cpp :: c++ find unused class methods 
Cpp :: easy way to learn file handling in c++ array 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =