再寫雙向循環鏈表

在這里插入圖片描述

#pragma once
#include<assert.h>
#include<malloc.h>
#include<stdio.h>
typedef int DLDataType;//定義鏈表結點結構
typedef struct DListNode{DLDataType value;struct DListNode *prev; //指向前一個結點struct DListNode *next; //指向后一個結點
} DListNode;//定義雙向鏈表結構
typedef struct DList{DListNode *head; //表示鏈表頭結點
}DList;DListNode *DListBuyNode(DLDataType value){DListNode *node = (DListNode *)malloc(sizeof (DListNode));node->value = value;node->next =node->prev= NULL;return node;
}//初始化
void DListInit(DList *dlist){dlist->head = DListBuyNode(0);dlist->head->next = dlist->head;dlist->head->prev = dlist->head;
}//銷毀
//1. 清空鏈表
void DListClear(DList *dlist){DListNode *cur, *next;cur = dlist->head->next;while (cur != dlist->head){next = cur->next;free(cur);cur = next;}dlist->head->next = dlist->head->prev = dlist->head;
}//2. 徹底銷毀鏈表
void DListDestroy(DList *dlist){DListClear(dlist);free(dlist->head);dlist->head = NULL; 
}//頭插
void DListPushFront(DList *dlist, DLDataType value){DListNode *node = DListBuyNode(value);node->prev = dlist->head;node->next = dlist->head->next;dlist->head->next->prev = node;dlist->head->next = node;}//尾插
void DlistPushBack(DList *dlist, DLDataType value){DListNode *node = DListBuyNode(value);node->prev = dlist->head->prev;node->next = dlist->head;node->prev->next = node;node->next->prev = node;
}//頭刪
void DlistPopFront(DList *dlist){assert(dlist->head->next != dlist->head);DListNode *cur = dlist->head->next; //cur指向第一個結點dlist->head->next = cur->next;	//頭結點指向第二個結點cur->next->prev = dlist->head;	//第二個結點前指針指向頭結點free(cur);		//刪除第一個結點
}//尾刪
void DlistPopBack(DList *dlist){assert(dlist->head->next != dlist->head);    //確保鏈表不為空DListNode *cur = dlist->head->prev;cur->prev->next = dlist->head;cur->next->prev = cur->prev;free(cur);
}//查找
DListNode * DListFind(const DList *dlist, DLDataType value){DListNode *cur;for (cur = dlist->head->next; cur != dlist->head; cur = cur->next){if (cur->value == value){return cur;}}return NULL;
}void DListInsert(DListNode *pos, DLDataType value){DListNode *node = DListBuyNode(value);node->prev = pos->prev;node->next = pos;node->prev->next = node;pos->prev = node;
}void DListErase(DListNode *pos){pos->prev->next = pos->next;pos->next->prev = pos->prev;free(pos);
}void DListPrintf(const DList *dlist){for (DListNode *cur = dlist->head->next; cur != dlist->head; cur = cur->next){printf("%d-->", cur->value);}printf("\n");
}void TestDList(){DList list;DListInit(&list);DListPrintf(&list);DlistPushBack(&list, 1);DlistPushBack(&list, 2);DlistPushBack(&list, 3);DListPrintf(&list);DListPushFront(&list, 11);DListPushFront(&list, 12);DListPushFront(&list, 13);DListPrintf(&list);
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/383178.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/383178.shtml
英文地址,請注明出處:http://en.pswp.cn/news/383178.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

鏈表題目--1 刪除鏈表中所有等于val的值

注意事項 要刪除的結點相鄰第一個結點就是要刪除的結點 /*** Definition for singly-linked list.* struct ListNode {* int val;* struct ListNode *next;* };*/struct ListNode* removeElements(struct ListNode* head, int val){if(headNULL){return NULL;}struct …

鏈表題目--2 求鏈表的中間結點 和 求鏈表中倒數第k個結點

求鏈表的中間結點 思路 一個走兩步&#xff0c;一個走一步。一個走到尾&#xff0c;另外一個就走到了中間 /*** Definition for singly-linked list.* struct ListNode {* int val;* struct ListNode *next;* };*/struct ListNode* middleNode(struct ListNode* head…

鏈表題目---3 合并兩個有序單鏈表 和 分割鏈表

合并兩個有序單鏈表 /*** Definition for singly-linked list.* struct ListNode {* int val;* struct ListNode *next;* };*/struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2){if(l1 NULL){return l2;}else if(l2 NULL){return l1;}struc…

鏈表題目---4 刪除鏈表中重復的結點 和 判斷鏈表是否為回文鏈表

刪除鏈表中重復的結點 /* struct ListNode {int val;struct ListNode *next;ListNode(int x) :val(x), next(NULL) {} }; */ class Solution { public:ListNode* deleteDuplication(ListNode* pHead){if(pHead NULL){return NULL;}ListNode *prev NULL; //用于刪除的結點, 是…

鏈表題目----5 相交鏈表 和 環形鏈表 和 返回鏈表開始入環的第一個節點

相交鏈表 思路 鏈表交叉不可能是x型因為有可能兩個鏈表不等長&#xff0c;所以我們必須讓他們從同一起跑位置去起跑從同一起跑位置出發&#xff0c;依次比較每個結點的地址是否相同 /*** Definition for singly-linked list.* struct ListNode {* int val;* struct L…

鏈表題目---6 復制帶隨機指針的鏈表

思路 將新結點放在老結點的后面 復制random 將鏈表拆開 /* // Definition for a Node. class Node { public:int val;Node* next;Node* random;Node() {}Node(int _val, Node* _next, Node* _random) {val _val;next _next;random _random;} }; */ class Solution { publi…

括號匹配問題(c和c++版本實現)

括號匹配問題 思路 遇見左括號入棧&#xff0c;遇見一個右括號彈出棧頂元素右括號入棧前如果棧已經為空&#xff0c;則不匹配如果不為空則讀取并彈出&#xff0c;彈出來的元素與右括號做比較&#xff0c;必須匹配&#xff0c;不匹配返回false;如果最后棧里還有元素&#xff0c…

用隊列實現棧 AND 用棧實現隊列

用隊列實現棧 思路 入隊列就是入棧出隊列的時候&#xff0c;就是把前面size-1個隊列中的元素先出&#xff0c;這樣最后一個元素就成隊首元素了&#xff0c;再把出去的元素再次入隊列讀棧頂元素&#xff0c;過程和第二步是一樣的&#xff0c;就是彈出后&#xff0c;再把它入隊列…

最小棧的實現(設計一個支持 push,pop,top 操作,并能在常數時間內檢索到最小元素的棧。)

最小棧的實現 思路 兩個棧&#xff0c;左邊棧接受元素&#xff0c;右邊棧存最小的元素入棧時&#xff0c;先入左邊棧&#xff0c;隨后進行比較&#xff0c;左邊和右邊棧頂元素進行比較&#xff0c;如果新元素小&#xff0c;就把新元素放在右邊的棧頂位置&#xff0c;如果新元素…

再寫循環隊列----c++實現

再寫循環隊列 class MyCircularQueue { public:/** Initialize your data structure here. Set the size of the queue to be k. */MyCircularQueue(int k) {array (int *)malloc(sizeof(int)*k);capacity k;size 0;front 0;rear 0;}/** Insert an element into the circu…

再談二叉樹(二叉樹概念,二叉樹的性質,二叉樹的存儲結構)

樹的概念 樹的概念 樹是一種非線性的數據結構&#xff0c;它是由n&#xff08;n>0&#xff09;個有限結點組成一個具有層次關系的集合。把它叫做樹是因 為它看起來像一棵倒掛的樹&#xff0c;也就是說它是根朝上&#xff0c;而葉朝下的。它具有以下的特點&#xff1a;每個…

二叉樹題目----1 前序中序后序遍歷二叉樹并返回相應的遍歷(不是打印)

前序遍歷 /*** Definition for a binary tree node.* struct TreeNode {* int val;* struct TreeNode *left;* struct TreeNode *right;* };*//*** Note: The returned array must be malloced, assume caller calls free().*/ int *array; int size;void _preorde…

二叉樹題目----2 檢查兩顆樹是否相同 和 對稱二叉樹的判定

檢查兩顆樹是否相同 思路 根要相等 p->val q->val左子樹相等 isSameTree(p->left,q->left)右子樹也要相等 isSameTree(p->right,q->right) /*** Definition for a binary tree node.* struct TreeNode {* int val;* struct TreeNode *left;* …

二叉樹題目---3 另一個樹的子樹 AND 二叉樹最大深度

另一個樹的子樹 思路 兩個數都遍歷一遍&#xff0c;找到一個根結點相同時&#xff0c;判斷以這個根結點為首的二叉樹是否相等 前序遍歷判斷兩棵樹是否相同對于返回值的處理是難點 bool isSameTree(struct TreeNode *p, struct TreeNode *q) {if(p NULL && q NULL)…

二叉樹題目----4 前序遍歷重構二叉樹 AND 求二叉樹中所有結點的個數

前序遍歷重構二叉樹 思路 整個二叉樹用數組存儲因為先序遍歷它先遍歷根&#xff0c;再遍歷左&#xff0c;左邊沒有跑完是不會去遍歷右邊的&#xff0c;所以遍歷左子樹&#xff0c;就是數組元素每回向后一個&#xff0c;個數-1遍歷右邊時&#xff0c;就是數組起始位置左子樹跑到…

二叉樹的進階操作---(求二叉樹中所有結點個數,求葉子結點個數,求第k層結點個數;在二叉樹中查找某一結點;層序遍歷;判斷是否為完全二叉樹)

typedef struct TreeNode {struct TreeNode *left;struct TreeNode *right;char val; }TreeNode;typedef struct Result{TreeNode * root; //構建的樹的根結點int used; //構建過程中用掉的val個數 } Result;求二叉樹中所有結點個數 void TreeSize(TreeNode *root, int …

c++中的智能指針詳解(RAII, auto_ptr的原理及其改進,unique_ptr的原理,shared_ptr的原理,shared_ptr的缺陷及其改進)

為什么需要智能指針 代碼中途退出&#xff0c;也能保證資源的合理釋放&#xff0c;在c中沒有垃圾回收機制的情況下&#xff0c;智能指針就可以保證我們申請的資源&#xff0c;最后忘記釋放的問題&#xff0c;防止內存泄露&#xff0c;也幫我們減少了一定的負擔&#xff0c;不用…

Job for mariadb.service failed because the control process exited with error code. Se

錯誤&#xff1a;[rootlocalhost ~]# systemctl start mariadb.service Job for mariadb.service failed because the control process exited with error code. See “systemctl status mariadb.service” and “journalctl -xe” for details. 解決辦法&#xff1a; [rootl…

二叉樹題目----5 平衡二叉樹 AND 根據二叉樹創建字符串

平衡二叉樹 /*** Definition for a binary tree node.* struct TreeNode {* int val;* struct TreeNode *left;* struct TreeNode *right;* };*/int MAX(int a,int b) {return a > b ? a : b; }//求高度 int getHeight(struct TreeNode *root){if(root NULL){…

再寫堆(堆的性質,向下調整,建堆,堆的插入刪除初始化,堆排序,TopK問題)

堆的概念 如果有一個關鍵碼的集合K{k0,k1,k2,…,kn-1},把它的所有元素按完全二叉樹的順序存儲方式存儲再一個一維數組中&#xff0c;并滿足:Ki<K2i1且Ki<K2i1(Ki > K2i1 且 Ki > K2i2),i0,1,2,3…。則稱為小堆(或大堆)。將根結點最大的堆叫做最大堆或大根堆&#…