Skip to content

Latest commit

 

History

History
53 lines (46 loc) · 772 Bytes

73.md

File metadata and controls

53 lines (46 loc) · 772 Bytes

题目:反向输出一个链表。 

#include <stdio.h>
#include <malloc.h>

struct LNode
{
	int data;
	struct LNode *next;
};

LNode* createList(int n)
{
	LNode *head, *p, *q;
	head = (LNode*)malloc(sizeof(LNode));
	printf("请输入第1个元素的值:");
	scanf("%d", &(head->data));
	head->next = nullptr;

	q = head;
	for (int i = 1; i < n; ++i)
	{
		p = (LNode*)malloc(sizeof(LNode));
		printf("请输入第%d个元素的值:", i + 1);
		scanf("%d", &(p->data));
		p->next = q;
		q = p;
	}
	return q;
}

void print(LNode* list)
{
	printf("链表各值为:\n");

	LNode* p = list;
	while (p != nullptr)
	{
		printf("%d\n", p->data);
		p = p->next;
	}
}

int main()
{
	int n;
	scanf("%d", &n);
	LNode* list = createList(n);
	print(list);

	return 0;
}