
正文
单链表的删除java代码 删除单链表所有结点
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
在单链表中删除最小值结点,写出函数.(JAVA)
package testonly;
/**
* 删除单链表中所有最小的节点
* @author baidu an0011121
*
*/
public class LinkedListDemo {
/**
* 链表节点结构,直接在这里表示了,为了贴出来代码,你可以单独把这个节点类放到一个java文件中
* @author Administrator
*
*/
public static class Node {
public int data;
public Node next;
}
/**
* 核心方法,删除单链表中所有的最小的节点
* @param head
*/
public static void delMin(Node head) {
//寻找最小
Node current = head.next;
int min = current.data;
while (null != current) {
if (current.data min) {
min = current.data;
}
current=current.next;
}
//删除最小
Node previous = head;
current = head.next;
//两层循环为了删除所有最小
while(null!=current){
while (current.data != min) {
previous = current;
current = current.next;
}
previous.next=current.next;
current=previous.next;
}
}
/**
* 打印链表
* @param head
*/
public static void print(Node head){
String printStr="";
Node p=head.next;
while(null!=p){
printStr+=p.data+"--";
p=p.next;
}
System.out.println(printStr.substring(0,printStr.length()-3));
}
/**
* 构造链表
* @param arr
* @return
*/
public static Node construct(int[] arr){
Node headNode=new Node();
Node p=headNode;
for(int a:arr){
Node dataNode=new Node();
dataNode.data=a;
dataNode.next=null;
p.next=dataNode;
p=dataNode;
}
return headNode;
}
/**
* 主方法
* @param args
*/
public static void main(String[] args) {
int [] arr=new int[]{6,3,5,2,9,7,6,4,3,2,6,6,9,6,3,4,6,5,2};
//构造
Node head=construct(arr) ;
//删除最小
delMin(head);
//打印结果
print(head);
}
}
以上是刚写出来的。按照你的要求构造了单链表,然后测试了删除最小节点的方法。如果不符合你的要求或者哪块代码不明白,可以追问或者私信我。
相关问答
Q1: 用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;
}
Q2: 1、编程实现单链表的建立、插入、删除和查找算法,语言采用C或JAVA等。
/*P33用头插法建立带头结点单链表的删除java代码的单链表*/
#include
"stdio.h"
#define
NULL
#define
LEN
sizeof(linklist)
typedef
struct
node
{int
data;
struct
node
*next;
}linklist;
linklist
*head;
void
hhead_creat()/*用头插法建立带头结点单链表的删除java代码的单链表*/
{int
x;
linklist
*p;
head=(struct
node*)malloc(LEN);
head-data=-999;
head-next=NULL;
printf("\n\n\t\t请随机输入一组正整数以0作为结束符单链表的删除java代码:\n\n\t\t");
scanf("%d",x);
while(x!=0)
{
p=(struct
node*)malloc(LEN);
p-data=x;
p-next=head-next;
head-next=p;
scanf("%d",x);
}
}/*hrear_creat*/
void
print_linklist(head)/*打印该链表*/
linklist
*head;
{linklist
*p;
int
n=0;
p=head-next;
printf("\n\n\t\t");
while(p!=NULL)
{
printf("%5d",p-data);
p=p-next;
n=n+1;
if((n+1)%10==0)
printf("\n\t\t");
}
}/*print_linklist*/
main()
{hhead_creat(head);
print_linklist(head);
}
Q3: 用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代码和删除单链表所有结点的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。








