C语言实现由输入的数据来创建链表。

插入部分分为头部插入和尾部插入。

取消掉对应的注释就行。

注意:#define N 10的N一般根据题目要求取最大值+1.保证能够输入所有数。

点击这里,了解更多算法与通信专业知识


#include<stdio.h>
using namespace std;

#define N 10
typedef struct Node
{
	int data;
	struct Node *next;
} myNode;

myNode *createNode(int *a, int n) {
	myNode *head, *rear, *p;
	int i;
	/********前插法**********/
	head = rear = NULL;//创建头结点和尾结点 最开始都指向同一点
	for (i = 0; i < n; i++) {//前插法(在链表头插入)
		p = (myNode *)malloc(sizeof(myNode*));
		p->data = a[i];
		p->next = rear;
		rear = p;
	}
	return head = rear;//在头部插入 返回时需要更新头结点

	/********后插法**********/
	//因为这里使用了rear->next 所以在用之前分配内存
	//rear = (myNode*)malloc(sizeof(myNode));
	//rear->next = NULL;
	//head = rear;			//记录头结点
	//for(i = 0; i < n; i&#