C/C++ | 26-17 已知一个单向链表的头,删除其某一个结点的算法

/*
已知一个单向链表的头,请写出删除其某一个结点的算法,
要求,先找到此结点,然后删除。
*/




#include <cstdio>
#include <deque>
#include <algorithm>
#include <iterator>
#include <stdio.h>  
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <assert.h>

using namespace std;

typedef struct LNode
{
	int data;
	struct LNode *next;
}LNode;

LNode *CreatLink(int num)  // 构造
{
	int i=1;
	LNode *head, *tail, *p;
	head = (LNode *)malloc(sizeof(LNode));
	tail = head;
	while (num--)
	{
		p = (LNode *)malloc(sizeof(LNode));
		p->data = i;
		tail->next = p;
		tail = p;
		i++;
	}
	tail->next = NULL;
	return head;
}

LNode *FindLink(LNode *head,int X)  //查找
{
	assert( head != NULL);
	LNode *chick=head->next;
	while (chick != NULL)
	{
		if (chick->data == X)
			return chick;
		else
			chick = chick->next;
	}
	return NULL;
}

void DeleteLink(LNode *head, int X)
{
	assert(head != NULL);
	LNode *p;
	LNode *q;
	p=FindLink(head, X);
	q = p->next;
	p->data = q->data;
	p->next = q->next;
	free(q);
}


int main()
{
	LNode *head = CreatLink(50);
	DeleteLink(head, 5);
	system("pause");
	return 0;
}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值