-
Notifications
You must be signed in to change notification settings - Fork 0
/
lec79_linkedlist_implementation.java
71 lines (66 loc) · 1.71 KB
/
lec79_linkedlist_implementation.java
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
package shreya.java;
public class lec79_linkedlist_implementation {
public static class Node{ //user defined data type
int val;
Node next;
Node (int val){
this.val = val;
}
}
public static class queueLL{
Node head = null;
Node tail = null;
int size =0;
public void add(int x){
Node temp = new Node(x);
if(size==0){
head = tail = temp;
}
else{
tail.next = temp;
tail = temp;
}
size++;
}
public int peek(){
if(size==0){
System.out.println("queue is empty");
return -1;
}
return head.val;
}
public int remove (){
if (size == 0) {
System.out.println("Queue is empty");
return -1;
}
int x = head.val;
head = head.next;
size--;
return x;
}
public void display(){
Node temp = head;
while(temp!= null){
System.out.print(temp.val+" ");
temp = temp.next;
}
System.out.println();
}
public boolean isEmpty(){
if(size==0)return true;
else return false;
}
}
public static void main(String[] args) {
queueLL q1 = new queueLL();
q1.add(3);
q1.add(4);
q1.add(7);
q1.add(10);
q1.display();
System.out.println(q1.peek());
q1.remove();
q1.display();
}
}