-
Notifications
You must be signed in to change notification settings - Fork 383
Update Stack_Using_Singly_Linked_List.js #785
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
188 changes: 126 additions & 62 deletions
DSA Javascript/Stack_Using_Singly_Linked_List.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,147 @@ | ||
//Stack using linkedlist | ||
function stackUsingLL(){ | ||
//Node | ||
let Node = function(elm){ | ||
this.element = elm; | ||
this.next = null; | ||
} | ||
// //Stack using linkedlist | ||
// function stackUsingLL(){ | ||
// //Node | ||
// let Node = function(elm){ | ||
// this.element = elm; | ||
// this.next = null; | ||
// } | ||
|
||
//To keep track of the size | ||
let length = 0; | ||
// //To keep track of the size | ||
// let length = 0; | ||
|
||
//To keep track of the list | ||
let head = null; | ||
// //To keep track of the list | ||
// let head = null; | ||
|
||
//Push data in the stack | ||
this.push = function(elm){ | ||
//Create a new node | ||
let node = new Node(elm), | ||
current; | ||
// //Push data in the stack | ||
// this.push = function(elm){ | ||
// //Create a new node | ||
// let node = new Node(elm), | ||
// current; | ||
|
||
//Add the new node at the top | ||
current = head; | ||
node.next = current; | ||
head = node; | ||
// //Add the new node at the top | ||
// current = head; | ||
// node.next = current; | ||
// head = node; | ||
|
||
length++; | ||
} | ||
// length++; | ||
// } | ||
|
||
//Pop the item from the stack | ||
this.pop = function(){ | ||
let current = head; | ||
// //Pop the item from the stack | ||
// this.pop = function(){ | ||
// let current = head; | ||
|
||
//If there is item then remove it | ||
//and make the next element as the first | ||
if(current){ | ||
let elm = current.element; | ||
current = current.next; | ||
head = current; | ||
length--; | ||
return elm; | ||
} | ||
// //If there is item then remove it | ||
// //and make the next element as the first | ||
// if(current){ | ||
// let elm = current.element; | ||
// current = current.next; | ||
// head = current; | ||
// length--; | ||
// return elm; | ||
// } | ||
|
||
return null; | ||
} | ||
// return null; | ||
// } | ||
|
||
//Return the first element in the stack | ||
this.peek = function(){ | ||
if(head){ | ||
return head.element; | ||
} | ||
// //Return the first element in the stack | ||
// this.peek = function(){ | ||
// if(head){ | ||
// return head.element; | ||
// } | ||
|
||
return null; | ||
} | ||
// return null; | ||
// } | ||
|
||
// //Convert the stack to an array | ||
// this.toArray = function(){ | ||
// let arr = []; | ||
// let current = head; | ||
// while(current){ | ||
// arr.push(current.element); | ||
// current = current.next; | ||
// } | ||
|
||
// return arr; | ||
// } | ||
|
||
// //Check if stack is empty | ||
// this.isEmpty = function(){ | ||
// return length === 0; | ||
// } | ||
|
||
// //Return the size of the stack | ||
// this.size = function(){ | ||
// return length; | ||
// } | ||
|
||
//Convert the stack to an array | ||
this.toArray = function(){ | ||
// //Clear the stack | ||
// this.clear = function(){ | ||
// head = null; | ||
// length = 0; | ||
// } | ||
|
||
// } | ||
class Node { | ||
constructor(element) { | ||
this.element = element; | ||
this.next = null; | ||
} | ||
} | ||
|
||
class Stack { | ||
constructor() { | ||
this.head = null; // Top of stack | ||
this.length = 0; // Stack size | ||
} | ||
|
||
// Push element onto the stack | ||
push(element) { | ||
const node = new Node(element); | ||
node.next = this.head; | ||
this.head = node; | ||
this.length++; | ||
} | ||
|
||
// Pop the top element from the stack | ||
pop() { | ||
if (this.isEmpty()) return null; // Handle empty stack | ||
|
||
const poppedElement = this.head.element; | ||
this.head = this.head.next; | ||
this.length--; | ||
|
||
return poppedElement; | ||
} | ||
|
||
// Peek at the top element without removing it | ||
peek() { | ||
return this.head ? this.head.element : null; | ||
} | ||
|
||
// Convert stack to an array (from top to bottom) | ||
toArray() { | ||
let arr = []; | ||
let current = head; | ||
while(current){ | ||
let current = this.head; | ||
while(current){ | ||
arr.push(current.element); | ||
current = current.next; | ||
} | ||
|
||
return arr; | ||
} | ||
//Check if stack is empty | ||
this.isEmpty = function(){ | ||
return length === 0; | ||
|
||
//Check if the stack is empty | ||
isEmpty() { | ||
return this.length === 0; | ||
} | ||
//Return the size of the stack | ||
this.size = function(){ | ||
return length; | ||
|
||
//Return the size of the stack | ||
size() { | ||
return this.length; | ||
} | ||
//Clear the stack | ||
this.clear = function(){ | ||
head = null; | ||
length = 0; | ||
|
||
//Clear the stack | ||
clear() { | ||
this.head = null; | ||
this.length = 0; | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.