正在加载图片...
《数据结构》实验指导/实验二:单链表的存储及操作 3 public string DispList( ∥将单链表所有结点值构成一个字符串返回 Linklist p= head next; p指向开始结点 f(p=nu)str="空串" while(p!=null) 作不为nu输出p结点的data字段 str +=p data +" p-p. next p移向下一个结点 return str public int ListLengtho ∥|求单链表数据结点个数 LinkList p p= head 指向头结点n置为0即头结点的序号为0) while(p. next !=null n++ return (n) 循环结束p指向尾结点其序号n为结点个数 public bool GetElem(inti, ref string e)∥求单链表中某个数据元素值 int j=0 LinkList p p=head 指向头结点j置为即头结点的序号为0) while g<i&& p I=null) 找第i个结点 H+; if (p== null) 不存在第i个数据结点返回 false return false. else 存在第i个数据结点返回true return true: public int Locate Elem(string e) ∥按元素值查找 管理科学与工程学科/共7页第3页《数据结构》实验指导 / 实验二:单链表的存储及操作 3 管理科学与工程学科 / 共7页,第3页 } public string DispList() //将单链表所有结点值构成一个字符串返回 { string str = ""; LinkList p; p = head.next; //p 指向开始结点 if (p == null) str = "空串"; while (p != null) //p 不为 null,输出 p 结点的 data 字段 { str += p.data + " "; p = p.next; //p 移向下一个结点 } return str; } public int ListLength() //求单链表数据结点个数 { int n = 0; LinkList p; p = head; //p 指向头结点,n 置为 0(即头结点的序号为 0) while (p.next != null) { n++; p = p.next; } return (n); //循环结束,p 指向尾结点,其序号 n 为结点个数 } public bool GetElem(int i, ref string e) //求单链表中某个数据元素值 { int j = 0; LinkList p; p = head; //p 指向头结点,j 置为 0(即头结点的序号为 0) while (j < i && p != null) //找第 i 个结点 p { j++; p = p.next; } if (p == null) //不存在第 i 个数据结点,返回 false return false; else //存在第i个数据结点,返回true { e = p.data; return true; } } public int LocateElem(string e) //按元素值查找 {
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有