鍊錶基本操作

2022-03-23 16:22:49 字數 1989 閱讀 5815

#define _crt_secure_no_warnings#include

#include

#include

typedef

struct

node

slist;

slist *slist_create(); //

建立鍊錶

int slist_nodeinsert(slist *phead, int x, int y);//

插入鍊錶

int slist_nodedel(slist *phead, int y);//

刪除鍊錶

int slist_destroy(slist *phead);//

銷毀slist *slist_create()

phead->data = 0

; phead->next =null;

printf(

"\nplease enter your data ");

scanf("%d

", &data);

pcut =phead;

//不斷接受輸入malloc的新結點

while (data != -1

)

pm->data =data;

pm->next =null;

//2新結點,入鍊錶

pcut->next =pm;

pcut =pm;

printf(

"\nplease enter your data ");

scanf("%d

", &data);

}return

phead;

}int slist_print(slist *phead)

tmp = phead->next;

printf(

"begin...");

while

(tmp)

return0;

}int slist_nodeinsert(slist *phead, int x, int

y)

//初始化

pm->next =null;

pm->data =y;

//遍歷鍊錶

ppre =phead;

pcut = phead->next;

while

(pcut)

ppre =pcut;

pcut = pcut->next;

}//新結點,連線後續結點

pm->next = ppre->next;

//讓前驅節點,連線新節點

ppre->next =pm;

return0;

}int slist_nodedel(slist *phead, int

y) ppre =pcur;

pcur = pcur->next;

}if (pcur ==null)

ppre->next = pcur->next;

if (pcur !=null)

return0;

}int slist_destroy(slist *phead)

tmp =phead;

while (phead!=null)

return0;

}int slist_reverse(slist *phead)

p = phead->next;

q = phead->next->next;

//p = phead;

//q = phead->next;

//乙個節點,乙個結點的位置

while

(q)

//頭結點變尾節點後 置null

phead->next->next =null;

phead->next =p;

return0;

}int

main()

鍊錶基本操作

include include string h include typedef struct stust void xj st h 生成單鏈表 l l null void shc st h 輸出鍊錶 printf d n h d void chr st h 按大小插入元素 else h h l v...

鍊錶基本操作

動態記憶體的相關知識int p null 定義指標變數 p int malloc sizeof int 申請記憶體空間,並且進行強制型別轉換 p 3 使用指標變數 typedef struct list typedef struct listnode listpointer struct listn...

鍊錶基本操作

鍊錶就是c中利用結構體,將資料和下乙個結構體的位址封裝在乙個結構體中形成乙個節點,這些節點組合起來就是乙個基礎的鍊錶,根據需要可以擴充套件其中的內容來實現不同的需求。實現乙個鍊錶需要定義節點,建立,初始化,插入,刪除這些基本操作。include stdafx.h include stdlib.h i...