首頁  >  文章  >  web前端  >  使用 Javascript 實作堆疊(鍊錶)

使用 Javascript 實作堆疊(鍊錶)

王林
王林原創
2024-08-14 17:19:52306瀏覽

Stack Implementation Using Javascript (Linked List)

介紹

如果您沒有信心或想了解更多關於鍊錶及其類型以及我們如何對其進行操作,請參閱我的另一篇有關單鍊錶和雙鍊錶的文章

使用 Javascript 處理單鍊錶和雙鍊錶的所有操作:- 最後一站解決方案

  1. 本文是關於使用單鍊錶並建立堆疊資料結構.

如果您有任何疑問,請隨時與我聯絡

享受程式碼,快樂編碼。

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

 class LinkedList {
     constructor() {
         this.head = this.tail = null;
         this.size = 0;
     }

     append(value) {
         const newNode = new Node(value);
         if (this.head === null) {
             console.log('Inside strange')
             this.head = this.tail = newNode;
             this.size = 1;
             return;
         }
         this.tail.next = newNode;
         this.tail = newNode;
         this.size++;
     }

     deletAtEnd() {
         if (this.size <= 1) {

             this.head = this.tail = null;
             this.size = 0;
             return;
         }
         let currentNode = this.head;
         const pos = this.size -1;
         let counter = 1;
         while (currentNode) {
             if (counter++ === pos) {
                 currentNode.next = null;
                 this.tail = currentNode;
                 this.size--;
                 return ;
             }
             currentNode = currentNode.next;
         }
     }

     traversal() {
         let currentNode = this.head;
         while (currentNode) {
             console.log(currentNode.value);
             currentNode = currentNode.next;
         }
     }

     reverse() {
        let currentNode = this.head;
        let prevNode = null;
        let nextNode = null;

        while (currentNode) {
            nextNode = currentNode.next;
            currentNode.next = prevNode;
            if (prevNode === null) {
                this.tail = currentNode;
            }

            prevNode = currentNode;
            currentNode = nextNode;
        }

        this.head = prevNode;

        this.traversal();
     }
 }

 class Stack {
     stack;
     constructor() {
         this.stack = new LinkedList();
     }

     push(value) {
        this.stack.append(value);
     }

     pop() {
        this.stack.deletAtEnd()
     }

     peak() {
        console.log('Peak Value ---> ', this.stack.tail.value); 
     }
     traversal() {
         this.stack.reverse();
     }
 }


 const test = new Stack();

 test.push(20);
 test.push(13);
 test.push(3);
 test.push(5);
 test.push(9);

 console.log(test.stack)
 console.log('---------------Peak-------------')
 test.peak()
 console.log('-------------After Pop ------------');
 test.pop();
 test.peak()
 test.traversal()

/*
LinkedList {
  tail: Node { value: 9, next: null },
  head: Node { value: 20, next: Node { value: 13, next: [Node] } },
  size: 5
}
---------------Peak-------------
Peak Value --->  9
-------------After Pop ------------
Peak Value --->  5
5
3
13
20

*/

以上是使用 Javascript 實作堆疊(鍊錶)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn