链表写的小程序

                    今天没有讲新东西,而是做了一天的题目,下面是用链表写的一个查询成绩的小程序,写的并不好吧:


#include<iostream>
#include<string>
using namespace std;
class student{
public:
string name;//学生姓名
int xuehao;//学号
string banji; //班级
int cj;    //成绩
int cj1;
int all;
student *next;
private:
};
void main(){
student *head;
student mn;
head = &mn;
mn.name = "王小二";
mn.xuehao = 1;
mn.banji = "三年二班";
mn.cj = 90;
mn.cj1 = 95;
mn.all = mn.cj + mn.cj1;
student mn1;
mn.next = &mn1;
mn1.name = "何小洋";
mn1.xuehao = 2;
mn1.banji = "三年二班";
mn1.cj = 80;
mn1.cj1 = 85;
mn1.all= mn1.cj + mn1.cj1;
mn1.next = NULL;
cout << "姓名 " << head->name << endl;
cout << "学号 " << head->xuehao<< endl;
cout << "班级 " << head->banji << endl;
cout << "语文成绩 " << head->cj << endl;
cout << "英语成绩 " << head->cj1 << endl;
cout << "总成绩 " << head->all << endl;
cout << "姓名 " << head->next->name << endl;
cout << "学号 " << head->next->xuehao << endl;
cout << "班级 " << head->next->banji << endl;
cout << "语文成绩 " << head->next->cj << endl;
cout << "英语成绩 " << head->next->cj1 << endl;
cout << "总成绩 " << head->next->all << endl;
/*while (head != NULL){
cout << "姓名 " << head->name << endl;
cout << "学号 " << head->xuehao<< endl;
cout << "班级 " << head->banji << endl;
cout << "语文成绩 " << head->cj << endl;
cout << "英语成绩 " << head->cj1 << endl;
cout << "总成绩 " << head->all << endl;
head = head->next;
}*/
int j;
cout << "请输入你要查询的方式:1.学号查询 2.姓名查询" << endl;
cin >> j;
if (j == 1){
while (1){
int i;
cout << "请输入你要查找的学生的学号" << endl;
cin >> i;
switch (i){
case 1:
cout << "姓名 " << head->name << endl;
cout << "学号 " << head->xuehao << endl;
cout << "班级 " << head->banji << endl;
cout << "语文成绩 " << head->cj << endl;
cout << "英语成绩 " << head->cj1 << endl;
cout << "总成绩 " << head->all << endl;
break;
case 2:
cout << "姓名 " << head->next->name << endl;
cout << "学号 " << head->next->xuehao << endl;
cout << "班级 " << head->next->banji << endl;
cout << "语文成绩 " << head->next->cj << endl;
cout << "英语成绩 " << head->next->cj1 << endl;
cout << "总成绩 " << head->next->all << endl;
break;
}
}
}
else {
while (1){
cout << "请输入你要查询的学生的姓名" << endl;
string a;
cin >> a;
if (a == mn.name){
cout << "姓名 " << head->name << endl;
cout << "学号 " << head->xuehao << endl;
cout << "班级 " << head->banji << endl;
cout << "语文成绩 " << head->cj << endl;
cout << "英语成绩 " << head->cj1 << endl;
cout << "总成绩 " << head->all << endl;
}
else if (a == mn1.name){
cout << "姓名 " << head->next->name << endl;
cout << "学号 " << head->next->xuehao << endl;
cout << "班级 " << head->next->banji << endl;
cout << "语文成绩 " << head->next->cj << endl;
cout << "英语成绩 " << head->next->cj1 << endl;
cout << "总成绩 " << head->next->all << endl;
}
}
}
system("pause");


}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的使用C语言创建链表的程序: ``` #include <stdio.h> #include <stdlib.h> struct node { int data; struct node* next; }; struct node* create_node(int data) { struct node* new_node = (struct node*)malloc(sizeof(struct node)); new_node->data = data; new_node->next = NULL; return new_node; } struct node* create_linked_list(int arr[], int size) { struct node* head = create_node(arr[0]); struct node* current = head; for (int i = 1; i < size; i++) { struct node* new_node = create_node(arr[i]); current->next = new_node; current = new_node; } return head; } void print_linked_list(struct node* head) { struct node* current = head; while (current != NULL) { printf("%d ", current->data); current = current->next; } printf("\n"); } int main() { int arr[] = {1, 2, 3, 4, 5}; int size = sizeof(arr) / sizeof(arr[0]); struct node* head = create_linked_list(arr, size); print_linked_list(head); return 0; } ``` 该程序定义了一个结构体`node`,其中包含一个整数数据`data`和一个指向下一个节点的指针`next`。然后定义了两个函数`create_node`和`create_linked_list`,前者用于创建一个新节点,后者用于创建一个新的链表。`create_linked_list`函数接受一个整数数组和其大小作为参数,然后遍历整个数组,为每个数组元素创建一个新的节点,并将其链接到链表中。最后,程序定义了一个`print_linked_list`函数,用于打印链表的内容。在`main`函数中,程序创建了一个整数数组`arr`,将其大小计算出来,并使用`create_linked_list`函数创建了一个新的链表。然后,程序使用`print_linked_list`函数打印了新链表的内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值