2430: C語言習題 鏈表建立,插入,刪除,輸出 時間限制: 1 Sec 記憶體限制: 128 MB
提交: 576 解決: 280
題目描述
編寫一個函數creatlink,用來建立一個動態鏈表。(包含學號和成績)
編寫一個函數printlink,用來輸出一個鏈表。
編寫一個函數dellink,用來刪除動態鏈表中一個指定的結點(由實參指定某一學號,表示要刪除該學生結點)。
編寫一個函數insertlink,用來向動態鏈表插入一個結點。
編寫一個函數freelink,用來釋放一個動態鏈表。 輸入
輸入多個學生的學號和成績,建立動態鏈表,以0 0 結束
輸入學號,刪除鏈表中的對應結點
插入兩個鏈表結點 輸出
輸出的鏈表 範例輸入
1001 1001002 951005 901008 760 010051006 981009 99
範例輸出
1001 100.001002 95.001006 98.001008 76.001009 99.00
提示
主函數已給定如下,提交時不需要包含下述主函數
/* C代碼 */
int main()
{
struct student *creatlink(void);
struct student *dellink(struct student *,long);
struct student *insertlink(struct student *,struct student *);
void printlink(struct student *);
void freelink(struct student *);
struct student *head,stu;
long del_num;
head=creatlink();
scanf("%ld",&del_num);
head=dellink(head,del_num);
scanf("%ld%f",&stu.num,&stu.score);
head=insertlink(head,&stu);
scanf("%ld%f",&stu.num,&stu.score);
head=insertlink(head,&stu);
printlink(head);
freelink(head);
return 0;
}
/* C++代碼 */
int main()
{
student *creatlink(void);
student *dellink(student *,long);
student *insertlink(student *,student *);
void printlink(student *);
void freelink(student *);
student *head,stu;
long del_num;
head=creatlink();
cin>>del_num;
head=dellink(head,del_num);
cin>>stu.num>>stu.score;
head=insertlink(head,&stu);
cin>>stu.num>>stu.score;
head=insertlink(head,&stu);
cout<<setiosflags(ios::fixed);
cout<<setprecision(2);
printlink(head);
freelink(head);
return 0;
}
迷失在幽穀中的鳥兒,獨自飛翔在這偌大的天地間,卻不知自己該飛往何方……
#include<stdio.h>#include<iostream>#include<stdlib.h>#include<string.h>#include<iomanip>using namespace std;struct student{ long int num; float score; struct student *next;};struct student *creatlink(){ struct student *p1,*p2,*head=NULL; p1=p2=(struct student*)malloc(sizeof(struct student)); while(~scanf("%ld%f",&p1->num,&p1->score)&&(p1->score||p1->num)) { if(head==NULL)head=p1; else p2->next=p1; p2=p1; p1=p1->next=(struct student*)malloc(sizeof(struct student)); } p2->next=NULL; return head;};struct student *dellink(struct student *a,long b){ struct student *head=a,*p=a,*p2=a; while(p!=NULL) { if(p->num==b) p2->next=p->next; p2=p; p=p->next; } return head;};struct student *insertlink(struct student *a,struct student *b){ struct student *head=a,*p=a,*p2=a,*k; k=(struct student*)malloc(sizeof(struct student)); k->num=b->num,k->score=b->score; int n=0; while(p!=NULL) { if(p->num>b->num) { p2->next=k; k->next=p; n=1; } p2=p; p=p->next; } if(n==0)p2->next=k,k->next=NULL; return head;};void printlink(struct student *a){ struct student *p=a; while(p!=NULL) { printf("%ld %.2f\n",p->num,p->score); p=p->next; }}void freelink(struct student *a){ while(a!=NULL) { delete(a); a=a->next; }}int main(){ student *creatlink(void); student *dellink(student *,long); student *insertlink(student *,student *); void printlink(student *); void freelink(student *); student *head,stu; long del_num; head=creatlink(); cin>>del_num; head=dellink(head,del_num); cin>>stu.num>>stu.score; head=insertlink(head,&stu); cin>>stu.num>>stu.score; head=insertlink(head,&stu); cout<<setiosflags(ios::fixed); cout<<setprecision(2); printlink(head); freelink(head); return 0;}