0% found this document useful (0 votes)
15K views39 pages

DS Assignment 2

The document contains 5 programming questions related to data structures and algorithms. It includes programs to create and display singly linked lists, circular linked lists, and doubly linked lists. Methods like insertion, deletion, traversal etc are implemented in the linked list programs.

Uploaded by

Atharv Gaikwad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15K views39 pages

DS Assignment 2

The document contains 5 programming questions related to data structures and algorithms. It includes programs to create and display singly linked lists, circular linked lists, and doubly linked lists. Methods like insertion, deletion, traversal etc are implemented in the linked list programs.

Uploaded by

Atharv Gaikwad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

Assignment 2

Name: Bhavna Narwade


Div: B
Roll No: 115
Subject – Data Structure and Algorithms
Q 1. Program to create and display a singly linked list
<!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;

Output :
Q 2. Program to create a singly linked list of n nodes and display it in reverse order.
<!DOCTYPE html>
<html>
<body>
<script>
class LinkNode
{
constructor(data,first)
{
this.data = data;
this.next = first;
}
}
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)
{
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();
console.log("\n After Reversing Linked List: ");
cll.reverse();
cll.display();
</script>
</body>
</html>

Output:
Q 3. Program to delete a new node from the singly linked list
Program:
<!DOCTYPE html>
<html>

<head>
<script>

class Node {
constructor (element) {
this.element = element;
this.next = null
}
}
class DelSinglyLL {
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++;
}

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 {

while (it < index) {


it++;
prev = curr;
curr = curr.next;
}

prev.next = curr.next;
}
this.size--;

return curr.element;
}
}

removeElement(element) {
var current = this.head;
var prev = null;

while (current != null) {

if (current.element === element) {


if (prev == null) {
this.head = current.next;
} else {
prev.next = current.next;
}
this.size--;
return current.element;
}
prev = current;
current = current.next;
}
return -1;
}
printList() {
var curr = this.head;
var str = "";
while (curr) {
str += curr.element + " ";
curr = curr.next;
}
console.log(str);
}

var ll = new DelSinglyLL();


ll.add(2);
ll.add(4);
ll.add(6);
ll.add(8);
ll.add(10);
ll.add(20);
ll.printList();
console.log("Remove element: " + ll.removeElement(10));
ll.printList();
console.log("Removing an element at index position 3: ")
ll.removeFrom(3);
ll.printList();

</script>
</head>
</html>
Output:
Q 4 Program to create and display a Circular linked list.
Program:
<!DOCTYPE html>
<html>

<body>
<script>

function circularLinkedList() {
let Node = function (element) {
this.element = element;
this.next = null;
}

let length = 0;
let head = null;

this.getElementAt = function (index) {


if (index >= 0 && index <= length) {
let node = head;
for (let i = 0; i < index && node != null; i++) {
node = node.next;
}
return node;
}
return undefined;
}

this.append = function (element) {


const node = new Node(element);
let current;

if (head === null) {


head = node;
} else {
current = this.getElementAt(length - 1);
current.next = node;
}

node.next = head;
length++;
}

this.insert = function (element, index) {


if (index >= 0 && index <= length) {
const node = new Node(element);
let current = head;

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;
}

this.removeAt = function (index) {


if (index >= 0 && index < length) {
let current = head;

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;
}

this.indexOf = function (elm) {


let current = head,
index = -1;

while (current) {
if (elm === current.element) {
return ++index;
}

index++;
current = current.next;
}

return -1;
};

this.isPresent = (elm) => {


return this.indexOf(elm) !== -1;
};

this.getHead = function () {
return head;
}
this.delete = (elm) => {
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;
}

string += current.element + (current.next ? '\n' : '');


current = current.next;
}

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;
}

let cll = new circularLinkedList();


console.log("Is the list empty?: " + cll.isEmpty());
cll.append(20);
cll.append(30);
cll.append(40);
cll.append(50);
cll.append(60);
console.log(cll.toArray());

console.log("Removing an element at index 3: "+ cll.removeAt(3));


console.log(cll.toArray());

console.log("Inserting an element at index 3: " );


cll.insert(70,3);
console.log(cll.toArray());

console.log("Deleting the head element of the list: ");


cll.deleteHead();
console.log(cll.toArray());

console.log("Deleting an element: " + cll.delete(40));


console.log(cll.toArray());

</script>
</body>
</html>

Output:
Q 5. Program to create and display a Doubly linked list
Program:
<html>
<body>
<script>

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;

append = function (element) {

let node = new Node(element),


current = this.head,
previous;

if (!this.head) {
this.head = node;
this.tail = node;
} else {
node.prev = this.tail;
this.tail.next = node;
this.tail = node;
}
this.length++;
}

insert = function (position, element) {

if (position >= 0 && position <= this.length) {


let node = new Node(element),
current = this.head,
previous,
index = 0;
if (position === 0) {
if (!this.head) {
this.head = node;
this.tail = node;
} else {
node.next = current;
current.prev = node;
this.head = node;
}
} else if (position === this.length) {
current = this.tail;
current.next = node;
node.prev = current;
this.tail = node;
} else {
while (index++ < position) {
previous = current;
current = current.next;
}
node.next = current;
previous.next = node;

current.prev = node;
node.prev = previous;
}
this.length++;
return true;
} else {
return false;
}
}

removeAt = function (position) {

if (position > -1 && position < this.length) {


let current = this.head, previous, index = 0;

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;
}
}

indexOf = function (elm) {


let current = this.head,
index = -1;

while (current) {
if (elm === current.element) {
return ++index;
}
index++;
current = current.next;
}
return -1;
};

isPresent = (elm) => {


return this.indexOF(elm) !== -1;
};

delete = (elm) => {


return this.removeAt(this.indexOf(elm));
};

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;
}
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>

