数据结构__1__常见数据结构

136 阅读1分钟

1、队列

 var Queue = function(){
          var items = [];
          // 尾进队列
          this.enqueue = function(element){
               items.push(element);
          }
          // 尾出队列
          this.dequeue = function(){
               return items.shift();
          }
          // 查看队列头
          this.front = function(){
               return items[0];
          }
          // 判断队列是否为空
          this.isEmpty = function(){
               return items.length==0;
          }
          // 获取队列的长度
          this.size = function(){
               return items.length;
          }
     }

2、栈

 var Stack = function() {
        var items = []; //对象是有成员
        // this.items = []; items将变成公有属性
        // items = [];
        // push(1);  items = [1];
        // push(2)  items = [1,2] 1为栈底
        this.push = function(element) {
            items.push(element)
        }
        this.getItems = function() {
            return items;
        }
        // 获取items最后一项
        this.peek = function() {
            return items[items.length - 1];
        }
        //  items = [1,2,3];
        // items.pop();  ==3     items = [1,2];
        this.pop = function() {
            return items.pop();
        }
        // items 长度为0 则为true
        this.isEmpty = function() {
            return items.length == 0
        }
        // 将items置空
        this.clear = function() {
            items = [];
        }
       
        // 返回items的长度
        this.size = function() {
            return items.length;
        }
    }

3、链表

var linkSheet = function() {
        var head = null;
        var length = 0;
        var Node = function(element) {
            this.element = element;
            this.next = null;
        }
        // 链表尾部添加元素
        this.append = function(element) {
            var node = new Node(element)
            if (head == null) {
                head = node;
            } else {
                var current = head;
                while (current.next) {
                    current = current.next;
                }
               current.next = Node;
            }
            length++;
        }
        //链表插入元素
        this.insert = function(position, element) {
            if (position >= -1 && position < length) {
                var node = new Node(element);
                if (position == 0) {
                    var current = head;
                    head = node;
                    head.next = current;
                } else {
                    var index = 0;
                    var current = head;
                    var previous = null;
                    while (index < position) {
                        previous = current;
                        current = current.next;
                        index++;
                    }
                    previous.next = node;
                    node.next = current;
                }
                length++;
            }
        }
        // 移除下标元素
        this.removeAt = function(position){
        	if(position>-1&&position<length){
        		if(position==0){
        			var current = head;
        			head = current.next;
        		}
        		else{
        			var current = head;
        			var previous = null;
        			var index = 0;
        			while(index<position){
        				previous = current;
        				current = current.next;
        				index++;
        			}
        			previous.next= current.next;
        		}
        		length--;
        		return current;
        	}
        	return null;
        }
        // 获取元素下标
        this.indexOf = function(element){
        	var current = head;
        	var index = 0;
        	while(current){
        		if(current.element==element){
        			return index;
        		}
        		current= current.next;
        		index++;
        		return index;
        	}
        	return -1;
        }

       // 移除元素
       this.remove = function(element){
           return this.removeAt(this.indexOf(element));
       }
       // 判断为空
       this.isEmpty = function(){
       	return length==0;
       }
       // 获取长度
       this.size = function(){
       	return length;
       }
       // 返回头部
       this.getHead = function(){
       	return head;
       }
    }