>  기사  >  웹 프론트엔드  >  Javascript를 사용한 스택 구현(연결된 목록)

Javascript를 사용한 스택 구현(연결된 목록)

王林
王林원래의
2024-08-14 17:19:52311검색

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으로 문의하세요.
이전 기사:Angular의 국제화 (i)다음 기사:Angular의 국제화 (i)