Implementation stack using linked list

Here given code implementation process.

//C program
//Implementation of stack using linked list
#include <stdio.h>

#include <stdlib.h> //for malloc function

//Create structure of stack node
struct Node
{
	int data;
	struct Node *next;
};
//Display element of Stack
void display(struct Node *top)
{
	if (top != NULL)
	{
		printf("\nStack Elements is : ");
	}
	while (top != NULL)
	{
		printf("%d  ", top->data);
		top = top->next;
	}
}
//Returns number of element in stack
int size(struct Node *top)
{
	int counter = 0;
	while (top != NULL)
	{
		counter++;
		top = top->next;
	}
	return counter;
}
//insert Stack element
void push(struct Node **top, int value)
{
	//Create dynamic node
	struct Node *node = (struct Node *) malloc(sizeof(struct Node));
	if (node == NULL)
	{
		printf("Memory overflow\n");
	}
	else
	{
		node->data = value;
		node->next = *top;
      	*top = node;
		printf("\n Push Stack element %d", value);
		display( *top);
	}
}
//pop Stack element
void pop(struct Node **top)
{
	if ( *top == NULL)
	{
		printf("Empty Stack\n");
	}
	else
	{
		struct Node *temp = *top;
      	*top = temp->next;
		printf("\n Pop Stack element %d", temp->data);
		free(temp);
		temp = NULL;
		display( *top);
	}
}
int main()
{
	struct Node *top = NULL;
	//Add element
	push( &top, 5);
	push( &top, 4);
	push( &top, 3);
	push( &top, 2);
	push( &top, 1);
	printf("\n Size : %d", size(top));
	//Delete Stack Element
	pop( &top);
	pop( &top);
	pop( &top);
	pop( &top);
}

Output

 Push Stack element 5
Stack Elements is : 5
 Push Stack element 4
Stack Elements is : 4  5
 Push Stack element 3
Stack Elements is : 3  4  5
 Push Stack element 2
Stack Elements is : 2  3  4  5
 Push Stack element 1
Stack Elements is : 1  2  3  4  5
 Size : 5
 Pop Stack element 1
Stack Elements is : 2  3  4  5
 Pop Stack element 2
Stack Elements is : 3  4  5
 Pop Stack element 3
Stack Elements is : 4  5
 Pop Stack element 4
Stack Elements is : 5
/*
  Java Program
  Implementation of stack using linked list
*/
  
//Stack Node
class Node
{
  public int data;
  public Node next;
  public Node(int data)
  {
    this.data = data;
    this.next = null;
  }
}
class MyStack
{
  public Node top;
  public MyStack()
  {
    top = null;
  }
  public void display()
  {
    if (top != null)
    {
      Node temp = top;
      while (temp != null)
      {
        System.out.print(" " + temp.data);
        temp = temp.next;
      }
    }
    System.out.print("\n");
  }
  //Count number of elements in stack 
  public int is_size()
  {
    int counter = 0;
    Node temp = top;
    while (temp != null)
    {
      counter++;
      temp = temp.next;
    }
    return counter;
  }
  //Add a new element in stack
  public void push(int data)
  {
    //Make a new stack node
    Node new_node = new Node(data);
    if (new_node != null)
    {
      new_node.next = top;
      top = new_node;
      System.out.print("\n Push Stack element "+data+"\n");
      this.display();
    }
    else
    {
      System.out.print("Memory overflow\n");
    }
  }
  //Add a top element in stack
  //When stack is empty it returns -1
  public int pop()
  {
    int temp = -1;
    if (top != null)
    {
      temp = top.data;
      top = top.next;
     
    }
    return temp;
  }

  public boolean is_empty()
  {
    if (this.top != null)
    {
      return false;
    }
    else
    {
      return true;
    }
  }
  //Used to get top element of stack
  public int peek()
  {
    if (top != null)
    {
      return top.data;
    }
    else
    {
      return -1;
    }
  }
  public static void main(String[] args)
  {

    MyStack obj = new MyStack();
	int data = 0;
    
    System.out.print("\n Is empty : "+obj.is_empty()+"\n");

    //Add element
    obj.push(5);
    obj.push(4);
    obj.push(3);
    obj.push(2);
    obj.push(1);

    System.out.print("\n Top  : "+obj.peek()+"\n");

    System.out.print("\n Size : "+obj.is_size());
    //Delete Stack Element
    data=obj.pop();
    System.out.print("\n Pop A Stack element "+data+"\n");
    obj.display();
    System.out.print("\n Size : "+obj.is_size());
    data=obj.pop();
    System.out.print("\n Pop A Stack element "+data+"\n");
    obj.display();
   
  }
}

