//链表节点的定义
public class Node {
	int data;//数据域
	Node next;//递归定义
	public Node(int data) {
		
		this.data = data;
		this.next=null;
	}
	

	public static void main(String[] args) {

		Node a=new Node(1);
		Node b=new Node(2);
		Node c=new Node(3);
		Node d=new Node(4);
		Node e=new Node(5);
		//将a.next连的b上
		a.next=b;
		b.next=c;
		c.next=d;
		d.next=e;
		
		//根据前驱b删除后断c
		b.next=c.next;
		c.next=null;
		
		//根据前驱b插入后继c
		c.next=b.next;
		b.next=c;
		
		//链表遍历 已知第一个节点,遍历出所有节点
		Node p=a;
		while(p!=null){
			System.out.print(p.data+"--");
			p=p.next;//相当于数组i++
			
		}
		
		
	
	}
	
}

 

 

import java.util.Scanner;

public class 链栈 {
	public static void main(String[] args) {
		Node head = new Node(-1);
		Scanner input = new Scanner(System.in);
		for (int i = 1; i <= 5; i++) {
			System.out.println("请输入第" + i + "个节点的值");
			Node in = new Node(input.nextInt());
			in.next = head.next;
			head.next = in;
		}
		System.out.println("删除前:");
		// 遍历
		Node p = head.next;
		while (p != null) {
			System.out.print(p.data + "--");
			p = p.next;
		}

		System.out.println("请输入你要删除的节点值");
		int del = input.nextInt();

		p = head.next;
		Node q = head;
		while (p.data != del) {
			p = p.next;
			q = q.next;
		}
		// 根据q删p
		q.next = p.next;
		p.next = null;

		System.out.println("删除后:");
		p = head.next;
		while (p != null) {
			System.out.print(p.data + "--");
			p = p.next;
		}
	}

}

 

posted on 2016-10-18 13:50  天之城  阅读(118)  评论(0编辑  收藏  举报