2-2 学生成绩链表处理 (20 分)
生活随笔
收集整理的這篇文章主要介紹了
2-2 学生成绩链表处理 (20 分)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2-2?學生成績鏈表處理?(20?分)
本題要求實現兩個函數,一個將輸入的學生成績組織成單向鏈表;另一個將成績低于某分數線的學生結點從鏈表中刪除。
函數接口定義:
struct stud_node *createlist(); struct stud_node *deletelist( struct stud_node *head, int min_score );函數createlist利用scanf從輸入中獲取學生的信息,將其組織成單向鏈表,并返回鏈表頭指針。鏈表節點結構定義如下:
struct stud_node {int num; /*學號*/char name[20]; /*姓名*/int score; /*成績*/struct stud_node *next; /*指向下個結點的指針*/ };輸入為若干個學生的信息(學號、姓名、成績),當輸入學號為0時結束。
函數deletelist從以head為頭指針的鏈表中刪除成績低于min_score的學生,并返回結果鏈表的頭指針。
裁判測試程序樣例:
#include <stdio.h> #include <stdlib.h>struct stud_node {int num;char name[20];int score;struct stud_node *next; };struct stud_node *createlist(); struct stud_node *deletelist( struct stud_node *head, int min_score );int main() {int min_score;struct stud_node *p, *head = NULL;head = createlist();scanf("%d", &min_score);head = deletelist(head, min_score);for ( p = head; p != NULL; p = p->next )printf("%d %s %d\n", p->num, p->name, p->score);return 0; }/* 你的代碼將被嵌在這里 */輸入樣例:
1 zhang 78 2 wang 80 3 li 75 4 zhao 85 0 80輸出樣例:
2 wang 80 4 zhao 85一個存一個刪
鏈表操作
struct stud_node *createlist(){int number,score;char name[100];struct stud_node *p=NULL,*head=NULL,*tail=NULL;//當前節點 頭結點 尾部節點scanf("%d",&number);while(number!=0){ scanf("%s %d",name,&score);//輸入信息p=(struct stud_node *)malloc(sizeof(struct stud_node));//申請內存p->num=number;p->score=score;strcpy(p->name,name);p->next=NULL;//置空if(NULL==head)head=p;//處理第一個特殊情況else tail->next=p;//存表tail=p;//尾結點后移scanf("%d",&number);}return head;//返回頭結點 } struct stud_node *deletelist( struct stud_node *head, int min_score ) { struct stud_node *ptr=NULL,*ptr1=NULL,*ptr2=NULL;if(head==NULL) return NULL;for(ptr=head;ptr!=NULL;ptr=ptr->next){if(ptr->score<min_score){if(ptr==head)head=head->next;else ptr1->next=ptr->next;free(ptr);//刪}else ptr1=ptr;}return head; }?
總結
以上是生活随笔為你收集整理的2-2 学生成绩链表处理 (20 分)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 摸鱼周报年终总结
- 下一篇: TS战队拿下冠军,微博拿下TS战队