Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

Queue Implementation Using Array

Implementation of queue via array

Uploaded by

Amita Barwan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Queue Implementation Using Array

Implementation of queue via array

Uploaded by

Amita Barwan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Main1.

java 12/6/2020 1:26 PM

1 /*
2 Queue implementation using Array.
3 Intialize size of array, front,
4 rear and queue array.
5 */
6
7 class Queue
8 {
9 int s = 5;
10 int f = -1;
11 int r = -1;
12 int queue[] = new int[s];
13
14 /*
15 Enqueue method is used to insert an
16 element in a queue.
17 */
18
19 void enqueue(int val)
20 {
21 if (r == s-1)
22 {
23 System.out.println("Queue overflow");
24 }
25 else
26 {
27 if (f==-1)
28 {
29 f=0;
30 }
31 r++;

Page 1 of 4
Main1.java 12/6/2020 1:26 PM

32 queue[r] = val;
33 System.out.println(val + " enter into queue");
34 }
35 }
36
37 /*
38 Dequeue method is used to remove an
39 element in a queue.
40 */
41
42 void dequeue()
43 {
44 if (r==-1 || f>r)
45 {
46 System.out.println("Queue underflow");
47 }
48 else
49 {
50 int val = queue[f];
51 System.out.println(val+" delete from queue");
52 f++;
53 }
54 }
55
56 /*
57 Peek method is used to show the front
58 element of a queue if queue is not
59 empty.
60 */
61
62 void peek()

Page 2 of 4
Main1.java 12/6/2020 1:26 PM

63 {
64 if (r==-1 || f>r)
65 {
66 System.out.println("Stack underflow");
67 }
68 else
69 {
70 int val = queue[f];
71 System.out.println("Peek element is "+val);
72 }
73 }
74
75 /*
76 Display method is used to show the all
77 element of a queue.
78 */
79
80 void display()
81 {
82 if (r==-1 || f>r)
83 {
84 System.out.println("Queue underflow");
85 }
86 else
87 {
88 for (int i = f; i<=r; i++)
89 {
90 System.out.println("|"+queue[i]+"|");
91 }
92 }
93 }

Page 3 of 4
Main1.java 12/6/2020 1:26 PM

94 }
95
96 // Main class
97 class Main1
98 {
99 public static void main(String[] args)
100 {
101 Queue qu = new Queue();
102 qu.enqueue(10);
103 qu.enqueue(20);
104 qu.enqueue(30);
105 qu.enqueue(40);
106 qu.display();
107 qu.dequeue();
108 qu.peek();
109 }
110 }
111

Page 4 of 4

You might also like