資源簡介
C與語言實現鏈表的創建、插入(頭插法、尾插法)、遍歷、查找、刪除操作

代碼片段和文件信息
#include?
#include?
/*定義結構體*/
typedef?struct?stu{
int?num;
struct?stu?*next;
}STU*PSTU;
/*遍歷鏈表的實現*/
void?show(PSTU?head)
{
if(head?==?NULL){
printf(“There?is?no?data!\n“);
}
else{
/*當前結點部位空時,打印信息*/
for(;?head?!=?NULL;?head?=?head->next){
printf(“%d?->?“head->num);
}
printf(“NULL\n“);
}
}
/*創建鏈表函數的實現*/
PSTU?Create(PSTU?head)
{
int?n?=?0;
PSTU?tail?=?NULL;? /*定義尾指針并初始化為空*/
printf(“You?should?input?the?numbersand?you?can?input?‘-1‘?to?stop?create?link_table!\n“);
while(1){
PSTU?new?=?(PSTU)malloc(sizeof(STU));
printf(“Please?input?the?number:?“);
scanf(“%d“&n);
if(n?==?-1){
free(new);
printf(“create?over!\n“);
return?head;
}
new->num?=?n;
new->next?=?NULL;
if(head?==?NULL){ /*當頭結點為空時,當前結點設為頭結點,并使tail指針指向頭結點*/
head?=?new;
tail?=?new;
}
else{ /*當不是頭結點時,在尾部插入數據*/
tail->next?=?new;
tail?=?new;
}
}
return?head;
}
/*頭插法插入數據?的實現*/
PSTU?Insert_from_head(PSTU?head)
{
PSTU?new?=?(PSTU)malloc(sizeof(STU));
int?n?=?0;
printf(“Please?input?the?insert?number?n?=?“);
scanf(“%d“&n);
new->num?=?n;
new->next?=?NULL;
if(head?==?NULL){
head?=?new;
}
else{
new->next?=?head->next;
head->next?=?new;
}
return?head;
}
/*尾插法插入數據?的實現*/
PSTU?Insert_from_tail(PSTU?head)
{
PSTU?tail?=?NULL;
PSTU?new?=?(PSTU)malloc(sizeof(STU));
int?n?=?0;
printf(“Please?input?the?insert?number?n?=?“);
scanf(“%d“&n);
new->num?=?n;
new->next?=?NULL;
if(head?==?NULL){
head?=?new;
}
else{
tail?=?head;
/*將尾指針指向最后一個結點*/
for(;?tail->next?!=?NULL;?tail?=?tail->next);
tail->next?=?new;
}
return?head;
}
/*查找函數的實現*/
void?Find(PSTU?head)
{
PSTU?ptr?=?NULL;
int?n?=?0;
printf(“Please?input?the?number?you?will?find:?“);
scanf(“%d“&n);
if(head?==?NULL){
printf(“There?is?no?data!\n“);
}
else{
ptr?=?head;
/*當當前結點的數據與查找的數據不相符時移向下一個結點*/
for(;?(ptr->num?!=?n)?&&?(ptr?!=?NULL);?ptr?=?ptr->next);
if(ptr?==?NULL){
printf(“Can‘t?find?!\n“);
}
else{
printf(“find!\nThe?number?is?:%d\n“ptr->num);
}
}
}
/*刪除結點函數的實現*/
PSTU?Delete(PSTU?head)
{
PSTU?ptr?=?NULL;
PSTU?pf?=?NULL;
int?n?=?0;
printf(“Please?input?the?number?you?will?delete?:“);
scanf(“%d“&n);
if(head?==?NULL){
printf(“There?is?no?data!\n“);
}
else{
ptr?=?head;
for(;?(ptr->num?!=?n)?&&?(ptr?!=?NULL);?ptr?=?ptr->next){
pf?=?ptr;
}
if(ptr?==?NULL){
printf(“The?number?is?no?in?the?link_table?!\n“);
}
else{
pf->next?=?ptr->next;
free(ptr);
}
}
printf(“now?The?link_table?is?:“);
show(head);
return?head;
}
int?main(void)
{
PSTU?head?=?NULL;
int?choose?=?0;
/*簡易的菜單*/
while(1){
printf(“\t\t-----------------Menu-------------------\n“);
printf(“\t\t--------------??1.?Create??------------?\n“);
printf(“\t\t--------------??2.?Insert?from?head??--?\n“);
printf(“\t\t--------------??3.?Insert?from?tail??--?\n“);
printf(“\t\t--------------??4.?Find??--------------?\n“);
printf(“\t\t--------------??5.?Delte??-------------?\n“);
printf(“
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2013-09-13?16:39??鏈表操作\
?????文件????????4199??2013-09-13?16:38??鏈表操作\li
- 上一篇:VxWorks串口編程代碼
- 下一篇:c++編寫的簡單的匯編器
評論
共有 條評論