Output

 Is empty : true

 Push Stack element 5
 5

 Push Stack element 4
 4 5

 Push Stack element 3
 3 4 5

 Push Stack element 2
 2 3 4 5

 Push Stack element 1
 1 2 3 4 5

 Top  : 1

 Size : 5
 Pop A Stack element 1
 2 3 4 5

 Size : 4
 Pop A Stack element 2
 3 4 5
/*
  C++ Program
  Implementation of stack using linked list
*/
//Stack Node
#include<iostream>

using namespace std;
class Node
{
	public: int data;
	Node * next;
	Node(int data)
	{
		this->data = data;
		this->next = NULL;
	}
};
class MyStack
{
	public: Node * top;
	MyStack()
	{
		this->top = NULL;
	}
	void display()
	{
		if (this->top != NULL)
		{
			Node * temp = this->top;
			while (temp != NULL)
			{
				cout << " " << temp->data;
				temp = temp->next;
			}
		}
		cout << "\n";
	}
	//Count number of elements in stack 
	int is_size()
	{
		int counter = 0;
		Node * temp = this->top;
		while (temp != NULL)
		{
			counter++;
			temp = temp->next;
		}
		return counter;
	}
	//Add a new element in stack
	void push(int data)
	{
		//Make a new stack node
		Node * new_node = new Node(data);
		if (new_node != NULL)
		{
			new_node->next = this->top;
			this->top = new_node;
			cout << "\n Push Stack element " << data << "\n";
			this->display();
		}
		else
		{
			cout << "Memory overflow\n";
		}
	}
	//Add a top element in stack
	//When stack is empty it returns -1
	int pop()
	{
		int temp = -1;
		if (this->top != NULL)
		{
			temp = this->top->data;
			this->top = this->top->next;
		}
		return temp;
	}
	bool is_empty()
	{
		if (this->top != NULL)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	//Used to get top element of stack
	int peek()
	{
		if (this->top != NULL)
		{
			return this->top->data;
		}
		else
		{
			return -1;
		}
	}
};
int main()
{
	MyStack obj = MyStack();
	int data = 0;
	cout << "\n Is empty : " << ((obj.is_empty()==1)? "True" : "False") << "\n";
	//Add element
	obj.push(5);
	obj.push(4);
	obj.push(3);
	obj.push(2);
	obj.push(1);
	cout << "\n Top : " << obj.peek() << "\n";
	cout << "\n Size : " << obj.is_size();
	//Delete Stack Element
	data = obj.pop();
	cout << "\n Pop A Stack element " << data << "\n";
	obj.display();
	cout << "\n Size : " << obj.is_size();
	data = obj.pop();
	cout << "\n Pop A Stack element " << data << "\n";
	obj.display();
	return 0;
}

Output

 Is empty : True

 Push Stack element 5
 5

 Push Stack element 4
 4 5

 Push Stack element 3
 3 4 5

 Push Stack element 2
 2 3 4 5

 Push Stack element 1
 1 2 3 4 5

 Top : 1

 Size : 5
 Pop A Stack element 1
 2 3 4 5

