尾插法建立链表Establish a Linked List Using the Tail Interpolation

尾插法建立链表Establish a Linked List Using the Tail Interpolation,第1张

代码如下:

#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;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/634705.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-16
下一篇 2022-04-16

发表评论

登录后才能评论

评论列表(0条)

保存