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 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...
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 # ************************************************* #...
Suppose the interface and the class of stack already implemented, Write application program to ( java)...
Suppose the interface and the class of stack already implemented, Write application program to ( java) 1- insert 100 numbers to the stack                         2- Print the even numbers 3- Print the summation of the odd numbers
Using the Stack ADT: Create a program that uses a stack. Your program should ask the...
Using the Stack ADT: Create a program that uses a stack. Your program should ask the user to input a few lines of text and then outputs strings in reverse order of entry. (Optional) Create a similar program that uses a stack. Your new program should ask the user to input a line of text and then it should print out the line of text in reverse. To do this your application should use a stack of Character. In Java...
Write a java program to reverse element of a stack. For any given word (from input),...
Write a java program to reverse element of a stack. For any given word (from input), insert every character (from the word) into a stack. The output from the stack should be the same as the input. Your program should be using a stack and a queue to complete this process. 1. Push into stack 2. Pop from stack 3. Enqueue into queue 4. Dequeue from queue 5. Push into stack 6. Pop from stack and display java
Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
In this program, you'll create a program that utilizes an enumerated data type to manipulate the...
In this program, you'll create a program that utilizes an enumerated data type to manipulate the array.   Here are the requirements: Write a program that contains an enumerated data type named Letters.    In the declaration, include the following three enumerators: ALPHA, BETA, DELTA. Then, create an array of integers three elements long. The array should be initialized to 0 using a 1-element initialization list. Instead of using integers as subscripts, use the enumerators from your enumerated data type to assign...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT