-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathUnOrderLinKList.java
More file actions
85 lines (68 loc) · 1.5 KB
/
UnOrderLinKList.java
File metadata and controls
85 lines (68 loc) · 1.5 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
package com.bridgelabz.utility;
/*
* created by: Bridge Labz
* Date 12/05/2016
*
* Perpose: Un Order Link List implementation.
**/
import com.bridgelabz.model.Node;
public class UnOrderLinKList<E>{
private Node head;
private Node tail;
private int size;
//add elemnts to the start of the link List.
public void add(E element){
Node temp = new Node(element, head, null);
if(head!=null){
//Add first element as temp
head.left=temp;
}
head=temp;
if(tail==null){
//if elemnts enter is first element then tail is also pointing to the first elemnts.
tail=temp;
}
size++;
}
//Add element to the end of the the list
public void addLast(E data){
Node temp=new Node(data,null,tail);
if(tail != null)
tail.right=temp;
tail=temp;
if(head == null)
{
head = temp;
}
size++;
}
//Remove the first elements from the list.
public E remove()throws Exception{
if (size == 0)
throw new Exception();
Node temp=head;
head=head.right;
head.left=null;
size--;
return (E)temp.getData();
}
//Remove the last elemnts from the list.
public E removeLast()throws Exception{
if(size==0)
throw new Exception();
Node temp=tail;
tail=tail.left;
tail.right=null;
size--;
return (E)temp.getData();
}
public void print(){
Node tmp = head;
while(tmp != null){
System.out.println(tmp.getData());
tmp = tmp.right;
}
}
public int size() { return size; }
public boolean isEmpty(){ return size == 0; }
}