-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queue.java
56 lines (54 loc) · 1.06 KB
/
Queue.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
class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
class LinkedList {
Node head;
public LinkedList() {
this.head = null;
}
public void Append(int data) {
Node newnode = new Node(data);
if(head == null)
head = newnode;
else
{
Node temp;
for(temp = this.head; temp.next != null; temp = temp.next);
temp.next = newnode;
}
}
public void print(){
Node n;
for(n = this.head; n != null; n = n.next){
System.out.println(n.data);
}
}
public void delete() {
Node temp = head;
head = head.next;
temp.next = null;
}
}
public class Queue {
public static void main(String[] args) {
LinkedList l = new LinkedList();
l.Append(34);
l.Append(12);
l.Append(56);
l.Append(23);
l.Append(33);
System.out.println("After insertion ");
l.print();
System.out.println("After first deletion");
l.delete();
l.print();
System.out.println("After second deletion");
l.delete();
l.print();
}
}