單鏈表操作 無頭節點

2021-10-05 23:14:45 字數 1656 閱讀 8951

#ifndef singlelist_h

#define singlelist_h

/ 這是沒有頭結點的版本檔案 //

/ 有頭結點的版本更簡單些,操作統一 //

#include

#include

#include

#include

typedef

int elemtype;

typedef

struct nodenode,

*linklist;

// 分配新節點並初始化

linklist newnode

(void

)void

listinfo

(linklist list)

printf

("listinfo:\n");

while

(pos)

printf

("\n");

}// 在單鏈表list的第i個節點前插入節點e

intlistinsert

(linklist *list,

int i, elemtype e)

// 插入到煉表頭指標後面,頭指標不改變

else

new->next = pos->next;

pos->next = new;

//printf("pos=%p, pos->next=%p\n",pos,pos->next);

}return0;

}// 刪除單鏈表list的第i個節點,並將該節點的資料域返回給e。

intlistdelete

(linklist *list,

int i, elemtype *e)

else

// pos為刪除位置的前一項

if(e)

*e = pos->next->data;

linklist del = pos->next;

// 被刪除的元素

pos->next = del->next;

free

(del);}

return0;

}void

listfree

(linklist *list)

//*list = null;

}// 鍊錶最前面插入新元素,鍊錶沒有頭結點。

void

listaddhead

(linklist *list, linklist new)

// 建立帶有n個節點的鍊錶,沒有頭結點

linklist listcreate

(int n)

listaddhead

(&list,new);}

return list;

}// 快速找出鍊錶的中間節點,返回節點的序號

intlistmidnode

(linklist list)

else

}return midpos;

}// 不分配新空間,逆序鍊錶。

void

listreverse

(linklist *list)

/* 重新賦值鍊錶的頭指標 */

*list = head;

}#endif

// singlelist_h

單鏈表 無頭節點

就這書上 敲了一邊,加深印象,沒有頭結點的時候插入第乙個就有所不同了,而刪除時要找到前乙個,注意current link null 就這樣。include include include using namespace std struct linknode class list bool list...

無頭單鏈表的操作

節點結構 typedef定義 不帶頭節點的單鏈表 typedef int sdatatype 節點結構 typedef struct slistnode node,pnode 鍊錶結構 typedef定義 給乙個鍊錶結構 typedef struct slist slist 鍊錶的初始化 1.斷言 ...

無頭單鏈表

鍊錶有單鏈表 雙鏈表和雙向迴圈鍊錶,每種鍊錶都有無頭和帶頭兩種,帶頭就是頭結點不存放資料元素 ifndef linklist h define linklist h include stdio.h include assert.h include string.h include malloc.h ...