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 c++

#include <bits/stdc++.h> 

stack<int> stk;
stk.push(5);
int ans = stk.top(5); // ans =5
stk.pop();//removes 5
Comment

stack c++

/* Stack is a data structure that provides two O(1) time operations:
adding an element to the top and removing an element from the top.
It is only possible to access the top element of a stack. */
stack<int> s;
s.push(3);
s.push(2);
s.push(5);
cout << s.top(); // 5
s.pop();
cout << s.top(); // 2
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

stack in c++

#include <iostream>

using namespace std;
#define M 100 //defined the size of the stack ;

//stack class for controlling

class STACK
{
    int ara[M]; // declared an array for storing the stack elemenst
    int top; // finding the top value
public:
    //function for pushing values into the stack
    STACK() // constructor for initializing
    {
        top = 0;
    }
    void push(int n)
    {
        if(top == M) // checking if the stack is full or not
        {
            cout << "Stack is full
";
            return;
        }else // if the stack is not full, push the element
        {
            ara[top] = n;
            top++;
        }
    }
    //function for poping out elements from the stack or getting the elements
    int pop()
    {
        if(top == 0)
        {
            cout << "Stack is empty
";
            return 0;
        }else
        {
            top--;
            return ara[top];
        }
    }
    
};


int main()
{
    STACK stk1, stk2; //making two objects of type STACK
    
    //pushing elements
    for(int i = 0; i < 5; i++)
    {
        stk1.push(i);
        stk2.push(i+1);
    }
    
    //poping elements
    for(int i = 0; i < 5; i++)
    {
        cout << stk1.pop() << " ";
    }
    
    cout << "
";
    
    for(int i = 0; i < 5; i++)
    {
        cout << stk2.pop() << " ";
    }
    cout << "
";
    return 0;
}
Comment

Basic stack implementation in c++

/* C++ program to implement basic stack
   operations */
#include <bits/stdc++.h>

using namespace std;

#define MAX 1000

class Stack {
    int top;

public:
    int a[MAX]; // Maximum size of Stack

    Stack() { top = -1; }
    bool push(int x);
    int pop();
    int peek();
    bool isEmpty();
};

bool Stack::push(int x)
{
    if (top >= (MAX - 1)) {
        cout << "Stack Overflow";
        return false;
    }
    else {
        a[++top] = x;
        cout << x << " pushed into stack
";
        return true;
    }
}

int Stack::pop()
{
    if (top < 0) {
        cout << "Stack Underflow";
        return 0;
    }
    else {
        int x = a[top--];
        return x;
    }
}
int Stack::peek()
{
    if (top < 0) {
        cout << "Stack is Empty";
        return 0;
    }
    else {
        int x = a[top];
        return x;
    }
}

bool Stack::isEmpty()
{
    return (top < 0);
}

// Driver program to test above functions
int main()
{
    class Stack s;
    s.push(10);
    s.push(20);
    s.push(30);
    cout << s.pop() << " Popped from stack
";
    //print all elements in stack :
    cout<<"Elements present in stack : ";
    while(!s.isEmpty())
    {
        // print top element in stack
        cout<<s.peek()<<" ";
        // remove top element in stack
        s.pop();
    }

    return 0;
}
Comment

stack in c++

#take input 2D vector

vector<vector<int> > v;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
v[i].push_back(data);
}}
Comment

PREVIOUS NEXT
Code Example
Cpp :: flutter container margin 
Cpp :: cpp Case value is not a constant expression 
Cpp :: MPI_PUT 
Cpp :: sort c++ stl 
Cpp :: SDL_BlitSurface 
Cpp :: new lien c++ 
Cpp :: c++ cyclic barrier 
Cpp :: inorder to postorder converter online 
Cpp :: [3,2,4,-1,-4] 
Cpp :: c++ string to vector using delimiter 
Cpp :: Write C++ program that will ask to choose from three cases. 
Cpp :: Make them equal codechef solution in c++ 
Cpp :: ros pointcloud2 read_points c++ 
Cpp :: 01matrix 
Cpp :: 378. Kth Smallest Element in a Sorted Matrix using binary search 
Cpp :: stack in c++ data structure 
Cpp :: compilling c++ and c by console 
Cpp :: std::throw_with_nested 
Cpp :: Int main ( ) { int i,n; cinn; i=n; while(i=1) { i=i+5; i=i-6; } } 
Cpp :: c++ vs c# 
Cpp :: string class cpp 
Cpp :: android call custom managed method from native code 
Cpp :: C++ Things to Remember 
Cpp :: a variable with 2 independant variables plot 
Cpp :: arraylist equivalent cpp 
Cpp :: c++ string to vector int 
Cpp :: C++ area & circumference of a circle 
Cpp :: are arrays faster than vectors c++ 
Cpp :: vector erase iterator 
C :: trie tableau c 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =