欢迎来到天天文库
浏览记录
ID:59360487
大小:40.50 KB
页数:8页
时间:2020-09-04
《双向循环链表-C++实现.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、#ifndefNODE_H#defineNODE_H#defineNULL0classnode{friendclassLinkList;private:node(intelement,node*pre,node*next){this->m_element=element;this->m_next=next;this->m_pre=pre;}~node(){}private:intm_element;node*m_next;node*m_pre;};#endif#ifndefLINKLIST_H#defineLINKLIST_H#incl
2、ude"node.h"classLinkList{public:LinkList();virtual~LinkList();voidaddFirst(inti);voidaddLast(inti);voidaddList(inti,intpos);intremoveFirst();intremoveLast();intremoveList(intpos);intgetFirst();intgetLast();intgetList(intpos);voiditerate();private:node*m_head;intm_size;};
3、#endif#include#include"linkList.h"//双向循环链表LinkList::LinkList():m_size(0){m_head=newnode(NULL,NULL,NULL);m_head->m_next=m_head;m_head->m_pre=m_head;}LinkList::~LinkList(){if(m_size){node*p=m_head->m_next;node*q=p;for(inti=0;im_next->m_pre=q->m_pre
4、;q->m_pre->m_next=q->m_next;p=q;q=q->m_next;printf("%d..",p->m_element);deletep;}}else{printf("Thelistisempty!!!");}}voidLinkList::addFirst(inti){m_head->m_next=newnode(i,m_head,m_head->m_next);m_head->m_next->m_next->m_pre=m_head->m_next;++m_size;}voidLinkList::addLas
5、t(inti){m_head->m_pre=newnode(i,m_head->m_pre,m_head);m_head->m_pre->m_pre->m_next=m_head->m_pre;++m_size;}voidLinkList::addList(inti,intpos){if(pos<0
6、
7、pos>m_size){printf("posisoutofbound!!!");return;}node*p=m_head;if(pos<(m_size/2.0)){for(inti=0;im_next
8、;}}else{for(inti=m_size;i>=pos;i--){p=p->m_pre;}}p->m_pre=newnode(i,p->m_pre,p);p->m_pre->m_pre->m_next=p->m_pre;++m_size;}intLinkList::removeFirst(){if(!m_size){printf("Thelistisempty!!!");return-1;}node*p=m_head->m_next;m_head->m_next=p->m_next;p->m_pre=m_head;intrc=
9、p->m_element;deletep;--m_size;returnrc;}intLinkList::removeLast(){if(!m_size){printf("Thelistisempty!!!");return-1;}node*p=m_head->m_pre;m_head->m_pre=p->m_next;p->m_next=m_head;intlast=p->m_element;deletep;--m_size;returnlast;}intLinkList::removeList(intpos){if(!m_siz
10、e){printf("Thelistisempty!!!");return-1;}node*p=m_head;if(pos<(m_size/2.0)){for(inti=0;i
此文档下载收益归作者所有