雙向鍊錶常見操作

2021-09-27 13:47:07 字數 2338 閱讀 6287

雙向鍊錶的結構:資料域,以及兩個指標域,其中乙個指標指向前驅節點,乙個指標指向後繼節點。

#include

"twowaylist.h"

//思路:分插入的位置情況分別實現,插入的位置為1和其它。

void

insert_twowaynode

(twowaylist* clist,

int pos,

int value)

//若插入的位置為1,且當前鍊錶不為空

node->next = clist->next;

clist->next = node;

node->next->prior = node;

} twowaynode* currnode = clist->next;

//若插入的位置不是1,則從第乙個節點遍歷,找到待插入節點的前乙個節點

for(i =

1; currnode&&i < pos -

1; i++)if

(currnode)

node->next = currnode->next;

currnode->next = node;

clist->length++;}

}void

show_node

(twowaylist* clist)

for(i =

1; i <=clist->length; i++)}

void

del_twowaynode

(twowaylist* clist,

int pos)

free

(node)

; clist->length--;}

return;}

twowaynode* currnode = clist->next;

for(i =

1; currnode&&i

(currnode)

currnode->prior->next = currnode->next;

free

(currnode)

; clist->length--;}

return;}

#ifndef _twowaylist_h

#define _twowaylist_h

#include

#include

#include

//刪除和插入節點需要修改4個指標

typedef

int datatype;

typedef

struct twowaynode

twowaynode;

typedef

struct twowaylist

twowaylist;

extern

void

insert_twowaynode

(twowaylist* clist,

int pos,

int value)

;extern

void

show_node

(twowaylist* clist)

;extern

void

del_twowaynode

(twowaylist* clist,

int pos)

;#endif

#include

"twowaylist.h"

intmain

(void

)show_node

(clist)

;//列印

del_twowaynode

(clist,4)

;//刪除第四個元素

printf

("\n");

show_node

(clist)

;//列印

system

("pause");

return0;

}

插入的節點位置為1的情況

雙向鍊錶的操作

include using namespace std 列印選項 void printtheselect typedef struct dulnode dulnode,dulinklist 初始化雙向鍊錶 void initdlist dulinklist l cout 雙向鍊錶構造完畢 n 列印雙...

雙向鍊錶 基本操作

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...