Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
1. Implement your own Queue and Stack data structures using Linked List. Do not use the built in Queue and Stack from Java Collection Framework. You can name your queue and stack as MyQueue and MyStack, respectively.

public class MyQueue{
private Node front;
private Node rear;
private Node newNode;
//Constructor
public Queue() { }
// Method isEmpty()
public boolean isEmpty()
{ return front == null; }
// definition of other methods
...
}
public class MyStack{
private Node stackTop;
private Node newNode;
//Default Constructor
public MyStack()
{ stackTop = null;}
// definition of other methods
... }

What I have tried:

I tried myqueue already.. how to do my stack?

public class MyQueue
    {
        private Node front;
        private Node rear;
        private Node newNode;
        public MyQueue() { }
         public boolean isEmpty()
       { return front == null; }
       public Object front()
        {
            if (isEmpty())
                return null;
           else{
               newNode = front;
               return newNode.data;
            }
}
         public Object rear()
        {
            if (isEmpty())
                return null;
            else{
                newNode = rear;
                return newNode.data;
            }
}
        public void enqueue(Object item)
        {
           newNode = new Node(item);
           if(rear == null)
           front = rear = newNode;
else {
               newNode.next = null;
               rear.next = newNode;
               rear = newNode;
} }
        public Object dequeue()
        {
           Object itemRemoved = null;
           if ( isEmpty() )
          System.out.println( "The list is Empty, cannot remove item ");
        }
        itemRemoved = front.data;
        if(front == rear) // Check if only have one node in linked list
        front = rear = null;
        else // if there is more than one list
        {
            front = front.next;
}
        return itemRemoved;
    }
     public void print()
   {
      if ( isEmpty() ) {
         System.out.println( " Queue is Empty ");
return; }
      Node current = front;
      while ( current != null ) {
         System.out.print( current.data.toString() + " " );
         current = current.next;
      }
      System.out.println( "\n" );
   }
}
Posted
Updated 25-May-22 20:04pm
Comments
[no name] 26-May-22 7:49am    
there is lost parenthesis after if()
also add return if list is empty
public Object dequeue() {   
  Object itemRemoved = null;   
  if ( isEmpty() )   
  {   // add parenthesis    
    System.out.println( "The list is Empty, cannot remove item "); 
    return null; // added
  }

1 solution

I wonder if there is some info available on the web...(Stack Data Structure and Implementation in Python, Java and C/C++[^]).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900