Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

measifalam 🍉 #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
measifalam wants to merge 1 commit into sudheerj:master from measifalam:master
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 145 additions & 0 deletions src/javascript/dsa-functional Approach/linkedList.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
function createNode(value) {
return {
value,
next: null
}
}

function createLinkedList() {
return {
head: null,
tail: null,
length: 0,

push(value) {
const node = createNode(value)

if (this.head === null) {
this.head = node
this.tail = node
this.length++
return node
}

this.tail.next = node
this.tail = node
this.length++

return node
},

pop() {
if (this.isEmpty()) {
return null
}

const node = this.tail

if (this.head === this.tail) {
this.head = null
this.tail = null
this.length--
return node
}

let current = this.head
let penultimate
while (current) {
if (current.next === this.tail) {
penultimate = current
break
}

current = current.next
}

penultimate.next = null
this.tail = penultimate
this.length--

return node
},

get(index) {
if (index < 0 || index > this.length - 1) {
return null
}

if (index === 0) {
return this.head
}

let current = this.head
let i = 0
while (i < index) {
i++
current = current.next
}

return current
},

delete(index) {
if (index < 0 || index > this.length - 1) {
return null
}

if (index === 0) {
const deleted = this.head

this.head = this.head.next
this.length--

return deleted
}

let current = this.head
let previous
let i = 0

while (i < index) {
i++
previous = current
current = current.next
}

const deleted = current
previous.next = current.next

if (previous.next === null) {
this.tail = previous
}

this.length--

return deleted
},

isEmpty() {
return this.length === 0
},

print() {
const values = []
let current = this.head

while (current) {
values.push(current.value)
current = current.next
}

return values.join(' => ')
}
}
}

const list = createLinkedList()
const values = ['a', 'b', 'c', 'd', 'e']
const nodes = values.map(val => list.push(val))

list.pop()
console.log(list.delete(1))
console.log(list.print())

exports.createNode = createNode
exports.createLinkedList = createLinkedList

AltStyle によって変換されたページ (->オリジナル) /