首页 > 其他 > 详细

数据结构之链表

时间:2014-04-12 08:36:07      阅读:399      评论:0      收藏:0      [点我收藏+]
class Node{

    int val;
    Node next;

    Node(int val){
       this.val = val;
    }
}

1、栈(Stack)

class Stack{
    Node top;
    
    Node peek(){
       if(null != top){
           return top;
       }
       return null;
    }


     void push(Node e){
         if(null != top){
            e.next = top;
            top = e;
         }
     }

     Node pop(){
        if(null == top){
            return null;
        }else{
            Node temp = new Node(top.val);
            top = top.next;
            return temp;
        }
     }
}
2、队列(Queue)

class Queue{
   Node first,last;

   void enqueue(Node e){
      if(null == first){
         first = e;
         last = first;
      }else{
         last.next = e;
         last = e;
      }
   }

    Node dequeue(){
        if(null == first){
           return null;
        }else{
           Node temp = new Node(first.val);
           first= first.next;
           return temp;
        }
    }
}



数据结构之链表,布布扣,bubuko.com

数据结构之链表

原文:http://blog.csdn.net/lynnlovemin/article/details/23436563

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!