题目描述:
已知单链表L是一个递增有序表,试编写一高效算法删除表中值大于min且小于max的结点(若表中有这样的结点),同时释放被删除结点的空间,这里min和max是两个参数。请分析算法的时间复杂度。
定义结构体
struct LNode{
int data;
struct LNode *next;
};
typedef struct LNode LNode,* LinkList;
初始化链表
LinkList InitLNode(void){
LinkList head;
head = (LinkList)malloc(LEN);
if(!head){
printf("内存空间申请失败!\n");
exit(ERROR);
}
head->data = 0;
head->next = NULL;
return head;
}
创建一个链表
int CreatList(LinkList head){
LinkList pleft,pright;
pleft = head;
pright = (LinkList)malloc(LEN);
printf("请按照递增的顺序向链表中赋值:(输入-1认为输入终止)\n");
scanf("%d",&pright->data);
while(pright->data!=-1){
head->data++;
pleft->next = pright;
pleft = pright;
pright = (LinkList)malloc(LEN);
scanf("%d",&pright->data);
}
pleft->next = NULL;
free(pright);
return OK;
}
删除结点
int delLNode(LinkList head,int num){
if(!head->next){
printf("空链表!\n");
return ERROR;
}
LinkList pleft,pright;
pleft = head;
pright = head->next;
while(pright->data!=num&&pright!=NULL){
pleft = pright;
pright = pright->next;
}
if(pright->data==num){
pleft->next = pright->next;
free(pright);
head->data--;
}else{
printf("\n没有找到该数据!\n");
}
return OK;
}
删除介于MIN&MAN之间的结点
int delLink(LinkList head,float mink,float maxk){
LinkList pleft,pright;
pright = pleft = head->next;
while(pright->data<=mink&&pright){
pleft = pright;
pright = pright->next;
}
if(!pright){
printf("没有介于%g和%g之间的数据\n",mink,maxk);
return ERROR;
}
while(pright->data<maxk&&pright){
delLNode(head,pright->data);
pright = pleft->next;
}
return OK;
}
遍历输出链表
int PrintLNode(LinkList head){
if(!head->next){
printf("空链表!\n");
return ERROR;
}
LinkList p;
p = head->next;
printf("\n结果如下:\n");
while(p){
printf("%d ",p->data);
p = p->next;
}
return OK;
}
完整code:
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
#define LEN sizeof(struct LNode)
struct LNode{
int data;
struct LNode *next;
};
typedef struct LNode LNode,* LinkList;
LinkList InitLNode(void){
LinkList head;
head = (LinkList)malloc(LEN);
if(!head){
printf("内存空间申请失败!\n");
exit(ERROR);
}
head->data = 0;
head->next = NULL;
return head;
}
int CreatList(LinkList head){
LinkList pleft,pright;
pleft = head;
pright = (LinkList)malloc(LEN);
printf("请按照递增的顺序向链表中赋值:(输入-1认为输入终止)\n");
scanf("%d",&pright->data);
while(pright->data!=-1){
head->data++;
pleft->next = pright;
pleft = pright;
pright = (LinkList)malloc(LEN);
scanf("%d",&pright->data);
}
pleft->next = NULL;
free(pright);
return OK;
}
int PrintLNode(LinkList head){
if(!head->next){
printf("空链表!\n");
return ERROR;
}
LinkList p;
p = head->next;
printf("\n结果如下:\n");
while(p){
printf("%d ",p->data);
p = p->next;
}
return OK;
}
int delLNode(LinkList head,int num){
if(!head->next){
printf("空链表!\n");
return ERROR;
}
LinkList pleft,pright;
pleft = head;
pright = head->next;
while(pright->data!=num&&pright!=NULL){
pleft = pright;
pright = pright->next;
}
if(pright->data==num){
pleft->next = pright->next;
free(pright);
head->data--;
}else{
printf("\n没有找到该数据!\n");
}
return OK;
}
int delLink(LinkList head,float mink,float maxk){
LinkList pleft,pright;
pright = pleft = head->next;
while(pright->data<=mink&&pright){
pleft = pright;
pright = pright->next;
}
if(!pright){
printf("没有介于%g和%g之间的数据\n",mink,maxk);
return ERROR;
}
while(pright->data<maxk&&pright){
delLNode(head,pright->data);
pright = pleft->next;
}
return OK;
}
main(){
LinkList head;
float mink,maxk;
head = InitLNode();
CreatList(head);
PrintLNode(head);
printf("\n想删除介于哪两者之间的数据?\n");
scanf("%f%f",&mink,&maxk);
delLink(head,mink,maxk);
PrintLNode(head);
}
INPUT:
请按照递增的顺序向链表中赋值:(输入-1认为输入终止)
1 3 5 7 9 -1
OUTPUT:
结果如下:
1 3 5 7 9
想删除介于哪两者之间的数据?
3 9
结果如下:
1 3 9
数据结构知识点
线性表
栈&队列
串
二叉树