[C++练习]待Debug的单向链表

来源:互联网 发布:威科法律数据库 编辑:程序博客网 时间:2024/05/04 12:06
/*内存泄露*/ 
#include<iostream>using namespace std;class item{public:friend class List;item(){data=0; pre=0; }item(int x){data=x;pre=0;}private:int data;item *pre;};class List{public:List(){len=0;ed=0;}~List(){item *temp=ed->pre;while (temp){delete[] ed;ed=temp;temp=ed->pre;}}void Print(){item *temp=ed;while (ed){cout<<ed->data<<"  ";ed=ed->pre;}cout<<endl;}int Length(){return len;}bool IsEmpty(){if (len) return false;return true;}void Add(int x){item *temp= new item(x);temp->pre=ed;ed=temp;len++;}void Del(){if ( !len ){cout<<"There is nothing to delete!"<<endl;return;}item *temp=ed->pre;delete[] ed;ed=temp;}private:int len;item *ed;};int main(){List list;int n;while (1){cin>>n;if (n==-1) break;list.Add(n);}cout<<list.Length()<<endl;list.Print();return 0;}


 

0 0
原创粉丝点击