资源预览内容
第1页 / 共4页
第2页 / 共4页
第3页 / 共4页
第4页 / 共4页
亲,该文档总共4页全部预览完了,如果喜欢就下载吧!
资源描述
C+课程实训 建立线性单链表 要有初始化、插入、删除、查找、排序等常用功能的 C+程序,必须要有封装性!#include using namespace std; template class list;template class nodefriend list;private:T data;node* next;public:node():data(0),next(NULL);node()T getData()return data;node* getNext()return next;template class listprivate:node *head;public:list()head=new node;list();int insert(T&);int remove(T&);node* find(T&);void print();void sort();template int list:insert(T& x)node *p=new node;p-data=x; if(!p) return 0;p-next=head-next;head-next=p;return 1;templateint list:remove(T& x)node* p=find(x);if(!p) return 0;node *q=p-next;p-next=q-next;delete q;return 1;templatenode *list:find(T& x)node*q=head;while(q-next!=NULL)if(q-next-data=x)return q;q=q-next;return NULL;templatevoid list:sort()node* p=head-next,*q;for(;p!=NULL;p=p-next)for(q=p-next;q!=NULL;q=q-next)if(p-dataq-data)T temp=q-data;q-data=p-data;p-data=temp;templatevoid list:print()node *q=head-next;while(q!=NULL)coutdata;q=q-next;coutlist:list()node*p=head-next;while(p!=NULL)head-next=p-next;delete p;p=head-next;delete head;int main() list l;int x;do cinx;/input 0 exitif(x=0) break;l.insert(x); while (1);l.sort();l.print();cinx;node* p=l.find(x)-getNext();coutgetData()endl;l.remove(x);l.print();return 0;
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号