在matlab中实现链表

来源:互联网 发布:window查看端口进程 编辑:程序博客网 时间:2024/06/03 09:14
这几天我们老师让我们用Matlab做作业,需要实现一个链表。
昨天上google搜“matlab 实现链表”,无果,有意思的还有好多乱七八糟的人说Matlab有C/C++的接口,调用C/C++实现的链表就好了。
又搜"matlab linked list",哗啦哗啦,好多用matlab实现链表的内容,尤其是在Matlab帮助中就有一个例子链表的程序。
于是,我有点小感慨 -- 差距。
本着,分享的原则,我把我用Matlab实现的链表分享出来,这样搜关键词“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.       function disconnect(node)
  44.       % DISCONNECT  Removes a node from a linked list.  
  45.       % The node can be reconnected or moved to a different list.
  46.          Prev = node.Prev;
  47.          Next = node.Next;
  48.          if ~isempty(Prev)
  49.              Prev.Next = Next;
  50.          end
  51.          if ~isempty(Next)
  52.              Next.Prev = Prev;
  53.          end
  54.          node.Next = [];
  55.          node.Prev = [];
  56.       end
  57.       
  58.       function delete(node)
  59.       % DELETE  Deletes a dlnode from a linked list.
  60.          disconnect(node);
  61.       end        
  62.       function disp(node)
  63.       % DISP  Displays a link node.
  64.          disp('Doubly-linked list node with data:')
  65.          disp(node.Data);
  66.       end
  67.    end % methods
  68. end % classdef
  69.     
  70.     
doubleLinkedList.m  链表类
  1. classdef doubleLinkedList < handle
  2.    properties (GetAccess = private, SetAccess = private)
  3.       count = 0;
  4.       current = [];
  5.       currentpos = 0;
  6.    end
  7.    methods
  8.        function list = doubleLinkedList()
  9.            
  10.        end
  11.        
  12.        function SetPosition(list, p)
  13.            if (p < 0 || p >= list.count)
  14.                error('In SetPosition: Attempt to set a position not in the list.');
  15.            elseif(list.currentpos < p )
  16.                while( list.currentpos ~= p)
  17.                     list.currentpos = list.currentpos + 1;
  18.                     list.current = list.current.Next;
  19.                end
  20.            elseif(list.currentpos > p)
  21.                while( list.currentpos ~= p)
  22.                    list.currentpos = list.currentpos - 1;
  23.                    list.current = list.current.Prev;
  24.                end
  25.            end
  26.        end
  27.        
  28.        function InsertList(list, p, dlnode)
  29.            if (p < 0 || p > list.count)
  30.                error('In InsertList: Attempt to set a position not in the list.');
  31.            else
  32.                if( list.count == 0)
  33.                    list.current = dlnode;
  34.                    list.currentpos = 0;
  35.                elseif( p == list.count)
  36.                    SetPosition(list, p-1);
  37.                    insertAfter(dlnode, list.current)
  38.                else
  39.                    SetPosition(list, p);
  40.                    insertBefore(dlnode, list.current);
  41.                end
  42.                list.current = dlnode;
  43.                list.currentpos = p;
  44.                list.count = list.count + 1;
  45.            end
  46.        end    
  47.           
  48.        function ClearList(list)
  49.            list.count = 0;
  50.            list.current = [];
  51.            list.currentpos = 0;
  52.        end
  53.        
  54.        function isEmpty = ListEmpty(list)
  55.            isEmpty = (list.count == 0);
  56.        end
  57.        
  58.        function size = ListSize(list)
  59.            size = list.count;
  60.        end
  61.        
  62.        function isFull = ListFull(list)
  63.            isFull = 0;
  64.        end
  65.        
  66.        function aNode = RetrieveList(list, p)
  67.            SetPosition(list, p);
  68.            aNode = list.current;
  69.        end
  70.        
  71.        function aNode = DeleteList(list, p)
  72.            SetPosition(list, p);
  73.            aNode = list.current;
  74.            toBeDeleted = list.current;
  75.            if(list.count == 1)
  76.                 list.currentpos = 0;
  77.                 list.current = [];
  78.            elseif( list.count-1 == p)
  79.                list.current = list.current.Prev;
  80.                list.currentpos = list.currentpos - 1;
  81.            else
  82.                 list.current = list.current.Next;
  83.            end
  84.            disconnect(toBeDeleted);
  85.            list.count = list.count - 1;
  86.        end
  87.        
  88.        function aNode = ReplaceList(list, p, dlnode)
  89.            SetPosition(list, p);
  90.            toBeReplaced = list.current;
  91.            insertBefore(dlnode, list.current);
  92.            list.current = dlnode;
  93.            disconnect(toBeReplaced);
  94.            aNode = toBeReplaced;
  95.        end
  96.        
  97.        function position = FindFirstInList(list, func)
  98.            found = 0;
  99.            for i = 0:list.count - 1
  100.                SetPosition(list, i);
  101.                if( func(list.current))
  102.                    found = 1;
  103.                    position = i;
  104.                    break;
  105.                end               
  106.            end
  107.            if( found == 0)
  108.                position = list.count;
  109.            end
  110.        end
  111.        
  112.        function TraverseList(list, func)
  113.            for i = 0:list.count - 1
  114.                aNode = RetrieveList(list, i);
  115.                func(aNode);
  116.            end
  117.        end
  118.    end % methods
  119.    
  120. end % classdef

用法:
  1. %创建5个节点
  2. len = 5;
  3. for i = 1:len
  4.     node(i) = dlnode(i);
  5. end
  6. %创建链表,并向链表中插入节点
  7. list = doubleLinkedList();
  8. for i = 1:len
  9.     InsertList(list, ListSize(list), node(i));
  10. end
  11. %查找链表中第一个符合要求元素的位置
  12. pos = FindFirstInList(list, @isMoreThanOne)
  13. %打印链表内容
  14. 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
本人还是在校本科生,没有什么丰富项目经验编程。在这里写blog有班门弄斧之嫌。Matlab程序写得不好,请大家多包涵。
本文主要思想是表达一个意思:用Matlab可以面向对象编程,并且实现类似C++的链表
原创粉丝点击