「java单链表排序」单链表归并排序 java
今天给各位分享java单链表排序的知识,其中也会对单链表归并排序 java进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、用java编写程序实现单链表,要提供插入,删除,排序,统计等功能,链表节点中的数据要求是整数。
- 2、JAVA怎样用链表来实现冒泡排序,我需要整个编程过程,谢谢
- 3、单链表双非递减排序怎么算
- 4、Java合并两个排序的链表问题(剑指offer)
用java编写程序实现单链表,要提供插入,删除,排序,统计等功能,链表节点中的数据要求是整数。
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 = 0; n = i; n++) {
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 = 0; n i; n++) {
temp = point;
point = point.next;
}
temp.next = point.next;
}
Count--;
}
//排序
public void Sotr() {
for (Node i = head; i != null; i = i.next) {
for (Node j = i.next; j != null; j = j.next) {
if (i.vlaue j.vlaue) {
int t = i.vlaue;
i.vlaue = j.vlaue;
j.vlaue = t;
}
}
}
}
}
class Node {
int vlaue;
Node next;
}
JAVA怎样用链表来实现冒泡排序,我需要整个编程过程,谢谢
Java程序:
import java.util.Scanner;
public class TesLinkList {
public static void main(String[] args) {
LinkList list = new LinkList();
Scanner scan = new Scanner(System.in);
int i, size;
System.out.print("请输入链表的大小:");
size = scan.nextInt();
for(i=0; isize; i++){
list.addAt(i, scan.nextInt());
}
System.out.println("链表结点一览:");
list.listAll();
list.bubbleSort();
System.out.println("排序后的链表结点一览:");
list.listAll();
}
}
/**
* 链表结点类
*/
class Node{
private int data;
private Node next; //链表结点的指针域,指向直接后继结点
public Node(){
next = null;
}
public Node(int data, Node next){
this.data = data;
this.next = next;
}
public int getData(){
return this.data;
}
public void setData(int 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, int elem) {
if(i 0 || i size){
return false;
}
Node pre,curr;
int pos;
for(pre=head; i0 pre.getNext()!=null; i--,pre=pre.getNext());
curr = new Node(elem, pre.getNext());
pre.setNext(curr);
size++;
return true;
}
public Node getHead(){
return this.head;
}
public void setHead(Node head){
this.head = head;
}
public int getSize(){
return this.size;
}
public boolean isEmpty(){
return (size==0);
}
public void listAll(){
for(Node curr=head.getNext(); curr!=null; curr=curr.getNext()){
System.out.print(curr.getData() + "\t");
}
System.out.println();
}
public void bubbleSort(){
Node p, q;
int temp;
for(p=head.getNext(); p.getNext()!=null; p=p.getNext()){
for(q=head.getNext(); q.getNext()!=null; q=q.getNext()){
if(q.getData() q.getNext().getData()){
temp = q.getData();
q.setData(q.getNext().getData());
q.getNext().setData(temp);
}
}
}
}
}
运行测试:
请输入链表的大小:4
1
2
4
3
链表结点一览:
1 2 4 3
排序后的链表结点一览:
1 2 3 4
单链表双非递减排序怎么算
1、首先将双非递减排序的公式写出来。
2、其次将递减排序的单链表归并为一个非递增次序排列的单链表。
3、最后计算表长即可。
Java合并两个排序的链表问题(剑指offer)
1、先将两个链表分别进行各自排序。如果题目已说明原来的两个链表是已经排好序的话,此步可以省略。
2、新建一个空链表,按照顺序(或者由小到大或者由大到小),依次将两个链表的数据排列到新的链表中。
这样最后得到的链表就是最终合并的链表。
java单链表排序的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于单链表归并排序 java、java单链表排序的信息别忘了在本站进行查找喔。
发布于:2022-12-16,除非注明,否则均为
原创文章,转载请注明出处。