vc++相关程序问题以及其结构体与简单链表实践相关问题……谢谢了……
代码及运行结果如下:
#include <iostream>
#include <cstring>
using namespace std;
struct node
{
int num;//学号
double CPP;//成绩
node* next; //指向下一个节点的变量指针
};
node* creat()
{
int n, i;
node* head = 0, * p, * t=0;
cout << "请输入链表的长度:";
cin >> n;
for (i = 0; i < n; i++)
{
p = new node;
cout << "请输入第" << i + 1 << "个学生的学号:";
cin >> p->num;
cout << "请输入第" << i + 1 << "个学生的成绩:";
cin >> p->CPP;
p->next = 0;
if (head == 0)
{
head = p;
t = head;
}
else
{
t->next = p;
t = p;
}
}
cout << "链表创建完成!" << endl;
return head;
}
void print(node* head)
{
node* p = head;
cout << "显示链表数据:" << endl;
while (p)
{
cout << "学号:" << p->num << "\t成绩:" << p->CPP << endl;
p = p->next;
}
}
void release(node* head)
{
node* p;
cout << "释放链表" << endl;
while (head)
{
p = head->next;
delete head;
head = p;
}
head = 0;
}
int main()
{
node* head = creat();
print(head);
release(head);
return 0;
}
需要加几个学生信息呢
#include <iostream>
using namespace std;
struct node
{
int num;
double CPP;
struct node *next;
}
struct node * create()
{
struct node *head = new struct node;
head->next = NULL;
struct node *p = head;
for(int i=0;i<5;i++)
{
struct node * q = new struct node;
cin>>q->num>>q->CPP;
q->next = NULL;
p->next = q;
p = q;
}
return head;
}
void print(struct node *head)
{
struct node *p = head;
while(p->next != NULL)
{
cout<<p->num<<","<<p->CPP<<endl;
p= p->next;
}
}
void release(struct node *head)
{
struct node *p = head,*q;
while(p != NULL)
{
q = p->next;
delete p;
p = q;
}
}
int main()
{
struct node *head = create();
print(head);
release(head);
return 0;
}
struct node
{
int num;
double CPP;
node *next;
};
node *cerat()
{
node *h = new node;
node *p, *q;
int n;
q = h;
q->next = NULL;
cout << "输入学号和成绩:(0结束)\n";
while (1)
{
cin >> n;
if (n == 0)
return h;
p = new node;
p->num = n;
cin >> p->CPP;
p->next = NULL;
q->next = p;
q = p;
}
return h;
}
void print(node *head)
{
node *p = head->next;
while (p)
{
cout << p->num << " " << p->CPP << endl;
p = p->next;
}
}
void release(node *head)
{
node *q, *p = head;
while (p)
{
q = p->next;
delete p;
p = q;
}
}
int main()
{
node *head = cerat();
print(head);
release(head);
return 0;
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!