C 单向链表(不带头结点)

本文介绍了C语言中不带头结点的单向链表的操作,包括带头结点与不带头结点的区别,以及创建链表、头插法和尾插法插入新结点,指定数据域前后插入和删除结点的方法,最后讨论了如何删除整条链表。
摘要由CSDN通过智能技术生成

带头结点和不带头结点的区别

带头结点:链表的头结点不直接存储有效数据,而是在头结点的下一个才开始存储有效数据,为了方便管理,链表的头结点可以存储整条链表的结点的个数

不带头结点:从链表的头结点直接开始存储有效数据

无论带头结点和带不带头结点,链表头还是有的

创建链表及头插法插入新结点

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

struct Node
{
   
	int data;
	struct Node *next;
};

void printfLink(struct Node *head)
{
   
	struct Node *point = head;
	while(point != NULL)
	{
   
		printf("%d->", point->data);
		point = point->next;
	}
	printf("NULL\n");
}

struct Node *headInsert(struct Node *new, struct Node *head)
{
   
	if(head == NULL)
	{
   
		head = new;
	}
	else
	{
   
		new->next = head;
		head = new;
	}
	return head;
}

struct Node *createLink(struct Node *head, int len)
{
   
	int i;
	struct Node *new = NULL;

	for(i=1;i<=len;i++)
	{
   
		new = (struct Node *)malloc(sizeof(struct Node));
		memset(new, 0, sizeof(struct Node));
		new->next = NULL;
		scanf("%d", &(new->data));
		head = headInsert(new, head);
	}
	return head;
}

int main()
{
   
	struct Node *head = NULL;
	
	head = createLink(head, 5);
	printfLink(head);

	return 0;
}

创建链表及尾插法插入新结点

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


struct Node
{
   
	int data;
	struct Node *next;
};

void printfLink(struct Node *head)
{
   
	struct Node *point = head;
	while(point != NULL)
	{
   
		printf("%d->", point->data);
		point = point->next;
	}
	printf("NULL\n");
}

struct Node *tailInsert(struct Node *new, struct Node *head)
{
   
	struct Node *point = head
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值