您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > 数据结构第二次上机指导书
实验1线性表顺序存储结构实现3.1实验目的和要求掌握线性表的结构性质及其链式存储结构各种操作的实现;3.2实验内容用C#编程实现线性表的链式存储结构及各种操作,尤其要实现任意位置元素的插入和删除操作。3.3实验指导1、建立控制台应用程序,创建结点类NodeT和链式顺序表类SepLinkedListT,结点类NodeT包括两个字段成员_data和_next,分别表示该结点的结点元素和下一结点;包括两个属性成员Data和Next,分别完成对_data和_next读写;3个构造函数Node()、Node(Tdata)、Node(NodeTnext),分别表示创建无参数的对象、参数为T类型数据的对象、参数为NodeT类型结点的对象。链式顺序表类SepLinkedListT包括1个私有字段成员_head-头指针;包括1个属性成员Head,完成对_head读写;包括一系列操作方法成员;2、在主入口函数中创建类的实例,对该实例完成各种操作,每次操作完结果进行输出。3.4部分程序参考代码结点类PublicclassNodeT{privateT_data;publicTData{get{return_data;}set{_data=value;}}privateNodeT_next;publicNodeTNext{get{return_next;}set{_next=value;}}publicNode(){_data=default(T);_next=null;}publicNode(Tdata){_data=data;_next=null;}publicNode(NodeTnext){_next=next;}}单链表类PublicclassSepLinkedListT{privateNodeT_head;///头指针publicNodeTHead{get{return_head;}set{_head=value;}}///获取单链表长度publicintGetLength(){NodeTp=_head;intlength=0;while(p!=null){length++;p=p.Next;}returnlength;}///清空单链表publicvoidClear(){_head=null;}判断链表是否为空publicboolIsEmpty(){if(_head==null){returntrue;}else{returnfalse;}}///链表末尾追加数据元素publicvoidAppend(Titem){NodeTp=newNodeT();NodeTq=newNodeT(item);if(_head==null){_head=q;return;}p=_head;while(p.Next!=null){p=p.Next;}p.Next=q;}///删除第i个数据元素publicTDelete(inti){该部分代码自己实现}NodeTp=_head;intj=1;while(p.Next!=null&&ji){j++;q=p;p=p.Next;}if(j==i){q.Next=p.Next;returnp.Data;}else{Console.WriteLine(该位置不存在结点);returndefault(T);}}///在第i个位置前插入数据元素publicvoidInsert(Titem,inti){该部分代码自己实现}///读取第i位置元素publicTGetElem(inti){if(IsEmpty()){Console.WriteLine(链表为空);returndefault(T);}NodeTp=newNodeT();p=_head;intj=1;while(p.Next!=null&&ji){p=p.Next;j++;}if(j==i){returnp.Data;}else{Console.WriteLine(未找到该序号的结点);returndefault(T);}}///按值查找数据元素publicintLocate(Titem){if(IsEmpty()){Console.WriteLine(链表为空);return-1;}NodeTp=newNodeT();p=_head;inti=1;while(p!=null&&!item.Equals(p.Data)){p=p.Next;i++;}if(p!=null){returni;}else{Console.WriteLine(单链表中不存在指定数据元素);return-1;}}}2.2.3单链表的建立:第一种方式:(采用从尾部加入结点的方式)///建立单链表staticSepLinkedListintCreateLinkedList(){SepLinkedListintresult=newSepLinkedListint();Nodeintr=newNodeint();r=result.Head;intelem=Int32.Parse(Console.ReadLine());while(elem!=-1)//以-1做为结束标志{Nodeintp=newNodeint(elem);if(result.Head==null){result.Head=p;}else{r.Next=p;//加入链表}r=p;//记下最后一个结点elem=Int32.Parse(Console.ReadLine());}if(r!=null){r.Next=null;//最后一个节点地址域置空}returnresult;}第二种方式:(采用在头部加入结点的方式)staticSepLinkedListintCreateSepLinkedList(){SepLinkedListintresult=newSepLinkedListint();intd=Int32.Parse(Console.ReadLine());while(d!=-1){Nodeintelem=newNodeint(d);elem.Next=result.Head;result.Head=elem;d=Int32.Parse(Console.ReadLine());}returnresult;}
本文标题:数据结构第二次上机指导书
链接地址:https://www.777doc.com/doc-4673906 .html