Friday 11 December 2015

Stack and Queue - Java Applet

Q:- Design a Java Applet to implement stack and queue. Provide buttons for various operations like pop, push etc. Design separate classes for stack, queue and the applet.



Code for Applet Class


import java.applet.Applet; 
import java.awt.*;
import java.awt.event.*;
Java-Applet-Stack-Queue
public class Appletsq extends Applet implements ActionListener{
Stack s;
Queue q;
TextField ts=new TextField("Enter the number of elements for queue");
TextField tq=new TextField("Enter the number of elements for stack");
Button bs=new Button("Start");
Button pop=new Button("pop");
Button push=new Button("push");
Button sdisp=new Button("display");
Button insert=new Button("insert");
Button del=new Button("delete");
Button qdisp=new Button("display");
TextArea stext=new TextArea();
TextArea qtext=new TextArea();
public void init(){
add(ts);
add(tq);
ts.setVisible(true);
tq.setVisible(true);
add(bs);
bs.addActionListener(this);
bs.setVisible(true);
pop.setVisible(true);
push.setVisible(true);
sdisp.setVisible(true);
insert.setVisible(true);
del.setVisible(true);
qdisp.setVisible(true);
stext.setVisible(true);
qtext.setVisible(true);
add(stext);
add(pop);
add(push);
add(sdisp);
add(qtext);
add(insert);
add(del);
add(qdisp);
pop.addActionListener(this);
push.addActionListener(this);
sdisp.addActionListener(this);
insert.addActionListener(this);
del.addActionListener(this);
qdisp.addActionListener(this);
}
public void actionPerformed(ActionEvent e){// this method is the Action Listener Method for the various events performed
    String display;
    int g;
    if(e.getSource()==bs){
        s=new Stack(Integer.parseInt(ts.getText()));
        q=new Queue(Integer.parseInt(tq.getText()));
        remove(ts);
        remove(tq);
        remove(bs);
    }
    else if(e.getSource()==push){
        display=s.push(Integer.parseInt(stext.getText()));
         stext.setText(display);
    }
    else if(e.getSource()==pop){
         g=s.pop();
         if(g!=-1)
         display="g";
         else
         display="The Stack is empty";
         stext.setText(display);
    }
    else if(e.getSource()==sdisp){
        String z=s.display();
        if(s.top!=-1){
            z=z+"\n Top=";
            z=z.concat(String.valueOf(s.st[s.top]));
        }
      
        stext.setText(z);
    }
    else if(e.getSource()==insert){
    display=q.insert(Integer.parseInt(qtext.getText()));
        qtext.setText(display);
    }
    else if(e.getSource()==del){
    display=q.delete();
       qtext.setText(display);
    }
    else{
     display =q.display();
        display=display+"\nFront=";
        display=display.concat(String.valueOf(q.front));
        display=display+"\nRear=";
        display=display.concat(String.valueOf(q.rear));
        qtext.setText(display);
    }
}
public void start(){}
public void paint(Graphics g){   
}   
}


Java-Applet-Stack-Queue

Code for Stack

public class Stack {
int top;
int [] st;
int max;
Stack(int max){
    st=new int[max];
    top=-1;
    this.max=max;
}
boolean isEmpty(){
    if(top==-1)
        return true;
    else
        return false;
}
boolean isFull(){
    if(top==max-1)
        return true;
    else
        return false;
}
String display(){
    String z;
    z="";
   
    if (isEmpty())
        return "stack is empty";
    else{
       
        int i;
        for(i=0;i<=top;i++){
            z=z+String.valueOf(st[i]);
            z=z+"\t";
        }
           
        return z;
    }
}
int top(){
    return st[top];
}
String push(int value){
    if(isFull())
        return "Stack is full";
    else{
    st[++top]=value;
    return "Successfully pushed";
    }
}
int pop(){
    if(isEmpty()){
        System.out.println("The stack is empty");
        return -1;
    }
       
    else
        return st[top--];
}

Code for Queue

public class Queue {
int front,rear;
int []q;
int max;
Queue(int max){
    front=0;
    rear=0;
    this.max=max;
    q=new int[max];
}
boolean isEmpty(){
    if(front==rear)
        return true;
else
    return false;
}
boolean isFull(){
    if((rear==max) && (front==0))
        return true;
else
    return false;
}

String insert(int value){
    if (isFull())
    return "The queue is full";
    else if(rear==max){
        int i;
        int f2=front;
        for(i=0;i<max-front;i++){//code segment for shifting of values
            q[i]=q[f2++];
        }
        front=0;    //set the front value after shifting
        rear=i;        //set the rear value after shifting
        q[rear++]=value;    //append the queue
        return "sucessfully inserted";
    }
    else
    q[rear++]=value;
    return "successfull inserted";
}
String delete()
{
if (isEmpty()){
    return  "The list is empty no elements to delete";

}
else
return  String.valueOf(q[front++]);
}
String display(){
    if (isEmpty())
        return "stack is empty";
    else{
        int i ;
        String z;
        z="";
        for(i=front;i<rear;i++)
            {z=z+String.valueOf(q[i]);
            z=z+"\t";}
        return z;
   
    }
   
}
}

1 comment: