Question

Java Write a pseudocode and Java program to implement the Stack using arrays. Write Push(), Pop(),and...

Java

Write a pseudocode and Java program to implement the Stack using arrays. Write Push(), Pop(),and Display() methods to demonstrate that it works.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

pseudocode:

procedure push(int x, stack[])
{
   if top == max_size_of_stack-1
   {
       display overflow
       return
   }
   else
   {
       top = top+1
       stack[top] = x
   }
   return
}

procedure pop(stack[])
{
   if top == -1
   {
       dispaly underflow
       return
   }

   else
   {
       top = top-1
       return stack[top]
   }
}

procedure display(stack[])
{
   if top == -1
   {
       display empty
       return
   }
   else
   {
       for i=top..0
       {
           print stack[i]
       }
   }
}

Source Code:

import java.util.*;

class Stack
{
   private int arr[];
   private int top;
   private int size;

   // Stack Constructor
   Stack(int size)
   {
       arr = new int[size];
       this.size = size;
       top = -1;
   }

   // pushes data x on to the stack
   public void push(int x)
   {

       // if stack is full exit
       if (top == size - 1)
       {
           System.out.println("Stack Overflow");
           return;
       }

       System.out.println(x + " Pushed!");
       // else push the data on to the stack
       arr[++top] = x;
   }

   // pops element from the stack
   public int pop()
   {
       // if stack is empty
       if (top == -1)
       {
           System.out.println("Stack Underflow!");
           return -1;
       }

       System.out.println(arr[top] + " popped!");

       // return the top of the array and decrement the top value
       return arr[top--];
   }

   // displays the content of the stack
   public void display()
   {
       if(top == -1)
       {
           System.out.println("Stack is Empty");
           return;
       }
       // dispalays stack in the format as the stack
       for(int i=top; i>=0; i--)
       {
           System.out.println(arr[i]);
       }
   }


   public static void main (String[] args)
   {

        // size of stack is 5
       Stack stack = new Stack(5);

       System.out.println("Pushing data!!");
       stack.push(1);       // pushing 1 on to stack
       stack.push(2);       // pushing 2 on to stack
       stack.push(3);       // pushing 3 on to stack
       stack.push(4);       // pushing 4 on to stack

       System.out.println("\nAfter pushing elements onto stack: ");
       stack.display();

       System.out.println("\nPopping data!!");
       stack.pop();       // removing the top 2
       stack.pop();       // removing the top 1

       System.out.println("\nContents Of stack after all operations: ");
       stack.display();
   }
}

Output:

Pushing data!! 1 Pushed! 2 Pushed! 3 Pushed! 4 Pushed! After pushing elements onto stack: Popping data!! 4 popped! 3 popped!

if size of stack is 1

Pushing data!! 1 Pushed! Stack Overflow Stack Overflow Stack Overflow After pushing elements onto stack: Popping data!! 1 pop

Hope your problem solved and appreciating this solution.

Feel free to ask doubts in comment section.

Add a comment
Know the answer?
Add Answer to:
Java Write a pseudocode and Java program to implement the Stack using arrays. Write Push(), Pop(),and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please write a Java interface for an integer stack (should have the methods push, pop, toString)....

    Please write a Java interface for an integer stack (should have the methods push, pop, toString). Then implement this interface using one of our linked list nodes. Then please write an interface for an integer queue ( should have methods enqueue, dequeue, and toString). Then implement this interface using one of our linked list objects. Please see chapter 3 in the text for a definition of a stack. Please write a program that asks the user for how many numbers...

  • Use Java to implement a basic stack using an array of integers. For the stack, you...

    Use Java to implement a basic stack using an array of integers. For the stack, you will create an array of integers that holds 5 numbers. To make it easier, you can declare the array at the class level. That way you will be able to use the array in any method in your class without using parameters. Your input/output interface should look something like the following: What operation do you want to do? push What number do you want...

  • JAVA PROGRAM Design a stack that supports push, pop, top, and retrieving the minimum element in...

    JAVA PROGRAM Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. • push(x) -- Push element x onto stack. • pop() -- Removes the element on top of the stack. • top() -- Get the top element. • getMin() -- Retrieve the minimum element in the stack. Example 1: Input ["MinStack", "push", "push","push", "getMin","pop","top", "getMin"] [1], [-2], [0], [-3],[1,1],[1, [1] Output [null, null, null,null,-3, null,0,-2] Explanation MinStack minStack = new MinStack(); minStack.push(-2); minStack.push(0);...

  • Write a program that uses a stack to reverse its inputs. Your stack must be generic...

    Write a program that uses a stack to reverse its inputs. Your stack must be generic and you must demonstrate that it accepts both String and Integer types. Your stack must implement the following methods: push, pop, isEmpty (returns true if the stack is empty and false otherwise), and size (returns an integer value for the number of items in the stack). You may use either an ArrayList or a LinkedList to implement your stack. Also, your pop method must...

  • How do you implement a stack using a singly linked list in Java? Can you make...

    How do you implement a stack using a singly linked list in Java? Can you make it a simple implementation with just push and pop methods.

  • Describe, in pseudocode, how you can implement all the functions of the stack ADT using two...

    Describe, in pseudocode, how you can implement all the functions of the stack ADT using two queues. What is the running time of the push and pop functions in this case?

  • Create a Java Program that uses a Stack to implement a deck of cards. Import java.util.stack...

    Create a Java Program that uses a Stack to implement a deck of cards. Import java.util.stack and use its classes and methods. Note that java.uitl.stack uses peek instead of top (as we did in class). Look up the documentation for java.uitl.stack to see what methods it contains and how to call them. Create a Stack of cards that will hold strings. Push new items to the stack (Use "Qclubs" for Queen of clubs and so on) After your 'deck' has...

  • I need to implement raw array Stack for create empty stack, isEmpty, isFull, push, pop, and...

    I need to implement raw array Stack for create empty stack, isEmpty, isFull, push, pop, and size using below pseudo code. I need two classes with stack pseudo code implementation and the main method to demonstrate the correct working of each operation. pseudo code StackADT (using raw array) class StackADT { int top int items[] int max StackADT(int n) Initialize array to n capacity top = 0 max = n boolean isEmpty() if array has no elements return true else...

  • Create a Stack class based on java.util.LinkedList class. Your Stack class should have a push(), pop(),...

    Create a Stack class based on java.util.LinkedList class. Your Stack class should have a push(), pop(), peek(), and isEmpy() methods. Create a new Java Application that has the following methods. Write a method reverseChar() to print a sentence in reverse order. Use a Stack to reverse each character. Example: if the user enters a sentence “ABC DEFG”, the program will display “GFED CBA” Write a method reverseWord() to print a sentence reverse order. Use a Stack to reverse each word....

  • Need help with java In this program you will use a Stack to implement backtracking to solve Sudok...

    need help with java In this program you will use a Stack to implement backtracking to solve Sudoku puzzles. Part I. Implement the stack class.  Created a generic, singly-linked implementation of a Stack with a topPtr as the only instance variable. Implement the following methods only: public MyStack() //the constructor should simply set the topPtr to null public void push(E e) public E pop()  Test your class thoroughly before using it within the soduku program Part II. Create...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT