C語言鍊錶綜合操作

2021-06-04 18:32:06 字數 4089 閱讀 3202

/*----------------------------------預處理命令-----------------------------------------*/

#include

#include

#define len sizeof(struct student)

#define format "%ld,%f"

#define p_format "%ld   %5.1f\n"      

#define null 0

/*---------------------------------構造結構體------------------------------------------*/

struct student//構造乙個結構體型別 

;int n = 0;                   //用於統計節點個數的全域性變數

/*---------------------------------建立鍊錶函式--------------------------------------- */

struct student *creat(void) //建立鍊錶的函式

else

p2 = p1;//將p2指向表尾(當前鍊錶的最後乙個節點)

p1 = (struct student *)malloc(len);//再開闢乙個新節點,使p1指向它

scanf(format,&p1->num,&p1 ->score);

}p2->next = null;//若新開闢的節點num=0,那麼表尾節點的指標變數置為null

return (head);

}/*-----------------------------------輸出鍊錶函式--------------------------------------- */

void print(struct student *head)//輸出鍊錶的函式

while (p != null);

}else

return;

}/*----------------------------------刪除鍊錶節點函式----------------------------------- */

struct student *del(struct student *head,long num)//刪除鍊錶節點的函式

else

if (p1->num == num)//判斷p1是否是要刪除的節點

else

printf("\n%ld has been deleted\n",num);

n=n-1;

}else}

return (head);

}/*----------------------------------增加鍊錶節點函式----------------------------------- */

struct student *insert(struct student *head,struct student *stu)//插入鍊錶節點的函式

else                        

if (p0->num  <= p1->num) //判斷是否找到大於等於要插入的節點num的節點

else

}else                    //插到表尾之後

}n = n + 1;

return (head);

}/*----------------------------------主函式----------------------------------- */

int main(void)

printf("\ninput the inserted record: ");

stu = (struct student*)malloc(len);

scanf(format,&stu->num, &stu->score);

while (stu->num != 0)

printf("the end\n");

return 0;

}/*在vc++6.0中的輸出結果為:

------------------------------------------

input record:

10101,90

10102,876

10103,87

10104,100

10105,82

10106,76

10107,92

10108,91

10109,76

10110,82

10111,988

0,0now,these 11 record are:

10101    90.0

10102   876.0

10103    87.0

10104   100.0

10105    82.0

10106    76.0

10107    92.0

10108    91.0

10109    76.0

10110    82.0

10111   988.0

input the deleted number:10102

10102 has been deleted

now,these 10 record are:

10101    90.0

10103    87.0

10104   100.0

10105    82.0

10106    76.0

10107    92.0

10108    91.0

10109    76.0

10110    82.0

10111   988.0

input the deleted number:10104

10104 has been deleted

now,these 9 record are:

10101    90.0

10103    87.0

10105    82.0

10106    76.0

10107    92.0

10108    91.0

10109    76.0

10110    82.0

10111   988.0

input the deleted number:10111

10111 has been deleted

now,these 8 record are:

10101    90.0

10103    87.0

10105    82.0

10106    76.0

10107    92.0

10108    91.0

10109    76.0

10110    82.0

input the deleted number:0

input the inserted record: 10102,81

now,these 9 record are:

10101    90.0

10102    81.0

10103    87.0

10105    82.0

10106    76.0

10107    92.0

10108    91.0

10109    76.0

10110    82.0

input the inserted record: 10104,93

now,these 10 record are:

10101    90.0

10102    81.0

10103    87.0

10104    93.0

10105    82.0

10106    76.0

10107    92.0

10108    91.0

10109    76.0

10110    82.0

input the inserted record: 0,0

the end

press any key to continue

l鍊錶的綜合操作

define null 0 define len sizeof struct student struct student main p2 next null printf head 0x,last 0x n head,p1 鍊錶生成結束 輸出鍊錶 p1 head printf nnow the n...

對鍊錶的綜合操作

對鍊錶的綜合操作 功能有建立,排序,插入,刪除,輸出 include include typedef int elemtype typedef struct nodetype nodetype,linktype linktype create p2 next null free p1 return ...

鍊錶操作集合 C語言

主要是給自己以後複習知識,還有進行修改優化用的 歡迎批評,其中鍊錶合併的就地合併我並未掌握,我用了新的頭結點以後要補上 首先是標頭檔案定義 ifndef andrew define andrew include include include include include define size ...