 Size : 4
 Pop A Stack element 2
 3 4 5
/*
  C# Program
  Implementation of stack using linked list
*/
//Stack Node
using System;
class Node
{
	public int data;
	public Node next;
	public Node(int data)
	{
		this.data = data;
		this.next = null;
	}
}
class MyStack
{
	public Node top;
	public MyStack()
	{
		top = null;
	}
	public void display()
	{
		if (top != null)
		{
			Node temp = top;
			while (temp != null)
			{
				Console.Write(" " + temp.data);
				temp = temp.next;
			}
		}
		Console.Write("\n");
	}
	//Count number of elements in stack 
	public int is_size()
	{
		int counter = 0;
		Node temp = top;
		while (temp != null)
		{
			counter++;
			temp = temp.next;
		}
		return counter;
	}
	//Add a new element in stack
	public void push(int data)
	{
		//Make a new stack node
		Node new_node = new Node(data);
		if (new_node != null)
		{
			new_node.next = top;
			top = new_node;
			Console.Write("\n Push Stack element " + data + "\n");
			this.display();
		}
		else
		{
			Console.Write("Memory overflow\n");
		}
	}
	//Add a top element in stack
	//When stack is empty it returns -1
	public int pop()
	{
		int temp = -1;
		if (top != null)
		{
			temp = top.data;
			top = top.next;
		}
		return temp;
	}
	public Boolean is_empty()
	{
		if (this.top != null)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	//Used to get top element of stack
	public int peek()
	{
		if (top != null)
		{
			return top.data;
		}
		else
		{
			return -1;
		}
	}
	public static void Main(String[] args)
	{
		MyStack obj = new MyStack();
		int data = 0;
		Console.Write("\n Is empty : " + obj.is_empty() + "\n");
		//Add element
		obj.push(5);
		obj.push(4);
		obj.push(3);
		obj.push(2);
		obj.push(1);
		Console.Write("\n Top : " + obj.peek() + "\n");
		Console.Write("\n Size : " + obj.is_size());
		//Delete Stack Element
		data = obj.pop();
		Console.Write("\n Pop A Stack element " + data + "\n");
		obj.display();
		Console.Write("\n Size : " + obj.is_size());
		data = obj.pop();
		Console.Write("\n Pop A Stack element " + data + "\n");
		obj.display();
	}
}

Output

 Is empty : True

 Push Stack element 5
 5

 Push Stack element 4
 4 5

 Push Stack element 3
 3 4 5

 Push Stack element 2
 2 3 4 5

 Push Stack element 1
 1 2 3 4 5

 Top : 1

 Size : 5
 Pop A Stack element 1
 2 3 4 5

 Size : 4
 Pop A Stack element 2
 3 4 5
<?php
/*
  Php Program
  Implementation of stack using linked list
*/
//Stack Node
class Node
{
	public $data;
	public $next;

	function __construct($data)
	{
		$this->data = $data;
		$this->next = null;
	}
}
class MyStack
{
	public $top;

	function __construct()
	{
		$this->top = null;
	}
	public 	function display()
	{
		if ($this->top != null)
		{
			$temp = $this->top;
			while ($temp != null)
			{
				echo(" ". $temp->data);
				$temp = $temp->next;
			}
		}
		echo("\n");
	}
	//Count number of elements in stack 
	public 	function is_size()
	{
		$counter = 0;
		$temp = $this->top;
		while ($temp != null)
		{
			$counter++;
			$temp = $temp->next;
		}
		return $counter;
	}
	//Add a new element in stack
	public 	function push($data)
	{
		//Make a new stack node
		$new_node = new Node($data);
		if ($new_node != null)
		{
			$new_node->next = $this->top;
			$this->top = $new_node;
			echo("\n Push Stack element ". $data ."\n");
			$this->display();
		}
		else
		{
			echo("Memory overflow\n");
		}
	}
	//Add a top element in stack
	//When stack is empty it returns -1
	public 	function pop()
	{
		$temp = -1;
		if ($this->top != null)
		{
			$temp = $this->top->data;
			$this->top = $this->top->next;
		}
		return $temp;
	}
	public 	function is_empty()
	{
		if ($this->top != null)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	//Used to get top element of stack
	public 	function peek()
	{
		if ($this->top != null)
		{
			return $this->top->data;
		}
		else
		{
			return -1;
		}
	}
}

function main()
{
	$obj = new MyStack();
	$data = 0;
	echo("\n Is empty : ". (($obj->is_empty()==1)?"True":"False" )."\n");
	//Add element
	$obj->push(5);
	$obj->push(4);
	$obj->push(3);
	$obj->push(2);
	$obj->push(1);
	echo("\n Top : ". $obj->peek() ."\n");
	echo("\n Size : ". $obj->is_size());
	//Delete Stack Element
	$data = $obj->pop();
	echo("\n Pop A Stack element ". $data ."\n");
	$obj->display();
	echo("\n Size : ". $obj->is_size());
	$data = $obj->pop();
	echo("\n Pop A Stack element ". $data ."\n");
	$obj->display();
}
main();

Output

 Is empty : True

 Push Stack element 5
 5

 Push Stack element 4
 4 5

 Push Stack element 3
 3 4 5

 Push Stack element 2
 2 3 4 5

 Push Stack element 1
 1 2 3 4 5

 Top : 1

 Size : 5
 Pop A Stack element 1
 2 3 4 5

