首先链表分三种:
单链表,双链表,循环单链表
写个例子说明列表的创建和遍历:
-
publicclassLinkList{ privateNode head; privateNode current; privatevoid add(int data){ if(head ==null){ head =newNode(data,null); current = head; }else{ //创建新的结点 Node node =newNode(data,null); //新创建的节点和列表进行关联 current.next = node; //移动当前链表的索引位置 current = node; } } privatevoid print(Node node){ if(node ==null){ return; } Node current = node; while(current !=null){ System.out.println(current.data); current = current.next; } } /** * @ClassName: Node * @Description: TODO(定义Node数据类型) */ classNode{ int data; Node next; publicNode(){ } publicNode(int data,Node next){ super(); this.data = data; this.next = next; } } publicstaticvoid main(String[] args){ LinkList list =newLinkList(); for(int i =0; i <10; i++){ list.add(i); } list.print(list.head); } }
运行结果:
0
1
2
3
4
5
6
7
8
9
时间: 2024-12-28 22:47:17