C语言实现单链表(不带头节点)的删除插入完整代码

 注意:不带头节点的单链表删除插入操作第一个节点的时候都要特殊讨论

以下代码是用C语言实现的完整代码可上机运行


#include<stdio.h>
#include<malloc.h>
struct linknode
{
	int data;
	struct linknode* next;
};
void init(struct linknode** L)
{
	(*L) = NULL;
}
void insect(struct linknode** L, int i, int n)
{
	if (i < 1)
		printf("插入位置错误\n");
	else if (i == 1)
	{
		printf("开始插入\n");
		struct linknode* s = (struct linknode*)malloc(sizeof(struct linknode));
		printf("申请位置成功\n");
		s->data = n;
		s->next = (*L);
		(*L) = s;
		printf("插入成功\n");
	}
	else
	{
		int count = 1;
		struct linknode* p = (*L);
		while (p&&count < i - 1)
		{
			p = p->next;
			count++;
		}
		if (p)
		{
			struct linknode* s = (struct linknode*)malloc(sizeof(struct linknode));
			s->data = n;
			s->next = p->next;
			p->next = s;
			printf("插入成功\n");
		}
		else
			printf("插入失败\n");
	}
}
void delete(struct linknode** L,int i)
{
	if(i<1||(*L)==NULL)
	printf("删除失败");
	else if(i==1)
	{
		(*L)=(*L)->next;
	} 
	else
	{
	 	int count=1;
	 	struct linknode* p=(*L);
	 	while(p&&count<i-1)
	 	{
	 		p=p->next;
	 		count++;
		}
		if(p)
		{
			p->next=p->next->next;
			printf("删除成功\n");
		}
		else
		printf("删除位置不恰当\n");
	}
}
void point(struct linknode* L)
{
	while (L)
	{
		printf("%d,", L->data);
		L = L->next;
	}
}
int main()
{
	struct linknode* L;
	init(&L);
	printf("初始化成功不带头节点单链表\n");
	insect(&L, 1, 11);//在第一个位置插入元素11
	insect(&L, 2, 22);
	insect(&L, 3, 33);
	insect(&L, 3, 44);
	insect(&L, 6, 66); 
	delete(&L,1);//删除第一个元素 
	delete(&L,3);//删除第三个元素 
	delete(&L,5);//删除第五个元素 
	printf("请打印输出链表\n");
	point(L);//打印函数
	return 0;
}

运行结果:

 

  • 8
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是用C语言实现不带头结点单链表的基本操作的代码: ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点结构体 struct Node { int data; // 数据域 struct Node* next; // 指针域 }; // 在链表尾部插入节点 void insertAtEnd(struct Node** head_ref, int new_data) { struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); // 创建新节点 struct Node* last = *head_ref; // 定义指向最后一个节点的指针 new_node->data = new_data; // 填充新节点数据 new_node->next = NULL; // 新节点的 next 指针赋值为空 // 如果链表为空,将新节点设为头节点 if (*head_ref == NULL) { *head_ref = new_node; return; } // 遍历链表,找到最后一个节点 while (last->next != NULL) { last = last->next; } // 将新节点接在链表尾部 last->next = new_node; } // 在链表删除指定值的节点 void deleteNode(struct Node** head_ref, int key) { struct Node* temp = *head_ref; // 定义指向当前节点的指针 struct Node* prev = NULL; // 定义指向当前节点前一个节点的指针 // 如果头节点的数据为要删除的数据 if (temp != NULL && temp->data == key) { *head_ref = temp->next; // 修改头节点 free(temp); // 释放原头节点内存 return; } // 遍历链表,找到要删除节点 while (temp != NULL && temp->data != key) { prev = temp; temp = temp->next; } // 如果找到了要删除节点 if (temp != NULL) { prev->next = temp->next; // 修改前一个节点的 next 指针 free(temp); // 释放要删除节点内存 } } // 打印链表 void printList(struct Node* node) { while (node != NULL) { printf("%d ", node->data); node = node->next; } printf("\n"); } int main() { struct Node* head = NULL; // 定义头节点 // 在链表尾部插入节点 insertAtEnd(&head, 1); insertAtEnd(&head, 2); insertAtEnd(&head, 3); printf("Original list: "); printList(head); // 删除节点 deleteNode(&head, 2); printf("Updated list: "); printList(head); return 0; } ``` 运行结果: ``` Original list: 1 2 3 Updated list: 1 3 ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值