C語言 單向鍊錶

2021-07-08 13:44:41 字數 1667 閱讀 5215

/* 1.c語言單向鍊錶 

* 2.鍊錶的(增,刪,查,遍歷)

* author: mr.long

* date : 2015-12-1 15:24:26

*/#include#include/* 結構定義 */

struct node ;

/* 函式定義 */

void printnode(struct node *head);

struct node* addfirstnode(struct node *head,int);

struct node* findnode(struct node *head,int);

int deletnode(struct node *head,int);

int main() else

printf("*****************刪除節點******************\r\n");

/* 刪除指定節點 成功返回1 失敗返回0 */

int dest = 0;

bool state = deletnode(head,dest);

if(state)

printf("刪除[%d]成功... \n",dest);

else

printf("刪除[%d]失敗... \n",dest);

printf("*****************所有節點******************\r\n");

/* 遍歷節點並輸出 */

printnode(head);

return 0;

}int deletnode(struct node *head,int dest)

//記錄上乙個節點

lp = p;

//移動指標

p = p->next;

} return false;

}struct node* findnode(struct node *head,int dest)

p = p->next; //移動指標

} return pc;

} if(data == null) return head;

if(head == null)

/*易錯點

* 這裡千萬注意迴圈條件是p->next而不是p

* 因為當執行到鍊錶尾節點時p已經為null

* 而當p為null時56行的空指標操作會導致程式崩潰

*/struct node *p = head;

while(p->next)

struct node *pnew;

pnew = (struct node *)malloc(sizeof(struct node));

pnew->data = data;

pnew->next = null;

p->next = pnew;

/*易錯點:

* 如果這裡返回 p 則鍊錶只保留了最後兩項資料

* 原因在於47行的while迴圈移動了p的指標

*/return head;

}struct node* addfirstnode(struct node *head,int data)

void printnode(struct node *head)

}

C語言 單向鍊錶

1 單向鍊錶的定義 struct student next作為同型別指標,指向與它所在節點一樣的節點。1 建立鍊錶 int main 定義建立函式create,建立乙個有n個節點的單向鍊錶 struct student create int n ptail next null return head...

C語言單向鍊錶實現

include include typedef struct node listnode typedef listnode linklist 帶頭節點的單鏈表 初始化單鏈表只有頭節點 void initlinklist linklist linklist 建立乙個單鏈表 linklist creat...

C語言之單向鍊錶

1,單向鏈簡潔。單向鍊錶 單鏈表 是鍊錶的一種,其特點是鍊錶的鏈結方向是單向的,對鍊錶的訪問要通過順序讀取從頭部開始 鍊錶是使用指標進行構造的列表 又稱為結點列表,因為鍊錶是由乙個個結點組裝起來的 其中每個結點都有指標成員變數指列表中的下乙個結點 列表是由結點構成,由head指標指向第乙個成為表頭的...