一道数据结构链表题(c⼀c++)

2024-12-23 10:20:38
推荐回答(4个)
回答1:

输入:学生总数n
输入:
名字1 分数1 分数2 分数3
名字2 分数1 分数2 分数3
名字3 分数1 分数2 分数3
。。。
名字n 分数1 分数2 分数3
------------------------
例如:
5
zhao 60.0 70.0 80.0
qian 50.0 65.0 90.0
sun 100.0 100.0 100.0
li 30.0 40.0 20.0
zhou 100.0 90.0 70.0

#include
#include

typedef struct
{
char name[32];
float mark[3];
} ST;

void main()
{
ST *all_st;
float f1,f2,f3;
int N,i;

printf("Enter the Number of students\nN= ");
scanf("%d",&N);

all_st = (ST *) malloc( N * sizeof(ST));
if (!all_st) {
printf("No enough memory .\n");
exit(0);
}

for (i=0;iprintf("First_LastName mark1 mark2 mark3:\n");
scanf("%s %f %f %f",&all_st[i].name,&f1,&f2,&f3);
all_st[i].mark[0] = f1;
all_st[i].mark[1] = f2;
all_st[i].mark[2] = f3;
}

for (i=0;if1 = all_st[i].mark[0] + all_st[i].mark[1] + all_st[i].mark[2] ;
if (f1 >= 180.0) {
printf("%s -- pass exam\n",all_st[i].name);
}
}

for (i=0;if1 = all_st[i].mark[0] + all_st[i].mark[1] + all_st[i].mark[2] ;
if (f1 < 180.0) {
printf("%s -- not pass exam\n",all_st[i].name);
}
}

for (i=0;if1 = all_st[i].mark[0] + all_st[i].mark[1] + all_st[i].mark[2] ;
if (f1 >= 300.0) {
printf("%s -- 3 x 100.0\n",all_st[i].name);
}
}
exit(0);
}

输出:
zhao -- pass exam
qian -- pass exam
sun -- pass exam
zhou -- pass exam
li -- not pass exam
sun -- 3 x 100.0

回答2:

定义一个节点类,或者结构体

然后定义一个链表类,然后继续,,,,

回答3:

include
include

using namespace std;

typedef struct _node
{
string name;
int grade;
_node* next;
}node,*pnode;

class linklist
{
public:
linklist() : m_head(0),m_tail(0),m_size(0){}
virtual ~linklist(){ if(m_size!=0) destroy(); }
void print(pnode pn)
{
cout << pn->name << "," << pn->grade << endl;
}
void add(string n,int g)
{
pnode p = new node;
p->name = n;
p->grade = g;
if(m_head==0)
{
m_head = m_tail = p;
}
m_tail->next = p;
m_tail = p;
m_size++;
}
private:
void destroy()
{
pnode p = m_head;
while(p!=0)
{
m_head = p->next;
delete p;
p = m_head;
}
m_tail = m_head = 0;
}
private:
pnode m_head;
pnode m_tail;
int m_size;
};

回答4:

ls手很快。