-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoublyEndedQueueUsingLinkedList.java
More file actions
129 lines (129 loc) · 3.37 KB
/
DoublyEndedQueueUsingLinkedList.java
File metadata and controls
129 lines (129 loc) · 3.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* 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.
*/
package chapter05;
import java.util.Scanner;
/**
*
* @author harsh
*/
class node{
public int data;
public node next;
node(){
data=0;
next=null;
}
}
class Queue{
Scanner scan = new Scanner(System.in);
public int x;
node front,rear;
Queue(){
front=rear=null;
}
node newNode(int n){
node temp = new node();
temp.data=n;
temp.next=null;
return temp;
}
void pushEnd(){
System.out.println("Enter element");
x = scan.nextInt();
node temp=newNode(x);
if(front==null){
front = rear = temp;
System.out.println("First element pushed");
}else{
rear.next=temp;
rear=temp;
System.out.println("Element pushed at end");
}
}
void pushFront(){
System.out.println("Enter a element");
x = scan.nextInt();
node temp=newNode(x);
if(front == null){
front = rear= temp;
System.out.println("First element pushed");
}
else{
temp.next=front;
front= temp;
System.out.println("Element pushed at front");
}
}
void popFront(){
if(front==null){
System.out.println("List is empty");
}else{
front=front.next;
System.out.println("Element Poped from front");
}
}
void popEnd(){
if(front==null){
System.out.println("List is empty");
}else{
node prev=front;
while(prev.next!=rear){
prev=prev.next;
}
prev.next=null;
rear=prev;
System.out.println("Element Poped");
}
}
void display(){
if(front==null){
System.out.println("List is empty");
}
else{
node temp=front;
System.out.println("------------------------------");
while(temp!=null){
System.out.print(temp.data+"\t");
temp=temp.next;
}
System.out.println("\n------------------------------");
System.out.println("Queue Displayed");
}
}
}
public class DoublyEndedQueueUsingLinkedList {
public static void main(String[] args){
Queue abc=new Queue();
int choice;
do
{
System.out.println(" 1.Push element from front\t2.Push element from end \t 2.Pop element from front\t3.Pop element from end\t 5.Display \t 6.EXIT\nEnter choice:");
choice = Integer.parseInt(new Scanner(System.in).nextLine());
switch(choice)
{
case 1:
abc.pushFront();
break;
case 2:
abc.pushEnd();
break;
case 3:
abc.popFront();
break;
case 4:
abc.popEnd();
break;
case 5:
abc.display();
default:
System.out.println("INVALID COMMAND");
break;
case 6:
break;
}
}while(choice!=6);
}
}