zoukankan      html  css  js  c++  java
  • LinkList

    第一节:单向链表:链表数据存储原有数据还有下一个数据的地址,用指针

      链表有许多相同类型的元素按照特定的顺序排列形成的线性表,最重要两个操作 插入、删除。优点:插入和删除十分方便。

    在java中模拟节点,采用class建立一个节点,只能采用类代替C++中指针,只能够用first,last两个

    Node类点节点来表示指针。

     1 class Node{
     2     int Data;
     3     Node next;//C语言中运用的是指针
     4     
     5     //构造函数初始化
     6     public Node(int Data)
     7          {
     8     this.Data=Data;this next=Null;
     9     }    
    10 
    11 }        

    在java中没有指针类型,只能够用类代替指针。C++通过申请地址来进行下一个节点的链接。

    BuffereadReader java中一个缓冲内,从外界输入的值先放入缓冲中,在写入内存,防止外界输入的被失去。

    建立单向链表,包括其中插入和删除方法:

      1 package linklist;
      2 import java.io.*;
      3 public class CH03_1 {
      4 
      5     
      6     public static void main(String[] args) throws  IOException {
      7         // TODO Auto-generated method stub
      8         BufferedReader buff;//防止外界的值输入过快被舍弃
      9         buff=new BufferedReader(new InputStreamReader(System.in));
     10         
     11         int num;String name;int score;
     12         LinkList list=new LinkList();//本包之中Node中LinkList类
     13         System.out.println("输入5位同学的数据");
     14         for(int i=1;i<6;i++)
     15         {
     16             System.out.println("input the Student number:");
     17             num=Integer.parseInt(buff.readLine());
     18             
     19             System.out.println("input the Student name:");
     20             name=buff.readLine();
     21             
     22             System.out.println("input the Student Score:");
     23             score=Integer.parseInt(buff.readLine());
     24             
     25             
     26             list.Insert(score, name, num);
     27                     
     28         }
     29         list.print();
     30         Node delNode=new Node("xiao ming",70,3);
     31         list.remove(delNode);
     32         list.print();
     33         
     34     }
     35 
     36 }
     37 
     38  class Node {
     39     String name;
     40     int score,no;
     41     Node next;
     42     
     43     //构造函数初始化,模拟单个小节点
     44     public Node(String name,int score,int no)
     45     {
     46         this.name=name;
     47         this.score=score;
     48         this.no=no;
     49         this.next=null;
     50     }
     51 }
     52 
     53 class LinkList{
     54     //这两个东西表示指针
     55     private Node first;
     56     private Node last;
     57     public boolean isEmpty()
     58     {
     59         return first==null;
     60     }
     61     
     62     public void Insert(int Data,String name,int np)
     63     {
     64         Node newNode=new Node(name,Data,np);
     65         if(this.isEmpty())
     66         {
     67             this.first=newNode;
     68             this.last=newNode;
     69         }
     70         else
     71         {
     72             this.last.next=newNode;
     73             last=newNode;
     74         }
     75     }
     76     public void remove(Node delNode)
     77     {    Node newNode,tmp;
     78         //分为最前,中间和末尾三种方式删除
     79         if(first.no==delNode.no)
     80         {
     81             first=first.next;
     82         }
     83         else if(delNode.no==last.no)
     84         {
     85             //删除最后一个节点
     86             newNode=first;
     87             while(newNode.next!=last)
     88                 newNode=newNode.next;
     89             newNode.next=last.next;//指针移动到倒数第二个节点位置上,更新最后一个节点
     90             last=newNode;
     91             
     92             
     93         }
     94         else
     95         {
     96             newNode=first;tmp=first;
     97             while(newNode.no!=delNode.no)
     98             {
     99                 tmp=newNode;//截止条件在于newNode.no=delNode.no
    100                 newNode=newNode.next;
    101                 
    102             }
    103             System.out.println("newNode"+newNode.name);
    104             tmp.next=delNode.next;
    105         }
    106     }
    107     public void print()
    108     {
    109         Node current=first;
    110         while(current!=null)
    111         {
    112             System.out.println("["+current.no+current.name+current.score+"]");
    113             current=current.next;
    114         }
    115         System.out.println();
    116     }
    117 }

     //对单向列表反向,特别注意其中的反向:

     1 package linklist;
     2 
     3 public class CH03_03 {
     4 
     5     public static void main(String[] args) {
     6         // TODO Auto-generated method stub
     7       //写入函数进行test
     8     }
     9 
    10 }
    11 
    12 class ReverseStudentLinkList extends StudentList{
    13     //子类继承父类运用父类的方法:
    14     public void print()
    15     {
    16         Node front=null;
    17         Node current=first; 
    18         Node nextNode=null;
    19         
    20         System.out.println("链表反向");
    21         while(current!=null)
    22         {
    23             nextNode=current.next;//保存正向的顺序
    24             current.next=front;
    25             //向前移动
    26             front=current;
    27             current=nextNode;
    28 
    29             
    30             
    31         }
    32         
    33         //查看书上的利用first,current,last节点
    34         while(current!=null)
    35         {
    36             last=front;
    37             front=current;
    38             current=current.next;
    39             front.next=last;
    40         }
    41         
    42         //j接下来直接遍历
    43         current=front;
    44         
    45         while(current!=null)
    46         {
    47             System.out.println("["+current.no+current.name+current.score);
    48             current=current.next;
    49         }
    50         System.out.println();
    51         
    52         
    53     }
    54     
    55 }
  • 相关阅读:
    Java实现 蓝桥杯VIP 算法提高 11-2删除重复元素
    Java实现 蓝桥杯VIP 算法提高 11-2删除重复元素
    Java实现 蓝桥杯VIP 算法提高 11-2删除重复元素
    Java实现 蓝桥杯VIP 算法提高 P0401
    Java实现 蓝桥杯VIP 算法提高 P0401
    Java实现 蓝桥杯VIP 算法提高 P0401
    Java实现 蓝桥杯VIP 算法提高 P0401
    Java实现 蓝桥杯VIP 算法提高 P0401
    QApplication::alert 如果窗口不是活动窗口,则会向窗口显示一个警告(非常好用,效果就和TeamViewer一样)
    QString::toWCharArray可以拷贝到宽字符串里
  • 原文地址:https://www.cnblogs.com/woainifanfan/p/6003350.html
Copyright © 2011-2022 走看看