Professional Documents
Culture Documents
<!DOCTYPE html>
<html>
<head>
<script>
class Node {
constructor(element) {
this.element = element;
this.next = null
}
}
class LinkedList {
constructor() {
this.head = null;
this.size = 0;
}
add(element) {
var node = new Node(element);
var current;
if (this.head == null)
this.head = node;
else {
current = this.head;
while (current.next) {
current = current.next;
}
current.next = node;
}
this.size++;
}
insertAt(element, index) {
if (index < 0 || index > this.size)
return console.log("Please enter a valid index. ");
else {
var node = new Node(element);
var curr, prev;
curr = this.head;
if (index == 0) {
node.next = this.head;
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala
this.head = node;
} else
{
curr = this.head;
var it = 0;
node.next = curr;
prev.next = node;
}
this.size++;
}
}
removeFrom(index) {
if (index < 0 || index >= this.size)
return console.log("Please Enter a valid index. ");
else {
var curr, prev, it = 0;
curr = this.head;
prev = curr;
if (index === 0) {
this.head = curr.next;
} else {
prev.next = curr.next;
}
this.size--;
return curr.element;
}
}
removeElement(element) {
var current = this.head;
var prev = null;
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala
indexOf(element) {
var count = 0;
var current = this.head;
return -1;
}
isEmpty() {
return this.size == 0;
}
size_of_list() {
console.log(this.size);
}
printList() {
var curr = this.head;
var str = "";
while (curr) {
str += curr.element + " ";
curr = curr.next;
}
console.log(str);
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala
}
}
ll.add(10);
ll.printList();
console.log(ll.size_of_list());
ll.add(2);
ll.add(4);
ll.add(6);
ll.add(8);
ll.add(20)
ll.printList();
ll.insertAt(15, 2);
ll.printList();
</html>
Output:
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala
}
class circularlinklist
{
constructor()
{
this.head = null;
}
insert (value)
{
var node = new LinkNode(value, this.head);
if(this.head == null)
{
this.head = node;
node.next = this.head;
}
else
{
var temp = this.head;
while(temp.next != this.head)
{
temp = temp.next;
}
temp.next = node;
}
}
display()
{
if (this.head == null)
{
console.log("Empty linked list ");
}
else{
console.log("Linked List elements: ");
var temp = this.head;
while(temp != null)
{
console.log(" "+temp.data);
temp= temp.next;
if(temp == this.head)
{
return;
}
}
}
}
reverse()
{
if (this.head == null)
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala
{
console.log("Empty linked list ");
return;
}
var temp= this.head;
var back = null;
var curr = null;
while (temp != null && temp.next != this.head)
{
curr = temp;
temp = temp.next;
curr.next = back;
back = curr;
}
this.head.next = temp;
if(back != null)
{
temp.next = back;
}
this.head = temp;
}
}
var cll = new circularlinklist();
cll.insert(15);
cll.insert(25);
cll.insert(35);
cll.insert(45);
cll.insert(55);
console.log("Original Linked List: ");
cll.display();
Output:
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala
3. Program to delete a new node from the index of the singly linked list.
<!DOCTYPE html>
<html>
<head>
<script>
class Node {
constructor (element) {
this.element = element;
this.next = null
}
}
class DelSinglyLL {
constructor () {
this.head = null;
this.size = 0;
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala
}
add(element) {
var node = new Node(element);
var current;
if (this.head == null)
this.head = node;
else {
current = this.head;
while (current.next) {
current = current.next;
}
current.next = node;
}
this.size++;
}
removeFrom(index) {
if (index < 0 || index >= this.size)
return console.log("Please Enter a valid index. ");
else {
var curr, prev, it = 0;
curr = this.head;
prev = curr;
if (index === 0) {
this.head = curr.next;
} else {
prev.next = curr.next;
}
this.size--;
return curr.element;
}
}
removeElement(element) {
var current = this.head;
var prev = null;
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala
printList() {
var curr = this.head;
var str = "";
while (curr) {
str += curr.element + " ";
curr = curr.next;
}
console.log(str);
}
}
</script>
</head>
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala
</html>
Output:
<body>
<script>
function circularLinkedList() {
let Node = function (element) {
this.element = element;
this.next = null;
}
let length = 0;
let head = null;
}
node.next = head;
length++;
}
if (index === 0) {
if (head === null) {
head = node;
node.next = head;
} else {
node.next = current;
current = this.getElementAt(length);
head = node;
current.next = head;
}
} else {
const previous = this.getElementAt(index - 1);
node.next = previous.next;
previous.next = node;
}
length++;
return true;
}
return false;
}
if (index === 0) {
if (length === 1) {
head = undefined;
} else {
const removed = head;
current = this.getElementAt(length - 1);
head = head.next;
current.next = head;
current = removed;
}
} else {
const previous = this.getElementAt(index - 1);
current = previous.next;
previous.next = current.next;
}
length--;
return current.element;
}
return undefined;
}
while (current) {
if (elm === current.element) {
return ++index;
}
index++;
current = current.next;
}
return -1;
};
this.getHead = function () {
return head;
}
return this.removeAt(this.indexOf(elm));
};
this.deleteHead = function () {
this.removeAt(0);
}
this.toString = function () {
let current = head,
string = '';
const temp = head.element;
while (current) {
if (temp === current.next.element) {
string += current.element + (current.next ? '\n' :
'');
break;
}
return string;
};
this.toArray = function () {
let arr = [],
current = head;
const temp = head.element
while (current) {
if (temp === current.next.element) {
arr.push(current.element);
break;
}
arr.push(current.element);
current = current.next;
}
return arr;
};
this.isEmpty = function () {
return length === 0;
};
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala
this.size = function () {
return length;
}
}
</script>
</body>
</html>
Output:
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala
class Node {
constructor(elm, next = null, prev = null) {
this.element = elm;
this.next = next;
this.prev = prev;
}
}
class doublyLinkedList {
constructor() {
this.length = 0;
this.tail = null;
this.head = null;
}
previous;
if (!this.head) {
this.head = node;
this.tail = node;
} else {
node.prev = this.tail;
this.tail.next = node;
this.tail = node;
}
this.length++;
}
current.prev = node;
node.prev = previous;
}
this.length++;
return true;
} else {
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala
return false;
}
}
if (position === 0) {
this.head = current.next;
if (length === 1) {
this.tail = null;
} else {
this.head.prev = null;
}
} else if (position === this.length - 1) {
current = this.tail;
this.tail = current.prev;
this.tail.next = null;
} else {
while (index++ < position) {
previous = current;
current = current.next;
}
previous.next = current.next;
current.next.prev = previous;
}
this.length--;
return current.element;
} else {
return null;
}
}
while (current) {
if (elm === current.element) {
return ++index;
}
index++;
current = current.next;
}
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala
return -1;
};
deleteHead = function () {
this.removeAt(0);
}
deleteTail = function () {
this.removeAt(this.length - 1);
}
toString = function () {
let current = this.head,
string = '';
while (current) {
string += current.element + (current.next ? '\n' : '');
current = current.next;
}
return string;
};
toArray = function () {
let arr = [],
current = this.head;
while (current) {
arr.push(current.element);
current = current.next;
}
return arr;
};
isEmpty = function () {
return this.length === 0;
};
size = function () {
return this.length;
}
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala
getHead = function () {
return this.head;
}
getTail = function () {
return this.tail;
}
}
let dll = new doublyLinkedList();
dll.append('Harry');
dll.append('Ron');
dll.append('Hermoine');
dll.insert(1, 'Snape');
document.writeln(dll.toArray());
</script>
</body>
</html>
Output:
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala
constructor(val) {
this.data = val;
this.prev = null;
this.next = null;
}
}
function push(new_data) {
}
if (head == del) {
head = del.next;
}
if (del.next != null) {
del.next.prev = del.prev;
}
if (del.prev != null) {
del.prev.next = del.next;
}
return;
}
push(5);
push(9);
push(13);
push(21);
push(29)
document.write("Created Doubly Linked List is: ");
printlist(head);
document.write("Deleting head element: ");
deleteNode(head);
printlist(head);
Output:
<head>
<script>
function circularDoublyLL() {
let Node = function (element) {
this.element = element;
this.next = null;
this.prev = null;
}
let length = 0;
let head = null;
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala
if (!head) {
head = node;
tail = node;
} else {
node.prev = tail;
tail.next = node;
tail = node;
}
head.prev = tail;
tail.next = head;
length++;
}
if (position === 0) {
if (!head) {
head = node;
tail = node;
} else {
node.next = current;
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala
current.prev = node;
head = node;
}
} else if (position === length) {
current = tail;
current.next = node;
node.prev = current;
tail = node;
} else {
while (index++ < position) {
previous = current;
current = current.next;
}
node.next = current;
previous.next = node;
current.prev = node;
node.prev = previous;
}
head.prev = tail;
tail.next = head;
length++;
return true;
} else {
return false;
}
}
if (index === 0) {
if (length === 1) {
head = undefined;
} else {
const removed = head;
current = this.getElementAt(length - 1);
head = head.next;
current.next = head;
current = removed;
}
} else {
const previous = this.getElementAt(index - 1);
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala
current = previous.next;
previous.next = current.next;
}
if (head) {
head.prev = tail;
tail.next = head;
}
length--;
return current.element;
}
return undefined;
}
while (current) {
if (elm === current.element) {
return ++index;
}
index++;
current = current.next;
}
return -1;
};
this.getHead = function () {
return head;
}
this.getTail = function () {
return tail;
}
this.deleteHead = function () {
this.removeAt(0);
}
this.toString = function () {
let current = head,
string = '';
const temp = head.element;
while (current) {
if (temp === current.next.element) {
string += current.element + (current.next ? '\n' :
'');
break;
}
return string;
};
this.toArray = function () {
let arr = [],
current = head;
const temp = head.element
while (current) {
if (temp === current.next.element) {
arr.push(current.element);
break;
}
arr.push(current.element);
current = current.next;
}
return arr;
};
this.isEmpty = function () {
return length === 0;
};
this.size = function () {
return length;
}
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala
}
cLL.insert(3, 25);
console.log(cLL.toArray());
cLL.removeAt(2);
console.log(cLL.toArray());
</script>
</head>
</html>
Output:
<html>
<head>
<script>
var head;
class Node
{
constructor(val)
{
this.data = val;
this.prev = null;
this.next = null;
}
}
function reverse()
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala
{
var temp = null;
var current = head;
while (current != null)
{
temp = current.prev;
current.prev = current.next;
current.next = temp;
current = current.prev;
}
if (temp != null)
{
head = temp.prev;
}
}
function push(new_data)
{
var new_node = new Node(new_data);
new_node.prev = null;
new_node.next = head;
if (head != null)
{
head.prev = new_node;
}
head = new_node;
}
function printList(node)
{
while (node != null)
{
document.write(node.data + " ");
node = node.next;
}
}
push(21);
push(10);
push(33);
push(7);
FYMCA DS Practical No: 02 Date: 11/02/2022
Div: B Abdullah Shethwala
Output: