c_cpp c链表

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp c链表相关的知识,希望对你有一定的参考价值。

#include <stdio.h>
#include <stdlib.h>

// define link list node
typedef struct node
{
	int val;
	struct node * next;
} node_t;

void print_list(node_t * head);
void push(node_t * head, int val);


int main(int argc, char const *argv[])
{
	node_t * head = NULL;
	head = malloc(sizeof(node_t));
	head->val = 1;

	head->next = malloc(sizeof(node_t));
	head->next->val = 2;
	head->next->next = NULL;

	print_list(head);
	push(head, 3);
	push(head, 4);
	push(head, 5);
	push(head, 6);
	print_list(head);

	return 0;
}

void print_list(node_t * head) {
	node_t * current = head;
	while (current != NULL) {
		printf("%d\n", current->val);
		current = current->next;
	}
}

void push(node_t * head, int val) {
	node_t * current = head;
	while (current->next != NULL) {
		current = current->next;
	}

	current->next = malloc(sizeof(node_t));
	current->next->val = val;
	current->next->next = NULL;
}

以上是关于c_cpp c链表的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp C ++中的链表实现

c_cpp 反转链表

c_cpp Ç链表操作

c_cpp 链表排序算法

c_cpp 链表类模板代码

c_cpp 双重链表实施