FUNCTION SUMMARY: Subroutine SelfLink search the Link array in by self referencing. The searching process will stop until a zero is found. Every non-zero element found in the searching process will be stored in LinkArray and their corresponding index pointers will be stored in LinkPos. Couter will count the number of numzero element in LinkArray. [LinkPos,LinkArray,Counter] = SelfLink(Link,Start) INPUT DATA: Link - N * 1 array containing the link list Start- scalar is the starting point of the searching process OUTPUT DATA: Counter - scalar, number of non-zero elements in LinkArray LinkArray - N*1 array, containing the non-zero element found in the searching process LinkPos - N*1 array, containing the index pointers of the non-zero elements stored in LinkArray INTERNAL DATA: SelRef - scalar, used to do searching in Link list, which is equal to the current found element and also pointer to the next element NOTE: 1. Initiate the SelfRef=Start and Counter=0. Go to step 2. Note: The following steps until step 3 are done in the while loop 2. While the element found in the Link list (Link(SelfRef)) is not zero go to step 2.1 if equal to zero go to step 3. 2.1 Increment Counter by 1. Go to step 2.2 2.2 Store SelfRef to LinkPos which is the index of current non-zero element. Go to step 2.3 2.3 Update the SelfRef equal to Link(SelfRef). Go to step 2.4 2.4 Store the SelfRef value to LinkArray which is the value of current element. Go to step 2. 3. End of the subroutine. Return.
0001 function [LinkPos,LinkArray,Counter] = SelfLink(Link,Start) 0002 % FUNCTION SUMMARY: 0003 % Subroutine SelfLink search the Link array in by self referencing. The 0004 % searching process will stop until a zero is found. Every non-zero 0005 % element found in the searching process will be stored in LinkArray and 0006 % their corresponding index pointers will be stored in LinkPos. Couter 0007 % will count the number of numzero element in LinkArray. 0008 % 0009 % [LinkPos,LinkArray,Counter] = SelfLink(Link,Start) 0010 % 0011 % INPUT DATA: 0012 % Link - N * 1 array containing the link list 0013 % Start- scalar is the starting point of the searching process 0014 % 0015 % OUTPUT DATA: 0016 % Counter - scalar, number of non-zero elements in LinkArray 0017 % LinkArray - N*1 array, containing the non-zero element found in the searching 0018 % process 0019 % LinkPos - N*1 array, containing the index pointers of the non-zero 0020 % elements stored in LinkArray 0021 % 0022 % INTERNAL DATA: 0023 % SelRef - scalar, used to do searching in Link list, which is equal to 0024 % the current found element and also pointer to the next element 0025 % 0026 % NOTE: 0027 % 1. Initiate the SelfRef=Start and Counter=0. Go to step 2. 0028 % Note: The following steps until step 3 are done in the while loop 0029 % 2. While the element found in the Link list (Link(SelfRef)) is not zero 0030 % go to step 2.1 if equal to zero go to step 3. 0031 % 2.1 Increment Counter by 1. Go to step 2.2 0032 % 2.2 Store SelfRef to LinkPos which is the index of current non-zero 0033 % element. Go to step 2.3 0034 % 2.3 Update the SelfRef equal to Link(SelfRef). Go to step 2.4 0035 % 2.4 Store the SelfRef value to LinkArray which is the value of 0036 % current element. Go to step 2. 0037 % 3. End of the subroutine. Return. 0038 0039 % MATPOWER 0040 % Copyright (c) 2014-2015 by Power System Engineering Research Center (PSERC) 0041 % by Yujia Zhu, PSERC ASU 0042 % 0043 % $Id: SelfLink.m 2655 2015-03-18 16:40:32Z ray $ 0044 % 0045 % This file is part of MATPOWER. 0046 % Covered by the 3-clause BSD License (see LICENSE file for details). 0047 % See http://www.pserc.cornell.edu/matpower/ for more info. 0048 0049 SelfRef = Start; 0050 Counter = 0; 0051 while Link(SelfRef)~=0 0052 Counter = Counter+1; % count how many linkage in this list 0053 LinkPos(Counter)=SelfRef; 0054 SelfRef = Link(SelfRef); 0055 LinkArray(Counter)=SelfRef; % create the array of the self referential link list 0056 end 0057 end