?? c1.cpp
字號(hào):
#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{long num;
float score;
struct student *next;
};
int n; /* n為全局變量,本文件模塊中各函數(shù)均可使用它 */
struct student *creat(void)
/* 定義一個(gè)函數(shù)。此函數(shù)帶回一個(gè)指向鏈表頭的指針。 void沒有形參,不要傳遞數(shù)值。 */
{struct student *head;
struct student *p1,*p2;
n=0; /*n是節(jié)點(diǎn)個(gè)數(shù)*/
p1=p2=(struct student *) malloc(LEN); /* 開辟一個(gè)新單元,強(qiáng)制類型轉(zhuǎn)換使malloc返回指針轉(zhuǎn)換為指向struct student類型數(shù)據(jù)的指針*/
scanf("%ld,%f",&p1->num,&p1->score);
head=NULL;
while(p1->num!=0)
{n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *) malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return(head);
}
void print(struct student *head) /* 形參類型修改了 */
{
struct student *p;
printf("\nNow,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);
}
struct student *del(struct student *head,long num) /* num 要?jiǎng)h掉的值*/
{ struct student *p1,*p2;
if(head==NULL)
{printf("\nlist null!\n");goto end;}
p1=head;
while(num!=p1->num&&p1->next!=NULL) /*p1指向的不是所要找的結(jié)點(diǎn),并且后面還有結(jié)點(diǎn) */
{p2=p1;p1=p1->next;} /*p1后移一個(gè)結(jié)點(diǎn)*/
if(num==p1->num) /*找到了*/
{if(p1==head)
head=p1->next; /*若p1指向的是表結(jié)點(diǎn),把第二個(gè)結(jié)點(diǎn)地址賦予head*/
else
p2->next=p1->next; /*否則將下一結(jié)點(diǎn)的地址賦給前一結(jié)點(diǎn)的地址*/
printf("delete:%ld\n",num);
n=n-1;
}
else printf("%ld not been found!\n",num); /*找不到該結(jié)點(diǎn)*/
end:
return(head);
}
struct student *insert(struct student *head,struct student *stud)/*函數(shù)參數(shù)head和stud。stud是一個(gè)指針變量,從實(shí)參傳來(lái)待插入結(jié)點(diǎn)的地址給stud*/
{ struct student *p0,*p1,*p2;
p1=head; /*p1指向第一個(gè)結(jié)點(diǎn)*/
p0=stud; /*p0指向待插入的結(jié)點(diǎn)*/
if(head==NULL) /*原來(lái)的鏈表是空表*/
{head=p0;p0->next=NULL;} /*p0指向的結(jié)點(diǎn)作為頭結(jié)點(diǎn)*/
else
{while((p0->num>p1->num)&&(p1->next!=NULL))
{p2=p1; /*p2指向剛才p1指向的結(jié)點(diǎn)*/
p1=p1->next; /*p1后移一個(gè)結(jié)點(diǎn)*/
}
if(p0->num<=p1->num)
{if(head==p1)head=p0; /*插到原來(lái)第一個(gè)結(jié)點(diǎn)之前*/
else p2->next=p0; /*插到p2指向的節(jié)點(diǎn)之后*/
p0->next=p1;
}
else
{p1->next=p0;p0->next=NULL;} /*插到最后的結(jié)點(diǎn)之后*/
}
n=n+1; /*結(jié)點(diǎn)數(shù)加1*/
return(head);
}
void main()
{ struct student *head,*stu;
long del_num;
printf("input records:\n");
head=creat(); /*返回頭指針*/
print(head); /*輸出全部結(jié)點(diǎn)*/
printf("\ninput the deleted number:");
scanf("%ld",&del_num); /*輸入要?jiǎng)h除的學(xué)號(hào)*/
while(del_num!=0)
{
head=del(head,del_num); /*刪除后鏈表的頭地址*/
print(head); /*輸出全部結(jié)點(diǎn)*/
printf("input the deleted number:");
scanf("%ld",&del_num);
}
printf("\ninput the inserted record:");
stu=(struct student *)malloc(LEN);
scanf("%ld,%f",&stu->num,&stu->score);
while(stu->num!=0)
{ head=insert(head,stu);
print(head);
printf("input the inserted record:");
stu=(struct student *)malloc(LEN);
scanf("%ld,%f",&stu->num,&stu->score);
}
/*head=insert(head,&stu); 返回地址 */
/*print(head); */
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -