資源簡(jiǎn)介
VS2008運(yùn)行環(huán)境 c語(yǔ)言實(shí)現(xiàn)單鏈表增刪改查操作 并輸出到控制臺(tái)上

代碼片段和文件信息
//?CNode.cpp?:?定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。
//
#include?“stdafx.h“
#include?“windows.h“
typedef?struct?_Node
{
char?data;
_Node?*next;
}Node;
void?insertNode(Node?**headint?ichar?e);
void?printNode(Node?**head);?
void?deleteNode(Node?**headint?i);?
int?_tmain(int?argc?_TCHAR*?argv[])
{
Node?*h;?????
h?=?NULL;?
insertNode(&h0‘a(chǎn)‘);?????
insertNode(&h1‘b‘);?????
insertNode(&h0‘c‘);?????
insertNode(&h4‘d‘);?????
printNode(&h);?????
deleteNode(&h1);?????
printNode(&h);?????
deleteNode(&h0);?????
printNode(&h);?????
deleteNode(&h1);?????
printNode(&h);?
return?0;
}
/*??*插入操作要確保要插入位置的前一個(gè)節(jié)點(diǎn)不為空并標(biāo)記出其位置指針?
*因此要確保q的存在?
*節(jié)點(diǎn)從0計(jì)數(shù)?
第0個(gè)節(jié)點(diǎn)是頭指針?biāo)腹?jié)點(diǎn)?*/
void?insertNode(Node?**headint?ichar?e)
{???????
Node?*q;??
Node?*p;?????
int?j;??????
p?=?*head;???
if(i?==?0)?????
{
//插入位置在頭指針處?頭指針為空?不為空?時(shí)的情況?
if((p?=?(Node?*)malloc(sizeof(Node)))?==?NULL)???
{????????
//節(jié)點(diǎn)空間申請(qǐng)失敗??
}????????
p->next?=?*head;????
p->data?=?e;??????
*head?=?p;????????
printf(“%c\n“e);????
return;?????
}???????
q?=?*head;?
j?=?0;???
while(p?!=?NULL?&&?j? {?????????
q?=?p?;????
p?=?p->next;??
j++;????
}???????
if(i?==?j)?
{????????
if((p?=?(Node?*)malloc(sizeof(Node)))?==?NULL)
{????????????
//節(jié)點(diǎn)空間申請(qǐng)失敗
}??????
p->data?=?e;?
p->next?=?q->next;?//將新申請(qǐng)的節(jié)點(diǎn)next指向當(dāng)時(shí)的操作節(jié)點(diǎn)?
q->next?=?p;?????//將原操作節(jié)點(diǎn)next指向新申請(qǐng)的節(jié)點(diǎn)???????
printf(“%c\n“e);???????
return;?????
}?????
//當(dāng)?i?!=?j?時(shí)的情況輸出error?????
printf(“插入位置為%d的節(jié)點(diǎn)失敗?!\n“i);
}?
void?printNode(Node?**head)?
{?????
Node?*p?=?*head;????
if?(p?==?NULL)????
{????????
printf(“%s\n“NULL);
}????
while(p?!=?NULL)?????
{?????????
printf(“%c\n“p->data);??
p?=?p->next;??
}???????
}???
/*?
*刪除操作要求是?要?jiǎng)h除節(jié)點(diǎn)和其前一個(gè)節(jié)點(diǎn)都不可為空并標(biāo)記其位置指針??
*因此要確保p?q都不為空?
*節(jié)點(diǎn)從0計(jì)數(shù)?
第0個(gè)節(jié)點(diǎn)是頭指針?biāo)腹?jié)點(diǎn)?*/?
void?deleteNode(Node?**?head??int?i?)?
{???????????
Node?*?q;//標(biāo)識(shí)要?jiǎng)h除節(jié)點(diǎn)的前一個(gè)節(jié)點(diǎn)??
Node?*?p;//標(biāo)識(shí)要?jiǎng)h除的節(jié)點(diǎn)????
int?j;??????????
if(*head?==?NULL)??
{?????????
//empety?node?list????????
printf(“刪除元素不存在!\n“);?
return;???
}???
p?=?*head;????
if(i?==?0)???
{????????
*head?=?p->next;?
free(p);??????
printf(“成功刪除位置為%d的元素!\n“i);
//p?=?NULL;????????
return?;?????
}???????
q?=?*head;??
j?=?0;???
while(p->next?!=?NULL?&&?j? {????????
q?=?p;???
p?=?p->next;???
j?++;????
}???????
if(i?==?j)????
{????????
q->next?=?p->next;?
free(p);???
printf(“成功刪除位置為%d的元素!\n“i);?
return;????
//p?=?NULL;??
}????
//當(dāng)?i?!=?j?時(shí)的情況輸出error??
printf(“要?jiǎng)h除位置為%d的元素不存在!\n“i);??
}
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件???????2887??2012-09-21?18:11??CNode\CNode.cpp
?????文件????7695360??2012-09-21?18:11??CNode\CNode.ncb
?????文件????????875??2012-09-21?17:54??CNode\CNode.sln
????..A..H.?????11264??2012-09-21?18:11??CNode\CNode.suo
?????文件???????4492??2012-09-21?17:54??CNode\CNode.vcproj
?????文件???????1427??2012-09-21?18:11??CNode\CNode.vcproj.MICROSO-BJT4QU3.Administrator.user
?????文件???????1171??2012-09-21?17:54??CNode\ReadMe.txt
?????文件????????210??2012-09-21?17:54??CNode\stdafx.cpp
?????文件????????233??2012-09-21?17:54??CNode\stdafx.h
?????文件????????498??2012-09-21?17:54??CNode\targetver.h
?????目錄??????????0??2012-09-21?18:12??CNode
-----------?---------??----------?-----??----
??????????????7718417????????????????????11
評(píng)論
共有 條評(píng)論