《单链表按位查找的C程序》由会员分享,可在线阅读,更多相关《单链表按位查找的C程序(4页珍藏版)》请在金锄头文库上搜索。
1、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()h
2、ead=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;
3、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 *
4、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;