美文网首页
数据结构

数据结构

作者: 好奇男孩 | 来源:发表于2018-09-23 19:52 被阅读9次

    1.栈

    1.1栈的创建

    function Stack(){
    var items=[];
    //方法
    }
    var stack =new stack();
    

    1.1.1栈的方法

    this.pop=function (){
    return items.pop();
    };
    this.isEmpty=function (){
    return items.length===0;
    };
    this.size=function (){
    return items.length;
    };
    this.clear= function (){
    items=[];
    };
    this.print = function (){
    console.log(items.toString());
    };
    

    1.2 应用

    1.2.1 10进制转化为2进制

    function divdeBy2(decNumber){
    var remStack =new Stack(),
    rem,
    binaryString = " "
    while (decNumber >0){
    rem = Math.floor(devNumber%2);
    remstack.push(rem);
    decNumber=Math.floor(decNumber/2)
    };
    while( !remstack.isEmpty()){
    binarystring + =remStack.pop( ).toString();
    }
    return binaryString;
    }
    

    1.2.2 10进制转化为任意进制(2,8,16)

    function baseConverster(deNumer,base){
    var remstack = new Stack(),
    rem,binarystring=" ",
    digits = "0123456789ABCDEF"
    while(decNumber>0){
    rem =Math.floor(decNumer%base);
    remstack.push(rem);
    decNumber=Math.floor(decNumber/base)
    }
    while (!remstack.isEmpty()){
    basestring +=digits[remstack.pop()];
    }
    return  basestring
    }
    

    2 队列

    2.1创建队列

    function Quecue (){
    var items=[];
    //方法
    }
    

    2.1.1队列的方法

    this.enqueue=function (e){
    items.push()e
    };
    this.dequenue=function (){
    return items.shift();
    };
    this.front =function (){
    return items[0];
    ];
    this.isEmpty = function (){
    return items.length==0
    };
    this.size = function (){
    return items.length;
    };
    this.print = function (){
    console.log(items.toString())
    }
    

    2.2 最小优先队列

    function PriorityQueue(){
    var items=[];
    function QueueElement (e,proirity){
    this.element =e;
    this.priority = priority;
    };
    this.enqueue = function (e,priority){
    var queueElement = new QueueElement (e,priority);
    if(items.length ==0){
    items.push(queueELement);
    }else {
    var added = false;
    for (var i= 0;i<items.length;i++){
    if (queueElement.priority<items[i].priority){
    items.splice(i,0,queueElement);
    added=true;
    break;
    }
    }
    if(!added){
    items.push(queueElement)
    }
    }
    }
    }
    

    2.3 循环队列——击鼓传花

    function hotPotato (nameList,num){
    var queue = [];
    for (var i=0;i<namList.length;i++){
    queue.push(nameList[i]);
    };
    var eliminated = " ";
    while (queue.length>1){
    for ( var i=0;i<num;i++){
    queue.push(queue.pop());
    }
    eliminated = queue.pop();
    console.log(eliminated +"被淘汰")
    }
    return queue.pop;
    }
    

    3. 链表

    3.1创建链表

    function LinkList(){
    var Node=function (e){
    this.element=e;
    this.next=null;
    }
    var length=0;
    var head=null;
    //方法
    }
    

    3.1.1 链表尾部追加元素

    this.append =function (e){
    var node = new Node(e),
    current;
    if(head===null){
    head=node;
    }else{
    current=head;
    while(current.next){
    current=current.next;
    }
    current.next=node;
    }
    length++;
    };
    

    3.1.2 链表按位置移除元素

    this.removeAt = function (postion){
    if(postion > -1&& postion <length){
    var current =head,
    previous,index = 0;
    if(postion === 0){
    head =current.next;
    }else {
    while (index++< postion){
    previous =current;
    current =current.next;
    }
    previous.next = current.next;
    }
    length --;
    return current.element;
    }else {
    return null
    }
    };
    

    3.1.3 链表任意位置 插入元素

    this.insert = funtion(postion,element){
    if(position>=0 && postion <=length){
    var node=new Node(element),
    current = head,
    previous,
    index=0;
    if(postion === 0){
    node.next = current;
    head =node ;
    }else {
    while(index ++ <postion ){
    previous =current;
    current =current.next;
    }
    node.next = current;
    previous.next =node;
    }
    length++;
    return true;
    }else {
    return false;
    }
    };
    

    3.1.4链表的toString方法

    this.toString = function (){
    var current =head,
    string = " ";
    while (current){
    string + = " ,"+current.element;
    current = current.next;
    }
    return string.slice(1)
    };
    
    

    3.1.5 链表的indexOf方法

    this.indexOf = function (element){
    var current = head,
    index=0;
    while (current){
    if (element === current.element){
    return index;
    }
    index ++;
    current = current.next;
    }
    return -1;
    };
    

    3.1.6 链表按值移除元素

    this.remove =function (element){
    var index = this.indexOf(element);
    return this.removeAt(index);
    };
    

    3.1.7 链表的 isEmpty,size,getHead方法

    this.isEmpty = function (){
    return length ===0;
    };
    this.size =function (){
    return length;
    };
    this.getHead =function (){
    return read;
    };
    

    3.2双向链表

    function DoublylinkedList(){
    var Node = function(e){
    this.element= e;
    this.next=null;
    this.prev=null;
    }
    var length=0;
    var head=null;
    var tail = null;
    //方法
    }
    

    3.2.2双向链表在任意位置插入元素

    this.insert =function (postion ,element){
    if(postion >=0 && postion <= length){
    var node =new Node(element),
    current =head,
    previous,index=0;
    if(postion === 0){
    if(!head){
    head = node;
    tail = node;
    }else {
    node.next = current ;
    current.prev=node;
    head = node;
    }
    }else if (position===length ){
    current =tail;
    current.next=node;
    node.prev = current;
    tail = node;
    }else {
    while ( index ++ <position){
    previous = current;
    current =current.next;
    }
    node.next = current;
    previous.next = node;
    current.prev =node;
    node.prev = previous;
    }
    length ++;
    return true;
    }else {
    return false;
    }
    };
    

    3.2.3双向链表在任意位置移除元素

    
    this.removeAt = function (position){
    if( position > -1 && position <length){
    var current =head;
    previous,index=0;
    if( position ===0){
    head = current.next;
    if( length ===1){
    tail = null;
    }else {
    head.prev= null ;
    }
    }else if(postion === length - 1){
    current =tail;
    tail = current.prev;
    tail.next = null;
    }else {
    while (index++ <position){
    previous = current;
    current = current.next;
    }
    previous.next = current.next;
    current.next.prev = previous;
    }
    length --;
    return current.element;
    }else {
    return null;
    }
    };
    

    4集合

    4.1 集合的创建

    function Set (){
    var items={};
    //方法
    }
    

    4.1.1集合的方法

    this.has=function (value){
    return items.hasOwnProperty(value);
    }
    this.add = function (value){
    if(!this.has(value)){
    items[value]= value;
    return true;
    }
    return false;
    };
    
    this.remove = function (value){
    if(this.has(value)){
    delete items[value];
    return false;
    };
    
    this.clear = function (){
    items={};
    };
    
    this.size = function (){
    return Object.keys(items).length;
    };
    
    

    4.1.2 并集

    this.union = function (otherSet){
    var unionSet =new Set();
    var values = this.values();
    for(var i = 0; i< values.length;i++){
    unionSet.add(values[i]);
    }
    values = otherSet.values();
    for(var i=0; i<values.length;i++){
    unionSet.add(values[i])
    }
    return unionSet;
    }
    

    4.1.3 交集

    this.intersection = function (otherSet){
    var intersectionSet = new Set;
    var values = this.values();
    for(var i=0;i<values.length;i++){
    if(otherSet.has(values[i])){
    intersectionSet.add(values[i]);
    }
    }
    return intersectionSet;
    }
    

    4.1.4差集

    this.difference = function(otherSet){
    var differenceSet = new Set();
    var values = this.values();
    for(var i=0;i<values.length;i++){
    if(!otherSet.has(values[i])){
    differenceSet.add(values[i])
    }
    }
    return diffrenceSet;
    }
    

    4.1.5子集

    this.subset = function (otherSet){
    if(this.size()>otherSet.size()){
    return false;
    }else{
    var values = this.values();
    for(var i=0;i<values.length;I++){
    if(!ohterSet.has(values[i])){
    return false;
    }
    }
    return true;
    }
    };
    

    5字典

    5.1 字典的创建

    function Dictionary(){
    var items= {};
    }
    

    5.1.1 字典的方法

    this.has = function(key){
    return key in items;
    }
    
    this.set = functon(key,value){
    items[key]=value;
    }
    
    this.remove = function(key){
    if(this.has(key)){
    detele items[key]
    return ture
    }
    return false;
    }
    
    this.get = function (key){
    return this.has(key)  ? items[key]:undefined;
     };
    
    this.values = function (){
    var values = [];
    for ( var k in items){
    if( this.hasOwnProperty(k)){
    values.push(items[k])
    }
    }
    return values;
    };
    
    this.clears = function (){
    items={};
    }
    
    this.size = function (){
    return Object.keys(items).length;
    }
    
    this.keys = function (){
    return Object.keys(items);
    }
    
    this.getItems = function (){
    return items
    }
    
    

    相关文章

      网友评论

          本文标题:数据结构

          本文链接:https://www.haomeiwen.com/subject/dqfsuftx.html