#include "pch.h"
#include <iostream>
using namespace std
;
typedef struct node Student
;
typedef struct node
{
char name
[64];
int age
;
Student
*next
;
Student
*pre
;
}Student
;
Student
* createList( int n
)
{
Student
*head
= new Student
;
Student
*q
= head
;
*head
->name
='No';
head
->age
= 0;
for (int i
= 0; i
< n
; i
++)
{
Student
*p
= new Student
;
cout
<< "请输入第" << i
+ 1 << "个学生的姓名和年龄:";
cin
>> p
->name
>> p
-> age
;
q
->next
= p
;
p
->pre
= q
;
q
= q
->next
;
p
->next
= head
;
head
->pre
= p
;
}
return head
;
}
void display(Student
*head
)
{
Student
*p
= head
->next
;
int n
;
cout
<< "该双向链表的第一个元素为:";
cout
<< p
->name
<< " " << p
->age
<< endl
;
do {
cout
<< "请输入数字,1为往下查询,2为往上查询,0结束查询";
cin
>> n
;
if (n
== 1)
{
p
= p
-> next
;
cout
<< "下一位学生为:" << p
->name
<< " 年龄为:" << p
->age
<< endl
;
}
if (n
== 2)
{
p
= p
->pre
;
cout
<< "上一位学生为:" << p
->name
<< " 年龄为:" << p
->age
<< endl
;
}
} while (n
!= 0);
}
int main()
{
Student
*head
= createList(10);
display(head
);
return 0;
}
转载请注明原文地址: https://mac.8miu.com/read-488509.html