#include "sll_node.h"

創(chuàng)新互聯(lián)是一家專業(yè)提供應(yīng)縣企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、H5響應(yīng)式網(wǎng)站、小程序制作等業(yè)務(wù)。10年已為應(yīng)縣眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡(luò)公司優(yōu)惠進(jìn)行中。
#include stdlib.h
#define FALSE 0
#define TRUE 1
// insertNode2:把newValue的值插入到遞增排序的鏈表中,正確返回TRUE,錯(cuò)誤返回FALSE
// nextp是指向當(dāng)前節(jié)點(diǎn)的指針,最初是頭指針
int insertNode2(Node **nextp, int newValue)
{
Node *newNode; // 新節(jié)點(diǎn)指針
Node *current; // 當(dāng)前節(jié)點(diǎn)指針
current = *nextp; // 最初當(dāng)前節(jié)點(diǎn)為nextp指針指向的節(jié)點(diǎn)
// 查找新插入節(jié)點(diǎn)的位置
while (current != NULL current-value newValue)
{
nextp = ¤t-next;
current = current-next;
}
// 為新節(jié)點(diǎn)分配內(nèi)存
newNode = (Node *)malloc(sizeof(Node));
if (newNode == NULL)
return FALSE;
newNode-value = newValue;
// 統(tǒng)一了插入的步驟。即:每次插入,都是前一個(gè)指針指向新節(jié)點(diǎn),新節(jié)點(diǎn)指向下一個(gè)節(jié)點(diǎn)
*nextp = newNode;
newNode-next = current;
return TRUE;
}
main函數(shù)
[cpp] view plain copy
#include stdio.h
#include stdlib.h
#include time.h
#include "sll_node.h"
int insertNode(Node **rootp, int newValue);
int insertNode2(Node **nextp, int newValue);
int main()
{
srand(time(0));
Node *head = (Node *)malloc(sizeof(Node));
head-next = NULL;
for (int i = 0; i 5; i++)
{
int temp = rand() % 50;
printf("%d\n", temp);
//insertNode(head,temp);
insertNode2(head,temp);
}
Node *p = head-next;
while (p != NULL)
{
printf("%d\n", p-value);
p = p-next;
}
getchar();
getchar();
return 0;
}
#include?"sll_node.h"??
#include?stdlib.h??
#define?FALSE???0??
#define?TRUE????1??
//?insertNode2:把newValue的值插入到遞增排序的鏈表中,正確返回TRUE,錯(cuò)誤返回FALSE??
//?nextp是指向當(dāng)前節(jié)點(diǎn)的指針,最初是頭指針??
int?insertNode2(Node?**nextp,?int?newValue)??
{??
Node?*newNode;?//?新節(jié)點(diǎn)指針??
Node?*current;?//?當(dāng)前節(jié)點(diǎn)指針??
current?=?*nextp;?//?最初當(dāng)前節(jié)點(diǎn)為nextp指針指向的節(jié)點(diǎn)??
//?查找新插入節(jié)點(diǎn)的位置??
while?(current?!=?NULL??current-value??newValue)??
{??
nextp?=?¤t-next;??
current?=?current-next;??
}??
//?為新節(jié)點(diǎn)分配內(nèi)存??
newNode?=?(Node?*)malloc(sizeof(Node));??
if?(newNode?==?NULL)??
return?FALSE;??
newNode-value?=?newValue;??
//?統(tǒng)一了插入的步驟。即:每次插入,都是前一個(gè)指針指向新節(jié)點(diǎn),新節(jié)點(diǎn)指向下一個(gè)節(jié)點(diǎn)??
*nextp?=?newNode;??
newNode-next?=?current;??
return?TRUE;??
}??
main函數(shù)
[cpp]?view?plain?copy?
#include?stdio.h??
#include?stdlib.h??
#include?time.h??
#include?"sll_node.h"??
int?insertNode(Node?**rootp,?int?newValue);??
int?insertNode2(Node?**nextp,?int?newValue);??
int?main()??
{??
srand(time(0));??
Node?*head?=?(Node?*)malloc(sizeof(Node));??
head-next?=?NULL;??
for?(int?i?=?0;?i??5;?i++)??
{??
int?temp?=?rand()?%?50;??
printf("%d\n",?temp);??
//insertNode(head,temp);??
insertNode2(head,temp);??
}??
Node?*p?=?head-next;??
while?(p?!=?NULL)??
{??
printf("%d\n",?p-value);??
p?=?p-next;??
}??
getchar();??
getchar();??
return?0;??
}
首先,主函數(shù)中,“請輸入插入的數(shù)據(jù)”那里scanf應(yīng)該是b,這是引發(fā)崩潰的原因。
其次,insert函數(shù)的目的應(yīng)該是想插入數(shù)據(jù)后仍是有序鏈表。但你的insert函數(shù)邏輯太亂,有些不必要的判斷,我修正了你的代碼,貼給你看看。(雖然你insert是想保證有序,但你在創(chuàng)建的時(shí)候沒有保證有序,所以最終結(jié)果不一定是有序。例如,創(chuàng)建 1,5,2,插入3,最后輸出的是 1,3,5,2)
代碼修改:
scanf("%d", b);
重寫了insert函數(shù),簡化邏輯;
動(dòng)態(tài)分配的內(nèi)存記得釋放,增加freeNode釋放空間
#include?stdio.h
#include?stdlib.h
struct?link
{
int?data;
struct?link?*next;
};
struct?link?*add(struct?link?*head);//創(chuàng)建鏈表?
void?display(struct?link?*head);//輸出數(shù)據(jù)?
struct?link?*insert(struct?link?*head,?int?b);?//插入新節(jié)點(diǎn)?
void?freeNode(struct?link?*); //釋放空間
int?main()
{
char?c;
struct?link?*head?=?NULL;
printf("要?jiǎng)?chuàng)建一個(gè)鏈表嗎?");
scanf("?%c",?c);
while?(c?==?'y'?||?c?==?'Y')
{
head?=?add(head);
printf("要繼續(xù)創(chuàng)建節(jié)點(diǎn)嗎?");
scanf("?%c",?c);
}
display(head);
int?b;
printf("輸入插入的數(shù)據(jù)");
scanf("%d",?b);
head?=?insert(head,?b);
display(head);
freeNode(head);
}
struct?link?*add(struct?link?*head)
{
int?data;
struct?link?*p?=?(struct?link*)malloc(sizeof(struct?link));
if?(head?==?NULL)
{
head?=?p;
}
else
{
struct?link?*pr?=?head;//一個(gè)臨時(shí)指針pr先保存下head的地址?
while?(pr-next?!=?NULL)
{
pr?=?pr-next;
}
pr-next?=?p;
}
printf("輸入數(shù)據(jù)");
scanf("%d",?p-data);
p-next?=?NULL;
return?head;
}
void?display(struct?link?*head)
{
struct?link?*pr?=?head;
while?(pr?!=?NULL)
{
printf("%d\n",?pr-data);
pr?=?pr-next;
}
}
struct?link?*insert(struct?link?*head,?int?b)
{
struct?link?*ptr?=?head,?*prev?=?head;
struct?link?*newNode?=?(struct?link?*)malloc(sizeof(struct?link));
newNode-data?=?b;
while?(ptr??b??ptr-data)?{
prev?=?ptr;
ptr?=?ptr-next;
}
newNode-next?=?ptr;
if?(ptr?==?head) head?=?newNode;
else prev-next?=?newNode;
return?head;
}
void?freeNode(struct?link?*node)?{
if?(!node) return;
freeNode(node-next);
free(node);
}
當(dāng)前題目:c語言單鏈表插入節(jié)點(diǎn)函數(shù),c語言實(shí)現(xiàn)單鏈表的基本操作
轉(zhuǎn)載來于:http://chinadenli.net/article5/dsshdoi.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、關(guān)鍵詞優(yōu)化、電子商務(wù)、網(wǎng)站排名、小程序開發(fā)、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)