Java实现单向链表基础-单向链表的反转

设计一个Java程序,来实现学生成绩按照学号反转打印出来。

package LinkList;

public class Node {
	int data;
	int np;
	String names;
	Node next;
	
	public Node(int data,int np,String names) {
		this.np=np;
		this.data=data;
		this.names=names;
		this.next=null;
	}
	public Node first;
	public Node last;
	public boolean isEmpty() {
		return first==null;
	}
	public void print() {
		Node current=first;
		while(current!=null) {
			System.out.println("["+current.data+" "+current.names+" "+current.np+"]");
			current=current.next;
		}
		System.out.println();
	}
	public void insert(int data,String names,int np) {
		Node newNode=new Node(data,np,names);
		
		if(this.isEmpty()) {
			first=newNode;
			last=newNode;
		}else {
			last.next=newNode;
			last=newNode;
		}
		
	}
}

package LinkList;

public class reverse extends LinkList{

	
	public void reverse_print() {
		Node current=first;
		Node before=null;
		System.out.println("反转后的链表数据:");
		while(current!=null) {
			last=before;
			before=current;
			current=current.next;
			before.next=last;
			
		}
		current=before;
		while(current!=null) {
			System.out.println("["+current.data+" "+current.names+" "+current.np+"]");
			current=current.next;
		}
		
		System.out.println();
	}
}

package LinkList;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;

public class ReverseStu {

	public static void main(String args[]) throws IOException{
	BufferedReader buf;
	buf=new BufferedReader(new InputStreamReader(System.in));
	Random rand=new Random();
	reverse list =new reverse();
	
    int i,j,findword=0,data[][]=new int[12][10];
    String name[]=new String[] {"Allen","Scott","Marry","Jon","Mark","Ricky","Lisa","Jasica","Hanson","Amy","Bob","Jack"};
    System.out.println("学号\t成绩\t学号\t成绩\t学号\t成绩\t学号\t成绩\n ");
    System.out.println("单向链表1:");
    for(i=0;i<12;i++)
    {
    	data[i][0]=i+1;
		data[i][1]=(Math.abs(rand.nextInt(50)))+50;
		list.insert(data[i][0],data[i][1],name[i]);
	}
    for (i=0;i<3;i++)
	{
		for(j=0;j<4;j++)
		System.out.print("["+data[j*3+i][0]+"]  ["+data[j*3+i][1]+"]  ");
		System.out.println();
	}
    
    	list.reverse_print();
    }
	}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值