Skip to content

Commit eb7193d

Browse files
authored
Merge pull request #175 from Arkadyuti30/master
Stack and Queue added to new folder Data Structures
2 parents b0ed8b5 + 9e1ed06 commit eb7193d

File tree

4 files changed

+262
-0
lines changed

4 files changed

+262
-0
lines changed
Binary file not shown.
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package Data_Structures;
2+
import java.util.*;
3+
class Queue
4+
{
5+
private int q[], front, rear, size;
6+
7+
8+
/***** Constructor to initialize queue *****/
9+
queue(int n)
10+
{ //Begin constructor
11+
size = n;
12+
front = -1;
13+
rear = -1;
14+
q = new int[size];
15+
16+
for(int i = 0; i<size; i++)
17+
q[i] = 0;
18+
} //end constructor
19+
20+
21+
22+
/***** Fucntion to insert an item to queue *****/
23+
private void insert(int item)
24+
{ //Begin insert()
25+
if(rear == (size-1)) //Checks if rear pointer has reached the last limit (Queue is completely full or not)
26+
{
27+
System.out.println("Queue Overflow");
28+
}
29+
30+
elseif(front == -1 && rear == -1) //Checks if queue is empty to insert the very first element
31+
{
32+
front = 0; rear = 0;
33+
q[rear] = item;
34+
}
35+
36+
else
37+
{
38+
rear+=1;
39+
q[rear] = item;
40+
}
41+
42+
} //End insert()
43+
44+
45+
46+
47+
/***** Fucntion to delete an item from queue *****/
48+
private void delete()
49+
{ //Begin delete()
50+
51+
52+
if(front == -1 && rear == -1) //Checks if queue is empty
53+
{
54+
System.out.println("Queue Underflow");
55+
}
56+
57+
58+
59+
60+
elseif(front == rear) //Checks if only one element is present
61+
{
62+
System.out.println("Deleted element:\t"+q[front]);
63+
front = -1; rear = -1;
64+
}
65+
66+
else
67+
{
68+
System.out.println("Deleted element:\t"+q[front]);
69+
front += 1;
70+
}
71+
72+
} //End delete()
73+
74+
75+
76+
/***** Function to display the elements of the queue *****/
77+
private void display()
78+
{ //Begin display()
79+
if(front == - 1 && rear == -1)
80+
{
81+
System.out.println("Queue is EMPTY");
82+
}
83+
84+
else
85+
{
86+
System.out.print("Elements of the queue:\nfront ---> ");
87+
88+
for(int i = front; i <= rear; i++)
89+
System.out.print(q[i]+"\t");
90+
91+
System.out.println("<--- rear");
92+
93+
}
94+
} //End display()
95+
96+
97+
98+
99+
public static void main(String args[])
100+
{ //Begin main()
101+
102+
Scanner sc = new Scanner(System.in);
103+
System.out.println("Enter the size of the queue");
104+
int n = sc.nextInt();
105+
106+
queue q1 = new queue(n);
107+
108+
int choice;
109+
do
110+
{ //Begin do
111+
System.out.println("\nEnter:\n1. Insert\n2. Delete\n3. Display");
112+
choice = sc.nextInt();
113+
switch(choice)
114+
{ //Begin switch
115+
case 1: System.out.println("\nEnter an element to insert");
116+
int element = sc.nextInt();
117+
q1.insert(element);
118+
break;
119+
120+
case 2: q1.delete();
121+
break;
122+
123+
case 3: q1.display();
124+
break;
125+
126+
default: System.out.println("Wrong choice. Exitting.");
127+
break;
128+
} //End switch
129+
} //End do
130+
while(choice >= 1 && choice <= 3);
131+
} //End main()
132+
} //End class
Binary file not shown.
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package Data_Structures
2+
import java.util.*;
3+
class stack
4+
{ //Begin class
5+
6+
int top = -1, size, s[];
7+
8+
9+
/*****Constructor to initialise the stack *****/
10+
stack(int n)
11+
{ //Begin constructor
12+
size = n;
13+
14+
s = new int[size];
15+
for(int i = 0; i < size; i++)
16+
{ //Begin for
17+
s[i] = 0;
18+
} //End for
19+
} //End constructor
20+
21+
22+
23+
/***** Fucntion to insert/push item to stack *****/
24+
private void push(int item)
25+
{ //Begin push()
26+
27+
if(top == (size-1)) //Checks if stack is full
28+
{ //Begin if
29+
System.out.println("Stack overflow");
30+
} //End if
31+
32+
else //Element is entered to the stack when there is place
33+
{ //Begin else
34+
top++;
35+
s[top] = item;
36+
} //End else
37+
38+
} //End push()
39+
40+
41+
42+
/***** Function to delete/pop an item (topmost element) from stack *****/
43+
private void pop()
44+
{ //Begin pop()
45+
46+
if(top == -1) //Checks if stack is empty
47+
{ //Begin if
48+
System.out.println("Stack underflow");
49+
} //End if
50+
51+
else
52+
{ //Begin else
53+
System.out.println("Item deleted is:\t"+s[top]);
54+
top--;
55+
} //End else
56+
} //End pop()
57+
58+
59+
/***** Function to peep (show the topmost element in stack) *****/
60+
private void peep()
61+
{ //Begin peep()
62+
if(top == -1)
63+
System.out.println("No element to peep");
64+
65+
else
66+
System.out.println("Peep: top ---> "+s[top]);
67+
} //End peep()
68+
69+
70+
71+
/***** Function to display the elements of stack *****/
72+
private void display()
73+
{ //Begin display()
74+
75+
if(top == -1)
76+
{ //Begin if
77+
System.out.println("No elements in stack");
78+
} //End if
79+
80+
else
81+
{ //Begin else
82+
83+
System.out.print("Elements of the stack: top---> ");
84+
for(int i=top; i>=0; i--)
85+
{
86+
System.out.print(s[i]+"\t");
87+
}
88+
System.out.println();
89+
} //End else
90+
} //End display()
91+
92+
93+
/***** main fucntion *****/
94+
public static void main(String args[])
95+
{ //Begin main()
96+
97+
Scanner sc = new Scanner(System.in);
98+
System.out.println("Enter the size of the stack");
99+
int n = sc.nextInt();
100+
101+
stack st = new stack(n);
102+
103+
int choice;
104+
do
105+
{ //Begin do
106+
System.out.println("\nEnter:\n1. Push\n2. Pop\n3. Peep\n4. Display");
107+
choice = sc.nextInt();
108+
switch(choice)
109+
{ //Begin switch
110+
case 1: System.out.println("\nEnter an element to push");
111+
int element = sc.nextInt();
112+
st.push(element);
113+
break;
114+
115+
case 2: st.pop();
116+
break;
117+
118+
case 3: st.peep();
119+
break;
120+
121+
case 4: st.display();
122+
break;
123+
124+
default: System.out.println("Wrong choice. Exitting!");
125+
break;
126+
} //End switch
127+
} //End do
128+
while(choice >= 1 && choice <= 4);
129+
} //End main()
130+
} //End class

0 commit comments

Comments
 (0)