c语音#fin
来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/19 06:18:39
c语音#fin
c语音#fin
c语音#fin
//未调试
#include <stdio.h>
#include <malloc.h>
typedef struct TN{//定义表节点
int val;
struct TNode *Next;
}TNode;
TNode *MTable()//交互创建表
{
TNode *h,*p,*q,*r;
int temp;
h=NULL;//创建空表
printf("Please input value of nodes;");
scanf("%d",&temp);
if(temp)
{
h=(TNode *)malloc(sizeof(TNode));//创建单节点表
h->val=temp;
h->Next=NULL;
printf(",");
for(scanf("%d",&temp);temp;scanf("%d",&temp))//增加节点
{
IstByVal(h,temp);
printf(",");
}
}
return(h);
}
TNode *FinByVal(TNode *TH_Fin,int val_Fin)
{//查找val域不大于指定值的节点,除非头节点的val域就大于指定值
TNode *i
if(TH_Fin==NULL)
i=null;
else
for(i=TH_Fin;i->Next!=NULL&&i->val!=val_Fin&&i->Next->val<=val_Fin;i=i->Next);
return(i);
}
void IstByVal(TNode *TH_Ist,int Ist_val)
{//将节点插入适当位置
TNode *p_Ist,*q_Ist,*r_Ist;
q_Ist=(TNode *)malloc(sizeof(TNode));
q_Ist->val=temp;
r_Ist=FinByVal(TH_Ist,Ist_val);
if(r_Ist==TH_Ist&&TH_Ist->val>Ist_val)
{
p_Ist=TH_Ist;
TH_Ist=q_Ist;
q_Ist->Next=p_Ist;
}
else
{
p_Ist=r_Ist->Next;
r_Ist->Next=q_Ist;
q_Ist->Next=p_Ist;
}
}
void DelByVal(TNode *TH_Del,int Del_val)
{//删除指定节点,如果其存在
TNode *s;
s=FinByVal(TH_Del,Del_val-1);
if(s->Next!=NULL)
if(s->Next->val==Del_val)
s->Next=s->Next->Next;
}
void main()
{
TNode *Head;
int Value;
Head=MTable();
printf("\n");
scanf("%d",&Value);
IstByVal(Head,Value);
printf("\n");
scanf("%d",&Value);
DelByVal(Head,Value);
}