C++笔记[结构体]
c++
//这是一段伪代码,visit和next均为特定的操作,并非c++内置。
Student* p =head;
while(p)//也就是p不为NULL时,就继续循环。结构体的最后一个是NULL。
{
visit(p);
p=p->next;//走向下一个
}
c++
void output(Student& s)
{
Student* p = &s;
for (int i = 0; i < 5; i++)
{
cout << p->No << ' ' << p->Name << ' ' << p->score[0] << ' ' << p->score[1] << ' ' << p->score[2] << ' ' << p[i].score[0] + p[i].score[1] + p[i].score[2] << 'n';//p是由s开始的结构体的(亦是首地址),后面调用时为s1
p++;
}
c++
#include<iostream>
using namespace std;
struct Student
{
int No;
char Name[20];//名字最多9个汉字
double score[3];//三门成绩
Student* next;//节点指针,如果这个链表到尾部了,那么这是尾指针(暂时只考虑单向链表,也就是从头走到尾的)
}
int main()
{
Student ChainList;//先在运行时栈创建一个结构体,其实head = new Student也可以
Student* head = ChainList;//获取头指针
---录入数据---(链表操作在后面说,先创建,才能操作)
head.next = new Student;//在运行时堆里new一个新的空结构体
}
c++
ChainList.score[0]=第一门成绩;
ChainList.score[1]=第二门成绩;
ChainList.score[2]=第三门成绩;
c++
head->score[0]=第一门成绩;
head->score[1]=第二门成绩;
head->score[2]=第三门成绩;
c++
pioneer_pointer = pioneer_pointer->next;
pioneer_pointer = new Student;
c++
case 3://插入新的学生信息
if (n == 0)
{
cout << "[添加]请输入学生信息:n";
input(stu);
n++;
}
else if (n > 0)
{
cout << "[添加]请输入学生信息:n";
(*p).next = new Student;//此处的p即为pioneer_pointer
p = p->next;
input(*p);
}
p->next = nullptr;
if (moving_pin == nullptr)head = p;
break;
c++
void input(Student& v_stu)//我也忘记v是什么意思了,virtual?但形参是parameter。
{
cin >> v_stu.No >>v_stu.Name>> v_stu.score[0] >> v_stu.score[1] >> v_stu.score[2];
}
Loading...
c++
void swap_not_including_pin(Student& a, Student& b)
{
Student T;//中间变量
strcpy_s(T.Name, a.Name); T.No = a.No; T.score[0] = a.score[0]; T.score[1] = a.score[1]; T.score[2] = a.score[2];//T = a
strcpy_s(a.Name, b.Name); a.No = b.No; a.score[0] = b.score[0]; a.score[1] = b.score[1]; a.score[2] = b.score[2];//a = b;
strcpy_s(b.Name, T.Name); b.No = T.No; b.score[0] = T.score[0]; b.score[1] = T.score[1]; b.score[2] = T.score[2];//b = t;
}
c++
void swap_not_including__pointer(int &a,int &b)
{
int T;
T = a;
a = b;
b = t;
}