NODE * insert_note(NODE * head,NODE * p,int i)
在玉屏等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站設(shè)計、成都網(wǎng)站制作 網(wǎng)站設(shè)計制作按需求定制制作,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),全網(wǎng)整合營銷推廣,成都外貿(mào)網(wǎng)站建設(shè),玉屏網(wǎng)站建設(shè)費用合理。
{
NODE *pb=head,*pf=NULL ;
int n=0;
if(head==NULL)//如果為空就建立,空間在傳入前申請好
{
head=p;
p-next=NULL;
}
else
{
for (;i0;i--)
pb=pb-next;
pf=pb-next;
pb-next=p;
p-next=pf; //在表后插入
}
return head;
}
首先,主函數(shù)中,“請輸入插入的數(shù)據(jù)”那里scanf應(yīng)該是b,這是引發(fā)崩潰的原因。
其次,insert函數(shù)的目的應(yīng)該是想插入數(shù)據(jù)后仍是有序鏈表。但你的insert函數(shù)邏輯太亂,有些不必要的判斷,我修正了你的代碼,貼給你看看。(雖然你insert是想保證有序,但你在創(chuàng)建的時候沒有保證有序,所以最終結(jié)果不一定是有序。例如,創(chuàng)建 1,5,2,插入3,最后輸出的是 1,3,5,2)
代碼修改:
scanf("%d", b);
重寫了insert函數(shù),簡化邏輯;
動態(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é)點?
void?freeNode(struct?link?*); //釋放空間
int?main()
{
char?c;
struct?link?*head?=?NULL;
printf("要創(chuàng)建一個鏈表嗎?");
scanf("?%c",?c);
while?(c?==?'y'?||?c?==?'Y')
{
head?=?add(head);
printf("要繼續(xù)創(chuàng)建節(jié)點嗎?");
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;//一個臨時指針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);
}
//insert "OL" into Order_Linear_List @ "i"
Status ListInsert_OL(Order_Linear_List L , int i , Order_Linear_List OL){
cout"this function begain to run ...\n";
//check the "i" illegal or not
if(i1 || iL.length) return ERROR ;
//realloc the RAM when the previous storage space is full ...
if(L.length = L.listSize){
Order_Linear_List * newBaseAdd ;
newBaseAdd = (Order_Linear_List *)realloc(L.listBase,(L.listSize+LIST_INCREAMENT)*sizeof(Order_Linear_List));
if(!newBaseAdd)return(OVERFLOW);
L.listBase = newBaseAdd ;
L.listSize += LIST_INCREAMENT ;
}
//get the index of where to insert the element
Order_Linear_List *q ;
q = L.listBase[i-1];
//move the element behand of the insert-index
Order_Linear_List *p ;
for (p = L.listBase[L.length-1] ; p = q ; --p) *(p+1) = *p ;
*q = OL ;
L.length += 1;
return OK ;
}