-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsinglyqueue_example_basic
More file actions
106 lines (97 loc) · 2.37 KB
/
singlyqueue_example_basic
File metadata and controls
106 lines (97 loc) · 2.37 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
import javax.swing.*;
import java.util.Scanner;
public class singlyqueue_example_basic
{
int arr[];
int front,rear;
singlyqueue_example_basic()
{
arr= new int [5];
front=-1;
rear=-1;
}
void insert()
{
if (rear== arr.length-1)
{
System.out.println("queue full");
}
else
{
System.out.println("enter data");
Scanner sc = new Scanner(System.in);
int data = sc.nextInt();
if (front == -1)
{
front=0;
}
rear = rear+1;
arr[rear] = data;
System.out.println("data inserted");
}
}
void delete()
{
if (front==-1)
{
System.out.println("queue empty");
}
else
{
System.out.println("deleted"+ arr[front]);
if (front==rear)
{
front=-1;
rear=-1;
}
else if(front<rear)
{
front=front+1;
}
}
}
void traverse()
{
if (front == -1 || rear == -1)
{
System.out.println("queue empty");
}
else
{
for(int i = front ; i <= rear ; i++)
{
System.out.print(" "+ arr[i]);
}
}
}
public static void main(String[] args) {
singlyqueue_example_basic obj = new singlyqueue_example_basic();
while (true)
{
System.out.println("\npress 1 for insert");
System.out.println("press 2 for delete");
System.out.println("press 3 for traverse");
System.out.println("press 4 for exit");
System.out.println("enter ur choice");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
switch (choice)
{
case 1:
obj.insert();
break;
case 2:
obj.delete();
break;
case 3:
obj.traverse();
break;
case 4:
System.exit(0);
break;
default:
System.out.println("incorrect option");
}
}
}
}