恭喜,你发布的帖子
发布于 2022-12-16 21:35:12
3楼
/双向链表
//起始:.Prev = -1
//尾端:.Next = -1
//单链表无法设置首部标志
//单链表如果存储上前面的被删,后又补上,无法判断首部
//*********************************************************
REGION 如果 "RecipeName" 为空,退出
IF LEN(IN := #recipeName) = 0 THEN
#errorCode := 16#9001; //未输入配方名
#done := FALSE;
#error := TRUE;
#recipeIndexFound := -1;
RETURN;
END_IF;
END_REGION
REGION 输入数组上限
#tempUpper := #upperBound;
END_REGION
REGION 找第一条非空有效记录
FOR #temp_i := 0 TO #tempUpper DO
//链表指针不同,则为有效记录
IF #recipeList.RecipeList[#temp_i].Prev <> #recipeList.RecipeList[#temp_i].Next THEN
#tempFirstLog := #temp_i;
EXIT;
在这里不是查找到的是首记录而且是非空的吗?
END_IF;
//最后一条记录仍无效
IF #temp_i = #tempUpper THEN
#errorCode := 16#9002; //链表为空,无有效记录
#done := FALSE;
#recipeIndexFound := -1;
RETURN;
END_IF;
END_FOR;
END_REGION
REGION 正序查配方
#temp_i := #tempFirstLog; //从第一条记录开始
#tempIndexFound := -3; // >= 0为找到的记录索引
WHILE #temp_i >= 0 DO
IF #recipeName = #recipeList.RecipeList[#temp_i].ProductName THEN
#tempIndexFound := #temp_i;
EXIT;
END_IF;
IF #recipeList.RecipeList[#temp_i].Next = -1 THEN
//正向无结果
// Next = -1 为结束标志
#tempIndexFound := -1;
//退出循环
EXIT;
END_IF;
//链表指针指向下一条记录
#temp_i := #recipeList.RecipeList[#temp_i].Next;
END_WHILE;
END_REGION
REGION 反向查找
IF #tempIndexFound < 0 THEN
#temp_i := #recipeList.RecipeList[#tempFirstLog].Prev;
这个地方不太理解,反向查找不得从数组后边来吗,为这么从第一个开始,上来temp _i=-1吗后面无法进行了。
WHILE #temp_i >= 0 DO
IF #recipeName = #recipeList.RecipeList[#temp_i].ProductName THEN
#tempIndexFound := #temp_i;
EXIT;
END_IF;
IF #recipeList.RecipeList[#temp_i].Prev = -1 THEN
//反向查找无结果
// Prev = -1 为起始标志
#tempIndexFound := -7;
EXIT;
END_IF;
//查链表下一条记录
#temp_i := #recipeList.RecipeList[#temp_i].Prev;
END_WHILE;
END_IF;
END_REGION
REGION 输出
IF #tempIndexFound >= 0 THEN
//根据配方名称找了到配方
//如果需要输出找到的完整记录,添加输入参数: Recipe_Found 类型REF_TO "typeBeerRecipe_Link_x1"
//#Recipe_Found^ := #recipeList.RecipeList[#tempIndexFound];
#done := True; //输出状态
#error := FALSE;
#errorCode := 0;
#recipeIndexFound := #tempIndexFound;
RETURN; //结束查找
END_IF;
#done := FALSE; //未找到配方
#error := TRUE;
#errorCode := 16#9003;
#recipeIndexFound := -1;
END_REGION
请填写推广理由:
分享
只看
楼主