一个链表类 大成时应该做成模板类的

总结就是一句话:还没写完 

所以各位看官如果觉得写得不堪入目,我在这给您道歉了~~~

代码在codeblocks上调试通过

已定义的函数都能正常使用。

目前还没发现别的什么bug,如果有发现,还望指正~~~

/************START****************/

  1 #include <iostream>

  2 #include <cstdlib>

  3 #define badswap(a,b) {(a)+=(b);(b)=(a)-(b);(a)=(a)-(b);}

  4 #define swap(a,b) if(&a==&b);else{(a)+=(b);(b)=(a)-(b);(a)=(a)-(b);}

  5 //The bug of it is if a and b is the same one value,it will be zero

  6 //the uper define has risk to overflow

  7 //but I think it OK now

  8 /*

  9 Bug correct

 10 At the CreatList function,I think it's better for me to use

 11 new form to deal with list creating

 12 */

 13 using namespace std;

 14 

 15 typedef struct Item

 16 {

 17     int num;

 18     Item *next;

 19     //wait to be added in new elements

 20 }item;

 21 class List

 22 {

 23     private:

 24             item *head,*last,*pointer;

 25             int maxsize;

 26             int presize;

 27             int prelocation;//present location

 28             //

 29     public:

 30             List(int max=10);

 31             List(const List &);

 32             bool CreatList();

 33             bool insert(const item &it,int position);//two arguments is needed

 34             item* search(item)const;//no implementation

 35             bool sort();//finished

 36             void show()const;

 37             ~List();

 38             //friend istream &operator>>(istream & is,List )

 39             //It seems to be a little difficult to do this,so canceled temporary

 40 };

 41 List::List(int max)

 42 {

 43     head=last=pointer=NULL;

 44     maxsize=max;

 45     presize=0;

 46     prelocation=0;

 47 }

 48 List::List(const List &Another_L)//There is some problem in it~~~

 49 {

 50     cout<<"working~"<<endl;

 51     if(Another_L.head==NULL)

 52         abort();

 53     item *current,*pre,*Ahead=Another_L.head;

 54     head=NULL;

 55     while(Ahead!=NULL)

 56     {

 57         if(head==NULL)

 58         {

 59             head=new item;

 60             if(head==NULL)

 61                 abort();

 62             head->num=Ahead->num;

 63             pre=head;

 64             Ahead=Ahead->next;

 65             presize++;

 66             continue;

 67         }

 68         current=new item;

 69         if(current==NULL)

 70             abort();

 71         //checked that it's OK

 72         current->next=NULL;//next initial

 73         pre->next=current;//lingking~~~

 74         pre=current;//save present pointer

 75         //I think I am right to do so when there is no pointer in Item

 76         *current=*Ahead;

 77         //~~~~~~~~~~~~~~

 78         Ahead=Ahead->next;

 79         //finished

 80     }

 81     cout<<"Copy constructor is finished\n";

 82 }

 83 bool List::CreatList()

 84 {

 85     item *current,*pre;

 86     int inttemp;

 87     cout<<"This List support int Only now~~\n";

 88     head=new item;

 89     if(head==NULL)

 90         return false;

 91     pre=head;

 92     pre->next=NULL;

 93     cin>>pre->num;

 94     if(cin.fail())

 95     {

 96         delete head;

 97         head=NULL;

 98         cout<<"Wrong input\n";

 99         return false;

100     }

101     //cout<<maxsize<<endl;

102     presize++;//number counter

103     while(presize<maxsize)

104     {

105         cin>>inttemp;

106         if(cin.fail())

107             break;

108         current=new item;

109         if(current==NULL)

110             return false;

111         current->num=inttemp;

112         pointer=pre;//signal

113         pre->next=current;

114         pre=current;

115         current->next=NULL;

116         presize++;

117         //cin>>current->num;

118     }

119     return true;

120 }

121 bool List::insert(const item &it,int position)//insert function

122 {

123     item *temp=head,*pre;

124     if(position>presize)

125     {

126         cout<<"there is no position to add"<<endl;

127         return false;

128     }

129     for(int i=1;i<position;i++)

130     {

131         pre=temp;

132         temp=temp->next;

133     }

134     pre->next=new item;

135     if(pre->next==NULL)

136     {

137         pre->next=temp;

138         return false;

139     }

140     *(pre->next)=it;

141     pre->next->next=temp;

142     return true;

143 }

144 void List::show()const

145 {

146     item *temp;

147     temp=head;

148     cout<<"Present size of the List: "<<presize<<endl;

149     while(temp!=NULL)

150     {

151         cout<<temp->num<<ends;

152         temp=temp->next;

153     }

154     cout<<endl;

155 }

156 bool List::sort()

157 {

158     item *pointer=head;//initial

159     item *swappointer,*waittoswap;

160     if(pointer==NULL)

161         return false;

162     if(presize==1)

163     {

164         cout<<"there is olny one valid value in the list\n";

165         return true;

166     }

167     //temp data

168     int max;//used to sign the max number in the list

169     //save the present infomation

170     waittoswap=pointer;

171     max=pointer->num;

172     swappointer=pointer;

173     //used for swap

174     //the uper part has no problem;

175     //this part can find the maxnumber in the list

176     while(waittoswap->next!=NULL)

177     {

178         max=pointer->num;

179         while(pointer->next!=NULL)

180         {

181             if(max<pointer->next->num)

182             {

183                 max=pointer->next->num;

184                 swappointer=pointer->next;

185             }

186             pointer=pointer->next;//tempswap and pointer must be the same position at the beginning

187         }

188         //show();

189         cout<<max<<endl;

190         swap(waittoswap->num,swappointer->num);

191         waittoswap=waittoswap->next;

192         //this is used to initial the present pointer

193         //it's very important

194         pointer=waittoswap;

195         swappointer=waittoswap;

196     }

197     return true;

198     //

199 }

200 List::~List()

201 {

202     item *temp;

203     temp=head;

204     while(temp!=NULL)

205     {

206         //cout<<temp->num<<ends;

207         head=temp->next;

208         delete temp;

209         temp=head;

210     }

211     //abort();

212     cout<<"\ndestructor is finished\n";

213 }

214 int main()

215 {

216     cout << "Hello world!" << endl;

217     List temp;

218     temp.CreatList();

219     temp.sort();

220     temp.show();

221     return 0;

222 }

223     //insert test psrt

224     //item asd={99,NULL};

225     //temp.CreatList();

226     //temp.show();

227     //temp.insert(asd,10);

228     //temp.show();

229     /*int a=0xFFFFFFFF,b=12345;

230     swap(a,b);

231     cout<<(int*)a<<ends<<b<<ends;

232     */

233     /*cin>>a;

234     if(cin.fail())

235     cout<<"can't read\n";

236     else

237     cout<<"can read\n";*/

238     //int a;

239     /*

240         insidec=count;

241         while((insidec--)>1)

242             {

243                 if(swaptemp>pointer->next->num)

244                 {

245                     swaptemp=pointer->next->num;

246                 }

247                 pointer=pointer->next;

248                 cout<<pointer->next->num<<ends;

249             }

250         swap(swaptemp,swappointer->num);

251         pointer=head;

252         swappointer=pointer;

253         */

254 //#define swap1(a,b) {(a)+=(b);(b)=(a)-(b);(a)=(a)-(b);}

新添加了sort函数,排除了CreatList中的Bug。

另外新发现了一个这里的宏定义Bug

 

你可能感兴趣的:(链表)