Skip to content

Commit db53792

Browse files
Add files via upload
1 parent ebea270 commit db53792

File tree

26 files changed

+1963
-0
lines changed

26 files changed

+1963
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
import java.util.*;
2+
3+
public class Queue_Array {
4+
5+
public static void main(String[] args) {
6+
Scanner sc = new Scanner(System.in);
7+
System.out.println("Enter Size of Queue.");
8+
int size = sc.nextInt();
9+
Queue q = new Queue(size);
10+
11+
boolean flag = true;
12+
int val = 0;
13+
while (flag) {
14+
15+
System.out.println("1. Enqueue()");
16+
System.out.println("2. Dequeue()");
17+
System.out.println("3. Current Size of Queue");
18+
System.out.println("4. Peek()");
19+
System.out.println("5. View queue");
20+
System.out.println("6. Exit");
21+
22+
System.out.println("Enter Choice");
23+
int choice = sc.nextInt();
24+
switch (choice) {
25+
26+
case 1:
27+
System.out.println("Enter value");
28+
val = sc.nextInt();
29+
q.enqueue(val);
30+
break;
31+
32+
case 2:
33+
System.out.println(q.dequeue());
34+
break;
35+
36+
case 3:
37+
System.out.println(q.count());
38+
break;
39+
40+
case 4:
41+
System.out.println(q.peek());
42+
break;
43+
44+
case 5:
45+
q.viewQ();
46+
break;
47+
48+
case 6:
49+
flag = false;
50+
break;
51+
52+
default:
53+
System.out.println("Invalid Choice");
54+
}// switch
55+
System.out.println();
56+
} // while
57+
58+
}
59+
60+
}
61+
62+
class Queue {
63+
private int front;
64+
private int rear;
65+
private int n;
66+
private int arr[];
67+
68+
public Queue(int size) {
69+
this.n = size;
70+
arr = new int[n];
71+
front = -1;
72+
rear = -1;
73+
}
74+
75+
public boolean isEmpty() {
76+
return (rear == -1 && front == -1);
77+
}
78+
79+
public boolean isFull() {
80+
return (rear == n - 1);
81+
}
82+
83+
public int count() {
84+
return rear - front + 1;
85+
}
86+
87+
public void enqueue(int val) {
88+
if (isFull()) {
89+
System.out.println("Queue is Full");
90+
return;
91+
}
92+
else if (front == -1) {
93+
front = rear = 0;
94+
}
95+
else {
96+
rear++;
97+
}
98+
arr[rear] = val;
99+
}
100+
101+
public int dequeue() {
102+
int x = 0;
103+
if (isEmpty()) {
104+
System.out.println("Queue is Empty");
105+
return -999;
106+
} else if (rear == front) {
107+
x = arr[front];
108+
front = rear = -1;
109+
return x;
110+
} else {
111+
x = arr[front];
112+
front++;
113+
return x;
114+
115+
}
116+
}
117+
118+
public void viewQ() {
119+
if (isEmpty()) {
120+
System.out.println("Queue is empty");
121+
return;
122+
}
123+
for (int i = front; i <= rear; i++) {
124+
System.out.print(arr[i] + " ");
125+
}
126+
}
127+
128+
public int peek() {
129+
if (isEmpty()) {
130+
System.out.println("Queue is empty");
131+
return -999;
132+
}
133+
return arr[front];
134+
}
135+
136+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
## **Definition:**
2+
3+
Queue is a linear data structure which operates in a **First IN First OUT or Last IN Last OUT.** Queue is an abstract data type with a bounded (predefined) capacity. It is a simple data structure that allows adding and removing elements in a particular order.
4+
**The order is FIFO(First IN First OUT) or LILO(Last In Last Out).**
5+
6+
**It is named queue as it behaves like a real-world queue for examples –**
7+
8+
- queue(line) of cars in a single lane,
9+
- queue of people waiting at food counter etc.
10+
11+
## **Standard Queue Operations:**
12+
13+
- Enqueue() – Add item to the queue from the REAR.
14+
- Dequeue() – Remove item from the queue from the FRONT.
15+
- isFull() – Check if queue is full or not.
16+
- isEmpty() – Check if queue empty or not.
17+
- count() – Get number of items in the queue.
18+
- peek() – return front elemenet in the queue(line).
19+
20+
<h1><img src="https://simplesnippets.tech/wp-content/uploads/2019/04/queue-data-structure-diagram.jpg" width=700/></h1>
21+
22+
<!-- <br> -->
23+
24+
<h3> <b>Step 1:</b> <a href="https://youtu.be/fbonDkYsKj0">Watch this introductory video on apna college</a>
25+
26+
<h3><b>Step 2:</b> Visualize pseudocode</h3>
27+
28+
<h3><img src="./pseudocode.png"/></h3>
29+
30+
### **Step 3:** [See my code here in java](./Queue_Array.java)
31+
32+
<h1 align="Center">Thank You</h1>
255 KB
Loading
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include <stdio.h>
2+
#include <malloc.h>
3+
struct Queue
4+
{
5+
int val;
6+
struct Queue *next;
7+
};
8+
typedef struct Queue N;
9+
N *front = NULL, *rear = NULL, *ptr;
10+
11+
void Insert()
12+
{
13+
ptr = (N *)malloc(sizeof(N));
14+
printf("Enter value : ");
15+
scanf("%d",&ptr->val);
16+
ptr->next = NULL;
17+
if(front == NULL)
18+
{
19+
front = ptr;
20+
rear = ptr;
21+
front->next = NULL;
22+
rear->next = NULL;
23+
}
24+
else
25+
{
26+
rear->next = ptr;
27+
rear = ptr;
28+
rear->next = NULL;
29+
}
30+
}
31+
32+
void Delete()
33+
{
34+
if(front == NULL)
35+
{
36+
printf("\nUnderflow\n");
37+
return;
38+
}
39+
ptr = front;
40+
front = front->next;
41+
free(ptr);
42+
}
43+
44+
void Display()
45+
{
46+
ptr = front;
47+
while(ptr!=rear)
48+
{
49+
printf("%d ",ptr->val);
50+
ptr = ptr->next;
51+
}
52+
}
53+
54+
int main()
55+
{
56+
int ch;
57+
do
58+
{
59+
printf("\n\tM.E.N.U.\n=======================\n");
60+
printf("\n1.....Insert.");
61+
printf("\n2.....Delete.");
62+
printf("\n3.....Display.");
63+
printf("\n4.....Exit.");
64+
printf("\nEnter your choice : ");
65+
scanf("%d", &ch);
66+
switch (ch)
67+
{
68+
case 1:
69+
Insert();
70+
break;
71+
case 2:
72+
Delete();
73+
break;
74+
case 3:
75+
Display();
76+
break;
77+
case 4:
78+
printf("\nEnd of Program\n");
79+
break;
80+
default:
81+
printf("\nInvalid Input!!\n");
82+
break;
83+
}
84+
} while (ch != 4);
85+
return 0;
86+
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import java.util.*;
2+
3+
public class Queue_LinkedList {
4+
5+
public static void main(String[] args) {
6+
Scanner sc = new Scanner(System.in);
7+
Queue_LL q = new Queue_LL();
8+
boolean flag = true;
9+
int val = 0;
10+
while (flag) {
11+
12+
System.out.println("1. Enqueue()");
13+
System.out.println("2. Dequeue()");
14+
System.out.println("3. Current Size of Queue");
15+
System.out.println("4. Peek()");
16+
System.out.println("5. View Queue");
17+
System.out.println("6. Exit");
18+
19+
System.out.println("Enter Choice");
20+
int choice = sc.nextInt();
21+
switch (choice) {
22+
23+
case 1:
24+
System.out.println("Enter value");
25+
val = sc.nextInt();
26+
q.enqueue(val);
27+
break;
28+
29+
case 2:
30+
System.out.println(q.dequeue());
31+
break;
32+
33+
case 3:
34+
System.out.println(q.count());
35+
break;
36+
37+
case 4:
38+
System.out.println(q.peek());
39+
break;
40+
41+
case 5:
42+
q.viewQ();
43+
break;
44+
45+
case 6:
46+
flag = false;
47+
break;
48+
49+
default:
50+
System.out.println("invalid choice");
51+
}// switch
52+
System.out.println();
53+
} // while
54+
55+
}
56+
57+
}
58+
59+
class Queue_LL {
60+
61+
class Node {
62+
private int data;
63+
private Node next;
64+
65+
public Node(int data) {
66+
this.data = data;
67+
next = null;
68+
}
69+
}
70+
71+
private Node front;
72+
private Node rear;
73+
private int size;
74+
75+
public Queue_LL() {
76+
front = null;
77+
rear = null;
78+
size = 0;
79+
}
80+
81+
public boolean isEmpty() {
82+
return (front == null && rear == null);
83+
}
84+
85+
public int count() {
86+
return size;
87+
}
88+
89+
public void enqueue(int val) {
90+
Node n = new Node(val);
91+
if (isEmpty()) {
92+
front = rear = n;
93+
} else {
94+
rear.next = n;
95+
rear = n;
96+
}
97+
size++;
98+
}
99+
100+
public int dequeue() {
101+
if (isEmpty()) {
102+
System.out.println("Queue is empty");
103+
return -999;
104+
} else if (front == rear) {
105+
Node delD = front;
106+
front = rear = null;
107+
size--;
108+
return delD.data;
109+
} else {
110+
Node delD = front;
111+
front = front.next;
112+
delD.next = null;
113+
size--;
114+
return delD.data;
115+
}
116+
}
117+
118+
public int peek() {
119+
if (isEmpty()) {
120+
System.out.println("Queue is Empty");
121+
return -999;
122+
}
123+
return front.data;
124+
}
125+
126+
public void viewQ() {
127+
if (isEmpty()) {
128+
System.out.println("Queue is Empty");
129+
return;
130+
}
131+
Node t = front;
132+
while (t != null) {
133+
System.out.print(t.data + " ");
134+
t = t.next;
135+
}
136+
}
137+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### **Step 1:** [Visualize Queue using linklist here by Apna College in 6min only](https://youtu.be/Yi8Im4_eSQI)
2+
3+
### **Step 2:** [Recall your Linklist knowledge and try to understand my code here.](./Queue_LinkedList.java)

0 commit comments

Comments
 (0)