 Size : 4
 Pop A Stack element 2
 3 4 5
/*
  Node Js Program
  Implementation of stack using linked list
*/
//Stack Node
class Node
{
	constructor(data)
	{
		this.data = data;
		this.next = null;
	}
}
class MyStack
{
	constructor()
	{
		this.top = null;
	}
	display()
	{
		if (this.top != null)
		{
			var temp = this.top;
			while (temp != null)
			{
				process.stdout.write(" " + temp.data);
				temp = temp.next;
			}
		}
		process.stdout.write("\n");
	}
	//Count number of elements in stack 
	is_size()
	{
		var counter = 0;
		var temp = this.top;
		while (temp != null)
		{
			counter++;
			temp = temp.next;
		}
		return counter;
	}
	//Add a new element in stack
	push(data)
	{
		//Make a new stack node
		var new_node = new Node(data);
		if (new_node != null)
		{
			new_node.next = this.top;
			this.top = new_node;
			process.stdout.write("\n Push Stack element " + data + "\n");
			this.display();
		}
		else
		{
			process.stdout.write("Memory overflow\n");
		}
	}
	//Add a top element in stack
	//When stack is empty it returns -1
	pop()
	{
		var temp = -1;
		if (this.top != null)
		{
			temp = this.top.data;
			this.top = this.top.next;
		}
		return temp;
	}
	is_empty()
	{
		if (this.top != null)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	//Used to get top element of stack
	peek()
	{
		if (this.top != null)
		{
			return this.top.data;
		}
		else
		{
			return -1;
		}
	}
}

function main(args)
{
	var obj = new MyStack();
	this.data = 0;
	process.stdout.write("\n Is empty : " + obj.is_empty() + "\n");
	//Add element
	obj.push(5);
	obj.push(4);
	obj.push(3);
	obj.push(2);
	obj.push(1);
	process.stdout.write("\n Top : " + obj.peek() + "\n");
	process.stdout.write("\n Size : " + obj.is_size());
	//Delete Stack Element
	data = obj.pop();
	process.stdout.write("\n Pop A Stack element " + data + "\n");
	obj.display();
	process.stdout.write("\n Size : " + obj.is_size());
	data = obj.pop();
	process.stdout.write("\n Pop A Stack element " + data + "\n");
	obj.display();
}
main();

Output

 Is empty : true

 Push Stack element 5
 5

 Push Stack element 4
 4 5

 Push Stack element 3
 3 4 5

 Push Stack element 2
 2 3 4 5

 Push Stack element 1
 1 2 3 4 5

 Top : 1

 Size : 5
 Pop A Stack element 1
 2 3 4 5

 Size : 4
 Pop A Stack element 2
 3 4 5
#   Python 3 Program
#   Implementation of stack using linked list

# Stack Node
class Node :
	
	def __init__(self, data) :
		self.data = data
		self.next = None
	

class MyStack :
	
	def __init__(self) :
		self.top = None
	
	def display(self) :
		if (self.top != None) :
			temp = self.top
			while (temp != None) :
				print(" ", temp.data, end = "")
				temp = temp.next
			
		
		print("\n", end = "")
	
	# Count number of elements in stack 
	def is_size(self) :
		counter = 0
		temp = self.top
		while (temp != None) :
			counter += 1
			temp = temp.next
		
		return counter
	
	# Add a new element in stack
	def push(self, data) :
		# Make a new stack node
		new_node = Node(data)
		if (new_node != None) :
			new_node.next = self.top
			self.top = new_node
			print("\n Push Stack element ", data ,"\n", end = "")
			self.display()
		else :
			print("Memory overflow\n", end = "")
		
	
	# Add a top element in stack
	# When stack is empty it returns -1
	def pop(self) :
		temp = -1
		if (self.top != None) :
			temp = self.top.data
			self.top = self.top.next
		
		return temp
	
	def is_empty(self) :
		if (self.top != None) :
			return False
		else :
			return True
		
	
	# Used to get top element of stack
	def peek(self) :
		if (self.top != None) :
			return self.top.data
		else :
			return -1
		
	

def main() :
	obj = MyStack()
	data = 0
	print("\n Is empty : ", obj.is_empty() ,"\n", end = "")
	# Add element
	obj.push(5)
	obj.push(4)
	obj.push(3)
	obj.push(2)
	obj.push(1)
	print("\n Top : ", obj.peek())
	print("\n Size : ", obj.is_size(), end = "")
	# Delete Stack Element
	data = obj.pop()
	print("\n Pop A Stack element ", data )
	obj.display()
	print("\n Size : ", obj.is_size())
	data = obj.pop()
	print("\n Pop A Stack element ", data )
	obj.display()


if __name__ == "__main__": main()

Output

