Search
 
SCRIPT & CODE EXAMPLE
 

CPP

stack implementation using linked list in cpp

#include<iostream>
using namespace std;
struct node
{
	int data;
	node* next;
};
class Stack
{
private:
	node* top;
public:
	Stack()
	{
		top = NULL;
	}
	void Push(int n)
	{
		node* temp = new node();
		temp->data = n;
		temp->next = NULL;
		if (top == NULL)
		{
			top = temp;
		}
		else
		{
			temp->next = top;
			top = temp;
		}
	}
	void Pop()
	{
		
		if (top == NULL)
		{
			cout << "Error,Stack is Empty!" << endl;
			return;
		}
		node* temp = top;
		top = top->next;
		delete temp;
	}
	void Top()
	{
		cout << "Top Of Stack: " << top->data << endl;
	}
	void Display()
	{
		node* t = top;
		cout << "Stack: ";
		while (t != NULL)
		{
			cout << t->data << " ";
			t = t->next;
		}
		cout << "
";
	}
	void IsEmpty()
	{
		node* t = top;
		if (t == NULL)
		{
			cout << "Stack Is Empty!" << endl;
		}
		else
		{
			cout << "Stack Is Not Empty!" << endl;
			Display();
		}
	}
};
int main()
{
	Stack s;
	s.Push(1);
	s.IsEmpty();
	s.Pop();
	s.IsEmpty();

	return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: c++ how to convert string to long long 
Cpp :: c++ string to integer without stoi 
Cpp :: how to open and print in a file in c++ 
Cpp :: c++ find largest number in array 
Cpp :: hello world C++, C plus plus hello world 
Cpp :: make random nuber between two number in c++ 
Cpp :: priority queue c++ type of pairs 
Cpp :: fork was not declared in this scope 
Cpp :: initialize 2d array c++ memset 
Cpp :: cmath sqrt 
Cpp :: c++ matrix as argument 
Cpp :: in c++ ++ how to write if without if 
Cpp :: c++ code for insertion sort 
Cpp :: calling struct to a struct c++ 
Cpp :: check if float has decimals c++ 
Cpp :: c++ program to take input from user 
Cpp :: character array to string c++ stl 
Cpp :: sort vector in descending order 
Cpp :: c++ typedef 
Cpp :: c++ cin operator 
Cpp :: how to make a loop in c++ 
Cpp :: how to make copy constructor in c++ 
Cpp :: c++ init multidimensional vector 
Cpp :: how to scan array in c++ 
Cpp :: remove element from array c++ 
Cpp :: c++ load file as vector 
Cpp :: check if a string is palindrome cpp 
Cpp :: c++ split string by several space 
Cpp :: c++ public class syntax 
Cpp :: c++ cast to type of variable 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =