雙向鍊錶的基本操作

2021-07-25 08:29:55 字數 2841 閱讀 7955

雙向鍊錶的基本操作:

#ifndef _list_h

#define _list_h

typedef struct _node

node, *pnode;

//輸出結點

void show(pnode phead);

//頭插

void inserthead(pnode* pphead, int val);

//尾插

void inserttail(pnode* pphead, int val);

//根據索引插入結點

void insertbyindex(pnode* pphead, int index, int val);

//頭刪

void deletehead(pnode* pphead);//ok

//尾刪

void deletetail(pnode* pphead); //用不到前驅??//ok

//根據索引刪除

void deletebyindex(pnode* pphead, int index); // question

//根據值刪除

void deletebyvalue(pnode* pphead, int val);// question

//根據索引查詢值

pnode findnodebyindex(pnode phead, int index); //ok

//根據結點的值查詢

pnode findnodebyvalue(pnode phead, int val);//ok

//根據索引修改值

void modifyvaluebyindex(pnode phead, int index, int val); //ok

//根據結點的值修改值

void modifyvaluebyvalue(pnode phead, int oldval, int newval); //ok

//鍊錶清空

void clear(pnode* pphead);

#endif

以上操作的實現:

#include #include #include "list.h"

void show(pnode phead)

printf("\b \n");

}void inserthead(pnode *pphead, int val)

*pphead = pnew;

}void inserttail(pnode *pphead, int val)

else

ppos->next = pnew;

pnew->prev = ppos; }}

void deletehead(pnode* pphead)

}void deletetail(pnode* pphead)

else

ppos->prev->next = null;

free(ppos);

ppos = null; }}

void insertbyindex(pnode* pphead, int index, int val)

else if (index > 0 && ppos != null)

if (ppos != null)

}else }

void deletebyindex(pnode* pphead, int index)

else if (index > 0 && ppos != null)

if (null == ppos->next)

else if (ppos != null && ppos->next != null)

}}void deletebyvalue(pnode* pphead, int val)

if (null == ppos)

if (ppos->next != null && ppos->prev == null)

else

} }pnode findnodebyindex(pnode phead, int index)

else

return (ppos); }}

pnode findnodebyvalue(pnode phead, int val)

} return (null);

}void modifyvaluebyindex(pnode phead, int index, int val)

}void modifyvaluebyvalue(pnode phead, int oldval, int newval)

}void clear(pnode* pphead)

}

測試:

#include #include "list.h"

#include "usertype.h"

int main(void)

/*****************************/

/*ppos = findnodebyvalue(phead, 89);

if (ppos != null)

*/// modifyvaluebyindex(phead, 4, 89);

modifyvaluebyvalue(phead, 5, 55);

show(phead);

return 0;

}

雙向鍊錶 基本操作

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