数据结构学习——双向链表
生活随笔
收集整理的這篇文章主要介紹了
数据结构学习——双向链表
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 前言——雙向鏈表含義
- 一、雙向鏈表結構(圖解)
- 二、 雙向鏈表的實際操作
- 1、 結構
- 2、創建頭結點
- 3、打印
- 4、插入
- 5、定位
- 6、刪除
- 完整代碼
- 運行結果
前言——雙向鏈表含義
雙向鏈表也叫雙鏈表,是鏈表的一種,它的每個數據結點中都有兩個指針,分別指向直接后繼和直接前驅。所以,從雙向鏈表中的任意一個結點開始,都可以很方便地訪問它的前驅結點和后繼結點。一般我們都構造雙向循環鏈表。
一、雙向鏈表結構(圖解)
二、 雙向鏈表的實際操作
1、 結構
/*** Double linked list of integers. The key is char.*/ typedef struct DoubleLinkedNode {char data;struct DoubleLinkedNode *previous;struct DoubleLinkedNode *next; }DLNode,*DLNodePtr;2、創建頭結點
/*** Initialize the list with a header.* @return The pointer to the header.*/ DLNodePtr initLinkList(){DLNodePtr tempHeader =(DLNodePtr)malloc(sizeof(struct DoubleLinkedNode));tempHeader->data = '\0';tempHeader->previous = NULL;tempHeader->next=NULL;return tempHeader; }3、打印
/*** Print the list.* @param paraHeader The header of the list.*/ void printList(DLNodePtr paraHeader) {DLNodePtr p= paraHeader->next;while(p!=NULL){printf("%c",p->data);p = p->next;}printf("\r\n"); }4、插入
void insertElement(DLNodePtr paraHeader,char paraChar,int paraPosition) {DLNodePtr p,q,r;p = paraHeader;int i;for (i=0;i< paraPosition;i++){p=p->next;if(p==NULL){printf("The position %d is beyond the scope of the list.",paraPosition);return;}}q=(DLNodePtr)malloc(sizeof(struct DoubleLinkedNode));q->data = paraChar;r = p->next;q->next = p->next;q->previous = p;p->next = q;if(r!=NULL){r->previous = q;} }5、定位
void locateELement(DLNodePtr paraHeader,char paraChar) {DLNodePtr p,q,r;int i=1;p=paraHeader;while((p->next !=NULL)&&(p->next->data !=paraChar)){p=p->next;i++;}if(p->next == NULL){printf("The char '%c' does not exist.\r\n",paraChar);return;}printf("%d\n",i); }6、刪除
void deleteElement(DLNodePtr paraHeader,char paraChar) {DLNodePtr p,q,r;p=paraHeader;while((p->next !=NULL)&&(p->next->data !=paraChar)){p=p->next;}if(p->next == NULL){printf("The char '%c' does not exist.\r\n",paraChar);return;}q=p->next;r=q->next;p->next=r;if(r!=NULL){r->previous = p;}free(q); }完整代碼
#include <stdio.h> #include <stdlib.h> #include <malloc.h> typedef struct DoubleLinkedNode {char data;struct DoubleLinkedNode *previous;struct DoubleLinkedNode *next; }DLNode,*DLNodePtr;DLNodePtr initLinkList(){DLNodePtr tempHeader =(DLNodePtr)malloc(sizeof(struct DoubleLinkedNode));tempHeader->data = '\0';tempHeader->previous = NULL;tempHeader->next=NULL;return tempHeader; }void printList(DLNodePtr paraHeader) {DLNodePtr p= paraHeader->next;while(p!=NULL){printf("%c",p->data);p = p->next;}printf("\r\n"); }void insertElement(DLNodePtr paraHeader,char paraChar,int paraPosition) {DLNodePtr p,q,r;p = paraHeader;int i;for (i=0;i< paraPosition;i++){p=p->next;if(p==NULL){printf("The position %d is beyond the scope of the list.",paraPosition);return;}}q=(DLNodePtr)malloc(sizeof(struct DoubleLinkedNode));q->data = paraChar;r = p->next;q->next = p->next;q->previous = p;p->next = q;if(r!=NULL){r->previous = q;} }void locateELement(DLNodePtr paraHeader,char paraChar) {DLNodePtr p,q,r;int i=1;p=paraHeader;while((p->next !=NULL)&&(p->next->data !=paraChar)){p=p->next;i++;}if(p->next == NULL){printf("The char '%c' does not exist.\r\n",paraChar);return;}printf("%d\n",i); }void deleteElement(DLNodePtr paraHeader,char paraChar) {DLNodePtr p,q,r;p=paraHeader;while((p->next !=NULL)&&(p->next->data !=paraChar)){p=p->next;}if(p->next == NULL){printf("The char '%c' does not exist.\r\n",paraChar);return;}q=p->next;r=q->next;p->next=r;if(r!=NULL){r->previous = p;}free(q); }void insertDeleteTest() {DLNodePtr tempList = initLinkList();printList(tempList);insertElement(tempList,'p',0);insertElement(tempList,'o',1);insertElement(tempList,'l',2);insertElement(tempList,'l',3);insertElement(tempList,'e',4);insertElement(tempList,'n',5);printList(tempList) ;locateELement(tempList,'l');deleteElement(tempList,'o');deleteElement(tempList,'e');printList(tempList);}void basicAddressTest(){DLNode tempNode1,tempNode2;tempNode1.data = 4;tempNode1.next = NULL;tempNode2.data = 6;tempNode2.next = NULL;printf("The first node: %d,%d,%d\r\n",&tempNode1,&tempNode1.data,&tempNode1.next);printf("The Second node: %d,%d,%d\r\n",&tempNode2,&tempNode2.data,&tempNode2.next);tempNode1.next = &tempNode2;}int main() {insertDeleteTest();basicAddressTest(); }運行結果
總結
以上是生活随笔為你收集整理的数据结构学习——双向链表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【原创+转载】腾讯股票接口——实时行情解
- 下一篇: 假外汇平台杀入币圈:反向喊单,篡改数据,