如何使用头插法创建一个单向链表(带头结点)

这次主要是分享一下数据结构中单链表的创建及基本操作,这一部分也是属于比较基础的内容。第一是看自己有没有真正掌握这部分的知识,第二是为了锻炼一下写作能力。如果有什么写得不好的地方还请各位大佬勿喷

单链表的结构定义

typedef struct node//单向链表结构体定义
{
	ElemType data;//定义一个ElemType类型的数据域(ElemType是用typedef定义的一个int类型的别名)
	struct node *next;//定义一个指针域
}node;

代码展示

#include <stdio.h>
#include <stdbil.h>

typedef int ElemType;

//建立一个结构体来保存链表信息
typedef struct node
{
	ElemType data;
	struct node *next;
}node;

int head_insert(node* head, ElenType data)
{
	if(head==NULL)//先判断头节点是否为空指针,如果为空返回-1
		return -1;
	node *new = (node*)malloc(sizeof(node));
	new->next = NULL;
	new->data = data;

	new->next = head->next;
	head->next = new;

	return 0;//成功返回0
}

int main()
{
	node *head = (node*)malloc(sizeof(node));//创建一个头节点
	head->next = NULL;//使其指针域为空

	return 0;
}

本次分享就到这里了,虽然内容不多还是写很久说明知识掌握还是不牢固

#单向链表的创建#
全部评论

相关推荐

投递京东等公司10个岗位
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务