Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class IntQueue {
- private int[] items_;
- private int top_;
- private int capacity_;
- private int iter_;
- public IntQueue(int capacity)
- {
- if(capacity <=0) capacity = 10;
- capacity_ = capacity;
- top_=0;
- count_ = 0;
- iter_=0;
- items_= new int[capacity_];
- }
- public void push_back(int value)
- {
- if(top_>= capacity_)
- overflow();
- items_[top_++]=value;
- count_++;
- }
- public int front()
- {
- if(top_<=0)
- return 0;
- int temp=0;
- temp=items_[iter_];
- count_--;
- iter_++;
- return temp;
- }
- public IntQueue clone()
- {
- IntQueue result = new IntQueue(capacity_);
- for(int i=0 ; i<top_; ++i)
- {
- result.push_back(items_[i]);
- }
- /*for(int i=0 ; i<top_ ; ++i)
- {
- result.items_[i] = items_[i];
- }*/
- return result;
- }
- public void log()
- {
- for(int i=0 ; i <top_; ++i)
- {
- System.out.print(items_[i]);
- if(i<top_ -1)
- System.out.print(", ");
- }
- System.out.println();
- }
- }
- private void overflow()
- {
- int[] newItem = new int[capacity_*2];
- for(int i=0 ; i <top_; ++i)
- newItem[i] = items_[i];
- items_=newItem;
- capacity_ *=2;
- }
- public static void main(String args[])
- {
- IntQueue queue = new IntQueue(2);
- System.out.println("queue push 3: "); queue.push_back(3);
- System.out.println("queue push 2: "); queue.push_back(2);
- System.out.println("queue push 1: "); queue.push_back(1);
- System.out.print("queue log: "); queue.log();
- System.out.println("front " + queue.front());
- System.out.println("front " + queue.front());
- System.out.print("queue log: "); queue.log();
- System.out.println("queue push 12: "); queue.push_back(12);
- System.out.println("queue push 11: "); queue.push_back(11);
- System.out.println("queue push 21: "); queue.push_back(21);
- System.out.println("queue push 31: "); queue.push_back(31);
- System.out.print("queue log: "); queue.log();
- System.out.println("front " + queue.front());
- System.out.println("front " + queue.front());
- System.out.print("clone queue log: "); queue.clone().log();
- }
- }
- void removeAt(int i) {
- final E[] items = this.items;
- // if removing front item, just advance
- if (i == takeIndex) {
- items[takeIndex] = null;
- takeIndex = inc(takeIndex);
- } else {
- // slide over all others up through putIndex.
- for (;;) {
- int nexti = inc(i);
- if (nexti != putIndex) {
- items[i] = items[nexti];
- i = nexti;
- } else {
- items[i] = null;
- putIndex = i;
- break;
- }
- }
- }
- --count;
- notFull.signal();
- }
- import java.util.Iterator;
- import java.util.LinkedList;
- import java.util.Queue;
- public class QueueExample {
- public static void main(String[] args) {
- Queue<String> qe=new LinkedList<String>();
- qe.add("b");
- qe.add("a");
- qe.add("c");
- qe.add("e");
- qe.add("d");
- Iterator it=qe.iterator();
- System.out.println("Initial Size of Queue :"+qe.size());
- while(it.hasNext())
- {
- String iteratorValue=(String)it.next();
- System.out.println("Queue Next Value :"+iteratorValue);
- }
- // get value and does not remove element from queue
- System.out.println("Queue peek :"+qe.peek());
- // get first value and remove that object from queue
- System.out.println("Queue poll :"+qe.poll());
- System.out.println("Final Size of Queue :"+qe.size());
- }
- }
- int queue[SIZE];
- int first = 0;
- int last = 0;
- void enque(int i) {
- if(last == SIZE)
- throw new RuntimeExeption("Queue is full");
- queue[last++] = i;
- }
- int deque() {
- if(first == last)
- throw new RuntimeExeption("Queue is empty");
- return queue[first++];
- }
- import java.io.*;
- import java.lang.*;
- class clrqueue
- {
- DataInputStream get=new DataInputStream(System.in);
- int a[];
- int i,front=0,rear=0,n,item,count=0;
- void getdata()
- {
- try
- {
- System.out.println("Enter the limit");
- n=Integer.parseInt(get.readLine());
- a=new int[n];
- }
- catch(Exception e)
- {
- System.out.println(e.getMessage());
- }
- }
- void enqueue()
- {
- try
- {
- if(count<n)
- {
- System.out.println("Enter the element to be added:");
- item=Integer.parseInt(get.readLine());
- a[rear]=item;
- rear++;
- count++;
- }
- else
- System.out.println("QUEUE IS FULL");
- }
- catch(Exception e)
- {
- System.out.println(e.getMessage());
- }
- }
- void dequeue()
- {
- if(count!=0)
- {
- System.out.println("The item deleted is:"+a[front]);
- front++;
- count--;
- }
- else
- System.out.println("QUEUE IS EMPTY");
- if(rear==n)
- rear=0;
- }
- void display()
- {
- int m=0;
- if(count==0)
- System.out.println("QUEUE IS EMPTY");
- else
- {
- for(i=front;m<count;i++,m++)
- System.out.println(" "+a[i]);
- }
- }
- }
- class Myqueue
- {
- public static void main(String arg[])
- {
- DataInputStream get=new DataInputStream(System.in);
- int ch;
- clrqueue obj=new clrqueue();
- obj.getdata();
- try
- {
- do
- {
- System.out.println(" 1.Enqueue 2.Dequeue 3.Display 4.Exit");
- System.out.println("Enter the choice");
- ch=Integer.parseInt(get.readLine());
- switch (ch)
- {
- case 1:
- obj.enqueue();
- break;
- case 2:
- obj.dequeue();
- break;
- case 3:
- obj.display();
- break;
- }
- }
- while(ch!=4);
- }
- catch(Exception e)
- {
- System.out.println(e.getMessage());
- }
- }
- }
Add Comment
Please, Sign In to add comment