2.线性表习题答案.doc

上传人:博****1 文档编号:563230104 上传时间:2023-10-02 格式:DOC 页数:3 大小:39.51KB
返回 下载 相关 举报
2.线性表习题答案.doc_第1页
第1页 / 共3页
2.线性表习题答案.doc_第2页
第2页 / 共3页
2.线性表习题答案.doc_第3页
第3页 / 共3页
亲,该文档总共3页,全部预览完了,如果喜欢就下载吧!
资源描述

《2.线性表习题答案.doc》由会员分享,可在线阅读,更多相关《2.线性表习题答案.doc(3页珍藏版)》请在金锄头文库上搜索。

1、1、基于单链表写出向线性表的末尾添加一个元素的算法。Status Insert(LinkList L,ElemType e) LinkList p,q; q=L; p=(LinkList)malloc(sizeof(LNode);/为元素开辟空间 if(p=null) return error; p-data=e; while(q-next) q=q-next;/使p指向最后一个节点 p-next=q-next;/插入p节点 q-next=p; return ok;2、若L为有序表,基于单链表写出算法,使得向L中插入一个元素e后依然有序。Status InsertInOrder_L(LinkL

2、ist &L,ElemType e) LinkList p,q,s; q=L; p=L-next s=(LinkList)malloc(sizeof(LNode);/为元素开辟空间if(s=null) return error; s-data=e;while( p != NULL ) if( s-value = p-value ) / 找到了s该插入的位置,并且此时p,q已记录下要插入的位置 break else q = p; p = p-next;/ 将s节点插入到q,p节点之间s-next = p;q-next = s;return ok;3、基于单链表写出删除线性表尾元素的算法。Stat

3、us DeleteRear_L(LinkList &L,ElemType &e) LinkList p,q, P=L; Q=L-next; While(q-next!=null) p=q;Q=q-next;p-next=null;free(q);return ok;4. 基于单链表写出算法,删除等于给定值的第一个元素。Status Delete_L(LinkList &L,ElemType e) linklist p,q; If(L=null) return error Else P=L; Q=L-next; While (q!=null) If(q-data=e) break;/找到节点,提

4、前停止循环 Else p=q; Q=q-next; If(q=null) returnP-next=null;Free(q);return ok; 5. 试写一算法,实现顺序表的就地逆置,即利用原表的存储空间将线性表Status converse_sq(sqlist &L) for(i=0;inext; L-next=NULL; while(p!=NULL) q=p; p=p-next; q-next=head-next; L-next=q; Return ok;7. 假设p指向循环链表L中某一结点(简称p结点),试写出删除p结点的直接前驱(假设存在)的算法。Status Delete_L(L

5、inkList &L) linklist s, q;s=p;while(s-next-next!=p)s=s-next;q=s-next;s-next=p;free(q);return ok;8. 建立带表头节点的链表L ,满足用户从键盘正序位输入数据元素Status creatlist_L(LinkList & L) elemtype k; Linklist p,q;L=(linklist)malloc(sizeof(LNode);If (!L)return error;L-next=Null;Q=l; scanf(k)While(k!=-1)P=(linklist)malloc(sizeof(LNode);If (!p)return error;p-data=k;p-next=q-next;q-next=q/q始终指向表尾q=p;scanf(k);return ok;

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 生活休闲 > 社会民生

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号