Question

In: Computer Science

Exercise 3: Stack Write a program in Java to manipulate a Stack List: 1. Create Stack...

Exercise 3: Stack
Write a program in Java to manipulate a Stack List:

1. Create Stack List
2. Display the list
3. Create the function isEmply
4. Count the number of nodes
5. Insert a new node in the Stack List.
6. Delete the node in the Stack List.
7. Call all methods above in main method with the following data:

Test Data :
Input the number of nodes : 4
Input data for node 1 : 5
Input data for node 2 : 6
Input data for node 3 : 7

Input data for node 4 : 9

Exercise 4: Queue
Write a program in Java to manipulate a Stack List:

1. Create Queue List
2. Display the list
3. Count the number of nodes
4. Insert a new node in the Stack List.
5. Delete the node in the Stack List.
6. Call all methods above in main method with the following data:

Test Data :
Input the number of nodes : 4
Input data for node 1 : 5
Input data for node 2 : 6
Input data for node 3 : 7

Input data for node 4 : 9

Solutions

Expert Solution

All the explanation is present in the comments of the codes itseld.

EXERCISE 3--Stack:

Code--

import java.util.*;
class StackList
{
   //create an array list to implements stack
   ArrayList<Integer> stk=new ArrayList<Integer>();
   //display list
   public void display()
   {
       for(int i=0;i<stk.size();i++)
           System.out.print(stk.get(i)+" ");
       System.out.println();
   }
   public boolean isEmpty()
   {
       if(stk.size()==0)
           return true;
       else
           return false;
   }
   public int count()
   {
       return stk.size();
   }
   public void insert(int val)
   {
       //add at the beginning
       stk.add(0,val);
   }
   public void delete()
   {
       stk.remove(0);
   }
}
public class TestStack
{
   public static void main(String[] args)
   {
       Scanner sc=new Scanner(System.in);
       StackList stack=new StackList();
       int size;
       System.out.print("Input the number of nodes ");
       size=sc.nextInt();
       int i=0;
       while(i<size)
       {
           System.out.print("Input data for node "+(i+1)+": ");
           stack.insert(sc.nextInt());
           i++;
       }
       System.out.println("Stack currently:");
       stack.display();
       stack.delete();
       stack.delete();
       System.out.println("After deleting two nodes:");
       stack.display();
   }
}

Output Screenshot:

EXERCISE 4: Queue:

Code--

import java.util.*;
class QueueList
{
   //create an array list to implements queue
   ArrayList<Integer> qu=new ArrayList<Integer>();
   //display list
   public void display()
   {
       for(int i=0;i<qu.size();i++)
           System.out.print(qu.get(i)+" ");
       System.out.println();
   }
   public boolean isEmpty()
   {
       if(qu.size()==0)
           return true;
       else
           return false;
   }
   public int count()
   {
       return qu.size();
   }
   public void insert(int val)
   {
       //add at the last
       qu.add(val);
   }
   public void delete()
   {
       //remove from the last
       qu.remove(0);
   }
}
public class TestQueue
{
   public static void main(String[] args)
   {
       Scanner sc=new Scanner(System.in);
       QueueList q=new QueueList();
       int size;
       System.out.print("Input the number of nodes ");
       size=sc.nextInt();
       int i=0;
       while(i<size)
       {
           System.out.print("Input data for node "+(i+1)+": ");
           q.insert(sc.nextInt());
           i++;
       }
       System.out.println("Stack currently:");
       q.display();
       q.delete();
       q.delete();
       System.out.println("After deleting two nodes:");
       q.display();
   }
}

Output Screenshot:

Note--

Please upvote if you like the effort.


Related Solutions

Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly...
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly Linked List 2. Display the list 3. Count the number of nodes 4. Insert a new node at the beginning of a Singly Linked List. 5. Insert a new node at the end of a Singly Linked List 6. Insert a new node after the value 5 of Singly Linked List 7. Delete the node with value 6. 8. Search an existing element in...
Exercise 2: Write a program in Java to manipulate a Double Linked List: 1. Create Double...
Exercise 2: Write a program in Java to manipulate a Double Linked List: 1. Create Double Linked List 2. Display the list 3. Count the number of nodes 4. Insert a new node at the beginning of a Double Linked List. 5. Insert a new node at the end of a DoubleLinked List 6. Insert a new node after the value 5 of Double Linked List 7. Delete the node with value 6. 8. Search an existing element in a...
Write a C++ program to help the Administration of a football league to manipulate the list...
Write a C++ program to help the Administration of a football league to manipulate the list of players registered in different teams. There are 26 teams participating in the league, each is denoted by a letter in the range A to Z. Each team can have 11 players at most. The information of all teams' players are stored in a text file named 'players.dat'. Each line from the input file contains the details of one player; where the player's details...
JAVA Write a class for a Stack of characters using a linked list implementation. Write a...
JAVA Write a class for a Stack of characters using a linked list implementation. Write a class for a Queue of characters using a linked list implementation. Write a class for a Queue of integers using a circular array implementation.
Program in Java Create a stack class to store integers and implement following methods: 1- void...
Program in Java Create a stack class to store integers and implement following methods: 1- void push(int num): This method will push an integer to the top of the stack. 2- int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3- void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
3) Create a Java program that uses NO methods, but use scanner: Write a program where...
3) Create a Java program that uses NO methods, but use scanner: Write a program where you will enter the flying distance from one continent to another, you will take the plane in one country, then you will enter miles per gallon and price of gallon and in the end it will calculate how much gas was spend for that distance in miles. Steps: 1) Prompt user to enter the name of country that you are 2) Declare variable to...
Write a java program that uses a stack to reverse an integer of three digits. The...
Write a java program that uses a stack to reverse an integer of three digits. The program must prompt the user to input an integer of 3 digits and display it in reverse. - Your program must include the Stack class, the Reverse class, and the Test class. - In the Test class, the program must prompt the user to input a 3-digit number and display it in reverse. - Class Reverse must use the Stack class to reverse the...
JAVA Exercise BasketBall Bar Chart Write a program that allows you to create a bar chart...
JAVA Exercise BasketBall Bar Chart Write a program that allows you to create a bar chart for the active members of a basketball team during a game. (Recall: there are only 5 active players on the court per team in a standard basketball game.) Your program should do the following tasks: • Prompt the user to store the first name of the five players • Prompt the user to enter the points scored by each player a. Use a while...
in java we need to order a list , if we create a program in java...
in java we need to order a list , if we create a program in java what  are the possible ways of telling your program how to move the numbers in the list to make it sorted, where each way provides the required result. list the name of sorting with short explanation
Description of the Assignment: Write a Python program to (a) create a new empty stack. Then,...
Description of the Assignment: Write a Python program to (a) create a new empty stack. Then, (b) add items onto the stack. (c) Print the stack contents. (d) Remove an item off the stack, and (e) print the removed item. At the end, (f) print the new stack contents: Please use the following comments in your python script: # ************************************************* # COP3375 # Student's full name ( <<< your name goes here) # Week 8: Assignment 1 # ************************************************* #...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT