1、C+数据结构8#include using namespace std; template struct Node T data; Node *next; ; template class LinkList public: LinkList( ); /建立只有头结点的空链表 LinkList(); /析构函数 int Length(); /求单链表的长度 void Insert(T x); /在单链表中的表尾位置插入元素值为x的结点 void Reverse( ); /逆置链表 void DispList( ); /遍历单链表,按序号依次输出各元素 private: Node *first;
2、/单链表的头指针 ; template void LinkList:Reverse() Node *p=first-next; Node *s; first-next=first; while(p!=first) s=p; p=p-next; s-next=first-next; first-next=s; template void LinkList:DispList() Node *p=first-next; coutThe length:Length()endl; coutThe elements:endl; while(p!=first) coutdatanext; coutendl;
3、 template void LinkList:Insert (T x) Node *s , *p; p=first; while(p-next!=first&p-next-datanext; s=new Node; s-data=x; s-next=p-next; p-next=s; template int LinkList:Length() Node *p=first-next; int count = 0 ; while( p!=first ) count+; p=p-next; return count ; /构造函数 template LinkList: LinkList( ) f
4、irst=new Node; first-next=first; /析构函数:释放链表各个结点所占内存单元 template LinkList: LinkList( ) Node *s; s=first-next; while (s!=first) first-next=s-next; delete s; s=first-next; delete s; int main( ) LinkList sa; int x; while(1) cinx; if(!x)break; sa.Insert(x); sa.DispList(); sa.Reverse(); sa.DispList(); retu
5、rn 0; 9#include #include using namespace std; template struct DNode T data; DNode *prior, *next; ; template class DLinkList public: DLinkList( ); /建立只有头结点的双向循环链表链表 DLinkList(); /析构函数 int Length(); /求链表的长度 void Insert(T x); /表尾插入x void DispListF( ); /正向遍历链表,按照逻辑序号输出各元素 void DispListR( ); /反向遍历链表,按照反向
6、逻辑序号输出各元素 private: DNode *first; /链表的头指针 ; template void DLinkList:Insert(T x) DNode *s, *p = first, *q= first-next; s=new DNode; s-data=x; while(p!=first) p=p-next; p-next = s ; s-prior = p; s-next = q ; q-prior = s ; template void DLinkList: DispListR() DNode *p=first-next; coutThe length:Length()
7、endl; coutThe elements:endl; while(p!=first) coutdatanext; coutendl; template void DLinkList: DispListF() DNode *p=first-prior; coutThe length:Length()endl; coutThe elements:endl; while(p!=first) coutdataprior; coutendl; template int DLinkList:Length() int count = 0 ; DNode *p=first-next; while(p!=f
8、irst) count+; p=p-next; return count; /* *前置条件:链表不存在 *输 入:无 *功 能:构建一个只有头结点的双向循环链表 *输 出:无 *后置条件:构建一个链表 */template DLinkList: DLinkList( ) first=new DNode; first-next=first-prior=first; /* *前置条件:链表存在 *输 入:无 *功 能:释放链表各个结点所占内存单元 *输 出:无 *后置条件:链表结点被释放 */template DLinkList: DLinkList() DNode *s; s=first-ne
9、xt; while (s!=first) first-next=s-next; delete s; s=first-next; delete s; int main() DLinkList DL; string subject; int i,n; cinn; for(i=1;isubject; DL.Insert(subject); DL.DispListF(); DL.DispListR(); return 0; 10#include using namespace std; const int M=100; int main() int i,k,n,m,count; int nameM;
10、cinnm; for (i = 0;i n;i +)/编号 namei= i +1; k = 0; for (i = 0;i n;i +) count = 0; while (count m) /每到m个人就输入 while (namek = 0) k = (k + 1)%n; count +; k = (k + 1)%n; k -; if (k 0) k = n-1; coutnamek ; namek = 0; return 0; 15#include #include using namespace std; const int QueueSize=5; template /定义模板类C
11、irQueue class CirQueue public: CirQueue( ); /构造函数,置空队 CirQueue( ); /析构函数 void EnQueue(T x); /将元素x入队 T DeQueue( ); /将队头元素出队 T GetQueue( ); /取队头元素(并不删除) bool Empty( ); /判断队列是否为空,空返回true,否则返回false bool Full(); /判断队列是否为满,满返回true,否则返回false private: T dataQueueSize; /存放队列元素的数组 int front, rear; /队头和队尾指针,分别
12、指向队头元素所在数组的前一下标和队尾元素的数组下标 ; template T CirQueue:GetQueue() if(rear=front) throw Downflow; int i = (front+1)%QueueSize; return datai; template T CirQueue:DeQueue() if(rear=front) throw Downflow; front = (front+1)%QueueSize; return datafront; template void CirQueue:EnQueue(T x) if(rear+1)%QueueSize=fr
13、ont) throw Overflow; rear=(rear+1)%QueueSize; datarear=x; /* * 前置条件:队列不存在 * 输 入:无 * 功 能:初始化队列 * 输 出:无 * 后置条件:创建一个空队列 */template CirQueue:CirQueue( ) front=rear=QueueSize-1; /* * 前置条件:队列已存在 * 输 入:无 * 功 能:销毁队列 * 输 出:无 * 后置条件:释放队列所占用的存储空间 */template CirQueue:CirQueue( ) /* * 前置条件:队列已存在 * 输 入:无 * 功 能:判断
14、队列是否为空 * 输 出:如果队列为空,返回1,否则,返回0 * 后置条件:队列不变 */template bool CirQueue:Empty( ) return front=rear; /* * 前置条件:队列已存在 * 输 入:无 * 功 能:判断队列是否为满 * 输 出:如果队列为满,返回1,否则,返回0 * 后置条件:队列不变 */template bool CirQueue:Full( ) return (rear+1) % QueueSize = front; int main() CirQueue Q; string x; while(1) cinx; if(x=#) bre
15、ak; try coutEnQueue:; Q.EnQueue(x); coutxn; catch(const char *ms) coutx msendl; while(!Q.Empty() x=Q.DeQueue(); coutDeQueue:xendl; try x=Q.GetQueue(); catch(const char *ms) coutGetQueue:The queue is empty,msendl; return 0; 16#include #include using namespace std; const int QueueSize=5; template /定义模
16、板类CirQueue class CirQueue public: CirQueue( ); /构造函数,置空队 CirQueue( ); /析构函数 void EnQueue(T x); /将元素x入队 T DeQueue( ); /将队头元素出队 T GetQueue( ); /取队头元素(并不删除) bool Empty( ); /判断队列是否为空,空返回true,否则返回false bool Full(); /判断队列是否为满,满返回true,否则返回false private: T dataQueueSize; /存放队列元素的数组 int front, rear;/队头和队尾指针,
17、分别指向队头元素所在数组的前一下标和队尾元素的数组下标 int count; /记录队列中数据个数 ; template T CirQueue:GetQueue() if(Empty() throw Downflow; int i = (front+1) % QueueSize ; return datai; template T CirQueue:DeQueue() if(Empty() throw Downflow; count-; front = (front+1) % QueueSize ; return datafront; template void CirQueue:EnQueu
18、e(T x) if(Full() throw Overflow; count+; rear=(rear+1)%QueueSize; datarear=x; /* * 前置条件:队列不存在 * 输 入:无 * 功 能:初始化队列 * 输 出:无 * 后置条件:创建一个空队列 */template CirQueue:CirQueue( ) front=rear=QueueSize-1; count=0; /* * 前置条件:队列已存在 * 输 入:无 * 功 能:销毁队列 * 输 出:无 * 后置条件:释放队列所占用的存储空间 */template CirQueue:CirQueue( ) /*
19、* 前置条件:队列已存在 * 输 入:无 * 功 能:判断队列是否为空 * 输 出:如果队列为空,返回1,否则,返回0 * 后置条件:队列不变 */template bool CirQueue:Empty( ) if(count=0) return true; return false; /* * 前置条件:队列已存在 * 输 入:无 * 功 能:判断队列是否为满 * 输 出:如果队列为满,返回1,否则,返回0 * 后置条件:队列不变 */template bool CirQueue:Full( ) if (count=QueueSize) return true; return false;
20、 int main() CirQueue Q; string x; while(1) cinx; if(x=#) break; try coutEnQueue:; Q.EnQueue(x); coutxn; catch(const char *ms) coutx msendl; while(!Q.Empty() x=Q.DeQueue(); coutDeQueue:xendl; try x=Q.GetQueue(); catch(const char *ms) coutGetQueue:The queue is empty,msendl; return 0; 17#include using
21、namespace std; template struct Node T data; Node *next; ; template class Trans private: Node *first; public: Trans(); Trans(); void Convert(int num,int d); void Push(T x); ; template Trans:Trans() first=new Node; first-next=NULL; template Trans:Trans() Node *s; s=first; while(s) first=s-next; delete
22、 s; s=first; template void Trans:Push(T x) Node *p; p=new Node; p-data=x; p-next=first-next; first-next=p; template void Trans:Convert(int num,int d) while(num) int i=num%d; Push(i); num=(num-i)/d; Node *p; p=first-next; while(p) Node *s; s=p; first-next=s-next; coutdata; delete s; p=first-next; cou
23、tendl; int main() Trans sa; int n,m; cinnm; sa.Convert(n,m); return 0; 19#include #include #include using namespace std; const int QueueSize=100; /循环队列的最大长度 template /定义模板类CirQueue class CirQueue public: CirQueue( ); /构造函数,置空队 CirQueue( ); /析构函数 void EnQueue(T x); /将元素x入队 T DeQueue( ); /将队头元素出队 T GetQueue( ); /取队头元素(并不删除) bool Empty( ); /判断队列是否为空 bool Ful
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1