鍊錶的基本操作

2021-08-07 02:18:36 字數 2300 閱讀 6118

鍊錶的基本操作:

鍊錶的建立、遍歷、插入、刪除、銷毀、逆置

#define _crt_secure_no_warnings

#include #include #include typedef struct nodeslist;

slist *slist_creat(); //建立鍊錶

int slist_print(slist *phead); //遍歷鍊錶

int slist_insert(slist *phead,int x,int y); //插入元素

int slist_delete(slist *phead,int x); //刪除元素

void slist_destory(slist *phead); //銷毀鍊錶

int slist_reverse(slist *phead); //逆置鍊錶

slist *slist_creat()

phead->node = 0;

phead->next = null;

//接收使用者輸入

printf("\nplease enter your data:");

scanf("%d",&data);

pcur = phead;

while( data != -1 )

pm->next = null;

pm->node = data;

//2.新節點入鍊錶

pcur->next = pm;

pcur = pm;

printf("\nplease enter your data:");

scanf("%d",&data);

} return phead;

}int slist_creat2(slist **mylist)

phead->node = 0;

phead->next = null;

//接收使用者輸入

printf("\nplease enter your data:");

scanf("%d",&data);

pcur = phead;

while( data != -1 )

pm->next = null;

pm->node = data;

//2.新節點入鍊錶

pcur->next = pm;

pcur = pm;

printf("\nplease enter your data:");

scanf("%d",&data);

}end:

if( ret != 0 )

slist_destory(phead);

else

*mylist = phead;

return 0;

}int slist_print(slist *phead)

tmp = phead->next;

printf("begin\t");

while(tmp != null)

printf("end\n");

return 0;

}int slist_insert(slist *phead,int x,int y)

pm = (slist *)malloc(sizeof(slist));

if( pm == null )

pm->node = y;

pm->next = null;

ppre = phead;

pcur = phead->next;

while( pcur )

else

}pm->next = pcur;

ppre->next = pm;

return 0;

}int slist_delete(slist *phead,int x)

else

}if(pcur == null )

ppre->next = pcur->next;

if(pcur != null)

return 0;

}void slist_destory(slist *phead)

}int slist_reverse(slist *phead)

ppre = phead->next;

pcur = phead->next->next;

while( pcur )

phead->next->next = null;

phead->next = ppre;

return 0;

}void main()

鍊錶的基本操作

include include include include using namespace std struct listnode void initnode listnode node bool isempty listnode head void pushfront listnode hea...

鍊錶的基本操作

鍊錶操作是最基本的 必須掌握的知識點,最好滾瓜爛熟,透徹理解。工作時間短用的也不夠頻繁,還是總結一下比較好,以加強鞏固。1.單鏈表 結點形式 區分幾個概念 首節點 第乙個元素所在節點。頭指標 指向首節點的指標。頭結點 為了操作方便,在第乙個節點之前附設的乙個結點,此時指向頭結點的為頭指標。基本操作 ...

鍊錶的基本操作。。。

include node.h 列印鍊錶 void print node head printf n 從尾部插入 void insert tail node head,const int d while t next null t next p p next null 從頭部插入 void inser...