0% found this document useful (0 votes)
34 views26 pages

Anand - Arya - Dsa

The document describes code for implementing various data structures like single linked list, doubly linked list, stack, and reversing a string using a stack. It includes code for Node and LinkedList classes to implement single and doubly linked lists with methods to create, add, delete nodes. A Stack class is implemented with methods to push, pop, peek, check empty, size and clear. Code is also provided to reverse a string using a stack.

Uploaded by

Stan lee
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)
34 views26 pages

Anand - Arya - Dsa

The document describes code for implementing various data structures like single linked list, doubly linked list, stack, and reversing a string using a stack. It includes code for Node and LinkedList classes to implement single and doubly linked lists with methods to create, add, delete nodes. A Stack class is implemented with methods to push, pop, peek, check empty, size and clear. Code is also provided to reverse a string using a stack.

Uploaded by

Stan lee
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/ 26

Name:- Anand Arya

Roll no:-21302
Q1) a)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="SingleyLinkedList.js"
type="text/JavaScript"></script>
<input type="text" id="ivalue"><br>
<button
onclick="l1.create.call(l1)">Create</button><br>
<button onclick="l1.display.call(l1)">Display
List</button>
<button
onclick="l1.addFirst.call(l1)">AddFirst</button>
<button
onclick="l1.addLast.call(l1)">AddLast</button>
<button
onclick="l1.deleteLast.call(l1)">deleteLast</button>
<button
onclick="l1.deleteFirst.call(l1)">deleteFirst</button>

</body>
</html>

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

constructor(){
this.head=null;
this.length=0;
}

create(){
let
data=document.getElementById("ivalue").value;
let node=new Node(data);
if(this.head==null){
document.getElementById("ivalue").value="";
this.head=node;
this.length++;
}else{
document.getElementById("ivalue").value="";
let n=this.head;
while(n.next!=null){
n=n.next;
}
n.next=node; ; //this statement store
address of next node in next block
this.length++;
}
}
addFirst(){
let
data=document.getElementById("ivalue").value;
let node=new Node(data);
if(this.head==null){
document.getElementById("ivalue").value="";
this.head=node;
this.length++
}else{
document.getElementById("ivalue").value="";
node.next=this.head;
this.head=node;
this.length++;
}
}

addLast(){
let
data=document.getElementById("ivalue").value;
let node=new Node(data);
if(this.head==null){
document.getElementById("ivalue").value="";
this.head=node;
}else{
document.getElementById("ivalue").value="";
let temp=this.head;
while(temp.next!=null){
temp=temp.next;
}
temp.next=node;
this.length++;
}
}

deleteLast(){
if(this.head==null){
alert("List is empty");
}
else{
let current=this.head;
let previous=null;
while(current.next!=null){
previous=current;
current=current.next;
}
previous.next=null;
alert(current.data);
}
}
deleteFirst(){
if(this.head==null){
alert("List is empty");
}
else{
let current=this.head;
this.head=this.head.next;
current.next=null;
}
}

display(){
let temp=this.head;
while(temp!=null){
document.write(temp.data+"--->");
temp=temp.next;
}
document.write("<Br/>");
}
}
const l1=new LinkedList();
Q1b)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="doublylinkedlist.js"
type="text/JavaScript"></script>
<input type="text" id="ivalue"><br>
<button
onclick="l1.create.call(l1)">Create</button><br>
<button onclick="l1.display.call(l1)">Display
List</button>
<button onclick="l1.displayreverse.call(l1)">
Reverse Display</button>
<button
onclick="l1.addFirst.call(l1)">AddFirst</button>
<button
onclick="l1.addLast.call(l1)">AddLast</button>
<button
onclick="l1.deleteLast.call(l1)">deleteLast</button>
<button
onclick="l1.deleteFirst.call(l1)">deleteFirst</button>
</body>
</html>

