12.设计一个整型链表类list,能够实现链表节点的插入(insert)、删除(delete),以及链表数据的输出操作(print)
#include<iostream>
using namespace std;
class intLinkList{
private:
int *list;
int size;
int length;
public:
intLinkList(int s){
list = new int[s];
size=s;
length = 0;
}
int insert(int location, int data){
if(location<1 || location>length+1 || length>=size)
return 0; // 插入失败
for(int i=length; i>location; i--){
list[i]=list[i-1];
}
list[location-1]=data;
length++;
return 1; // 插入成功
}
int delet(int location){
if(location<1 || location>length || length<=0)
return 0;
for(int i=location; i<length; i++){
list[i-1]=i;
}
length--;
return 1;
}
int getElem(int location){
if(location<1 || location >length)
return 0;
return list[location-1];
}
};
int main(){
intLinkList list1(100);
list1.insert(1,1);
list1.delet(1);
cout<<list1.getElem(1);
}原文:http://javavwv.blog.51cto.com/8832079/1704087