无头单向不循环链表和带头双向循环链表

无头单向不循环链表
#include <stdio.h>

#include <stdlib.h>



// slist.h

typedef int SLTDateType;

typedef struct SListNode

{

SLTDateType data;

struct SListNode* next;

}SListNode;



// 动态申请一个节点

SListNode* BuySListNode(SLTDateType x)

{

SListNode* node = (SListNode*)malloc(sizeof(SListNode));

node->data = x;

node->next = NULL;

return node;

}



// 单链表打印

void SListPrint(SListNode* plist)

{

SListNode* cur = plist;

while (cur) {

printf("%d ", cur->data);

cur = cur->next;

}

printf("\n");

}



// 单链表尾插

void SListPushBack(SListNode** pplist, SLTDateType x)

{

SListNode* node = (SListNode*)malloc(sizeof(SListNode));

node->data = x;

SListNode* cur = *pplist;

if (*pplist == NULL) (*pplist) = node;

else {

while (cur->next) {

cur = cur->next;

}

cur->next = node;

cur = cur->next;

cur->next = NULL;

}

}



// 单链表的头插

void SListPushFront(SListNode** pplist, SLTDateType x)

{

SListNode* node = (SListNode*)malloc(sizeof(SListNode));

node->data = x;

if (*pplist == NULL) *pplist = node;

else {

node->next = *pplist;

*pplist = node;

}

}



// 单链表的尾删

void SListPopBack(SListNode** pplist)

{

SListNode* cur = *pplist;

SListNode* prev = NULL;

if (cur == NULL) return;

else {

if (cur->next == NULL) {

free(*pplist);

*pplist = NULL;

}

else {

while (cur->next) {

prev = cur;

cur = cur->next;

}

free(cur);

prev->next = NULL;

}

}

}



// 单链表头删

void SListPopFront(SListNode** pplist)

{

if (*pplist == NULL) return;

SListNode* next = (*pplist)->next;

free(*pplist);

*pplist = next;

}



// 单链表查找

SListNode* SListFind(SListNode* plist, SLTDateType x)

{

SListNode* cur = plist;

while (cur) {

if (cur->data == x) return cur;

cur = cur->next;

}

return NULL;

}



// 单链表在pos位置之后插入x

// 分析思考为什么不在pos位置之前插入?

//在pos之前插入需要遍历整个链表,时间复杂度为O(n);

void SListInsertAfter(SListNode* pos, SLTDateType x)

{

if (pos != NULL) {

SListNode* node = (SListNode*)malloc(sizeof(SListNode));

node->data = x;

node->next = pos->next;

pos->next = node;

}

}



// 单链表删除pos位置之后的值

// 分析思考为什么不删除pos位置?

// 删除pos位置需要遍历整个链表找到pos的前驱节点,才可以删除pos节点

void SListEraseAfter(SListNode* pos)

{

if (pos != NULL && pos->next != NULL) {

SListNode* next = pos->next->next;

free(pos->next);

pos->next = next;

}

}



// 单链表的销毁

void SListDestory(SListNode** plist)

{

while (*plist) {

if ((*plist)->next == NULL) {

free(*plist);

*plist = NULL;

}

else {

SListNode* next = (*plist)->next;

free(*plist);

*plist = NULL;

*plist = next;

next = (*plist)->next;

}

}

}



int main()

{

SListNode* head = BuySListNode(5);

SListPrint(head);

SListPushBack(&head, 6);

SListPushBack(&head, 7);

SListPushBack(&head, 8);

SListPrint(head);

SListPushFront(&head, 4);

SListPushFront(&head, 3);

SListPushFront(&head, 2);

SListPrint(head);

SListPopBack(&head);

SListPopBack(&head);

SListPrint(head);

SListPopFront(&head);

SListPopFront(&head);

SListPrint(head);

SListNode* find = SListFind(head, 5);

SListInsertAfter(find, 10);

SListPrint(head);

find = SListFind(head, 5);

SListEraseAfter(find);

SListPrint(head);

SListEraseAfter(find);

SListPrint(head);

SListEraseAfter(find);

SListPrint(head);

SListEraseAfter(find);

SListPrint(head);

SListDestory(&head);

SListPrint(head);

system("pause");

return 0;

}
带头双向循环链表
// 带头+双向+循环链表增删查改实现

typedef int LTDataType;

typedef struct ListNode

{

	LTDataType _data;

	struct ListNode* _next;

	struct ListNode* _prev;

}ListNode;



// 创建返回链表的头结点.

ListNode* ListCreate()

{

	ListNode* head = (ListNode*)malloc(sizeof(ListNode));

	head->_next = head;

	head->_prev = head;

	return head;

}

// 双向链表销毁

void ListDestory(ListNode* pHead)

{

	ListNode* cur = pHead->_next;

	while (cur != pHead) {

		ListNode* next = cur->_next;

		free(cur);

		cur = next;

	}

	pHead->_next = pHead;

	pHead->_prev = pHead;

}

// 双向链表打印

void ListPrint(ListNode* pHead)

{

	ListNode* cur = pHead->_next;

	while (cur != pHead) {

		printf("%d ", cur->_data);

		cur = cur->_next;

	}

	putchar('\n');

}

// 双向链表尾插

void ListPushBack(ListNode* pHead, LTDataType x)

{

	ListNode* node = (ListNode*)malloc(sizeof(ListNode));

	node->_data = x;

	ListNode* prev = pHead->_prev;

	node->_prev = prev;

	node->_next = pHead;

	prev->_next = node;

	pHead->_prev = node;

}

// 双向链表尾删

void ListPopBack(ListNode* pHead)

{

	if (pHead->_prev != pHead) {

		ListNode* cur = pHead->_prev;

		cur->_prev->_next = pHead;

		pHead->_prev = cur->_prev;

		free(cur);

		cur = NULL;

	}

}

// 双向链表头插

void ListPushFront(ListNode* pHead, LTDataType x)

{

	ListNode* node = (ListNode*)malloc(sizeof(ListNode));

	node->_data = x;

	ListNode* next = pHead->_next;

	node->_prev = pHead;

	node->_next = next;

	next->_prev = node;

	pHead->_next = node;

}

// 双向链表头删

void ListPopFront(ListNode* pHead)

{

	if (pHead->_next != pHead) {

		ListNode* cur = pHead->_next;

		pHead->_next = cur->_next;

		cur->_next->_prev = pHead;

		free(cur);

		cur = NULL;

	}

}

// 双向链表查找

ListNode* ListFind(ListNode* pHead, LTDataType x)

{

	ListNode* cur = pHead->_next;

	while (cur != pHead) {

		if (cur->_data == x)

			return cur;

		cur = cur->_next;

	}

	return NULL;

}

// 双向链表在pos的前面进行插入

void ListInsert(ListNode* pos, LTDataType x)

{

	ListNode* node = (ListNode*)malloc(sizeof(ListNode));

	ListNode* prev = pos->_prev;

	node->_data = x;

	node->_next = pos;

	node->_prev = prev;

	prev->_next = node;

	pos->_prev = node;

}

// 双向链表删除pos位置的节点

void ListErase(ListNode* pos)

{

	if (pos->_next != pos) {

		ListNode* prev = pos->_prev;

		ListNode* next = pos->_next;

		prev->_next = next;

		next->_prev = prev;

		free(pos);

		pos = NULL;

	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值