forked from coder2hacker/Explore-open-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DS_project LRU part2
120 lines (119 loc) · 3.28 KB
/
DS_project LRU part2
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
class LinkedList
{
data;
next;
constructor(data)
{
this.data=data
this.next=null
}
}
let new_data = (value, node) => {
const newone = new LinkedList(value)
if (node == null) {
return newone;
}
newone.next = node
node = newone
return node;
}
let exist_node_shift = (node, value) => {
c = node;
p = null;
while (c !== null) {
if (c.data[0] == value[0]) {
break;
}
p = c;
c = c.next;
}
if (c == null) {
return -1;
}
if (p == null) {
node.data[1] = value[1]
return 0;
}
p.next = c.next;
return 1;
}
let LRU_deletion = (node) => {
c = node
while (c.next.next != null) {
c = c.next;
}
c.next = null;
return node
}
let count = 3;
let chatbox = document.querySelector("#chatbox")
let traverse = (node) => {
let d = node
chatbox.innerHTML = ``
while (d != null) {
if(d.data[1].length>25)
{
d.data[1] = d.data[1].substring(0,25) + ".......";
}
let content = document.createElement("ul")
content.className="ks-items"
content.innerHTML = `
<li class="ks-item" id="ChatListItem">
<a href="#">
<span class="ks-avatar">
<img src="./default-user.png" width="36" height="36" id="Image"></img>
</span>
<div class="ks-body">
<div class="ks-name">
<span id="Name">${d.data[0]}</span>
<span id="Time" class="ks-datetime">${new Date().getHours() + ":" + new Date().getMinutes()}</span>
</div>
<div class="ks-message" id="Message">${d.data[1]}</div>
</div>
</a>
</li>`
chatbox.appendChild(content)
d = d.next
}
}
let Chatnode = new LinkedList(["Priyam", "Hi!Whassup?"]);
Chatnode = new_data(["Aman", "Bhai!"], Chatnode)
Chatnode = new_data(["Anupam", "Bhai Pranam!"], Chatnode)
traverse(Chatnode)
document.querySelector("#send").addEventListener("click", () => {
let name = document.getElementById("contactlist").value
let message = document.getElementById("message").value
if (message) {
if (count == 5) {
let indicator = exist_node_shift(Chatnode, [name, message]);
if (indicator == -1) {
Chatnode = LRU_deletion(Chatnode)
console.log(Chatnode)
Chatnode = new_data([name, message], Chatnode)
traverse(Chatnode)
}
else if (indicator == 1) {
Chatnode = new_data([name, message], Chatnode)
traverse(Chatnode)
}
else {
traverse(Chatnode)
}
}
else {
let indicator = exist_node_shift(Chatnode, [name, message]);
if (indicator == -1) {
count += 1;
Chatnode = new_data([name, message], Chatnode)
traverse(Chatnode)
}
else if (indicator == 1) {
Chatnode = new_data([name, message], Chatnode)
traverse(Chatnode)
}
else {
traverse(Chatnode)
}
}
}
})