代码如下:
#define _CRT_SECURE_NO_WARNINGS 1
#include
using namespace std;
#define maxSize 100
typedef struct LNode
{
int data;
struct LNode* next;
}LNode;
int main()
{
void createlistR(LNode*&, int[], int);
void printlist(LNode*);
LNode* C;
int a[maxSize], n;
cout << "Please enter the number of the elements: ";
cin >> n;
cout << "Please enter the elements one by one: ";
{
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
}
createlistR(C, a, n);
printlist(C);
return 0;
}
void createlistR(LNode*& C, int a[], int n)
{
LNode* s, * r;
C = (LNode*)malloc(sizeof(LNode));
r = C;
for (int i = 0; i < n; i++)
{
s = (LNode*)malloc(sizeof(LNode));
s->data = a[i];
r->next = s;
r = r->next;
}
r->next = NULL;
return;
}
void printlist(LNode* C)
{
C = C->next;
while (C)
{
printf("%d", C->data);
if (C->next)
{
printf(" -> ");
}
C = C->next;
}
return;
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)