无头单链表

目录

一、单链表结构的定义

二、单链表结点的创建

三、单链表打印

四、单链表尾插

五、单链表头插

六、单链表尾删

七、单链表头删

九、单链表任意位置插入

十、单链表任意位置删除

十一、单链表任意位置后插入

十二、单链表任意位置后删除

十三、单链表任意位置插入(不提供链表头结点)


一、单链表结构的定义

//单链表结构的定义
typedef int SLNDataType;//数据类型

typedef struct SListNode {
	SLNDataType val;//结点数据域
	struct SListNode* next;//结点指针域
}SLNode;

二、单链表结点的创建

//单链表结点的创建
SLNode* CreateNode(SLNDataType x)
{
	SLNode* newnode = (SLNode*)malloc(sizeof(SLNode));//为新结点申请空间
	if (newnode == NULL)//申请失败
	{
		perror("malloc fail");
		exit(-1);
	}
	newnode->val = x;//新结点数据域
	newnode->next = NULL;//新结点指针域
	return newnode;
}

三、单链表打印

//单链表的打印
void SLTPrint(SLNode* phead)
{
	SLNode* cur = phead;
	while (cur)
	{
		printf("%d->", cur->val);
		cur = cur->next;
	}
	if (cur == NULL)
		printf("NULL\n");
}

四、单链表尾插

//单链表的尾插
void SLTPushBack(SLNode** pphead, SLNDataType x)
{
    assert(pphead);
	SLNode* newnode = CreateNode(x);
	//链表为空
	if (*pphead == NULL)
	{
		*pphead = newnode;
	}
	else//链表不为空
	{
		SLNode* tail = *pphead;
		while (tail->next)//找尾结点
		{
			tail = tail->next;
		}
		tail->next = newnode;//尾插
	}
}

五、单链表头插

//单链表头插
void SLTPushFront(SLNode** pphead, SLNDataType x)
{
    assert(pphead);
	SLNode* newnode = CreateNode(x);

	newnode->next = *pphead;
	*pphead = newnode;

    //SLNode* newnode = CreateNode(x);
	链表为空
	//if (*pphead == NULL)
	//{
	//	*pphead = newnode;
	//}
	//else//链表不为空
	//{
	//	SLNode* tmp = *pphead;
	//	*pphead = newnode;
	//	(*pphead)->next = tmp;
	//}
}

六、单链表尾删

//单链表尾删
void SLTPopBack(SLNode** pphead)
{
	assert(pphead);
	assert(*pphead);//链表不能为空
	
	if ((*pphead)->next == NULL)//链表只有一个结点
	{
		free(*pphead);
		*pphead = NULL;
	}
	else//链表有多个结点
	{
		SLNode* tail = *pphead;
		SLNode* prev = NULL;
		while (tail->next != NULL)
		{
			prev = tail;
			tail = tail->next;
		}
		prev->next = NULL;
		free(tail);
		tail = NULL;
	}
}

七、单链表头删

//单链表头删
void SLTPopFront(SLNode** pphead)
{
	assert(pphead);
	assert(*pphead);
	//SLNode* tmp = *pphead;
	//*pphead = (*pphead)->next;
	//free(tmp);
	//tmp = NULL;
	SLNode* tmp = (*pphead)->next;
	free(*pphead);
	*pphead = tmp;
}

八、单链表查找(返回找到的结点)

//单链表查找(返回找到的结点)
SLNode* SLTFind(SLNode* phead, SLNDataType x)
{
	if (phead == NULL)
		return NULL;
	else
	{
		SLNode* cur = phead;
		while (cur)
		{
			if (cur->val == x)
				return cur;
			cur = cur->next;
		}
		return cur;//return NULL;
	}
}

九、单链表任意位置插入

//单链表任意位置插入
void SLTInsert(SLNode** pphead, SLNode* pos, SLNDataType x)
{
	assert(pphead);
	assert(pos);//必须在有效结点处插入
	assert(*pphead);//如果pos是有效结点,那么链表一定不为空

	SLNode* newnode = CreateNode(x);
	if (*pphead == pos)//插入位置在头结点处
	{
		//头插
		SLTPushFront(pphead, x);
	}
	else
	{
		SLNode* cur = *pphead;
		while (cur->next != pos)//找插入位置的前一结点
		{
			cur = cur->next;
		}
		newnode->next = cur->next;
		cur->next = newnode;
	}
}

十、单链表任意位置删除

//单链表任意位置删除
void SLTErase(SLNode** pphead, SLNode* pos)
{
	assert(pphead);
	assert(pos);//删除的必须是有效结点
	assert(*pphead);//链表不能为空

	if (*pphead == pos)//删除的结点是头结点
	{
		//头删
		SLTPopFront(pphead);
	}
	else
	{
		SLNode* cur = *pphead;

		while (cur->next != pos)//找删除结点的前一结点
		{
			cur = cur->next;
		}
		cur->next = pos->next;
		free(pos);
		pos = NULL;
	}
}

十一、单链表任意位置后插入

//单链表任意位置后插入
void SLTInsertAfter(SLNode* pos, SLNDataType x)
{
	assert(pos);//必须是有效结点

	SLNode* newnode = CreateNode(x);
	newnode->next = pos->next;
	pos->next = newnode;
}

十二、单链表任意位置后删除

//单链表任意位置后删除
void SLTEraseAfter(SLNode* pos)
{
	assert(pos);//必须是有效结点
	assert(pos->next);//如果pos是最后一个结点,那么pos的后一位置为空

	SLNode* tmp = pos->next;
	pos->next = tmp->next;
	free(tmp);
	tmp = NULL;
}

十三、单链表任意位置插入(不提供链表头结点)

先进行任意位置后插入,再交换两个结点的值

//单链表任意位置插入(不提供链表头结点)
void SLTInsertNohead(SLNode* pos, SLNDataType x)
{
	assert(pos);

	SLTInsertAfter(pos, x);//先在pos位置后插入

	pos->next->val = pos->val;
	pos->val = x;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 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
发出的红包

打赏作者

南林yan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值