单链表循环链表多项式及其相加双向链表稀疏矩阵.ppt
《单链表循环链表多项式及其相加双向链表稀疏矩阵.ppt》由会员分享,可在线阅读,更多相关《单链表循环链表多项式及其相加双向链表稀疏矩阵.ppt(90页珍藏版)》请在三一办公上搜索。
1、单链表 循环链表多项式及其相加双向链表稀疏矩阵,第三章 链表,一、单链表,线性表的链式表示,顺序表的优点是可以随机选取表中元素缺点是插入删除操作复杂。用指针将互不相连的内存结点串成的线性表叫线性链表。结点 node 由一个数据元素域,一个或几个指针域组成。单链表的结点只有一个指针域。,几个结点,前一个结点的指针,指向后一个结点,就连接成一个线性链表。,线性链表的优点则是插入,删除快捷,缺点是选取复杂。,#include template class Node Node*next;/next 是下一个结点的地址 public:T data;/the data is public Node(con
2、st T,1.结点类的定义,/constructor.initialize data and/pointer members,template Node:Node(const T&item,Node*ptrnext):data(item),next(ptrnext)),/return value of private member next,template Node*Node:NextNode(void)const return next;,/insert a node p after the current one,template void Node:InsertAfter(Node*p
3、)/p points to successor of the current/node,and current node points to p.p-next=next;next=p;,/delete the node following current and return its address,template Node*Node:DeleteAfter(void)/save address of node to be deleted Node*tempPtr=next;/if there isnt a successor,return NULL if(next=NULL)return
4、NULL;/current node points to successor of tempPtr.next=tempPtr-next;/return the pointer to the unlinked node return tempPtr;,2人工建立一个链表,void main(void)Node*a,*b,*c;a=new Node(a);b=new Node(b);c=new Node(c);Node*head,*p;head=new Node();p=head;head-InsertAfter(a);head-InsertAfter(b);head-InsertAfter(c)
5、;while(p!=NULL)cout dataNextNode();测试结果:打印 c b a,3.定义线性链表的一些操作,#include node.h/allocate a node with data member item and pointer nextPtrtemplate Node*GetNode(constT,enum AppendNewline noNewline,addNewline;,template/print a linked listvoid PrintList(Node*head,AppendNewline addnl=noNewline)/currPtr ch
6、ains through the list,starting at head Node*currPtr=head;/print the current nodes data until end of list while(currPtr!=NULL)/output newline if addl=addNewline if(addnl=addNewline)cout data data NextNode();,/find an item in a linked list head;return TRUE and,/value of previous pointer if found;other
7、wise return FALSEtemplate int Find(Node*head,T/failed to locate item,/insert item at the front of list,template void InsertFront(Node*,/find rear of the list and append item,template void InsertRear(Node*,/delete the first node of the list,template void DeleteFront(Node*,/delete the first occurrence
8、 of key in the list,template void Delete(Node*,/insert item into the ordered list,template void InsertOrder(Node*,/delete all the nodes in the linked list,template void ClearList(Node*,链表插入排序,#include#pragma hdrstop#include node.h#include nodelib.h,template void LinkSort(T a,int n),Node*ordlist=NULL
9、,*currPtr;int i;for(i=0;i data;currPtr=currPtr-NextNode();ClearList(ordlist);,/scan the array and print its elements,void PrintArray(int a,int n)for(int i=0;i n;i+)cout ai;,/*void main(void),/initialized array with 10 integer values int A10=82,65,74,95,60,28,5,3,33,55;LinkSort(A,10);/sort the array
10、cout Sorted array:;PrintArray(A,10);/print the array cout endl;*/#endif/NODE_LIBRARY,链表的游标类(Iterator),游标类主要用于单链表的搜索。游标类的定义原则:Iterator类是List类和ListNode类的友元类。Iterator对象引用已有的List类对象。Iterator类有一数据成员current,记录对单链表最近处理到哪一个结点。Iterator类提供若干测试和搜索操作,表示链表的三个类的模板定义 enum Boolean False,True;template class List;tem
11、plate class ListIterator;template class ListNode/表结点friend class List;friend class ListIterator;public:private:Type data;ListNode*link;,template class List/链表类public:private:ListNode*first,*last;template class ListIterator private:const List/当前结点指针public:,ListNode*listfirst;?,ListIterator(const List
12、/返回链表当前结点的下一个结点的地址,链表的游标类成员函数的实现template Boolean ListIterator:NotNull()/检查链表中当前元素是否非空 if(current!=NULL)return True;else return False;,current,current,情况 1 返回True 情况 2 返回False,template Boolean ListIterator:NextNotNull()/检查链表中下一元素是否非空 if(current!=NULL,current,current,情况 1 返回True 情况 2 返回False,template
13、ListNode*ListIterator:First()/返回链表中第一个结点的地址 if(list.first-link!=NULL)current=list.first-link;return current;else current=NULL;return NULL;,list.first,current,链表中有表头结点,current,template ListNode*ListIterator:Next()/返回链表中当前结点的下一个结点的地址 if(current!=NULL,current,利用游标类(iterator)计算元素的和int sum(const List,静态链
14、表结构,静态链表定义,const int MaxSize=100;/静态链表大小template class StaticList;template class SListNode friend class StaticList;Type data;/结点数据 int link;/结点链接指针template class StaticList SListNode NodesMaxSize;int newptr;/当前可分配空间首地址,将链表空间初始化,template void StaticList:InitList()Nodes0.link=-1;newptr=1;/当前可分配空间从 1 开
15、始建/立带表头结点的空链表 for(int i=1;i MaxSize-1;i+)Nodesi.link=i+1;/构成空闲链接表 NodesMaxSize-1.link=-1;/链表收尾,在静态链表中查找具有给定值的结点,template int StaticList:Find(Type x)int p=Nodes0.link;/指针 p 指向链表第一个结点 while(p!=-1)if(Nodesp.data!=x)p=Nodesp.link;else break;/逐个结点检测查找具有给定值的结点 return p;,在静态链表的表尾追加一个新结点,template int Static
16、List:Append(Type x)if(newptr=-1)return 0;/追加失败 int q=newptr;/分配结点 newptr=Nodesnewptr.link;Nodesq.data=x;Nodesq.link=-1;int p=0;/查找表尾 while(Nodesp.link!=-1)p=Nodesp.link;Nodesp.link=q;/追加 return 1;,在静态链表中查找第 i 个结点,template int StaticList:Locate(int i)if(i 0)return-1;/参数不合理 if(i=0)return 0;int j=0,p=N
17、odes0.link;while(p!=-1,在静态链表第 i 个结点处插入一个新结点,template int StaticList:Insert(int i,Type x)int p=Locate(i-1);if(p=-1)return 0;/找不到结点 int q=newptr;/分配结点 newptr=Nodesnewptr.link;Nodesq.data=x;Nodesq.link=Nodesp.link;/链入 Nodesp.link=q;return 1;,在静态链表中释放第 i 个结点,template int StaticList:Remove(int i)int p=Lo
18、cate(i-1);if(p=-1)return 0;/找不到结点 int q=Nodesp.link;/第 i 号结点 Nodesp.link=Nodesq.link;Nodesq.link=newptr;/释放 newptr=q;return 1;,二、循环链表(Circular List),循环链表是单链表的变形。循环链表的最后一个结点的 link 指针不为 NULL,而是指向了表的前端。为简化操作,在循环链表中往往加入表头结点。循环链表的特点是:只要知道表中某一结点的地址,就可搜寻到所有其他结点的地址。,循环链表的示例带表头结点的循环链表,a0,a1,a2,an-1,first,an-
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 单链表 循环 多项式 及其 相加 双向 稀疏 矩阵

链接地址:https://www.31ppt.com/p-4937239.html