In: Computer Science
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
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.