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

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
Max-CoderBoi wants to merge 1 commit into codemistic:main from Max-CoderBoi:patch-22
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
188 changes: 126 additions & 62 deletions DSA Javascript/Stack_Using_Singly_Linked_List.js
View file Open in desktop
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;
}

}

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /