• 链表实现stack和queue,java实现(ADT思想)


    第一部分:

    链表实现Stack

      1 package com.liu.Link;
      2 
      3 //栈处理类
      4 public class LinkStack3 {
      5     private LinkList3 theList;
      6     //构造函数防止空指针异常错误
      7     public LinkStack3()
      8     {
      9         theList = new LinkList3();
     10     }
     11     public void push(long d)
     12     {
     13         theList.insertFirst(d);
     14     }
     15     public long pop()
     16     {
     17         return theList.deleteFirst();
     18     }
     19     public boolean isEmpty()
     20     {
     21         return theList.isEmpty();
     22     }
     23     public void display()
     24     {
     25         System.out.print("Stack (top-->bottom):");
     26         theList.displayStack();
     27     }
     28 }
     29 
     30 class LinkStackApp
     31 {
     32     public static void main(String[] args)
     33     {
     34         LinkStack3 theStack = new LinkStack3();
     35         theStack.push(1111);
     36         theStack.push(40);
     37         theStack.display();
     38         
     39         theStack.push(60);
     40         theStack.push(80);
     41         theStack.display();
     42         
     43         theStack.pop();
     44         theStack.pop();
     45         theStack.display();
     46         
     47     }
     48 }
     49 
     50 
     51 //链表处理类
     52 class LinkList3
     53 {
     54     private Link3 first;
     55     public LinkList3()
     56     {
     57         first = null;
     58     }
     59     public boolean isEmpty()
     60     {
     61         return (first == null);
     62     }
     63     public void insertFirst(long dd)
     64     {
     65         Link3 newLink = new Link3(dd);
     66         newLink.next = first;
     67         first = newLink;
     68     }
     69     public long deleteFirst()
     70     {
     71         Link3 temp = first;
     72         first = first.next;
     73         return temp.dData;
     74     }
     75     public void displayStack()
     76     {
     77         Link3 current = first;
     78         while(current!=null)
     79         {
     80             //System.out.print(current.dData+" ");
     81             current.displayLink();
     82             current = current.next;
     83         }
     84         System.out.println("");
     85     }
     86 }
     87 
     88 
     89 //数据类型类
     90 class Link3
     91 {
     92     public long dData;
     93     public Link3 next;
     94     public Link3(long dd)
     95     {
     96         dData = dd;
     97     }
     98     public void displayLink()
     99     {
    100         System.out.print(dData+" ");
    101     }
    102 }

    第二部分:

    链表实现Queue

      1 package com.liu.Link;
      2 
      3 class LinkQueueApp
      4 {
      5     public static void main(String[] args)
      6     {
      7         LinkQueue theQueue = new LinkQueue();
      8         theQueue.insert(10);
      9         theQueue.insert(20);
     10         theQueue.displayQueue();
     11         
     12         theQueue.insert(30);
     13         theQueue.insert(40);
     14         theQueue.insert(50);
     15         theQueue.displayQueue();
     16         
     17         theQueue.remove();
     18         theQueue.remove();
     19         theQueue.displayQueue();
     20     }
     21 }
     22 
     23 
     24 public class LinkQueue {
     25     private LinkList4 theList;
     26     public LinkQueue()
     27     {
     28         theList = new LinkList4();
     29         
     30     }
     31     public boolean isEmpty()
     32     {
     33         return theList.isEmpty();
     34     }
     35     public void insert(long d)
     36     {
     37         theList.insertLast(d);
     38     }
     39     public long remove()
     40     {
     41         return theList.deleteFirst();
     42     }
     43     public void displayQueue()
     44     {
     45         System.out.print("Queue (front-->rear):");
     46         theList.displayList();
     47     }
     48 }
     49 
     50 class LinkList4
     51 {
     52     private Link4 first;
     53     private Link4 last;
     54     public LinkList4()
     55     {
     56         first = null;
     57         last = null;
     58     }
     59     public boolean isEmpty()
     60     {
     61         return first == null;
     62     }
     63     public void insertLast(long d)
     64     {
     65         Link4 newLink = new Link4(d);
     66         if(isEmpty())
     67             first = newLink;
     68         else
     69             last.next = newLink;
     70         last = newLink;
     71     }
     72     
     73     public long deleteFirst()
     74     {
     75         long temp = first.dData;
     76         //这种情况为只有一个元素的情况,要注意尾指针的位置
     77         if(first.next == null)
     78         {
     79             last = null;
     80         }
     81         first = first.next;
     82         return temp;
     83     }
     84     
     85     public void displayList()
     86     {
     87         Link4 current = first;
     88         while(current!=null)
     89         {
     90             current.displayLink();
     91             current = current.next;
     92         }
     93         System.out.println("");
     94     }
     95 }
     96 
     97 //链表类
     98 class Link4
     99 {
    100     public long dData;
    101     public Link4 next;
    102     public Link4(long d)
    103     {
    104         dData = d;
    105     }
    106     public void displayLink()
    107     {
    108         System.out.print(dData+" ");
    109     }
    110 }
  • 相关阅读:
    python 使用else代替状态变量
    python 实现线程安全的单例模式
    sql语句的执行顺序
    python 实现int函数
    python实现时间o(1)的最小栈
    python实现简单的负载均衡
    python实现求最长回文子串长度
    python lambda表达式
    sql针对某一字段去重,并且保留其他字段
    基本认识
  • 原文地址:https://www.cnblogs.com/speaklessdomore/p/3676573.html
Copyright © 2020-2023  润新知