带头双向循环链表基础

带头双向循环链表基础

销毁

//销毁
void ListDestory(ListNode* phead);
void ListDestory(ListNode* phead) {
	assert(phead);
	ListNode* cur = phead->next;
	while (cur != phead) {
		ListNode* next = cur->next;
		free(cur);
		cur = next;
	}
	free(phead);
	phead = NULL;
}

 

 尾插

//尾插
void ListPushBack(ListNode* phead, LTDataType x);
//尾插
void ListPushBack(ListNode* phead, LTDataType x) {
	assert(phead);

	/*ListNode* tail = phead->prev;
	ListNode* newnode = BuyListNode(x);
	tail->next = newnode;
	newnode->prev = tail;
	newnode->next = phead;
	phead->prev = newnode;*/
	ListInsert(phead, x);
}

 

 头插

//头插
void ListPushFront(ListNode* phead, LTDataType x);
//头插
void ListPushFront(ListNode* phead, LTDataType x) {
	//ListNode* first = phead->next;
	//ListNode* newnode = BuyListNode(x);
	///*phead->next = newnode;
	//newnode->prev = phead;
	//newnode->next = first;
	//first->prev = newnode;*/
	//newnode->next = phead->next;
	//phead->next->prev = newnode;
	//phead->next = newnode;
	//newnode->prev = phead;
	ListInsert(phead->next,x);
}

 

尾删 

//尾删
void ListPopBack(ListNode* phead);
//尾删
void ListPopBack(ListNode* phead) {
	/*assert(phead);
	assert(phead->next != phead);
	ListNode* tail = phead->prev;
	ListNode* prev = tail->prev;
	phead->prev = prev;
	prev->next = phead;
	free(tail);
	tail = NULL;*/
	ListErase(phead->prev);
}

 

头删 

//头删
void ListPopFront(ListNode* phead);
//头删
void ListPopFront(ListNode* phead) {
	/*assert(phead);
	assert(phead->next != phead);
	ListNode* first = phead->next;
	ListNode* second = first->next;
	phead->next = second;
	second->prev = phead;
	free(first);
	first = NULL;*/
	ListErase(phead->next);
}

 

打印 

打印
void ListPrint(ListNode* phead);
void ListPrint(ListNode* phead) {
	ListNode* cur = phead->next;
	while (cur!=phead) {
		printf("%d  ", cur->data);
        cur = cur->next;
	}
	printf("NULL\n");
}

 

 查找

查找
ListNode* ListFind(ListNode* phead, LTDataType x);
ListNode* ListFind(ListNode* phead, LTDataType x) {
	assert(phead);
	ListNode* cur = phead->next;
	while (cur != phead) {
		if (cur->data == x) {
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}

 

pos位置前插入x 

//pos位置前插入x
void ListInsert(ListNode* pos, LTDataType x);

 

void ListInsert(ListNode* pos, LTDataType x) {
	assert(pos);
	ListNode* prev = pos->prev;
	ListNode* newnode = BuyListNode(x);
	prev->next = newnode;
    newnode->prev = prev;
	newnode->next = pos;
	pos->prev = newnode;
}

删除pos位置的值 

//删除pos位置的值
void ListErase(ListNode* pos);
void ListErase(ListNode* pos) {
	ListNode* prev = pos->prev;
	ListNode* next = pos->next;
	prev->next = next;
	next->prev = prev;
}

相关推荐

  1. 带头双向循环基础

    2024-01-18 01:34:01       51 阅读
  2. 数据结构基础带头节点的双向循环

    2024-01-18 01:34:01       61 阅读
  3. 带头循环双向的实现

    2024-01-18 01:34:01       45 阅读
  4. 数据结构_带头双向循环

    2024-01-18 01:34:01       37 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-18 01:34:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-18 01:34:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-18 01:34:01       82 阅读
  4. Python语言-面向对象

    2024-01-18 01:34:01       91 阅读

热门阅读

  1. ubuntu server配置无线网络

    2024-01-18 01:34:01       52 阅读
  2. .gitignore文件设置了忽略但不生效

    2024-01-18 01:34:01       55 阅读
  3. Python中英文时间转换

    2024-01-18 01:34:01       61 阅读
  4. Ubuntu中用useradd创建用户后无法用su切换过去

    2024-01-18 01:34:01       54 阅读
  5. Spring MVC 日期转换器

    2024-01-18 01:34:01       43 阅读
  6. 在 【Linux Centos】下搭建 【Nginx Web】 服务器

    2024-01-18 01:34:01       37 阅读
  7. EXEC sp_addlinkedserver 指定服务器ip

    2024-01-18 01:34:01       46 阅读
  8. 前端导致浏览器奔溃原因分析

    2024-01-18 01:34:01       51 阅读