class Node{
constructor(data){
this.data=data;
this.next=null;
this.previous=null;
}
}
class doublyLinkedList{
constructor(){
this.head=null;
this.tail=null;
}
create(){
let data=document.getElementById("ivalue").value;
var node=new Node(data);
let current=this.head;
if(this.head==null){
document.getElementById("ivalue").value="";
node.previous=null;
node.next=null;
this.head=node;
this.tail=node;
}else{
document.getElementById("ivalue").value="";
while(current.next!=null){
current=current.next;
}
current.next=node;
node.previous=current;
this.tail=node;

}
}
addFirst(){
let
data=document.getElementById("ivalue").value;
let node=new Node(data);
let temp=this.head;
if(temp==null){
document.getElementById("ivalue").value="";
this.head=node;
this.tail=node;
}
else{
document.getElementById("ivalue").value="";
node.next=this.head;
this.head.previous=node
this.head=node;
}
}
addLast(){
let
data=document.getElementById("ivalue").value;
let node=new Node(data);
let current=this.head;
if(this.head==null){
document.getElementById("ivalue").value="";
this.head=node;
this.tail=node;
}else{
document.getElementById("ivalue").value="";
while(current.next!=null){
current=current.next;
}
current.next=node;
node.previous=current;
this.tail=node;
}
}

deleteFirst(){
let current=this.head;
this.head=this.head.next;
if(current==null){
alert("List is Empty");
}
else{
current.next=null;
this.head.previous=null;
alert("First node deleted --->"+current.data);
}
}
deleteLast(){
let temp=this.tail;
if(this.head==null){
alert("List is empty");
}
if(this.head.next==null){
this.tail=null;
this.head=nulll;
alert("deleted Last node ---->"+this.head.data);
}
else{
this.tail.previous.next=null;
this.tail=this.tail.previous;
temp.prev=null;
alert("deleted Last node ---->"+temp.data);
}
}
display(){
if(this.head==null){
alert("List is empty");
}else{
let current=this.head;
while(current!=null){
document.write(current.data+"--->");
current=current.next
}
document.write("null");
}
}
displayreverse(){
let temp=this.tail;
if(this.head==null){
alert("List is empty");
}else{
while(temp!=null){
document.write(temp.data+"--->");
temp=temp.previous;
}
document.write("null");
}

const l1=new doublyLinkedList();

Q2)
class Stack {
constructor() {
this.items = []
this.count = 0
}

// Add element to top of stack


push(element) {
this.items[this.count] = element
console.log(`${element} added to ${this.count}`)
this.count += 1
return this.count - 1
}

// Return and remove top element in stack


// Return undefined if stack is empty
pop() {
if(this.count == 0) return undefined
let deleteItem = this.items[this.count - 1]
this.count -= 1
console.log(`${deleteItem} removed`)
return deleteItem
}

// Check top element in stack


peek() {
console.log(`Top element is
${this.items[this.count - 1]}`)
return this.items[this.count - 1]
}
// Check if stack is empty
isEmpty() {
console.log(this.count == 0 ? 'Stack is empty' :
'Stack is NOT empty')
return this.count == 0
}

// Check size of stack


size() {
console.log(`${this.count} elements in stack`)
return this.count
}

// Print elements in stack


print() {
let str = ''
for(let i = 0; i < this.count; i++) {
str += this.items[i] + ' '
}
return str
}
// Clear stack
clear() {
this.items = []
this.count = 0
console.log('Stack cleared..')
return this.items
}
}

const stack = new Stack()

stack.isEmpty()

stack.push(100)
stack.push(200)

stack.peek()
stack.push(300)

console.log(stack.print())

stack.pop()
stack.pop()

stack.clear()

console.log(stack.print())

stack.size()

stack.isEmpty()

Q3)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="stringReverse.js"
type="text/JavaScript"></script>
<input type="text" id="ivalue"><br>
<button onclick="Reverse()">Reverse String</button>
</body>
</html>

class Stack {
constructor() {
this.top = null
}
push(ele) {
var node = new newNode(ele)
node.next = this.top
this.top = node
}
pop() {
var temp = this.top
var char = temp.data
this.top = this.top.next
temp = null
return char
}
reverseString(str) {
var i = 0
var reversestr = ""
while (i != str.length) {
this.push(str.charAt(i))
i++
}
var temp = this.top
while (temp != null) {
var char
char = this.pop()
reversestr += char
temp = this.top
}
return reversestr
}
}
class newNode {
constructor(data, next) {
this.data = data;
this.next = null;
}
}
function Reverse(){
const stack = new Stack();
const string = document.getElementById("ivalue").value;
const reverse = stack.reverseString(string);
document.write(`<h1>The String provided -
${string}</h1>`);
document.write("<br></br>");
document.write(`<h1>String in reverse format -
${reverse}</h1>`);
}
Q4)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="balancedparentheses.js"></script>
<input type="text" id="ivalue"><br>
<button onclick="check()">Check</button>
</body>
</html>

class Stack {
constructor() {
this.top = null
this.length=0;
}
push(ele) {

var node = new newNode(ele)


node.next = this.top;
this.top = node;
this.length++;
// alert(this.length);
}
pop() {
// alert("inside pop method");
var temp = this.top;
var char = temp.data;
this.top = this.top.next;
temp = null;
this.length--;
// alert(this.length)
return char
}
isBalanced(str){
for(let i=0;i<str.length;i++){
let letter=str.charAt(i);
//alert(letter);
// alert("2");
if(letter==='('||letter==='{'
||letter==='['){
this.push(letter);
}else if(letter===')'||letter===']'||
letter==='}'){
// let a=this.top.data;
// alert(a+"-->this.top.data")
if(this.top==null){
document.write("<h1>False</h1>");
}
if(letter===')'&& this.top.data==='(' ||
letter===']'&& this.top.data==='[' ||letter==='}'&&
this.top.data==='{'){
this.pop();
}else{
document.write("<h1>false</h1>");
process.exitCode = 1;

}
}
}
if(this.length===0){
document.write("<h1>True</h1>");
}
else{
document.write("<h1>false</h1>");
}
}
}

class newNode {
constructor(data, next) {
this.data = data;
this.next = null;
}
}
function check(){
const stack = new Stack();
const str = document.getElementById("ivalue").value;
//alert("1");
//document.write(str);
stack.isBalanced(str);
}

You might also like