-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.java
More file actions
111 lines (98 loc) · 2.11 KB
/
Queue.java
File metadata and controls
111 lines (98 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
* This file is written to implement queue data structure.
*/
/**
*
* @author ameya
*/
import java.util.*;
//structure of node of Queue
class MyNode{
int data;
MyNode link;
public MyNode(){
data=0;
link=null;}
public MyNode(int num,MyNode next)
{
data=num;
link=null;
}
}
//This class will contain all the function for Queue...
class queue_functions{
MyNode start=new MyNode();
MyNode end=new MyNode();
int size=0;
public queue_functions()
{
start=null;
end=null;
}
//method for adding data at the end of queue
void enqueue(int num)
{
MyNode mnode = new MyNode(num,null);
if(start==null)
{
start=mnode;
end=start;
}
else{
end.link=mnode;
end=mnode;
}
size++;
}
//method for deleting the top node.....
void dequeue()
{
if(start==null)
System.out.println("The Queue is empty");
else{
MyNode ptr = new MyNode();
ptr=start;
start=ptr.link;
}
size--;
}
//method for displaying the queue....
void display()
{
if(start==null)
System.out.println("The Queue is empty");
else{
MyNode ptr = new MyNode();
ptr=start;
System.out.println("The Queue has following data:");
//System.out.print(" | ");
String s="----";
for(int i=0;i<size;i++)
s=s+"----";
System.out.println(s);
while(ptr!=null)
{
System.out.print(" "+ptr.data);System.out.print(" |");
ptr=ptr.link;
}
System.out.println("");
System.out.println(s);
}
}
}
public class Queue {
public static void main(String[] args) {
queue_functions obj = new queue_functions();
obj.enqueue(32);
obj.enqueue(23);
obj.enqueue(76);
obj.enqueue(3);
obj.enqueue(6);
obj.enqueue(12);
obj.dequeue();
obj.display();
}
}