Output:

Q 6. Program to delete a new node from Doubly linked list


Program:
<html>
<head>
<script>

var head;

class Node {

constructor(val) {
this.data = val;
this.prev = null;
this.next = null;
}
}
function push(new_data) {

new_Node = new Node(new_data);


new_Node.next = head;
new_Node.prev = null;

if (head != null)
head.prev = new_Node;

head = new_Node;
}

function printlist( node) {


last = null;

while (node != null) {


document.write(node.data + " ");
last = node;
node = node.next;
}

document.write("<br/>");
}

function deleteNode( del) {

if (head == null || del == null) {


return;
}
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);
document.write("Deleting element next to head element: ");
deleteNode(head.next);
printlist(head);

document.write("Modified Linked list after deletion: ");


printlist(head);

</script>

</head>
</html>

Output:
Q 7. Program to create and display a Circular doubly linked list
Program:
<html>

<head>
<script>

function circularDoublyLL() {
let Node = function (element) {
this.element = element;
this.next = null;
this.prev = null;
}

let length = 0;
let head = null;
let tail = null;

this.getElementAt = function (index) {


if (index >= 0 && index <= length) {
let node = head;
for (let i = 0; i < index && node != null; i++) {
node = node.next;
}
return node;
}
return undefined;
}

this.append = function (element) {


let node = new Node(element),
current = head,
previous;

if (!head) {
head = node;
tail = node;
} else {
node.prev = tail;
tail.next = node;
tail = node;
}

head.prev = tail;

tail.next = head;
length++;
}

this.insert = function (position, element) {

if (position >= 0 && position <= length) {


let node = new Node(element),
current = head,
previous,
index = 0;

if (position === 0) {
if (!head) {
head = node;
tail = node;
} else {
node.next = current;
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;
}
}

this.removeAt = function (index) {


if (index >= 0 && index < length) {
let current = head;

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;
}

if (head) {
head.prev = tail;

tail.next = head;
}
length--;
return current.element;
}
return undefined;
}

this.indexOf = function (elm) {


let current = head,
index = -1;

while (current) {
if (elm === current.element) {
return ++index;
}

index++;
current = current.next;
}

return -1;
};

this.isPresent = (elm) => {


return this.indexOf(elm) !== -1;
};

this.getHead = function () {
return head;
}
this.getTail = function () {
return tail;
}

this.delete = (elm) => {


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;
}

string += current.element + (current.next ? '\n' : '');


current = current.next;
}

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;
}
}

let cLL = new circularDoublyLL();


cLL.append(10);
cLL.append(20);
cLL.append(30);
cLL.append(40);
cLL.append(50);
console.log(cLL.toArray());

cLL.insert(3, 25);
console.log(cLL.toArray());

cLL.removeAt(2);
console.log(cLL.toArray());

</script>
</head>

</html>
Output:
Q 8. Program to create a Doubly Linked List and display it in reverse order.
Program:
<html>
<head>
<script>

var head;

class Node
{
constructor(val)
{
this.data = val;
this.prev = null;
this.next = null;
}
}

function reverse()
{
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);

document.write("Original linked list: <br/>");


printList(head);

reverse();
document.write("<br/>");
document.write("The reversed Linked List is: <br/>");
printList(head);
</script>

</head>
</html>

Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy