用面向对象实现三种基本的数据结构: 栈、队列和链表

qiufuxing 10天前 136

用面向对象实现三种基本的数据结构: 栈、队列和链表

基本用法

// 使用栈
var stack = SimpleDS.Stack()
stack.push(1)
stack.push(2)
stack.push(3)
console.log("Stack: ", stack.pop(), stack.pop(), stack.pop())

// 使用队列
var queue = SimpleDS.Queue()
queue.enqueue("a")
queue.enqueue("b")
queue.enqueue("c")
console.log("Queue: ", queue.dequeue(), queue.dequeue(), queue.dequeue())

// 使用链表
var list = SimpleDS.LinkedList()
list.insert("x")
list.insert("y")
list.insert("z")
console.log("Linked List: ", list.display())
list.remove("y")
console.log("After removing 'y': ", list.display())

console.pause(true);

完整代码

import console;

namespace SimpleDS {
    Stack = class {
        ctor() {
            this.items = {}
        }
        
        push = function(item) {
            ..table.push(this.items, item)
        }
        
        pop = function() {
            if(this.isEmpty()) {
                return null
            }
            return ..table.pop(this.items)
        }
        
        peek = function() {
            if(this.isEmpty()) {
                return null
            }
            return this.items[#this.items]
        }
        
        isEmpty = function() {
            return #this.items == 0
        }
        
        size = function() {
            return #this.items
        }
    }
    
    Queue = class {
        ctor() {
            this.items = {}
        }
        
        enqueue = function(item) {
            ..table.push(this.items, item)
        }
        
        dequeue = function() {
            if(this.isEmpty()) {
                return null
            }
            return ..table.remove(this.items, 1)
        }
        
        front = function() {
            if(this.isEmpty()) {
                return null
            }
            return this.items[1]
        }
        
        isEmpty = function() {
            return #this.items == 0
        }
        
        size = function() {
            return #this.items
        }
    }
    
    LinkedList = class {
        ctor() {
            this.head = null
        }
        
        Node = class {
            ctor(data) {
                this.data = data
                this.next = null
            }
        }
        
        insert = function(data) {
            var newNode = this.Node(data)
            if(!this.head) {
                this.head = newNode
            }
            else {
                var current = this.head
                while(current.next) {
                    current = current.next
                }
                current.next = newNode
            }
        }
        
        remove = function(data) {
            if(!this.head) {
                return
            }
            if(this.head.data == data) {
                this.head = this.head.next
                return
            }
            var current = this.head
            while(current.next) {
                if(current.next.data == data) {
                    current.next = current.next.next
                    return
                }
                current = current.next
            }
        }
        
        find = function(data) {
            var current = this.head
            while(current) {
                if(current.data == data) {
                    return current
                }
                current = current.next
            }
            return null
        }
        
        display = function() {
            var elements = {}
            var current = this.head
            while(current) {
                ..table.push(elements, tostring(current.data))
                current = current.next
            }
            return ..string.join(elements, " -> ")
        }
    }
}



// 使用栈
var stack = SimpleDS.Stack()
stack.push(1)
stack.push(2)
stack.push(3)
console.log("Stack: ", stack.pop(), stack.pop(), stack.pop())

// 使用队列
var queue = SimpleDS.Queue()
queue.enqueue("a")
queue.enqueue("b")
queue.enqueue("c")
console.log("Queue: ", queue.dequeue(), queue.dequeue(), queue.dequeue())

// 使用链表
var list = SimpleDS.LinkedList()
list.insert("x")
list.insert("y")
list.insert("z")
console.log("Linked List: ", list.display())
list.remove("y")
console.log("After removing 'y': ", list.display())

console.pause(true);


最新回复 (2)
  • netfox 10天前
    0 2

    top....

  • jerry2cool 9天前
    0 3
    顶!!
返回