有头节点的链表,头插法

typedef struct Node {
	int data;                    // 存储链表数据
	struct Node* next;     		//  存储结点的地址
}node, * llist;
void creat(llist& head)
{
	head = (llist)malloc(sizeof(node));
	head->next = nullptr;
	llist node = nullptr;
	ifor(i, 0, 5)
	{
		node = (llist) malloc(sizeof(node));
		node->data = i;
        
		node->next = head->next;
		head->next = node;//导致头指针没有data数据,所以需要后面手动赋值一个
	}
    head->data=89;
}
void p(llist l)
{
	llist item = l;
	do {
		cout << item->data << " ";
		item = item->next;
	} while ( item );
}


int main()
{
	/*ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cin.tie(nullptr);*/
	// note: not ordered, only partitioned w.r.t. S defined below
	llist li;
	creat(li);
	p(li);

}

有头节点的链表,尾插法

typedef struct Node {
	int data;                    // 存储链表数据
	struct Node* next;     		//  存储结点的地址
}node, * llist;
void creat(llist& head)
{
	head = (llist)malloc(sizeof(node));
	head->next = nullptr;
	llist end = head;
	ifor(i, 0, 5)
	{
		llist node1 = (llist) malloc(sizeof(node));
		node1->data = i;
		end->next = node1;
		end = node1;
	}
	end->next = nullptr;
}
void p(llist l)
{
	llist item = l;
	do {
		cout << item->data << " ";
		item = item->next;
	} while ( item );
}


int main()
{
	/*ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cin.tie(nullptr);*/
	// note: not ordered, only partitioned w.r.t. S defined below
	llist li;
	creat(li);
	p(li);

}