链表(next)和节点(node)

这篇JAVA日常日记探讨了链表的基础操作,包括如何在链表末尾、首个节点后面以及指定位置插入节点。文章还介绍了使用假节点简化算法的方法,以及链表中的节点删除。此外,还提到了ArrayList作为容器类在Java中的应用,强调其数组链表特性,并展示了如何在main方法中创建和使用Student类。

JAVA 日常日记 基础篇

类
package Learn;

public class Monkey
{

	public int id; //  编号
	public String name; //  名字
	public Monkey next; //  它后面的猴子

	public Monkey(int id, String name)
	{
		this.id = id;
		this.name = name;
	}
	
	//Java里的对象是默认不能打印显示的,否则就会出现 my.Monkey@6d06d69c 类似的字样。(类名+对象地址)。此时我们应在Monkey类重写一下toString()方法,以便能以正常的字符串显示
	@Override
	public String toString()
	{
		return String.format("(%s, %s)", name, id);
	}
}

// 链表 节点(node)插入末端

	Monkey s1=new Monkey(5,"爱");
	Monkey s2=new Monkey(2,"千");
	Monkey s3=new Monkey(0,"惠");
	
	s1.next=s2;
	s2.next=s3;
	s3.next=null;
	
	Monkey tail =m1;
	while(tail.next != null)  //如果node.next 等于null  则找到尾巴 不执行   
	{
	tail=tail.next;
	}
	Monkey s4=new Monkey (1314,"sor")
	tail.next=s4;
	}

// 链表 节点(node)插入到首个节点后面

	Monkey s1=new Monkey(5,"爱");
	Monkey s2=new Monkey(2,"千");
	Monkey s3=new Monkey(0,"惠");
	
	s1.next=s2;
	s2.next=s3;
	s3.next=null;
	
	
### C语言中链表删除节点添加节点的实现方法 在C语言中,链表是一种动态数据结构,其节点通过指针连接。以下将详细介绍如何实现链表节点的删除添加操作,并提供相应的代码示例。 #### 删除链表节点 要删除链表中的某个节点,需要找到该节点的前驱节点,并将其指针指向被删除节点的下一个节点。以下是具体实现代码: ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点结构 typedef struct Node { int data; struct Node* next; } Node; // 删除指定值的节点 void deleteNode(Node** head_ref, int key) { Node* temp = *head_ref; // 保存头节点 Node* prev = NULL; // 如果头节点就是要删除的节点 if (temp != NULL && temp->data == key) { *head_ref = temp->next; // 修改头节点 free(temp); // 释放旧头节点内存 return; } // 遍历链表,查找要删除的节点 while (temp != NULL && temp->data != key) { prev = temp; temp = temp->next; } // 如果未找到要删除的节点 if (temp == NULL) return; // 断开链接并释放节点内存 prev->next = temp->next; free(temp); } ``` 上述代码实现了删除指定值的节点功能[^1]。 #### 添加链表节点链表中添加新节点通常包括两种情况:在链表头部插入或在链表尾部插入。以下是具体实现代码: 1. **在链表头部插入节点**: ```c // 在链表头部插入新节点 void push(Node** head_ref, int new_data) { // 分配新节点 Node* new_node = (Node*)malloc(sizeof(Node)); // 初始化新节点 new_node->data = new_data; new_node->next = (*head_ref); // 修改头节点指针 (*head_ref) = new_node; } ``` 2. **在链表尾部插入节点**: ```c // 在链表尾部插入新节点 void append(Node** head_ref, int new_data) { // 分配新节点 Node* new_node = (Node*)malloc(sizeof(Node)); Node* last = *head_ref; // 用于遍历的指针 // 初始化新节点 new_node->data = new_data; new_node->next = NULL; // 如果链表为空,则新节点成为头节点 if (*head_ref == NULL) { *head_ref = new_node; return; } // 遍历链表直到找到最后一个节点 while (last->next != NULL) last = last->next; // 将新节点添加到链表末尾 last->next = new_node; } ``` 上述代码展示了如何在链表头部或尾部插入新节点[^2]。 #### 示例完整代码 以下是一个完整的链表操作示例,包含添加删除节点的功能: ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点结构 typedef struct Node { int data; struct Node* next; } Node; // 打印链表 void printList(Node* node) { while (node != NULL) { printf(" %d ", node->data); node = node->next; } } // 在链表头部插入新节点 void push(Node** head_ref, int new_data) { Node* new_node = (Node*)malloc(sizeof(Node)); new_node->data = new_data; new_node->next = (*head_ref); (*head_ref) = new_node; } // 在链表尾部插入新节点 void append(Node** head_ref, int new_data) { Node* new_node = (Node*)malloc(sizeof(Node)); Node* last = *head_ref; new_node->data = new_data; new_node->next = NULL; if (*head_ref == NULL) { *head_ref = new_node; return; } while (last->next != NULL) last = last->next; last->next = new_node; } // 删除指定值的节点 void deleteNode(Node** head_ref, int key) { Node* temp = *head_ref; Node* prev = NULL; if (temp != NULL && temp->data == key) { *head_ref = temp->next; free(temp); return; } while (temp != NULL && temp->data != key) { prev = temp; temp = temp->next; } if (temp == NULL) return; prev->next = temp->next; free(temp); } int main() { Node* head = NULL; append(&head, 6); push(&head, 7); push(&head, 1); append(&head, 4); printf("Created Linked List: "); printList(head); deleteNode(&head, 7); printf("\nLinked List after Deletion of 7: "); printList(head); return 0; } ``` 上述代码展示了如何创建链表、打印链表、添加节点以及删除节点[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值