 Is empty :  True

 Push Stack element  5
  5

 Push Stack element  4
  4  5

 Push Stack element  3
  3  4  5

 Push Stack element  2
  2  3  4  5

 Push Stack element  1
  1  2  3  4  5

 Top :  1

 Size :  5
 Pop A Stack element  1
  2  3  4  5

 Size :  4

 Pop A Stack element  2
  3  4  5
# Ruby Program
# Implementation of stack using linked list

# Stack Node
class Node

	# Define the accessor and reader of class Node
	attr_reader :data, :next
	attr_accessor :data, :next

	def initialize(data)
	
		self.data = data
		self.next = nil
	end
end
class MyStack

	# Define the accessor and reader of class MyStack
	attr_reader :top
	attr_accessor :top

	def initialize()
		@top = nil
	end
	def display()
	
		if (@top != nil)
		
			temp = @top
			while (temp != nil)
			
				print(" ", temp.data)
				temp = temp.next
			end
		end
		print("\n")
	end
	# Count number of elements in stack 
	def is_size()
	
		counter = 0
		temp = @top
		while (temp != nil)
		
			counter += 1
			temp = temp.next
		end
		return counter
	end
	# Add a new element in stack
	def push(data)
	
		# Make a new stack node
		new_node = Node.new(data)
		if (new_node != nil)
		
			new_node.next = @top
			@top = new_node
			print("\n Push Stack element ", data ,"\n")
			self.display()
		else
		
			print("Memory overflow\n")
		end
	end
	# Add a top element in stack
	# When stack is empty it returns -1
	def pop()
	
		temp = -1
		if (@top != nil)
		
			temp = @top.data
			@top = @top.next
		end
		return temp
	end
	def is_empty()
	
		if (self.top != nil)
		
			return false
		else
		
			return true
		end
	end
	# Used to get top element of stack
	def peek()
	
		if (@top != nil)
		
			return @top.data
		else
		
			return -1
		end
	end
end
def main()

	obj = MyStack.new()
	@data = 0
	print("\n Is empty : ", obj.is_empty() ,"\n")
	# Add element
	obj.push(5)
	obj.push(4)
	obj.push(3)
	obj.push(2)
	obj.push(1)
	print("\n Top : ", obj.peek() ,"\n")
	print("\n Size : ", obj.is_size())
	# Delete Stack Element
	data = obj.pop()
	print("\n Pop A Stack element ", data ,"\n")
	obj.display()
	print("\n Size : ", obj.is_size())
	data = obj.pop()
	print("\n Pop A Stack element ", data ,"\n")
	obj.display()
end
main()

Output

 Is empty : true

 Push Stack element 5
 5

 Push Stack element 4
 4 5

 Push Stack element 3
 3 4 5

 Push Stack element 2
 2 3 4 5

 Push Stack element 1
 1 2 3 4 5

 Top : 1

 Size : 5
 Pop A Stack element 1
 2 3 4 5

