雙向鍊錶的基本操作

2021-06-28 01:32:58 字數 1258 閱讀 6490

雙向鍊錶的第個結點有兩個指標,分別指向其直接前驅結點和直接後驅結點。這樣就可以從表中任意的元素結點出發,從兩個方向遍歷鍊錶。

typedef struct node

node;

node * createdll_bytail()//尾插法構造帶頭結點的雙向鍊錶

r->next=null;

return head;

}

結尾的r->next=null;一定不能少。不然出現記憶體洩露。

node * createdll_byhead()//頭插法構造帶頭結點的雙向鍊錶

p->front=head;

head->next=r;

return head;

}

返回之前的最後兩句也不能少。不然出現指標指向未知。

void positiveprintdll(node *head)//雙向鍊錶的正向遍歷

}

while()迴圈體兩條語句不可以互換,可以修改為: printf("%d ",head->next->data);        head=head->next;

void reverseprintdll(node *tail)//雙向鍊錶的反向遍歷

}

while()迴圈體兩條語句不可以互換,應先輸出後,再向前移進。

s->front=p->front;

p->front->next=s;//或者s->front->next=s;

s->next=p;

p->front=s;

第三句與第四句可以互換,但第一句與第二句互換就只能:p->front->next=s;s->front=p->front;

s->next=p->next;

p->next->front=s;//或者s->next->front=s;

s->front=p;

p->next=s;

第三句與第四句可以互換,但第一句與第二句互換就只能:p->next->front=s;s->next=p->next;

p->next->front=p->front;

p->front->next=p->next;

free(p);

第一句與第二句的位置可以互換。

雙向鍊錶 基本操作

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