在matlab中实现链表

 这几天我们老师让我们用Matlab做作业,需要实现一个链表。
昨天上google搜“matlab 实现链表”,无果,有意思的还有好多乱七八糟的人说Matlab有C/C++的接口,调用C/C++实现的链表就好了。
又搜"matlab linked list",哗啦哗啦,好多用matlab实现链表的内容,尤其是在Matlab帮助中就有一个例子链表的程序。
于是,我有点小感慨 -- 差距。
本着,分享的原则,我把我用Matlab实现的链表分享出来,这样搜关键词“matlab 实现链表”的时候就能搜到结果了。

================================
用Matlab实现链表,的预备知识是:知道Matlab可以面向对象编程。在Matlab帮助中有详细介绍。

dlnode.m  定义了链表中的节点的类

classdef dlnode < handle
% DLNODE  A class to represent a doubly-linked list node.
% Multiple dlnode objects may be linked together to create linked listes.
% Each node contains a piece of data and provides access to the next
% and previous nodes.
   properties
      Data
   end
   properties(SetAccess = private)
      Next
      Prev
   end
   
   methods
      function node = dlnode(Data)
      % DLNODE  Constructs a dlnode object.
         if nargin > 0
            node.Data = Data;
         end
      end
     
      function insertAfter(newNode, nodeBefore)
      % insertAfter  Inserts newNode after nodeBefore.
         disconnect(newNode);
         newNode.Next = nodeBefore.Next;
         newNode.Prev = nodeBefore;
         if ~isempty(nodeBefore.Next)
            nodeBefore.Next.Prev = newNode;
         end
         nodeBefore.Next = newNode;
      end
     
      function insertBefore(newNode, nodeAfter)
      % insertBefore  Inserts newNode before nodeAfter.
         disconnect(newNode);
         newNode.Next = nodeAfter;
         newNode.Prev = nodeAfter.Prev;
         if ~isempty(nodeAfter.Prev)
             nodeAfter.Prev.Next = newNode;
         end
         nodeAfter.Prev = newNode;
      end

      function disconnect(node)
      % DISCONNECT  Removes a node from a linked list. 
      % The node can be reconnected or moved to a different list.
         Prev = node.Prev;
         Next = node.Next;
         if ~isempty(Prev)
             Prev.Next = Next;
         end
         if ~isempty(Next)
             Next.Prev = Prev;
         end
         node.Next = [];
         node.Prev = [];
      end
     
      function delete(node)
      % DELETE  Deletes a dlnode from a linked list.
         disconnect(node);
      end       
      function disp(node)
      % DISP  Displays a link node.
         disp('Doubly-linked list node with data:')
         disp(node.Data);
      end
   end % methods
end % classdef

   
   
doubleLinkedList.m  链表类

classdef doubleLinkedList < handle
   properties (GetAccess = private, SetAccess = private)
      count = 0;
      current = [];
      currentpos = 0;
   end

   methods
       function list = doubleLinkedList()
          
       end
      
       function SetPosition(list, p)
           if (p < 0 || p >= list.count)
               error('In SetPosition: Attempt to set a position not in the list.');
           elseif(list.currentpos < p )
               while( list.currentpos ~= p)
                    list.currentpos = list.currentpos + 1;
                    list.current = list.current.Next;
               end
           elseif(list.currentpos > p)
               while( list.currentpos ~= p)
                   list.currentpos = list.currentpos - 1;
                   list.current = list.current.Prev;
               end
           end
       end
      
       function InsertList(list, p, dlnode)
           if (p < 0 || p > list.count)
               error('In InsertList: Attempt to set a position not in the list.');
           else
               if( list.count == 0)
                   list.current = dlnode;
                   list.currentpos = 0;
               elseif( p == list.count)
                   SetPosition(list, p-1);
                   insertAfter(dlnode, list.current)
               else
                   SetPosition(list, p);
                   insertBefore(dlnode, list.current);
               end
               list.current = dlnode;
               list.currentpos = p;
               list.count = list.count + 1;
           end
       end   
         
       function ClearList(list)
           list.count = 0;
           list.current = [];
           list.currentpos = 0;
       end
      
       function isEmpty = ListEmpty(list)
           isEmpty = (list.count == 0);
       end
      
       function size = ListSize(list)
           size = list.count;
       end
      
       function isFull = ListFull(list)
           isFull = 0;
       end
      
       function aNode = RetrieveList(list, p)
           SetPosition(list, p);
           aNode = list.current;
       end
      
       function aNode = DeleteList(list, p)
           SetPosition(list, p);
           aNode = list.current;
           toBeDeleted = list.current;
           if(list.count == 1)
                list.currentpos = 0;
                list.current = [];
           elseif( list.count-1 == p)
               list.current = list.current.Prev;
               list.currentpos = list.currentpos - 1;
           else
                list.current = list.current.Next;
           end
           disconnect(toBeDeleted);
           list.count = list.count - 1;
       end
      
       function aNode = ReplaceList(list, p, dlnode)
           SetPosition(list, p);
           toBeReplaced = list.current;
           insertBefore(dlnode, list.current);
           list.current = dlnode;
           disconnect(toBeReplaced);
           aNode = toBeReplaced;
       end
      
       function position = FindFirstInList(list, func)
           found = 0;
           for i = 0:list.count - 1
               SetPosition(list, i);
               if( func(list.current))
                   found = 1;
                   position = i;
                   break;
               end              
           end
           if( found == 0)
               position = list.count;
           end
       end
      
       function TraverseList(list, func)
           for i = 0:list.count - 1
               aNode = RetrieveList(list, i);
               func(aNode);
           end
       end
   end % methods
  
end % classdef


用法:

%创建5个节点
len = 5;
for i = 1:len
    node(i) = dlnode(i);
end

%创建链表,并向链表中插入节点
list = doubleLinkedList();
for i = 1:len
    InsertList(list, ListSize(list), node(i));
end

%查找链表中第一个符合要求元素的位置
pos = FindFirstInList(list, @isMoreThanOne)

%打印链表内容
TraverseList(list, @printNode);

isMoreThanOne.m 和 printNode.m 是上面测试程序中用到的函数。
isMoreThanOne.m检查一个node中的数据是否大于1

function val = isMoreThanOne(x)
    val = (x.Data > 1);
end
printNode.m打印一个节点

function printNode(aNode)
    aNode.Data
end


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/freestyle_zhy/archive/2008/11/27/3391920.aspx

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值