demo9.8链表删除指定的节点.c

#include <stdio.h>
//数组:地址是连续的 不易增删 不灵活
//链表:不连续 灵活 
struct Test 
{
	int data;
	struct Test *next;
	
};
void printlink(struct Test *head)//链表动态遍历
{
	struct Test *point;
	point = head;
	while (point != NULL){
		printf("%d ",point ->data);
		point = point ->next;
	}
	putchar('\n');
	
	
}
int getNodesum(struct Test *head)//统计链表节点个数
{
	int count = 0;
	while(head != NULL){
		count++;
		head = head->next;
	}
	return count;
	
}
int searchNode(struct Test *head,int data)//查找指定的值是否在链表中
{
	while(head !=NULL ){
		if(head->data == data){
			return 1;
		}else{
			head = head -> next;
		}
	}
	return 0;
	
}
int insertNodebehind(struct Test *head,int data,struct Test *new)//从指定节点后方插入新节点
{
	struct Test *point = head;
	while(point != NULL){
		if(point->data == data){
			new->next = point->next;
			point->next = new;
			return 1;
		}
		point = point->next;
	}
	return 0;
}
struct Test *insertNodefront(struct Test *head,int data,struct Test *new2)//从指定节点前方插入新节点
{
	
	struct Test *point = head;
	if(point->data == data){
		new2->next = head;
		return new2;
	}
	while(point->next != NULL){
		if(point->next->data == data){
			new2->next = point->next;
			point->next = new2;
			printf("插入成功\n");
			return head;
		}
		point = point->next;
	}
	printf("插入失败:no this data\n");
	return head;
}
struct Test *reverseList(struct Test *head)//对链表进行逆向排序
{
	struct Test *prve = NULL;// 指向前一个节点的指针,初始为 NULL
	struct Test *current = head;// 指向当前节点的指针,初始为链表的头节点
	struct Test *next = NULL; // 指向下一个节点的指针,初始为 NULL
	
	while(current != NULL ){
		next = current->next;
		current->next = prve;
		prve = current;
		current = next;
	}
	
	
	return prve;
	
	
}
struct Test *deletNode(struct Test *head,int data)//删除链表的指定节点
{
	struct Test *p = head;
	if(p->data == data){
		head=head->next;
		return head;
	}
	while(p->next != NULL){
		if(p->next->data == data){
			p->next=p->next->next;
			return head;
		}
		p = p->next;
	}
	printf("链表中没有这个节点删除失败\n");
	return head;
	

}


int main()
{
	
	//链表的静态添加
	struct Test t1 = {1,NULL};
	struct Test t2 = {2,NULL};
	struct Test t3 = {3,NULL};
	struct Test t4 = {4,NULL};
	struct Test t5 = {5,NULL};
    struct Test t6 = {6,NULL};
	t1.next = &t2;
	t2.next = &t3;
	t3.next = &t4;
	t4.next = &t5;
	t5.next = &t6;
	
	struct Test *head;
	head = &t1;
	
	struct Test new={100,NULL};//创建一个新节点,插入到指定节点的后方
	printf("插入新节点100到指定节点的后面:\n");
	int info;
	info=insertNodebehind(head,6,&new);
	if(info == 0){
		printf("插入失败\n");
	}else{
		printf("插入成功\n");
	}
	printlink(head);//遍历插入后的链表
	
	struct Test new2={101,NULL};//创建一个新节点new2,插入到指定节点的前方
	printf("插入新节点101到指定节点的前面\n");
	head = insertNodefront(head,1,&new2);
	
	struct Test new3={106,NULL};//创建一个新节点new3,插入到指定节点的前方
	printf("插入新节点106到指定节点的前面\n");
	head = insertNodefront(head,5,&new3);
	
	printlink(head);//遍历插入后的链表
	
	int ret;
	ret=getNodesum(head);//统计链表节点个数
	printf("链表的节点个数=%d\n",ret);
	
	int search;
	search = searchNode(head,1);//查找指定的值是否在链表中
	if(search == 0){
		printf("no 1 in Node\n");
	}else{
		printf("have 1 in Node\n");
	}
	
	//head = reverseList(head);//对链表进行逆向排序
	//printlink(head);//遍历逆向排序后的链表
	
	head = deletNode(head,1001);//对链表的指定节点进行删除
	printlink(head);
	return 0;
}```

在C语言中,删除链表中的指定节点可以通过以下步骤实现: 1. **找到要删除节点**:遍历链表,找到包含指定值的节点。 2. **调整指针**:将前一个节点的指针指向要删除节点的下一个节点。 3. **释放内存**:释放要删除节点的内存。 下面是一个示例代码,展示了如何删除链表中的指定节点: ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点结构 struct Node { int data; struct Node* next; }; // 创建一个新节点 struct Node* createNode(int data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); if (!newNode) { printf("内存分配失败\n"); exit(1); } newNode->data = data; newNode->next = NULL; return newNode; } // 插入节点链表末尾 void insertEnd(struct Node** head_ref, int new_data) { struct Node* newNode = createNode(new_data); if (*head_ref == NULL) { *head_ref = newNode; return; } struct Node* last = *head_ref; while (last->next != NULL) { last = last->next; } last->next = newNode; } // 删除指定节点 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) return; // 从链表删除节点 prev->next = temp->next; free(temp); // 释放内存 } // 打印链表 void printList(struct Node* node) { while (node != NULL) { printf("%d -> ", node->data); node = node->next; } printf("NULL\n"); } // 主函数 int main() { struct Node* head = NULL; insertEnd(&head, 1); insertEnd(&head, 2); insertEnd(&head, 3); insertEnd(&head, 4); printf("原始链表: "); printList(head); deleteNode(&head, 3); printf("删除节点3后的链表: "); printList(head); return 0; } ``` 在这个示例中,我们定义了一个链表结构,并实现了插入节点删除节点和打印链表的功能。`deleteNode`函数用于删除指定值的节点
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值