假如我创建一个双向链表 a
存储的data是String
“a”-"b"-"c"
这个样子,a的size是3
我写了一个remove的程序, 让 b.previoud.next = b.next;
如果输出新的链表a, 结果是 “a”-“c”
但是a的size仍然是3,请问怎么办?
java怎么在双向链表中删除元素?
答案:2 悬赏:40 手机版
解决时间 2021-03-21 17:06
- 提问者网友:嘚啵嘚啵
- 2021-03-21 03:33
最佳答案
- 五星知识达人网友:几近狂妄
- 2021-03-21 04:31
你自己写的remove方法吗?
在你写的remove方法里size--
在你写的remove方法里size--
全部回答
- 1楼网友:空山清雨
- 2021-03-21 06:03
双向链表:就是有双向指针,即双向的链域。
链结点的结构:
┌────┬────┬────────┐
│ data │ next │ previous │
└────┴────┴────────┘
双向链表不必是双端链表(持有对最后一个链结点的引用),双端链表插入时是双向的。
有两条链:一条从头到尾,一条从尾到头,删除遍历时也是双向的。
public class doublylinkedlist {
private link head; //首结点
private link rear; //尾部指针
public doublylinkedlist() { }
public t peekhead() {
if (head != null) {
return head.data;
}
return null;
}
public boolean isempty() {
return head == null;
}
public void insertfirst(t data) {// 插入 到 链头
link newlink = new link(data);
if (isempty()) {//为空时,第1次插入的新结点为尾结点
rear = newlink;
} else {
head.previous = newlink; //旧头结点的上结点等于新结点
}
newlink.next = head; //新结点的下结点旧头结点
head = newlink; //赋值后,头结点的下结点是旧头结点,上结点null
}
public void insertlast(t data) {//在链尾 插入
link newlink = new link(data);
if (isempty()) {
head = newlink;
} else {
rear.next = newlink;
}
newlink.previous = rear;
rear = newlink; //赋值后,尾结点的上结点是旧尾结点,下结点null
}
public t deletehead() {//删除 链头
if (isempty()) return null;
link temp = head;
head = head.next; //变更首结点,为下一结点
if (head != null) {
head.previous = null;
} else {
rear = null;
}
return temp.data;
}
public t deleterear() {//删除 链尾
if (isempty()) return null;
link temp = rear;
rear = rear.previous; //变更尾结点,为上一结点
if (rear != null) {
rear.next = null;
} else {
head = null;
}
return temp.data;
}
public t find(t t) {//从头到尾find
if (isempty()) {
return null;
}
link find = head;
while (find != null) {
if (!find.data.equals(t)) {
find = find.next;
} else {
break;
}
}
if (find == null) {
return null;
}
return find.data;
}
public t delete(t t) {
if (isempty()) {
return null;
}
link current = head;
while (!current.data.equals(t)) {
current = current.next;
if (current == null) {
return null;
}
}
if (current == head) {
head = head.next;
if (head != null) {
head.previous = null;
}
} else if (current == rear) {
rear = rear.previous;
if (rear != null) {
rear.next = null;
}
} else {
//中间的非两端的结点,要移除current
current.next.previous = current.previous;
current.previous.next = current.next;
}
return current.data;
}
public boolean insertafter(t key, t data) {//插入在key之后, key不存在return false
if (isempty()) {
return false;
}
link current = head;
while (!current.data.equals(key)) {
current = current.next;
if (current == null) {
return false;
}
}
link newlink = new link(data);
if (current == rear) {
rear = newlink;
} else {
newlink.next = current.next;
current.next.previous = newlink;
}
current.next = newlink;
newlink.previous = current;
return true;
}
public void displaylist4head() {//从头开始遍历
system.out.println("list (first-->last):");
link current = head;
while (current != null) {
current.displaylink();
current = current.next;
}
}
public void displaylist4rear() {//从尾开始遍历
system.out.println("list (last-->first):");
link current = rear;
while (current != null) {
current.displaylink();
current = current.previous;
}
}
class link {//链结点
t data; //数据域
link next; //后继指针,结点 链域
link previous; //前驱指针,结点 链域
link(t data) {
this.data = data;
}
void displaylink() {
system.out.println("the data is " + data.tostring());
}
}
public static void main(string[] args) {
doublylinkedlist list = new doublylinkedlist();
list.insertlast(1);
list.insertfirst(2);
list.insertlast(3);
list.insertfirst(4);
list.insertlast(5);
list.displaylist4head();
integer deletehead = list.deletehead();
system.out.println("deletehead:" + deletehead);
list.displaylist4head();
integer deleterear = list.deleterear();
system.out.println("deleterear:" + deleterear);
list.displaylist4rear();
system.out.println("find:" + list.find(6));
system.out.println("find:" + list.find(3));
system.out.println("delete find:" + list.delete(6));
system.out.println("delete find:" + list.delete(1));
list.displaylist4head();
system.out.println("----在指定key后插入----");
list.insertafter(2, 8);
list.insertafter(2, 9);
list.insertafter(9, 10);
list.displaylist4head();
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