当前位置:高等教育资讯网  >  中国高校课件下载中心  >  大学文库  >  浏览文档

广东海洋大学:《C++程序设计》结构中的有关例题

资源类别:文库,文档格式:DOC,文档页数:6,文件大小:44.5KB,团购合买
1结构体数组元素的输入和输出 #include #define 4 void mainO struct student {int no; char name[16];
点击下载完整版文档(DOC)

结构体的有关例题 1.结构体数组元素的输入和输出 #includesiostream.h> #define 4 void main( i struct student char name 16]: float score struct student stN: int 1; cou>sti. no>>sti name>>stiscore; cout #define n4 void maino struct student int no char name 16]; float score; struct student stN, "p; int 1;

结构体的有关例题 ⒈结构体数组元素的输入和输出 #include #define N 4 void main() { struct student {int no; char name[16]; float score;} ; struct student st[N]; int i; cout>st[i].no>>st[i].name>>st[i].score; cout #define N 4 void main() { struct student {int no; char name[16]; float score; } ; struct student st[N], *p; int i;

cou←>st[i- no>>st. name>>stiscore coutnonamescore #define n 4 char name[ 16 int math: int float ave }stN={"Sun",78,91,0},{" Zhang",60,76,0},{Qian",88,95,0},{"Fang",60,70,0}; void show(record s) ave=(s math+s eng)2.0; cout #define n 4 struct record char name 16 int engi float ave

cout>st[i].no>>st[i].name>>st[i].score; coutnonamescore #define N 4 struct record { char name[16]; int math; int eng; float ave; } st[N]={{"Sun",78,91,0},{"Zhang",60,76,0},{"Qian",88,95,0},{"Fang",60,70,0}}; void show(record s) { s.ave=(s.math+s.eng)/2.0; cout #define N 4 struct record { char name[16]; int math; int eng; float ave;

record student; record getrecordo record temp; in>>temp. name>>temp. math>>temp. eng; temp. ave=(temp. math+temp. eng)/2.0 return temp;/返回结构体值 void show(record s) cout #define n 4 struct student∥定义链表结点类型 char name 16 float score

} ; record student[N]; record getrecord() { record temp; cin>>temp.name>>temp.math>>temp.eng; temp.ave=(temp.math+temp.eng)/2.0; return temp; //返回结构体值 } void show(record s) { cout #define N 4 struct student //定义链表结点类型 { int no; char name[16]; float score;

student*next; struct student *head. s: void create(/生成单链表 int i; struct student*rear; head=NULLs Ou"请输入"s->no>>s->name>>s> score;∥读入学生数据 if(head==NULL) head /将新插入结点为第一结点 rear>next=s;∥其余结点插入在表尾指针rear后 s→>next=NULL; rear-s ∥移动尾指针 void show0∥显示单链表 struct student*p coutnonamescore<<endl; p-p 击t 指针移到下一个结构体元素 void maino create;/生成单表 showO;∥显示单链表元素 6在单链表中插入一个结点 void inslink( student*st/插入结点 struct student*p, * q if (head==NULL) head是全局变量

student *next; } ; struct student *head,*s; void create() //生成单链表 { int i; struct student *rear; head=NULL; cout>s->no>>s->name>>s->score; //读入学生数据 if(head==NULL) head=s; //将新插入结点为第一结点 else rear->next=s; //其余结点插入在表尾指针 rear 后 s->next=NULL; rear=s; //移动尾指针 } } void show() //显示单链表 { struct student *p; coutnonamescorenext; //指针移到下一个结构体元素 } } void main() { create(); //生成单表 show(); //显示单链表元素 } 6 在单链表中插入一个结点 void inslink(student *st) //插入结点 { struct student *p,*q; if (head==NULL) //head 是全局变量

head ∥表示表头 st->nextNULL ∥表尾为空指针 return if(p->score>st->score) ∥在第一个结点前插入 & st->nextp p=st return; while(p & p->scorescore) q=p p=p>next;∥取下一个结点地址 st->nextq->next; 插入结点 q->nextst return void maino i struct student*st create 调生成单表程序 show 调显示单链表程序 udent: cou>st->no>>st->name>>st->score inslink(st);∥/调插入程序 showO 注意:本插入函数是对学生成绩按由小到大插入,所以输入学生成绩必须的序。 当输入下数据时: I WU 60 cHEN 70 5 WANG 80 7LI90 插入值为4 JIANG76 程序输出为 学号姓名分数 CHEN 3457 JIANG 76 WANG LI

{ head=st; // 表示表头 st->next=NULL; //表尾为空指针 return; } p=head; if (p->score>st->score) //在第一个结点前插入 { st->next=p; p=st; return; } while(p && p->scorescore) {q=p; p=p->next; //取下一个结点地址 } st->next=q->next; //插入结点 q->next=st; return; } void main() { struct student *st; create(); //调生成单表程序 show(); //调显示单链表程序 st=new student; cout>st->no>>st->name>>st->score; inslink(st); //调插入程序 show(); } 注意:本插入函数是对学生成绩按由小到大插入,所以输入学生成绩必须的序。 当输入下数据时: 1 WU 60 3 CHEN 70 5 WANG 80 7 LI 90 插入值为 4 JIANG 76 程序输出为: 学号 姓名 分数 --------------------- 1 WU 60 3 CHEN 70 4 JIANG 76 5 WANG 80 7 LI 90

7在单链表中删除一个结点。 void delink(int n) student*p, q:int F=0; ifchead) coun0=n)∥删除结点是第一个结点 head=head->next; delete p; coutno!=n)/取下一个结点地址 q-p, p=p->next; ip)/找到删除结点 q->next=p->next; delete p; cout<"学号是"<<n<<"结点已经删除"<end else cout<<"学号是"<n<<"结点没有找到<<end; return; void maino create0;∥生成单表 show 0: ∥显示单链表 delink(9901); showi

7 在单链表中删除一个结点。 void dellink(int n) { student *p,*q;int i=0; if(!head) { coutno==n) // 删除结点是第一个结点 { head=head->next; delete p; coutno!=n) //取下一个结点地址 { q=p; p=p->next; } if(p) //找到删除结点 { q->next=p->next; delete p; cout<<"学号是"<<n<<"结点已经删除"<<endl; } else cout<<"学号是"<<n<<"结点没有找到"<<endl; return; } void main() { create(); //生成单表 show(); //显示单链表 dellink(9901); show(); }

点击下载完整版文档(DOC)VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
已到末页,全文结束
相关文档

关于我们|帮助中心|下载说明|相关软件|意见反馈|联系我们

Copyright © 2008-现在 cucdc.com 高等教育资讯网 版权所有