注意:链表的结点数量用size表示,结点的位置为0~size-1
*/
import java.util.Scanner
public class Test7 {
public static void main(String[] args) {
try{
LinkList list = new LinkList()
Integer value
int pos = 0
Scanner input = new Scanner(System.in)
String choice = null
//卜禅测试A
while(true){
System.out.print("请输入待插入结点的值(x或X退出):")
choice = input.next()
if(choice.toUpperCase().equals("X")){
break
}
value = Integer.valueOf(choice)
if(list.addAt(pos, value) == true){
System.out.println("插入值为 " + value + " 的结点到当前链表成功!")
pos++
}
else{
System.out.println("插入结点失败!")
}
}
System.out.print("当前链表所有结点:")
list.listAll()
//测试B
while(true){
System.out.print("请输入待查询结点的值(x或X退型梁尘出):")
choice = input.next()
if(choice.toUpperCase().equals("X")){
break
}
value = Integer.valueOf(choice)
pos = list.findByValue(value)
if(pos == -1){
System.out.println("当前链表中不存在值为 " + value + " 的结点")
}
else{
System.out.println("值为 " + value + " 的结点在当前链表中的位置为 " + pos)
}
}
//测试C
while(true){
System.out.print("请输入待删除结点的位置[0~" + (list.getSize()-1) + "](x或X退出):")
choice = input.next()
if(choice.toUpperCase().equals("X")){
break
}
pos = Integer.valueOf(choice)
if(list.removeAt(pos) == true){
System.out.println("删除当前渣毕链表中 " + pos + " 位置的结点成功!")
}
else{
System.out.println("删除结点失败!")
}
}
System.out.print("当前链表所有结点:")
list.listAll()
}
catch(Exception e){
e.printStackTrace()
}
}
}
/**
* 链表结点类
*/
class Node{
private Object data //链表结点的数据域
private Node next //链表结点的指针域,指向直接后继结点
public Node(){
data = null
next = null
}
public Node(Object data, Node next){
this.data = data
this.next = next
}
public Object getData(){
return this.data
}
public void setData(Object data){
this.data = data
}
public Node getNext(){
return this.next
}
public void setNext(Node next){
this.next = next
}
}
/**
* 链表类
*/
class LinkList{
private Node head = null//头结点指针
private int size = 0
public LinkList(){
head = new Node()
size = 0
}
//在i位置插入元素elem
public boolean addAt(int i, Object elem) {
if(i <0 || i >size){
return false
}
Node pre,curr
int pos
for(pre=headi>0 &&pre.getNext()!=nulli--,pre=pre.getNext())
curr = new Node(elem, pre.getNext())
pre.setNext(curr)
size++
return true
}
//删除i位置的元素
public boolean removeAt(int i) {
if(i <0 || i >= size){
return false
}
Node pre,curr
for(pre=headi>0 &&pre.getNext()!=nulli--,pre=pre.getNext())
curr = pre.getNext()
pre.setNext(curr.getNext())
size--
return true
}
//根据值value查询结点是否存在,若存在返回位置,否则返回-1
public int findByValue(Object value){
Node curr
int pos
for(pos=0,curr=head.getNext()curr!=nullpos++,curr=curr.getNext()){
if(curr.getData().toString().equals(value.toString())){
break
}
}
if(curr==null){
return -1
}
return pos
//return (curr!=null ? pos : -1)
}
public int getSize(){
return size
}
public boolean isEmpty(){
return (size==0)
}
public void listAll(){
for(Node curr=head.getNext()curr!=nullcurr=curr.getNext()){
System.out.print(curr.getData() + "\t")
}
System.out.println()
}
}
java.util.Linkedlist是双弯袜向链表,当然也就包配闹埋括了单链表的功能,你可以去看他怎么写的啊public class SingleLinkedList<E>{
private Entry<E>first, last
private int size = 0
public void add(E element) {
Entry<E>newEntry = new Entry<培蚂E>(element, null)
if (first == null) {
first = last = newEntry
} else {
last.next = newEntry
last = newEntry
}
++size
}
public E get(int index) {
if (index <0 || index >= size)
throw new IndexOutOfBoundsException("Index: "+index+
", Size: "+size)
Entry<E>e = first
for (int i = 0i <index++i)
e = e.next
return e.data
}
private static class Entry<E>{
Entry(E data, Entry<E>next) {
this.data = data
this.next = next
}
E data
Entry<E>next
}
}
public class Link {Node head = null
Node point = null
Node newNode = null
public int Count = 0//统计值
//插入
public void AddNode(int t) {
newNode = new Node()
if (head == null) {
head = newNode
} else {
point = head
while (point.next != null) {
point = point.next
}
point.next = newNode
}
point = newNode
point.vlaue = t
point.next = null
Count++
}
//返回裂谨值
public int GetValue(int i) {
if (head == null || i <0 || i >Count)
return -999999
int n
Node temp = null
point = head
for (n = 0n <= in++) {
temp = point
point = point.next
}
return temp.vlaue
}
//删除
public void DeleteNode(int i) {
if (i <肆陪基 0 || i >Count) {
return
}
if (i == 0) {
head = head.next
} else {
int n = 0
point = head
Node temp = point
for (n = 0n <in++) {
temp = point
point = point.next
}
temp.next = point.next
}
Count--
}
//排序
public void Sotr() {
for (Node i = headi != nulli = i.next) {
for (Node j = i.nextj != nullj = j.next) {
if (i.vlaue >乱陵 j.vlaue) {
int t = i.vlaue
i.vlaue = j.vlaue
j.vlaue = t
}
}
}
}
}
class Node {
int vlaue
Node next
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)