#include<iostream>
using namespace std;
struct node
{
int data;
node* next;
};
node* createlist(int n)//创建一个链表,n为存储的数据个数
{
node* head = (node*)malloc(sizeof(head));//开辟空间
node* tail = new node;
head = NULL;
tail = NULL;
node* q;
for (int i = 0; i < n; i++)
{
node* p = new node;//尾插法
cin >> p->data;
if (head == NULL)
{
head = p;
tail = p;
}
else
{
tail->next = p;
tail = p;
p->next = NULL;
}
}
node* next;
node* prev=NULL;
while (head != NULL)//实现链表的逆序
{
next = head->next;
head->next = prev;
prev = head;
head = next;
}
q = prev;//逆序后的第一个节点
while (q != NULL)
{
cout << q->data << " ";
q = q->next;
}
return 0;
}
int main()
{
int n;
cin >> n;
createlist(n);
return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)