单链表的建立
来源:互联网 发布:网络利大于弊的提问 编辑:程序博客网 时间:2024/05/17 00:08
建立结点
package com.huachao;
private int data;
private LinkedListNode link;
public LinkedListNode getLink() {
return link;
}
public void setLink(LinkedListNode link) {
this.link = link;
}
LinkedListNode(int data){
this.data = data;
}
public int getData(){
return data;
}
public int setData(){
return data;
}
}
链表的创建插入和删除
package com.huachao;
import java.util.Scanner;
public class LinkedList {
LinkedListNode head;
private int length;
public int getlength(){
return length;
}
void create(){
Scanner in = new Scanner(System.in);
head = new LinkedListNode(in.nextInt());
length++;
LinkedListNode p = head;
int data;
while((data = in.nextInt() )!= -1){
LinkedListNode q = new LinkedListNode(data);
p.setLink(q);
p = q;
length++;
}
in.close();
}
boolean insert(int pos, int data){
int i = 0;
LinkedListNode p = head;
LinkedListNode q;
if(pos >= length){
System.out.print("dd");
return false;
}else{
while(i<pos){
++i;
p = p.getLink();
}
q =p.getLink() ;
p.setLink( new LinkedListNode(data) );
p = p.getLink();
p.setLink(q);
}
return true;
}
boolean delete(int pos){
int j = 0;
LinkedListNode p = head;
while(j<pos){
++j;
p = p.getLink();
}
p.setLink(p.getLink().getLink());
return true;
}
void print(){
LinkedListNode p = head;
while(p.getLink() != null){
System.out.print(p.getData() + " ");
p = p.getLink();
}
System.out.print(p.getData() + " ");
//System.out.print("length" + length);
}
}
测试:
package com.huachao;
public class Test {
public static void main(String[] args) {
LinkedList l = new LinkedList();
l.create();
l.print();
l.insert(1, 2);
//System.out.println(l.getlength());
l.print();
l.delete(1);
l.print();
}
}
- 单链表的建立
- 单链表的建立
- 单链表的建立
- 单链表的建立
- 循环单链表的建立
- 单链表的建立程序
- 单链表的建立
- 单链表的建立
- 建立排序的单链表
- 单链表的建立
- 有序单链表的建立
- 单链表的建立
- 单链表的建立
- 单链表的建立
- 单链表的建立
- 单链表的建立
- 单链表的建立
- 单链表(单链表的建立)
- 线程的调度-守护线程
- if语句的在括号和中括号的正确赋值操作
- 调用系统相机与图库
- POJ 2355 Railway tickets (线性dp)
- 线程的调度-合并
- 单链表的建立
- bootstrap布局常用,兼容ie(6还有点问题)
- MFC基本控件使用——按钮(Button)
- 如何学好acm
- 线程的调度-优先级
- github初体验
- ShareSDK分享记录
- 线程的调度-休眠
- ocx中fireevent注意要点