📄 reservelink.cpp
字号:
#include <iostream.h>
struct student {
int num;
struct student *next;
}; // 学生节点定义
int n = 0;
struct student *create(void)
{ struct student *head, *p1, *p2;
p1 = p2 = new struct student;
cin >> p1->num;
head = NULL;
while(p1->num != 0) {
n ++;
if(n == 1) head = p1;
else p2->next = p1;
p2 = p1;
p1 = new struct student;
cin >> p1->num;
}
p2->next = NULL;
return(head);
}
void print(struct student *head)
{ struct student *p = head;
cout << "链表数据如下:" << endl;
if(head != NULL)
do {
cout << p->num << " ";
p = p->next;
} while(p!=NULL);
cout << endl;
}
struct student *reserve(struct student * head)
{ struct student *p, *p1, *p2;
if(head == NULL) {
cout << "This list is NULL" << endl;
return(NULL);
}
p = head;
p1 = p->next;;
while(p1->next != NULL) {
p2 = p1->next;
p1->next = p;
p = p1;
p1 = p2;
}
p1->next = p;
head->next = NULL;
head = p1;
return(head);
}
main()
{ struct student *head, *p0;
head = create();
print(head);
cout << endl << "逆置后的链表数据如下:" << endl << endl;
head = reserve(head);
print(head);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -