Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
51 views

Dsa Stack2 Algorithm

This document discusses two approaches to implementing a stack data structure in Java - using a linked list and using an array. For the linked list implementation, nodes are used to store data and link to the next node. Elements can be pushed onto and popped off the stack. For the array implementation, an array of size 9 is used to store elements, with a top pointer tracking the current end of the stack. A menu driven program demonstrates pushing, popping and displaying elements on the stack using both implementations.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Dsa Stack2 Algorithm

This document discusses two approaches to implementing a stack data structure in Java - using a linked list and using an array. For the linked list implementation, nodes are used to store data and link to the next node. Elements can be pushed onto and popped off the stack. For the array implementation, an array of size 9 is used to store elements, with a top pointer tracking the current end of the stack. A menu driven program demonstrates pushing, popping and displaying elements on the stack using both implementations.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

1.

stack using linkedlist

import java.util.*;

class Main{

public static void main(String[] args)

StackUsingLinkedlist o= new StackUsingLinkedlist();

System.out.println("enter number of elements u want to insert ");

Scanner sc=new Scanner(System.in);

int p=sc.nextInt();

for(int i=0;i<p;i++){

System.out.println("enter the number");

int q=sc.nextInt();

o.push(q);

o.display();

System.out.println("Top element is "+o.peek());

o.pop();

o.pop();

o.display();

System.out.println("Top element is "+o.peek());

class StackUsingLinkedlist {

private class Node {

int data;

Node next;

Node top;

StackUsingLinkedlist() { this.top = null; }

public void push(int x)

{
Node temp = new Node();

if (temp == null) {

System.out.print("\nHeap Overflow");

return;

temp.data = x;

temp.next = top;

top = temp;

public boolean isEmpty() { return top == null; }

public int peek()

if (!isEmpty()) {

return top.data;

else {

System.out.println("Stack is empty");

return -1;

public void pop()

if (top == null) {

System.out.println("Stack is empty");

return;

top = (top).next;

public void display()

{
if (top == null) {

System.out.println("Stack is empty");

System.exit(1);

else {

Node temp = top;

while (temp != null) {

System.out.print(temp.data);

temp = temp.next;

if(temp != null)

System.out.print(" -> ");

System.out.println();

Algorithm:
1.take user input for number of elements needed to be
added in stack
2.take those elements from stack
3.push those elements into stack one top on other
4.can display the elements present in the stack
5.we can use peek() method to print the top most
element in the stock
6.pop() to remove the top element and pointing top to
the below element

2.stack using array


import java.util.Scanner;
class Stack
{
int top;
int maxsize = 9;
int[] arr = new int[maxsize];
boolean isEmpty()
{
return (top < 0);
}
Stack()
{
top = -1;
}
boolean push (Scanner sc)
{
if(top == maxsize-1)
{
System.out.println("Overflow !!");
return false;
}
else
{
System.out.println("Enter Value");
int val = sc.nextInt();
top++;
arr[top]=val;
System.out.println("Item pushed");
return true;
}
}
boolean pop ()
{
if (top == -1)
{
System.out.println("Underflow !!");
return false;
}
else
{
top --;
System.out.println("Item popped");
return true;
}
}
void display ()
{
System.out.println("Printing stack elements .....");
for(int i = top; i>=0;i--)
{
System.out.println(arr[i]);
}
}
}
public class Main {
public static void main(String[] args) {
int choice=0;
Scanner sc = new Scanner(System.in);
Stack s = new Stack();
while(choice != 4)
{
System.out.println("\nChose one from the below options...\n");
System.out.println("\n1.Push\n2.Pop\n3.Show\n4.Exit");
System.out.println("\n Enter your choice \n");
choice = sc.nextInt();
switch(choice)
{
case 1:
{
s.push(sc);
break;
}
case 2:
{
s.pop();
break;
}
case 3:
{
s.display();
break;
}
case 4:
{
System.out.println("Exiting....");
System.exit(0);
break;
}
default:
{
System.out.println("Please Enter valid choice ");
}
};
}
}
}

Algorithm:
1.create an array with a length of 9
2.using switch case 1.push() 2.pop() 3.show() 4.Exit()
3.call the function inside the cases
4.for push if maximum length is not reached element is added
4.for pop if array is not empty top is decremented
5.for display loop is transversed from top to 0 and printed

You might also like