java单链表的删除代码 java单链表删除指定元素

1、编程实现单链表的建立、插入、删除和查找算法,语言采用C或JAVA等 。/*P33用头插法建立带头结点的单链表*/
#include
"stdio.h"
#define
NULL
#define
LEN
sizeof(linklist)
typedef
struct
node
{int
data;
struct
node
*next;
}linklist;
linklist
*head;
void
hhead_creat()/*用头插法建立带头结点的单链表*/
{int
x;
linklist
*p;
head=(struct
node*)malloc(LEN);
head-data=https://www.04ip.com/post/-999;
head-next=NULL;
printf("\n\n\t\t请随机输入一组正整数以0作为结束符:\n\n\t\t");
scanf("%d",x);
while(x!=0)
{
p=(struct
node*)malloc(LEN);
p-data=https://www.04ip.com/post/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("]",p-data);
p=p-next;
n=n 1;
if((n 1)==0)
printf("\n\t\t");
}
}/*print_linklist*/
main()
{hhead_creat(head);
print_linklist(head);
}
求java 单链表基本操作的实现/*
注意:链表的结点数量用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 = https://www.04ip.com/post/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 = https://www.04ip.com/post/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" 位置的结点成功!");
【java单链表的删除代码 java单链表删除指定元素】}
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 = https://www.04ip.com/post/null;
next = null;
}
public Node(Object data, Node next){
this.data = https://www.04ip.com/post/data;
this.next = next;
}
public Object getData(){
return this.data;
}
public void setData(Object data){
this.data = https://www.04ip.com/post/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(i0 || isize){
return false;
}
Node pre,curr;
int pos;
for(pre=head; i0pre.getNext()!=null; i--,pre=pre.getNext());
curr = new Node(elem, pre.getNext());
pre.setNext(curr);
size;
return true;
}
//删除i位置的元素
public boolean removeAt(int i) {
if(i0 || i = size){
return false;
}
Node pre,curr;
for(pre=head; i0pre.getNext()!=null; i--,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!=null; pos,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!=null; curr=curr.getNext()){
System.out.print(curr.getData()"\t");
}
System.out.println();
}
}
在单链表中删除最小值结点,写出函数.(JAVA)package testonly;
/**
* 删除单链表中所有最小的节点
* @author baidu an0011121
*
*/
public class LinkedListDemo {
/**
* 链表节点结构java单链表的删除代码,直接在这里表示java单链表的删除代码了,为了贴出来代码 , java单链表的删除代码你可以单独把这个节点类放到一个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.datamin) {
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=https://www.04ip.com/post/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);
}
}
以上是刚写出来的 。按照你的要求构造了单链表,然后测试了删除最小节点的方法 。如果不符合你的要求或者哪块代码不明白,可以追问或者私信我 。
用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 || i0 || iCount)
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 (i0 || iCount) {
return;
}
if (i == 0) {
head = head.next;
} else {
int n = 0;
point = head;
Node temp = point;
for (n = 0; ni; 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.vlauej.vlaue) {
int t = i.vlaue;
i.vlaue = j.vlaue;
j.vlaue = t;
}
}
}
}
}
class Node {
int vlaue;
Node next;
}
用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 || i0 || iCount)
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 (i0 || iCount) {
return;
}
if (i == 0) {
head = head.next;
} else {
int n = 0;
point = head;
Node temp = point;
for (n = 0; ni; 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.vlauej.vlaue) {
int t = i.vlaue;
i.vlaue = j.vlaue;
j.vlaue = t;
}
}
}
}
}
class Node {
int vlaue;
Node next;
}
java单链表的删除代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java单链表删除指定元素、java单链表的删除代码的信息别忘了在本站进行查找喔 。

    推荐阅读