c語言鏈表基本操作(帶有創(chuàng)建鏈表 刪除 打印 插入)
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define LEN sizeof(struct Student)
struct Student
{
long num;
float score;
struct Student*next;
};
int n;
int main()
{
/*-----------------------------程序描述--------------------------------------------
題目:寫出一個(gè)主函數(shù),分別調(diào)用建立鏈表的函數(shù)create(),輸出鏈表的函數(shù)print(),
刪除鏈表結(jié)點(diǎn)的函數(shù)del(),插入結(jié)點(diǎn)的函數(shù)insert(),一共5個(gè)函數(shù)。
Author:KillerLegend
Date: 2013.12.6
----------------------------------------------------------------------------------*/
//函數(shù)聲明
struct Student* create();//創(chuàng)建動(dòng)態(tài)鏈表的函數(shù)聲明,函數(shù)類型為student結(jié)構(gòu)體類型,返回頭指針
struct Student* del(struct Student* ,long);//刪除指定位置結(jié)點(diǎn)的函數(shù)聲明,參數(shù):鏈表頭結(jié)點(diǎn)+刪除結(jié)點(diǎn)位置+返回頭指針
struct Student* insert(struct Student*,struct Student*);//插入一個(gè)Student類型數(shù)據(jù)的函數(shù)聲明
void print(struct Student*);//輸出鏈表中數(shù)據(jù)的函數(shù)聲明,參數(shù)為鏈表的頭指針
//定義變量
struct Student *head,*stu;//定義動(dòng)態(tài)鏈表的頭指針與新的結(jié)點(diǎn)
long del_num;
//建立鏈表操作
printf("Input records:\n");
head = create();//建立鏈表并返回頭指針
print(head);//輸出全部結(jié)點(diǎn)
//刪除結(jié)點(diǎn)操作
printf("\nInput the deleted number:");
scanf("%ld",&del_num);
while(del_num!=0)//當(dāng)輸入學(xué)號為0時(shí)結(jié)束循環(huán)
{
head = del(head,del_num);//刪除結(jié)點(diǎn)后返回鏈表的頭地址
print(head);//輸出全部結(jié)點(diǎn)
printf("Input the deleted number:");
scanf("%ld",&del_num);
}
//插入結(jié)點(diǎn)操作
printf("\nInput the inserted number:");
stu=(struct Student*)malloc(LEN);//每插入一個(gè)結(jié)點(diǎn)需要開辟一個(gè)新的結(jié)點(diǎn)
scanf("%ld %f",&stu->num,&stu->score);
while(stu->num!=0)//當(dāng)輸入的學(xué)號為0時(shí)結(jié)束循環(huán)
{
head = insert(head,stu);//返回鏈表的頭地址
print(head);
printf("\nInput the inserted number:");
stu = (struct Student*)malloc(LEN);
scanf("%ld %f",&stu->num,&stu->score);
}
return 0;
}
//建立鏈表的函數(shù)
struct Student* create()
{
struct Student *head;
struct Student *p1,*p2;
n=0;
p1=p2=(struct Student *)malloc(LEN);
scanf("%ld %f",&p1->num,&p1->score);
head=NULL;
while(p1->num!=0)
{
n++;
if(n==1)head=p1;
else p2->next=p1;//第一次執(zhí)行時(shí),這一步是將頭指針指向自身,當(dāng)n從2起,這一步用于使p2指向下一個(gè)元素
p2=p1;//使p2和p1指向同一個(gè)存儲(chǔ)區(qū)
p1=(struct Student*)malloc(LEN);//開辟動(dòng)態(tài)存儲(chǔ)區(qū),強(qiáng)制返回struct Student類型的指針
scanf("%ld %f",&p1->num,&p1->score);
}
p2->next=NULL;
return (head);
}
//刪除結(jié)點(diǎn)的函數(shù)
struct Student* del(struct Student* head,long num)
{
struct Student *p1,*p2;
if(head==NULL)
{
printf("List null!\n");
return (head);
}
p1=head;
while(num!=p1->num && p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(num==p1->num)
{
if(p1==head)
{
head=p1->next;
}
else
{
p2->next=p1->next;
}
printf("Delete:%ld\n",num);
n=n-1;
}
else
{
printf("%ld not been found!",num);
}
return (head);
}
//插入結(jié)點(diǎn)的函數(shù)
struct Student* insert(struct Student* head,struct Student * stud)
{
struct Student *p0,*p1,*p2;
p1=head;
p0=stud;
if(head==NULL)//原來的鏈表是空表
{
head=p0;p0->next=NULL;//空表時(shí)使插入的結(jié)點(diǎn)作為頭結(jié)點(diǎn)
}
else//如果不是空表,則遍歷尋找合適的插入位置
{
while((p0->num>p1->num)&&(p1->next!=NULL))//按學(xué)號順序插入,如果插入的學(xué)號數(shù)字比較大,則應(yīng)該向后推移
{
p2=p1;
p1=p1->next;//后移
}
}
if(p0->num<=p1->num)//找到插入的位置,插入的位置是p1所指向的位置之前,也就是p2指向的位置
{
if(head==p1)head=p0;//如果插入的位置是頭位置之前,則使head指向p0
else p2->next=p0;//如果不是頭位置之前,則使p2的next指針指向插入的數(shù)據(jù)地址即p0
p0->next=p1;//使p0的next指針指向p1,完成了數(shù)據(jù)的加入
}
else//插入的學(xué)號位置在最后一個(gè)
{
p1->next=p0;
p0->next=NULL;
}
n=n+1;//記錄數(shù)加一
return(head);
}
//輸出鏈表的函數(shù)
void print(struct Student * head)
{
struct Student * p;
printf("Now,These %d records are:\n",n);
p=head;
if(head!=NULL)
do
{
printf("%ld %5.1f\n",p->num,p->score);
p=p->next;
}while(p!=NULL);
}
欄 目:C語言
本文標(biāo)題:c語言鏈表基本操作(帶有創(chuàng)建鏈表 刪除 打印 插入)
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/3855.html
您可能感興趣的文章
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用函數(shù)刪除字符
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)式函數(shù)庫
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對數(shù)怎么表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求階乘


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery