无头单链表的创建

链表是一种链式存储的线性表, 用一组地址任意的存储单元 存放线性表的数据元素, 称为存储单元的一个结点

头指针:指向第一个结点的指针
单链表的定义
typedef struct Node{
	int value;//数据域
	struct Node *next;指针域
}Node;//结构体变量

初始化
void slistinit(Node **ppfirst){
	*ppfirst = NULL;  //*ppfirst ->结构体指针
}
创建一个新节点
Node* buynode(int d){
	Node* node = (Node*)malloc(sizeof(Node));
	assert(node);
	node->value = d;
	node->next = NULL;
	return node;
}
打印单链表
void slistprint(Node** ppfirst){
	Node* cur = *ppfirst;
	while (cur){
		printf("%d->", cur->value);
		cur = cur->next;
	}
	printf("NULL/n");
}
销毁单链表
void slistDestory(Node **ppfirst){
	assert(*ppfirst);
	while (*ppfirst!=NULL){
		Node* cur = *ppfirst;
		*ppfirst = (*ppfirst)->next;
		free(cur);
	}
}
删除指定结点
void slisterase(Node **ppfirst, Node *pos){
	assert(*ppfirst&&pos);
	Node *cur = *ppfirst;
	while (cur)
	{
		if (cur == pos){

			if (*ppfirst == pos){
				*ppfirst = (*ppfirst)->next;
			}
			else{
				cur->next = pos->next;
			}
		free(pos);
		}
		cur = cur->next;
	}
}
头插
void slistpushfront(Node **ppfirst, int v){
	assert(*ppfirst);
	Node* cur=buynode(v);
	cur->next=*ppfirst;
	*ppfirst = cur;
}
头删
void slistpopfront(Node **ppfirst){
	assert(*ppfirst);
	Node *node;
	node = *ppfirst;
	*ppfirst = (*ppfirst)->next;
	free(node);
}
尾插
void slistpushback(Node **ppfirst, int v){
	Node* cur=buynode(v);
	if (*ppfirst == NULL)
	{
		*ppfirst = cur;
		return;
	}
	Node* pre = *ppfirst;
	while (pre->next)
	{
		pre = pre->next;
	}
	pre->next = cur;
}
尾删
void slistpopback(Node **ppfirst){
	assert(*ppfirst);
	//存在一个结点时
	if ((*ppfirst)->next == NULL){
		*ppfirst = NULL;
		return;
	}
	//多个节点
	Node *cur = *ppfirst;
	while (cur->next->next){
		cur = cur->next;
	}
	free(cur->next);
	cur->next = NULL;
}
查找指定元素的结点
Node* slistfind(Node** ppfirst, int v){
	assert(*ppfirst);
	Node* cur;
	while (cur){
		if (cur->value == v){
			return cur;
		}
			cur = cur->next;
	}
	return NULL;
}

在指定位置前插入一个值
void insert(Node** ppfirst, int v,int pos){
	assert(*ppfirst&&pos);
	Node* pur = buynode(v);
	Node* cur = *ppfirst;
	while (cur->next!=NULL){
		if (cur->next = pos)
		{
			cur->next = pur;
			pur ->next = pos;
		}
		cur = cur->next;
	}
}
删除第一个值为v的元素
void slistremove(Node **ppfirst, int v){
	assert(*ppfirst);
	Node *pur = NULL;
	Node *pre = *ppfirst;
	while (pre)
	{
	//第一个节点值为v
		if (pre->value == v)
	{
		
				if(*ppfirst = pre)
				{
				*ppfirst=(*ppfirst)->next;
				}else{
				pur->next=pre->next;
				}
				free(pre);
				return;
			}
		}
		else{
			pur = pre;
			pre = pre->next;
		}
	}
}

删除所有值为v的元素
Node *slistremoveelements(Node **ppfirst, int v){
	assert(*ppfirst);
	Node *pre = NULL;
	Node *pur = *ppfirst;
	while (pur){
		if (pur->value == v){
			if (NULL == pre){
				*ppfirst = pur->next;
				free(pur);
				pur = *ppfirst;//重点
			}
			else{
				pre->next = pur->next;
				free(pur);
				pur = pre->next;
			}
		}
		else{
			pre = pur;
			pur = pur->next;

		}
	}
	return *ppfirst;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
无头单链表是一种常见的链表数据结构,在 Java 中可以使用自定义的节点类来实现。以下是一个简单的无头单链表实现示例: ```java class Node { int data; Node next; public Node(int data) { this.data = data; this.next = null; } } class LinkedList { Node head; public LinkedList() { this.head = null; } // 在链表头部插入一个新节点 public void insertAtHead(int data) { Node newNode = new Node(data); newNode.next = head; head = newNode; } // 在链表尾部插入一个新节点 public void insertAtTail(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; return; } Node current = head; while (current.next != null) { current = current.next; } current.next = newNode; } // 打印链表元素 public void printList() { Node current = head; while (current != null) { System.out.print(current.data + " "); current = current.next; } System.out.println(); } } public class Main { public static void main(String[] args) { LinkedList list = new LinkedList(); list.insertAtHead(3); list.insertAtHead(2); list.insertAtHead(1); list.printList(); // 输出: 1 2 3 list.insertAtTail(4); list.insertAtTail(5); list.printList(); // 输出: 1 2 3 4 5 } } ``` 在上述示例中,`Node` 类表示链表节点,包含一个整型数据 `data` 和一个指向下一个节点的引用 `next`。`LinkedList` 类表示链表本身,包含一个指向链表头部的引用 `head`。链表的操作包括在头部插入节点(`insertAtHead`)、在尾部插入节点(`insertAtTail`)和打印链表元素(`printList`)。在 `main` 方法中,我们创建一个链表实例并演示了插入节点和打印链表的操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值