第一个函数要实现删除链表位序为k的的节点,第二个实现在位序k插入新节点,且节点的值为X
我的代码
bool deleteKth(int k, List ptrL)//建议调用findKth(k-1,ptrL) 和 length(ptrL)完成
{
int len;
len=length(ptrL);
List p,s;
p=findKth(k-1,ptrL);
s=p->next;
p->next=s->next;
free(s);
if(k>len){
printf("the index to delete is invalid, please delete another node.");
}
}
bool insert(ElementType X, int k, List ptrL)//建议调用findKth(k-1,ptrL) 和 length(ptrL)完成
{
List p,s;
p=findKth(k-1,ptrL);
s->data=X;
s->next=p->next;
p->next=s;
}
供参考:
bool deleteKth(int k, List ptrL)//建议调用findKth(k-1,ptrL) 和 length(ptrL)完成
{
int len=length(ptrL);
if(k>len){
printf("the index to delete is invalid, please delete another node.");
return 0;
}
List p,s;
p=findKth(k-1,ptrL);
s=p->next;
p->next=s->next;
free(s);
return 1;
}
bool insert(ElementType X, int k, List ptrL)//建议调用findKth(k-1,ptrL) 和 length(ptrL)完成
{
int len=length(ptrL);
if(k>len){
printf("the index to insert is invalid, please input again.");
return 0;
}
List p,s=(List)malloc(sizeof(struct list)); //结构体名
p=findKth(k-1,ptrL);
s->data=X;
s->next=p->next;
p->next=s;
return 1;
}
# include <stdbool.h>
typedef int ElementType;// typedef在C语言中可用来为数据类型定义别名
//typedef double ElementType;
typedef struct LNode* List;
struct LNode{
ElementType data; // 此处等价于int Data;
List next;
};