網站首頁 編程語言 正文
本文實例為大家分享了C++數據結構之雙向鏈表的具體代碼,供大家參考,具體內容如下
#include <iostream>
using std::cout;
using std::endl;
struct Node
{
?? ?int data;
?? ?struct Node * next;
?? ?struct Node * pre;
};
一、創建雙向鏈表
Node * createList()
{
?? ?Node * head = new Node;
?? ?if (NULL == head)
?? ??? ?exit(-1);
?? ?head->next = head;
?? ?head->pre = head;
?? ?return head;
}
二、插入元素(頭插法)
讓新來的節點先有所指
void insertList(Node * head,int n)
{
?? ?Node * cur = new Node;
?? ?if (NULL == cur)
?? ??? ?exit(-1);
?? ?cur->next = head->next;
?? ?cur->pre = head;
?? ?head->next = cur;
?? ?cur->next->pre = cur;
?? ?
?? ?cur->data = n;
}
三、鏈表長度
int lenList(Node * head)
{
?? ?int i = 0;
?? ?Node * t = head->next;
?? ?while (t != head)
?? ?{
?? ??? ?i++;
?? ??? ?t = t->next;
?? ?}
?? ?return i;
}
四、查找遍歷
Node * findList(Node * head,int fn)
{
?? ?Node * forward = head->next;
?? ?Node * back = head->pre;
?? ?while (forward != back->next)
?? ?{
?? ??? ?if (forward->data == fn)
?? ??? ??? ?return forward;
?? ??? ?if (back->data == fn)
?? ??? ??? ?return back;
?? ??? ?if (forward == back)
?? ??? ??? ?break;
?? ??? ?forward = forward->next;
?? ??? ?back = back->pre;
?? ?}
?? ?return NULL;
}
五、刪除其中元素
void deleteList(Node * pFind)
{
?? ?pFind->pre->next = pFind->next;
?? ?pFind->next->pre = pFind->pre;
?? ?delete pFind;
}
六、排序
(類似于先刪除 再插入)
void sortDlist(Node * head)
{
?? ?int len = lenList(head);
?? ?Node *prep = NULL;
?? ?Node *p = NULL;
?? ?Node *q = NULL;
?? ?Node *t = NULL;
?? ?for (int i = 0;i < len - 1;i++)
?? ?{
?? ??? ?p = head->next;
?? ??? ?q = p->next;
?? ??? ?for (int j = 0;j < len - 1 - i;j++)
?? ??? ?{
?? ??? ??? ?if ((p->data)<(q->data))
?? ??? ??? ?{
?? ??? ??? ??? ?p->pre->next = q;
?? ??? ??? ??? ?q->pre = p->pre;
?? ??? ??? ??? ?p->next = q->next;
?? ??? ??? ??? ?p->pre = q;
?? ??? ??? ??? ?q->next = p;
?? ??? ??? ??? ?p->next->pre = p;
?? ??? ??? ??? ?t = p;
?? ??? ??? ??? ?p = q;
?? ??? ??? ??? ?q = t;
?? ??? ??? ?}
?? ??? ??? ?p = p->next;
?? ??? ??? ?q = q->next;
?? ??? ?}
?? ?}
}
七、銷毀鏈表
void desList(Node * head)
{
?? ?head->pre->next = NULL;
?? ?Node *t = NULL;
?? ?while (head != NULL)
?? ?{
?? ??? ?t = head;
?? ??? ?head = head->next;
?? ??? ?delete t;
?? ?}
}
原文鏈接:https://blog.csdn.net/qq_42972267/article/details/86561274
相關推薦
- 2023-07-05 React解決setState異步帶來的多次修改合一和修改后立即使用沒有變化問題
- 2022-09-24 關于R語言包的升級與降級問題_R語言
- 2023-03-19 匯編語言LDR指令和LDR偽指令詳解_匯編語言
- 2022-02-09 將?C++?類型屬性暴露給?QML_C 語言
- 2023-11-17 在python中按不同的條件在列表list、字典dict和集合set中篩選想要的數據(以實際案例進行
- 2022-12-11 React?狀態的不變性實例詳解_React
- 2022-06-11 docker安裝elastic?search和kibana的實現_docker
- 2022-12-01 Golang打印復雜結構體兩種方法詳解_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支