南南思密达加 南南思密达加
关注数: 9 粉丝数: 20 发帖数: 102 关注贴吧数: 11
简单的单链表 编译显示无错误 为什么运行的时候不能插入数据 #include<iostream> using namespace std; class Node { private: int date; Node*next; friend class list;//class listÊÇclass oneµÄÓÑÔªÀà¿ÉÒÔ·ÃÎÊclass one ÖеÄprivateÊý¾Ý }; class list { private: Node*head; public: list(int len = 0); //~list(){delet head}; bool empty();//boolÀàÐÍÐèÒªÓÃtrueºÍfalseдÊÇ·ñΪ¿Õ int length();//¼ÆË㳤¶È Node* find(int pos);//¸ù¾ÝµØÖ·²éÕÒ void insert(int pos,int val);//²åÈëÊý¾Ý void display(); }; list::list(int len)//²åÈëÊý¾Ý { head = new Node; Node*p = head; head->next = NULL;//³õʼ»¯ for(int i = 0;i<len;i++) { Node*q = new Node; q->date = i; q->next = p->next; p->next = q; } } bool list::empty() { if(head->next == NULL) return true; else return false; } int list::length() { Node*p=head->next; int i = 1; while(p!=NULL) { i++; p=p->next; return i; } } Node*list::find(int pos) { Node*p = head; int i; while(p!=NULL&&i<pos) { i++; p=p->next; } if(p==NULL||i>pos) { cout<<"can not find"<<endl; } else { return p; } } void list::insert(int pos,int val) { Node*p = find(pos); Node*q = new Node; q->date = val; q->next = p->next; p->next = q; } void list::display() { Node*p=head->next; while(p!=NULL) { cout<<p->date<<" "; p=p->next;//Ï൱ÓÚp++ } } void main() { list t(5); t.display(); cout<<t.empty()<<endl; list g; cout<<g.length()<<endl; list f; cout<<f.find(2)<<endl; t.insert(2,7);//ÔÚ2µÄºóÃæ²åÈëÒ»¸ö7 t.display(); }
1 下一页