#include<iostream>
using namespace std;
#define Elemtype int
typedef struct node
{
int data;
node* next;
}node;
node* generate_list(int*nums,int n)//头结点指向了一个叫做nodee的结点,此时的nodee还是个概念(想象)上的东西,实际上不存在
{
node* head = NULL;//此句很重要,头结点
node* nodee=NULL;
int i = 0;
if (n <= 0)
return nodee = NULL;
else
{
while (i < n)
{
if (i == 0)
{
nodee = (node*)malloc(sizeof(node));
nodee->data = nums[0];
i++;
head = nodee;
}
else
{
nodee->next = (node*)malloc(sizeof(node));//node->next一定是实例化的存在
nodee->next->data = nums[i];
i++;
nodee=nodee->next;
}
}
nodee->next = NULL;
return head;
}
}
void list_reverse(node*& list)
{
node* re=NULL;
node* front = list;
node* next_node=list->next;
node* temp = list;
front->next = NULL;
while (next_node != NULL)//区别尾结点,空节点
{
front=next_node;
next_node = next_node->next;
front->next = temp;
temp = front;
}
list= front;
}
int main()
{
int n;
cout << "input n" << endl;
cin >> n;
int * nums=(int*)malloc(sizeof(int));
for (int i = 0; i < n; i++)
{
int temp;
cin >> temp;
nums[i] = temp;
}
node* list;//栈上分配头指针
list=generate_list(nums,n);
list_reverse(list);
return 0;
}
using namespace std;
#define Elemtype int
typedef struct node
{
int data;
node* next;
}node;
node* generate_list(int*nums,int n)//头结点指向了一个叫做nodee的结点,此时的nodee还是个概念(想象)上的东西,实际上不存在
{
node* head = NULL;//此句很重要,头结点
node* nodee=NULL;
int i = 0;
if (n <= 0)
return nodee = NULL;
else
{
while (i < n)
{
if (i == 0)
{
nodee = (node*)malloc(sizeof(node));
nodee->data = nums[0];
i++;
head = nodee;
}
else
{
nodee->next = (node*)malloc(sizeof(node));//node->next一定是实例化的存在
nodee->next->data = nums[i];
i++;
nodee=nodee->next;
}
}
nodee->next = NULL;
return head;
}
}
void list_reverse(node*& list)
{
node* re=NULL;
node* front = list;
node* next_node=list->next;
node* temp = list;
front->next = NULL;
while (next_node != NULL)//区别尾结点,空节点
{
front=next_node;
next_node = next_node->next;
front->next = temp;
temp = front;
}
list= front;
}
int main()
{
int n;
cout << "input n" << endl;
cin >> n;
int * nums=(int*)malloc(sizeof(int));
for (int i = 0; i < n; i++)
{
int temp;
cin >> temp;
nums[i] = temp;
}
node* list;//栈上分配头指针
list=generate_list(nums,n);
list_reverse(list);
return 0;
}
5万+

被折叠的 条评论
为什么被折叠?



