写一个简单的Linkedlist,实现增删改查

linkedlist和arraylist一样都是实现了list,只是它们底层不同,一个是数组一个是链表,就造就了它们的一些特性不同

linkedlist增删快,查找慢。主要是因为链表查找要将链表遍历一边找到该数据输入。当要删除时,只需要将其结点的前后两个结点连接起来即可

package Array;

import java.util.NoSuchElementException;

public class MyLinkedList<T> {
private class Node<T>{
	T t;
	Node<T> next;//后继
	Node<T> prev;//前驱
	Node(T t){
		this.t=t;
	}
}
Node<T> frist;
Node<T> last;
int size;//链表长度
public MyLinkedList() {
	frist=last=null;
}
//加一个元素
public void add(T t) {
	Node<T> n=new Node<T>(t);
	if(last==null) {
		frist=n;
		last=n;
	}else {
	last.next=n;//最后一个元素下一个为此次添加的元素
	n.prev=last;//添加元素的前驱指前一个元素
	last=n;//此时最后一个元素为刚添加进入的
	}
	size++;
}
//添加元素到第一个
public void addFrist(T t) {
	Node<T> f=new Node<T>(t);	
	if(last==null) {
		frist=f;
		last=f;
	}else {
	f.next=frist;
	frist.prev=f;
	frist=f;
	}
	size++;
}
//将元素添加到最后一个位置
public void linkedFrist(T t) {
	add(t);
}
//删除第一个元素
public void deleteFrist() {
	if(size<1) {
		throw new NoSuchElementException("linked为空,无法进行此操作");
	}
 frist.next.prev=null;
 frist=frist.next;
 size--;
}
//删除最后一个元素
public void deleteLast() {
	if(size<1) {
		throw new NoSuchElementException("linked为空,无法进行此操作");
	}
last=last.prev;
last.next=null;
size--;
}
//删除指定元素
public void delete(T t) {
	Node<T> n=findNode(t);
	n.prev.next=n.next;
	n.next.prev=n.prev;
	size--;
}
//删除指定位置的元素
public void delete(int index) {
	Node<T> n=findex(index);
	n.prev.next=n.next;
	n.next.prev=n.prev;
	size--;
}
//将更改第一个元素,返回其原来的元素
public T setFrist(T t) {
	T t1=frist.t;
	frist.t=t;
	return t1;
}
//更改最后一个元素
public T setLast(T t) {
	T t1=last.t;
	last.t=t;
	return t1;
}
//更改指定位置的元素,返回该位置的元素
public T set(int index,T t) {
	Node<T> n=findex(index);//获取这个位置的结点
	T t1=n.t;
	n.t=t;
	return t1;
}
//获取第一个元素
public T getFrist() {
	if(size()==0) {
		throw new NoSuchElementException("linkedlist为空,无法进行此操作");
	}
	return frist.t;
}
//获取最后一个元素
public T getLast() {
	if(size()==0) {
		throw new NoSuchElementException("linkedlist为空,无法进行此操作");
	}
	return last.t;
}
//获取指定位置的元素
public T get(int index) {
	Node<T> n=findex(index);
	return n.t;
}
//输入指定元素,获取其结点
private Node<T> findNode(T t){
	Node<T> n=frist;
	for(int i=0;i<size;i++) {
		if(n.t.equals(t)) {
			return n;
		}
		n=n.next;
	}
	throw new NoSuchElementException("linkedlist无此元素,无法进行此操作");
}
//输入指定位置,获取其结点
private Node<T> findex(int index) {
	if(index<1||index>size()) {
		
		throw new NoSuchElementException("linkedlist输入错误,无法进行此操作:"+index);
	}
	Node<T> n=frist;
	for(int i=0;i<size;i++) {
		if(i==index-1) {
			return n;
		}
		n=n.next;
	}
	return null;
}
//判断是否为空
public boolean isEmpty() {
	return size()==0;
}
//判断长度
public int size() {
	return size;
}
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的 C++ 代码实现实现了对链表的增删改查功能: ```cpp #include <iostream> using namespace std; // 链表节点的定义 struct ListNode { int val; ListNode* next; ListNode(int x) : val(x), next(NULL) {} }; // 链表的类定义 class LinkedList { public: // 构造函数,初始化链表为空 LinkedList() { head = NULL; } // 在链表尾部插入一个节点 void insert(int val) { ListNode* newNode = new ListNode(val); if (head == NULL) { head = newNode; } else { ListNode* cur = head; while (cur->next != NULL) { cur = cur->next; } cur->next = newNode; } } // 删除指定位置上的节点 bool remove(int index) { if (index < 0) { return false; } if (index == 0) { ListNode* tmp = head; head = head->next; delete tmp; return true; } ListNode* cur = head; for (int i = 0; i < index - 1; i++) { if (cur->next == NULL) { return false; } cur = cur->next; } ListNode* tmp = cur->next; if (tmp == NULL) { return false; } cur->next = tmp->next; delete tmp; return true; } // 修改指定位置上节点的值 bool update(int index, int val) { ListNode* cur = head; for (int i = 0; i < index; i++) { if (cur == NULL) { return false; } cur = cur->next; } if (cur == NULL) { return false; } cur->val = val; return true; } // 查找指定位置上节点的值 int get(int index) { ListNode* cur = head; for (int i = 0; i < index; i++) { if (cur == NULL) { return -1; } cur = cur->next; } if (cur == NULL) { return -1; } return cur->val; } private: ListNode* head; }; int main() { LinkedList list; list.insert(1); list.insert(2); list.insert(3); cout << "get(0) = " << list.get(0) << endl; cout << "get(1) = " << list.get(1) << endl; cout << "get(2) = " << list.get(2) << endl; list.update(1, 4); cout << "get(0) = " << list.get(0) << endl; cout << "get(1) = " << list.get(1) << endl; cout << "get(2) = " << list.get(2) << endl; list.remove(1); cout << "get(0) = " << list.get(0) << endl; cout << "get(1) = " << list.get(1) << endl; return 0; } ``` 这个代码实现了链表的增删改查功能,包括: - 在链表尾部插入一个节点。 - 删除指定位置上的节点。 - 修改指定位置上节点的值。 - 查找指定位置上节点的值。 其中,链表的节点使用结构体 `ListNode` 定义,包含了节点的值 `val` 和指向下一个节点的指针 `next`。链表的类定义为 `LinkedList`,其中包含了链表头节点的指针 `head`,以及上述四个操作的具体实现。在 `main` 函数中,我们创建了一个链表对象 `list`,并对其进行了一些操作,来验证链表的实现是否正确。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值