雙向鍊錶也叫雙鏈表,是鍊錶的一種,它的每個資料結點中都有兩個指標,分別指向直接後繼和直接前驅。
所以,從雙向鍊錶中的任意乙個結點開始,都可以很方便地訪問它的前驅結點和後繼結點。
鍊錶結構:
typedef struct node
node,*dplist;
鍊錶的操作:
初始化鍊錶;
求鍊錶長度;
銷毀鍊錶;
元素的操作:
建立新的結點;
元素的插入(頭插、尾插、按位置插);
元素的刪除(頭刪、尾刪、按位置刪);
dseqlist.h函式的宣告
typedef
int elemtype;
typedef
struct node
node,
*dplist;
void
init
(dplist plist)
;int
getlength
(dplist plist)
;node*
buynode
(elemtype val)
;int
inserthead
(dplist plist,elemtype val)
;int
inserttail
(dplist plist, elemtype val)
;int
insertpos
(dplist plist,
int pos,elemtype val)
;int
empty
(dplist plist)
;int
deletehead
(dplist plist)
;int
deletetail
(dplist plist)
;int
deletepos
(dplist plist,
int pos)
;void
show
(dplist plist)
;void
destory
(dplist plist)
;
dseqlist.cpp實現**
#include
#include
#include
"dseqlist.h"
void
init
(dplist plist)
//初始化
plist->prev =
null
; plist->next =
null;}
node*
buynode
(elemtype val)
//建立乙個新的結點
intinserthead
(dplist plist,elemtype val)
//頭插
plist->next = pnewnode;
return1;
}int
inserttail
(dplist plist, elemtype val)
//尾插
ptail->next = pnewnode;
pnewnode->prev = ptail;
return1;
}int
getlength
(dplist plist)
//求鍊錶的長度
return count;
}int
insertpos
(dplist plist,
int pos,elemtype val)
//按位置插
for(i; i
) node* pnewnode =
buynode
(val)
; pnewnode->next = pfront->next ;
pnewnode->prev = pfront;
if(pfront->next !=
null
) pfront->next = pnewnode;
return1;
}void
show
(dplist plist)
//查printf
("\n");
}int
empty
(dplist plist)
//判空
intdeletehead
(dplist plist)
//頭刪
node* pcur = plist->next ;
plist->next = pcur->next ;
if(pcur->next !=
null
)free
(pcur)
;return1;
}int
deletetail
(dplist plist)
//尾刪
node* ptail2 = plist;
node* pcur =
null
;while
(ptail2->next !=
null
) ptail2 = ptail2->next;
} pcur = ptail2->next ;
ptail2->next =
null
;free
(pcur)
;return1;
}int
deletepos
(dplist plist,
int pos)
//按位置刪
node* pfront = plist;
node* pcur =
null
;int i =0;
for(i; i
) pcur = pfront->next ;
pfront->next = pcur->next ;
if(pcur->next !=
null
)free
(pcur)
;return1;
}void
destory
(dplist plist)
//銷毀函式
plist->next =
null
;}
main.cpp測試程式
#include
#include
"dseqlist.h"
intmain()
for(
int i=
0; i<
3; i++
)show
(&head)
;insertpos
(&head,5,
100)
;show
(&head)
;deletehead
(&head)
;show
(&head)
;deletetail
(&head)
;show
(&head)
;deletepos
(&head,4)
;show
(&head)
;destory
(&head)
;show
(&head)
;return0;
}
雙向鍊錶 基本操作
test.c define crt secure no warnings 1 include doubleslishtnode.h void test1 initdslist pushback printfdslist popback void test2 pushfront popfront vo...
雙向鍊錶基本操作
帶頭節點的雙向鍊錶操作 include include include define ok 1 define error 0 define overflow 0 using namespace std typedef int status typedef int elemtype typedef s...
雙向鍊錶基本操作
package com.bei.linkedlist auther honeysky date 2020 11 10 13 38 public class doublelinkedlistdemo 建立乙個雙向鍊錶的類 class doublelinkedlist 遍歷雙向鍊錶的方法 public ...