數據結構對學生成績鏈表排序
Ⅰ c語言!!!程序設計:建立一個學生信息鏈表,包括學號,姓名,成績.(實現添加,刪除,查詢,排序,平均)
#include<iostream>
using namespace std;
struct stu{
char name[20];
int num;
int age;
char sex;
int grade;
struct stu *next;
};
struct stu *mythis,*mynew;
void newrecord(struct stu *head)
{
mythis=head->next;
while(mythis!=NULL)
mythis=mythis->next;
mynew=(struct stu *)malloc(sizeof(struct stu));
cin>>mynew->name>>mynew->num>>mynew->age>>mynew->sex>>mynew->grade;
mynew->next=NULL;
if(mythis==NULL)
{
mythis=(struct stu *)malloc(sizeof(struct stu));
mythis=mynew;
}
}
void listall(stu *head)
{
mythis=head->next;
while(mythis!=NULL)
{
cout<<mythis->name<<mythis->num<<mythis->age<<mythis->sex<<mythis->grade;
mythis=mythis->next;
}
}
int main()
{
char decide;
struct stu *head;
head=(struct stu *)malloc(sizeof(struct stu));
head->next=NULL;
while(1)
{
cout<<"Please input decide:"<<endl;
cin>>decide;
if(decide=='n')
newrecord(head);
else
if(decide=='1')
listall(head);
else
return 0;
}
}
拓展資料
C語言是一門通用計算機編程語言,應用廣泛。C語言的設計目標是提供一種能以簡易的方式編譯、處理低級存儲器、產生少量的機器碼以及不需要任何運行環境支持便能運行的編程語言。
盡管C語言提供了許多低級處理的功能,但仍然保持著良好跨平台的特性,以一個標准規格寫出的C語言程序可在許多電腦平台上進行編譯,甚至包含一些嵌入式處理器(單片機或稱MCU)以及超級電腦等作業平台。
二十世紀八十年代,為了避免各開發廠商用的C語言語法產生差異,由美國國家標准局為C語言制定了一套完整的美國國家標准語法,稱為ANSI C,作為C語言最初的標准。目前2011年12月8日,國際標准化組織(ISO)和國際電工委員會(IEC)發布的C11標準是C語言的第三個官方標准,也是C語言的最新標准,該標准更好的支持了漢字函數名和漢字標識符,一定程度上實現了漢字編程。
Ⅱ c語言數據結構(雙向鏈表排序)
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
float data;
struct node *pre;
struct node *next;
}*DoubleNode;
void sort_DoubleNode(DoubleNode &head, float data)
{
DoubleNode node = (DoubleNode)malloc(sizeof(struct node));
DoubleNode p,q;
node->data = data;
if(head == NULL)
{
head = node;
head->next = NULL;
head->pre = NULL;
return;
}
p = head;
while(p != NULL && p->data < data)
{
q = p;
p = p->next;
}
if(p == head)
{
node->next = head;
node->pre = NULL;
head ->pre = node;
head = node;
}
else{
q->next = node;
node->pre = q;
node->next = p;
if(p != NULL) p->pre = node;
}
}
void out_DoubleNode(DoubleNode &head)
{
DoubleNode p = head;
while(p != NULL)
{
printf("%.4f ", p->data);
p = p->next;
}
printf("\n");
}
void main()
{
int n;
float data;
DoubleNode head = NULL;
scanf("%d", &n);
for(int i=0; i<n; ++i)
{
scanf("%f", &data);
sort_DoubleNode(head, data);
}
out_DoubleNode(head);
}
/*
15
23.4 4.5 56 67 7.5 90 8.97 90.6 25.6 145
856 10.2 0.36 54.6 56.1
*/
第二題: 測試數據同第一題
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
float data;
struct node *pre;
struct node *next;
}*DoubleNode;
bool bigger(float a, float b)
{
return (a > b);
}
bool smaller(float a, float b)
{
return (a < b);
}
void sort_DoubleNode(DoubleNode &head, float data, bool (*comp)(float a, float b))
{
DoubleNode node = (DoubleNode)malloc(sizeof(struct node));
DoubleNode p,q;
node->data = data;
if(head == NULL)
{
head = node;
head->next = NULL;
head->pre = NULL;
return;
}
p = head;
while(p != NULL && comp(p->data, data))
{
q = p;
p = p->next;
}
if(p == head)
{
node->next = head;
node->pre = NULL;
head ->pre = node;
head = node;
}
else{
q->next = node;
node->pre = q;
node->next = p;
if(p != NULL) p->pre = node;
}
}
void merge_DoubleNode(DoubleNode &head, DoubleNode &head1, DoubleNode &head2)
{
DoubleNode p = head2;
head = head1;
if(head1 == NULL || head2 == NULL) return;
while((head1->next != NULL) && (head2 != NULL))
{
p = head2->next;
head1->next->pre = head2;
head2->next = head1->next;
head2->pre = head1;
head1->next = head2;
head1 = head2->next;
head2 = p;
}
if((head1->next == NULL) && (head2 != NULL))
{
head1->next = head2;
head2->pre = head1;
}
}
void out_DoubleNode(DoubleNode &head)
{
DoubleNode p = head;
while(p != NULL)
{
printf("%.4f ", p->data);
p = p->next;
}
printf("\n");
}
void main()
{
int n;
float data;
DoubleNode head = NULL;
DoubleNode head1 = NULL;
DoubleNode head2 = NULL;
scanf("%d", &n);
for(int i=0; i<n; ++i)
{
scanf("%f", &data);
if((i+1)%2 == 0) sort_DoubleNode(head2, data, &bigger);
else sort_DoubleNode(head1, data, &smaller);
}
merge_DoubleNode(head, head1, head2);
out_DoubleNode(head);
}
/*
15
23.4 4.5 56 67 7.5 90 8.97 90.6 25.6 145
856 10.2 0.36 54.6 56.1
*/
Ⅲ 編寫一個程序,要求能完成排序和查找,分別使用鏈表,數組和二叉樹等數據結構,比較各種方法的優缺點。
思路很簡單,根來放在源0位置,以後假定當前位置是i,那麼左子結點在2i+1,右子結點在2i+2。比如根的左子結點在1,右子結點在2。結點1的左子結點在3,右子結點在4。定義一種空值表示沒有子結點,比如empty。
假定一個結點由3個成員組成:value, left, right
數組假定是全局的,如果不是可以作為參數傳送。
遞歸實現比較簡單:
void btree2array(node, index)
{
if(node == NULL)
array[index] = empty;
array[index] = node->value;
btree2array(node->left, index * 2 + 1);
btree2array(node->right, index * 2 + 2);
}
開始調用的時候就一句話:
btree2array(root, 0);
Ⅳ 數據結構實驗--學生成績管理,要求建立學生成績表,用單鏈表實現
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct
{
char name[20];
char num[10];
float score;
}Grade;
typedef struct Lnode
{
Grade mark;
struct Lnode *next;
}Lnode,*Linklist;
Linklist create()
{
int flag=1;
Linklist p,head;
head=NULL;
while(flag!=0)
{
p=(Linklist)malloc(sizeof(Lnode));
printf("請輸入學生的姓名、學生的學號以及學生的成績\n");
scanf("%s%s%f",p->mark.name,p->mark.num,&p->mark.score);
fflush(stdin);
p->next=head;
head=p;
printf("請輸入flag的值,為0或者為1,0代表結束輸入、1代表繼續輸入\n");
scanf("%d",&flag);
fflush(stdin);
}
return head;
}
void output(Linklist head)
{
Linklist p;
p=(Linklist)malloc(sizeof(Lnode));
printf("學生姓名、學號、成績分別為:\n");
for(p=head;p!=NULL;p=p->next)
printf("%s %s %.2f\n",p->mark.name,p->mark.num,p->mark.score);
}
void search(Linklist head,char *num)
{
Linklist p;
p=(Linklist)malloc(sizeof(Lnode));
for(p=head;p!=NULL && strcmp(p->mark.num,num)!=0;p=p->next);
printf("查詢的結果為\n");
printf("%s %s %.2f\n",p->mark.name,p->mark.num,p->mark.score);
}
void insert(Linklist head,Grade mark,char *num)//在學號num之前插入一條學生成績記錄
{
Linklist p,q;
p=(Linklist)malloc(sizeof(Lnode));
q=(Linklist)malloc(sizeof(Lnode));
for(p=head;p!=NULL && strcmp(p->next->mark.num,num)!=0;p=p->next);
strcpy(q->mark.name,mark.name);
strcpy(q->mark.num,mark.num);
q->mark.score=mark.score;
q->next=p->next;
p->next=q;
}
void delet(Linklist head,char *num)
{
Linklist p;
p=(Linklist)malloc(sizeof(Lnode));
for(p=head;p!=NULL && strcmp(p->next->mark.num,num)!=0;p=p->next);
p->next=p->next->next;
}
void main()
{
Linklist head;
Grade mark;
head=(Linklist)malloc(sizeof(Lnode));
head=NULL;
head=create();
printf("請輸入姓名、學號、成績");
scanf("%s%s%f",mark.name,mark.num,&mark.score);
output(head);
search(head,"03");
insert(head,mark,"02");
output(head);
delet(head,"03");
output(head);
}
Ⅳ 用C語言實現數據結構中常用演算法,如對鏈表的操作、查找、排序等。
#include <iostream.h>
class ram
{
public:
char wenzi[200];
ram *p;
};
ram wo,*ai=&wo;
int num=0;//我申請了幾次內存了
void xie(void);//輸入數據,然後分配內存為下次做准備。
void (void);//把寫入的數據全部顯示出來
int main(void)
{
while(1)//故意死循環
{
xie();
();
}
return NULL;
}
void xie(void)
{
cin.getline(ai->wenzi,200);
cout<<"\n______________________________________________________\n";
if((ai->p=new ram)==NULL) {cout<<"內存申請失敗\n";return;}
ai=ai->p;
num++;
}
void (void)
{
ram *ni;
ni=&wo;
int a;
for(a=num;a>0;a--)
{
cout<<ni->wenzi;
cout<<"\n";
ni=ni->p;
}
cout<<"__________________________________________________________\n";
}
Ⅵ 數據結構鏈表c語言建立學生成績管理系統
#include<stdio.h>
#include<memory.h>
#include<stdlib.h>
#include<string.h>
typedefstructdata{
intnumber;
charname[20];
charid[20];
doublescore[3];
}dataType;
typedefstructlist{
dataTypepauline;
structlist*next;
}*LinkList,*pNode,Node;
void*getMemory(size_tsize){
returnmalloc(size);
}
LinkListgetEmptyList(){
LinkListhead=(pNode)getMemory(sizeof(Node));
memset(head,0,sizeof(Node));
returnhead;
}
intaddNode(LinkListhead,pNodepnode){
pNodeq;
for(q=head;q->next;q=q->next)
if(q->next->pauline.number==pnode->pauline.number){
printf("重復的學號:%d ",pnode->pauline.number);
return0;
}
q->next=pnode;
pnode->next=NULL;
return1;
}
//按學號升排序
voidsortNumber(LinkListhead){
pNodep,q,pt,qt;
p=head;
while(p->next){
qt=p;
q=p->next;
while(q->next){
if(qt->next->pauline.number>q->next->pauline.number)
qt=q;
q=q->next;
}
if(qt!=p){
pt=p->next;
p->next=qt->next;
qt->next=qt->next->next;
p->next->next=pt;
}
p=p->next;
}
}
//按第th門成績降排序,th=1,2,3
voidsortScore(LinkListhead,intth){
pNodep,q,pt,qt;
inti;
if(th<1||th>3)return;
i=th-1;
for(p=head;p->next;p=p->next){
qt=p;
q=p->next;
while(q->next){
if(qt->next->pauline.score[i]<q->next->pauline.score[i])
qt=q;
q=q->next;
}
if(qt!=p){
pt=p->next;
p->next=qt->next;
qt->next=qt->next->next;
p->next->next=pt;
}
}
}
voidshow(LinkListhead){
pNodep;
for(p=head->next;p;p=p->next ){
printf("%d %s %s %.2lf %.2lf %.2lf ",
p->pauline.number,p->pauline.name,p->pauline.id,
p->pauline.score[0],p->pauline.score[1],p->pauline.score[2]);
}
}
pNodereadData(){
pNodepnode=(pNode)getMemory(sizeof(Node));
inti;
printf("學號:");
scanf("%d",&pnode->pauline.number);
printf("姓名:");
scanf("%s",pnode->pauline.name);
printf("身份證:");
scanf("%s",pnode->pauline.id);
for(i=0;i<3;++i){
printf("第%d門成績:",i+1);
scanf("%lf",&pnode->pauline.score[i]);
}
returnpnode;
}
voidmenu(){
printf("******************************** ");
printf("******學生成績管理系統****** ");
printf("******************************** ");
printf("*1、添加學生信息* ");
printf("*2、顯示學生信息* ");
printf("*3、按學號排序* ");
printf("*4、按成績排序* ");
printf("******************************** ");
printf("*0、退出* ");
printf("******************************** ");
}
intmain(){
charop[20];
intselect;
LinkListhead=getEmptyList();
do{
menu();
printf("請選擇:");
fflush(stdin);
fgets(op,20,stdin);
fflush(stdin);
switch(op[0]-'0'){
case1:addNode(head,readData());break;
case2:show(head);break;
case3:sortNumber(head);break;
case4:printf("按第幾門功課排序;");
scanf("%d",&select);
sortScore(head,select);
break;
}
}while(op[0]-'0');
printf("END ");
return0;
}
Ⅶ 數據結構,用C語言實現,基於鏈表的學生成績管理系統,根據學號和姓名創建索引
看可以不咯?#includeintavgGrade(inta[50]){inti,sum=0,max=0,min=0;doubleavg=0.0;max=a[0];min=a[0];for(i=0;imax)max=a[i];if(a[i]a[i]){temp=a[i];a[i]=a[j];a[j]=temp;}}else{if(a[j]a[i]){for(s=49;s>=i;s--)a[s+1]=a[s];break;}a[i]=n;}intdeleteGrade(inta[50]){intx,i,j;printf("請輸入你要刪除的成績:\n");scanf("%d",&x);for(i=0;i<10;i++){if(a[i]==x)for(j=i+1;j<10;j++)a[i]=a[j];a[j]='\0';}}voidmain(){intn,i,a[50];printf("請輸入50個學生的成績:\n");for(i=0;i<50;i++){scanf("%d",&a[i]);}while(1){printf("--------------------------\n");printf("請選擇您的功能:\n\n");printf("0錄入成績\n");printf("1輸出成績\n");printf("2輸出平均分、最高分、最低分\n");printf("3成績降序或升序排列\n");printf("4插入一個成績\n");printf("5刪除用戶給定的成績\n");printf("6退出\n\n");printf("--------------------------\n");scanf("%d",n);switch(n){case0:printf("請輸入50個學生的成績:\n");for(i=0;i<50;i++){scanf("%d",&a[i]);}break;case1:for(i=0;i<50;i++){printf("%d",a[i]);}break;case2:avgGrade(a);break;case3:compositor(a);break;case4:insertGrade(a);break;case5:deleteGrade(a);break;case6:exit(0);}}return0;}
Ⅷ 數據結構中:編寫10個學生成績的鏈表,包括學號、姓名、成績,並用函數求出最高值最低值和平均值
struct stu
{
int id;
char name[20];
float score;
stu *next;
};
stu *create();
double max(stu *h);
double avg(stu *h);
void main()
{
stu *h=create();
cout<<"最大分數是:"<<max(h)<<endl;
cout<<"平均分:"<<avg(h)<<endl;
}
stu *create()
{
stu *h,*p,*q;
h=(stu*)malloc(sizeof(stu));
h->next=NULL;
p=h;
int i;
for(i=0;i<10;i++)
{
q=(stu*)malloc(sizeof(stu));
q->next=NULL;
cout<<"請輸入學號:";
cin>>q->id;
cout<<"請輸入姓名:";
cin>>q->name;
cout<<"請輸入分數:";
cin>>q->score;
p->next=q;
p=q;
}
return h;
}
double max(stu *h)
{
stu *q;
double max=0;
for(q=h->next;q!=NULL;q=q->next)
{
if(max<q->score)
max=q->score;
}
return max;
}
double avg(stu *h)
{
stu *q;
double sum=0;
for(q=h->next;q!=NULL;q=q->next)
{
sum+=q->score;
}
return sum/10;
}
Ⅸ 數據結構 程序設計 用雙向循環鏈表建立一個學生管理系統,要求實現插入,刪除,排序,修改等功能。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct student
{
int num;
char name[10];
float score[3];
float total;
struct student *next,*prior;
}DLNode,*DLinkList;
//建立鏈表
DLinkList Creat_DLinkList()
{
int x;
char y[10];
float s[3];
DLinkList DL=NULL;
DLNode *p=NULL,*q=NULL;
DL=malloc(sizeof(DLNode));
if(DL==NULL)
exit(0);
q=DL;
DL->next=NULL;
DL->prior=NULL;
printf("\nPlease enter students' information:\n");
scanf("%d%s%f%f%f",&x,y,&s[0],&s[1],&s[2]);
while(x!=0)
{
int i;
p=malloc(sizeof(DLNode));
p->num=x;
strcpy(p->name,y);
for(i=0;i<3;i++)
p->score[i]=s[i];
p->total=p->score[0]+p->score[1]+p->score[2];
q->next=p;
p->prior=q;
p->next=DL;
q=p;
scanf("%d%s%f%f%f",&x,y,&s[0],&s[1],&s[2]);
}
DL->prior=q;
return q;
}
//查找
int search(DLinkList r,int n)
{
DLNode *p,*h;
h=r->next;
p=h->next;
while(p!=h)
{
if(p->num==n)
return 1;
p=p->next;
}
return 0;
}
//添加
DLinkList add(DLinkList r)
{
int x;
char y[10];
float s[3];
DLNode *h=r->next,*p=NULL;
p=malloc(sizeof(DLNode));
printf("\nPlease enter students' information:\n");
scanf("%d%s%f%f%f",&x,y,&s[0],&s[1],&s[2]);
while(x!=0)
{
int i;
p=malloc(sizeof(DLNode));
p->num=x;
strcpy(p->name,y);
for(i=0;i<3;i++)
p->score[i]=s[i];
p->total=p->score[0]+p->score[1]+p->score[2];
p->prior=r;
r->next=p;
p->next=h;
h->prior=p;
r=p;
scanf("%d%s%f%f%f",&x,y,&s[0],&s[1],&s[2]);
}
return r;
}
//刪除
void delete(DLinkList r)
{
char na[10];
DLNode *h=r->next;
DLNode *p=h->next;
DLNode *q=NULL;
while(p!=h)
{
q=p->next;
while(q!=h)
{
if(!strcmp(q->name,p->name))
{
q->prior->next=q->next;
q->next->prior=q->prior;
free(q);
}
q=q->next;
}
p=p->next;
}
}
//修改
int modify(DLinkList r,int n)
{
float s[3];
int i;
DLNode *h=r->next;
DLNode *p=h->next;
while(p!=h)
{
if(p->num==n)
{
printf("\nPlease enter new score:\n");
scanf("%f%f%f",&s[0],&s[1],&s[2]);
for(i=0;i<3;i++)
p->score[i]=s[i];
p->total=p->score[0]+p->score[1]+p->score[2];
break;
}
p=p->next;
}
if(p==h)
return 0;
else
return 1;
}
//排序
DLinkList sort(DLinkList r)
{
DLNode *t=NULL,*s=NULL;
DLNode *h=r->next;
DLNode *p=h->next;
DLNode *q=NULL;
while(p!=h)
{
q=h->next->next;
while(q!=h)
{
t=q->prior;
if(t->total<q->total)
{
s=t->prior;
t->next=q->next;
q->next->prior=t;
t->prior=q;
q->next=t;
q->prior=s;
s->next=q;
q=t;
}
q=q->next;
}
p=p->next;
}
return h->prior;
}
//輸出
void print_DLinkList(DLinkList r)
{
int i;
DLNode *p,*h;
h=r->next;
p=h->next;
while(p!=h)
{
printf("number:%3d\tname:%s\tscore:%5.2f\t%5.2f\t%5.2f\ttotal:%5.2f\n",p->num,p->name,p->score[0],p->score[1],p->score[2],p->total);
p=p->next;
}
}
//釋放內存
void destory(DLinkList r)
{
DLNode *h,*p,*t=NULL;
h=r->next;
p=h->next;
while(p!=h)
{
t=p->next;
free(p);
p=t;
}
free(h);
}
int main()
{
DLinkList r;
int x,n,k;
r=Creat_DLinkList();
print_DLinkList(r);
printf("\nChoose what you want:\n");
printf("1:Search information:\n");
printf("2:Add information:\n");
printf("3:Delete same name:\n");
printf("4:Modify score:\n");
printf("5:Sort degrdation:\n");
scanf("%d",&x);
switch(x)
{
case 1: printf("\nPlease enter a number:");
scanf("%d",&n);
k=search(r,n);
if(k)
print_DLinkList(r);
else
printf("\nerror!!!\n");
break;
case 2: r=add(r);
print_DLinkList(r);
break;
case 3: delete(r);
print_DLinkList(r);
break;
case 4: printf("\nPlease enter a number:");
scanf("%d",&n);
k=modify(r,n);
if(k)
print_DLinkList(r);
else
printf("\nNot found!!!\n");
break;
case 5: r=sort(r);
print_DLinkList(r);
break;
default:printf("\nEnter error!!!\n");
}
destory(r);
return 0;
}
最近在學這里,所以就幫樓主編了一下,順便鞏固鞏固所學。lz有什麼不懂的盡管問,我會盡量回答的。
Ⅹ C程序數據結構線性鏈表 排序sort(面試題 填空題 )自己做了一部分,後面不會做 前面的還不知對否
struct node *create(int a[],int n)
{
struct node *h,*q;
for(h=NULL;n;n--)
{
q=(struct node *)malloc(sizeof(struct node));
q->value=________________________; //應該是n-1,你看main函數里的調用用的是數組大小,而不是用數組下標上限
________________________________; //應該是q->next=h,表頭添加節點法,保證表尾為NULL
________________________________; //h=q;
}
return h;
}
while(q-next !=NULL)
{
if(q->next->value<__________________) //應嘎是 r->next->value,因為r是個臨時變數,相當於數組排序中的tmp,這里的r是用來保存最小的數的頭節點的。這個循環的作用是找剩下的鏈表中的最小節點r->next。
r=q;
q=q->next; //向後遍歷節點
}
if(r !=p) //如果r!=p 就說明 r==p->nex
{
s=_________________________; //s=r->next; 保存最小節點到s
________________________=s->next; //r->next=s->next; 將最小節點從剩下的鏈表中分離出來
s->next=________________________; //s->next=p->next; 將最小節點連接到p->next
______________________________=s; //p->next=s;
}