 Size : 4
 Pop A Stack element 2
 3 4 5
/*
  Scala Program
  Implementation of stack using linked list
*/
//Stack Node
class Node(var data: Int,
	var next: Node)
{
	def this(data: Int)
	{
		this(data,null);
	}
}
class MyStack(var top: Node)
{
	def this()
	{
		this(null);
	}
	def display(): Unit = {
		if (top != null)
		{
			var temp: Node = top;
			while (temp != null)
			{
				print(" " + temp.data);
				temp = temp.next;
			}
		}
		print("\n");
	}
	//Count number of elements in stack 
	def is_size(): Int = {
		var counter: Int = 0;
		var temp: Node = top;
		while (temp != null)
		{
			counter += 1;
			temp = temp.next;
		}
		return counter;
	}
	//Add a new element in stack
	def push(data: Int): Unit = {
		//Make a new stack node
		var new_node: Node = new Node(data);
		if (new_node != null)
		{
			new_node.next = top;
			top = new_node;
			print("\n Push Stack element " + data + "\n");
			this.display();
		}
		else
		{
			print("Memory overflow\n");
		}
	}
	//Add a top element in stack
	//When stack is empty it returns -1
	def pop(): Int = {
		var temp: Int = -1;
		if (top != null)
		{
			temp = top.data;
			top = top.next;
		}
		return temp;
	}
	def is_empty(): Boolean = {
		if (this.top != null)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	//Used to get top element of stack
	def peek(): Int = {
		if (top != null)
		{
			return top.data;
		}
		else
		{
			return -1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyStack = new MyStack();
		var data: Int = 0;
		print("\n Is empty : " + obj.is_empty() + "\n");
		//Add element
		obj.push(5);
		obj.push(4);
		obj.push(3);
		obj.push(2);
		obj.push(1);
		print("\n Top : " + obj.peek() + "\n");
		print("\n Size : " + obj.is_size());
		//Delete Stack Element
		data = obj.pop();
		print("\n Pop A Stack element " + data + "\n");
		obj.display();
		print("\n Size : " + obj.is_size());
		data = obj.pop();
		print("\n Pop A Stack element " + data + "\n");
		obj.display();
	}
}

Output

 Is empty : true

 Push Stack element 5
 5

 Push Stack element 4
 4 5

 Push Stack element 3
 3 4 5

 Push Stack element 2
 2 3 4 5

 Push Stack element 1
 1 2 3 4 5

 Top : 1

 Size : 5
 Pop A Stack element 1
 2 3 4 5

 Size : 4
 Pop A Stack element 2
 3 4 5
/*
  Swift Program
  Implementation of stack using linked list
*/
//Stack Node
class Node
{
	var data: Int;
	var next: Node? ;
	init(_ data: Int)
	{
		self.data = data;
		self.next = nil;
	}
}
class MyStack
{
	var top: Node? ;
	init()
	{
		self.top = nil;
	}
	func display()
	{
		if (self.top != nil)
		{
			var temp: Node? = self.top;
			while (temp != nil)
			{
				print(" ", temp!.data, terminator: "");
				temp = temp!.next;
			}
		}
		print("\n", terminator: "");
	}
	//Count number of elements in stack 
	func is_size() -> Int
	{
		var counter: Int = 0;
		var temp: Node? = self.top;
		while (temp != nil)
		{
			counter += 1;
			temp = temp!.next;
		}
		return counter;
	}
	//Add a new element in stack
	func push(_ data: Int)
	{
		//Make a new stack node
		let new_node: Node? = Node(data);
		if (new_node != nil)
		{
			new_node!.next = self.top;
			self.top = new_node;
			print("\n Push Stack element ", data ,"\n", terminator: "");
			self.display();
		}
		else
		{
			print("Memory overflow\n", terminator: "");
		}
	}
	//Add a top element in stack
	//When stack is empty it returns -1
	func pop() -> Int
	{
		var temp: Int = -1;
		if (self.top != nil)
		{
			temp = self.top!.data;
			self.top = self.top!.next;
		}
		return temp;
	}
	func is_empty() -> Bool
	{
		if (self.top != nil)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	//Used to get top element of stack
	func peek() -> Int
	{
		if (self.top != nil)
		{
			return self.top!.data;
		}
		else
		{
			return -1;
		}
	}
}
func main()
{
	let obj: MyStack = MyStack();
	var data: Int = 0;
	print("\n Is empty : ", obj.is_empty() ,"\n", terminator: "");
	//Add element
	obj.push(5);
	obj.push(4);
	obj.push(3);
	obj.push(2);
	obj.push(1);
	print("\n Top : ", obj.peek() ,"\n", terminator: "");
	print("\n Size : ", obj.is_size(), terminator: "");
	//Delete Stack Element
	data = obj.pop();
	print("\n Pop A Stack element ", data ,"\n", terminator: "");
	obj.display();
	print("\n Size : ", obj.is_size(), terminator: "");
	data = obj.pop();
	print("\n Pop A Stack element ", data ,"\n", terminator: "");
	obj.display();
}
main();

Output

 Is empty :  true

 Push Stack element  5
  5

 Push Stack element  4
  4  5

 Push Stack element  3
  3  4  5

 Push Stack element  2
  2  3  4  5

 Push Stack element  1
  1  2  3  4  5

 Top :  1

 Size :  5
 Pop A Stack element  1
  2  3  4  5

 Size :  4
 Pop A Stack element  2
  3  4  5


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved