在matlab中實現鏈表

用Matlab實現鏈表,的預備知識是:知道Matlab可以面向對象編程。在Matlab幫助中有詳細介紹。

dlnode.m  定義了鏈表中的節點的類
 

  1. classdef dlnode < handle
  2. % DLNODE  A class to represent a doubly-linked list node.
  3. % Multiple dlnode objects may be linked together to create linked listes.
  4. % Each node contains a piece of data and provides access to the next
  5. % and previous nodes.
  6.    properties
  7.       Data
  8.    end
  9.    properties(SetAccess = private)
  10.       Next
  11.       Prev
  12.    end
  13.     
  14.    methods
  15.       function node = dlnode(Data)
  16.       % DLNODE  Constructs a dlnode object.
  17.          if nargin > 0
  18.             node.Data = Data;
  19.          end
  20.       end
  21.       
  22.       function insertAfter(newNode, nodeBefore)
  23.       % insertAfter  Inserts newNode after nodeBefore.
  24.          disconnect(newNode);
  25.          newNode.Next = nodeBefore.Next;
  26.          newNode.Prev = nodeBefore;
  27.          if ~isempty(nodeBefore.Next)
  28.             nodeBefore.Next.Prev = newNode;
  29.          end
  30.          nodeBefore.Next = newNode;
  31.       end
  32.       
  33.       function insertBefore(newNode, nodeAfter)
  34.       % insertBefore  Inserts newNode before nodeAfter.
  35.          disconnect(newNode);
  36.          newNode.Next = nodeAfter;
  37.          newNode.Prev = nodeAfter.Prev;
  38.          if ~isempty(nodeAfter.Prev)
  39.              nodeAfter.Prev.Next = newNode;
  40.          end
  41.          nodeAfter.Prev = newNode;
  42.       end 
  43.  
  44.       function disconnect(node)
  45.       % DISCONNECT  Removes a node from a linked list.  
  46.       % The node can be reconnected or moved to a different list.
  47.          Prev = node.Prev;
  48.          Next = node.Next;
  49.          if ~isempty(Prev)
  50.              Prev.Next = Next;
  51.          end
  52.          if ~isempty(Next)
  53.              Next.Prev = Prev;
  54.          end
  55.          node.Next = [];
  56.          node.Prev = [];
  57.       end
  58.       
  59.       function delete(node)
  60.       % DELETE  Deletes a dlnode from a linked list.
  61.          disconnect(node);
  62.       end        
  63.       function disp(node)
  64.       % DISP  Displays a link node.
  65.          disp('Doubly-linked list node with data:')
  66.          disp(node.Data);
  67.       end
  68.    end % methods
  69. end % classdef
  70.  
  71.     
  72.     

doubleLinkedList.m  鏈表類
 

  1. classdef doubleLinkedList < handle
  2.    properties (GetAccess = private, SetAccess = private)
  3.       count = 0;
  4.       current = [];
  5.       currentpos = 0;
  6.    end
  7.  
  8.    methods
  9.        function list = doubleLinkedList()
  10.            
  11.        end
  12.        
  13.        function SetPosition(list, p)
  14.            if (p < 0 || p >= list.count)
  15.                error('In SetPosition: Attempt to set a position not in the list.');
  16.            elseif(list.currentpos < p )
  17.                while( list.currentpos ~= p)
  18.                     list.currentpos = list.currentpos + 1;
  19.                     list.current = list.current.Next;
  20.                end
  21.            elseif(list.currentpos > p)
  22.                while( list.currentpos ~= p)
  23.                    list.currentpos = list.currentpos - 1;
  24.                    list.current = list.current.Prev;
  25.                end
  26.            end
  27.        end
  28.        
  29.        function InsertList(list, p, dlnode)
  30.            if (p < 0 || p > list.count)
  31.                error('In InsertList: Attempt to set a position not in the list.');
  32.            else
  33.                if( list.count == 0)
  34.                    list.current = dlnode;
  35.                    list.currentpos = 0;
  36.                elseif( p == list.count)
  37.                    SetPosition(list, p-1);
  38.                    insertAfter(dlnode, list.current)
  39.                else
  40.                    SetPosition(list, p);
  41.                    insertBefore(dlnode, list.current);
  42.                end
  43.                list.current = dlnode;
  44.                list.currentpos = p;
  45.                list.count = list.count + 1;
  46.            end
  47.        end    
  48.           
  49.        function ClearList(list)
  50.            list.count = 0;
  51.            list.current = [];
  52.            list.currentpos = 0;
  53.        end
  54.        
  55.        function isEmpty = ListEmpty(list)
  56.            isEmpty = (list.count == 0);
  57.        end
  58.        
  59.        function size = ListSize(list)
  60.            size = list.count;
  61.        end
  62.        
  63.        function isFull = ListFull(list)
  64.            isFull = 0;
  65.        end
  66.        
  67.        function aNode = RetrieveList(list, p)
  68.            SetPosition(list, p);
  69.            aNode = list.current;
  70.        end
  71.        
  72.        function aNode = DeleteList(list, p)
  73.            SetPosition(list, p);
  74.            aNode = list.current;
  75.            toBeDeleted = list.current;
  76.            if(list.count == 1)
  77.                 list.currentpos = 0;
  78.                 list.current = [];
  79.            elseif( list.count-1 == p)
  80.                list.current = list.current.Prev;
  81.                list.currentpos = list.currentpos - 1;
  82.            else
  83.                 list.current = list.current.Next;
  84.            end
  85.            disconnect(toBeDeleted);
  86.            list.count = list.count - 1;
  87.        end
  88.        
  89.        function aNode = ReplaceList(list, p, dlnode)
  90.            SetPosition(list, p);
  91.            toBeReplaced = list.current;
  92.            insertBefore(dlnode, list.current);
  93.            list.current = dlnode;
  94.            disconnect(toBeReplaced);
  95.            aNode = toBeReplaced;
  96.        end
  97.        
  98.        function position = FindFirstInList(list, func)
  99.            found = 0;
  100.            for i = 0:list.count - 1
  101.                SetPosition(list, i);
  102.                if( func(list.current))
  103.                    found = 1;
  104.                    position = i;
  105.                    break;
  106.                end               
  107.            end
  108.            if( found == 0)
  109.                position = list.count;
  110.            end
  111.        end
  112.        
  113.        function TraverseList(list, func)
  114.            for i = 0:list.count - 1
  115.                aNode = RetrieveList(list, i);
  116.                func(aNode);
  117.            end
  118.        end
  119.    end % methods
  120.    
  121. end % classdef
  122.  


用法:
 

  1. %創建5個節點
  2. len = 5;
  3. for i = 1:len
  4.     node(i) = dlnode(i);
  5. end
  6.  
  7. %創建鏈表,並向鏈表中插入節點
  8. list = doubleLinkedList();
  9. for i = 1:len
  10.     InsertList(list, ListSize(list), node(i));
  11. end
  12.  
  13. %查找鏈表中第一個符合要求元素的位置
  14. pos = FindFirstInList(list, @isMoreThanOne)
  15.  
  16. %打印鏈表內容
  17. TraverseList(list, @printNode);


isMoreThanOne.m 和 printNode.m 是上面測試程序中用到的函數。
isMoreThanOne.m檢查一個node中的數據是否大於1
 

  1. function val = isMoreThanOne(x)
  2.     val = (x.Data > 1);
  3. end 

printNode.m打印一個節點
 

  1. function printNode(aNode)
  2.     aNode.Data
  3. end
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章