Skip to content

Commit

Permalink
feat:完成链表
Browse files Browse the repository at this point in the history
  • Loading branch information
Jalor authored and Jalor committed Sep 4, 2020
1 parent 53644c8 commit b8f1321
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 1 deletion.
Binary file not shown.
89 changes: 89 additions & 0 deletions ADT/LINKLIST.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,93 @@ void LinkList<DataType>::PrintList() {
cout<<endl;
}

/*输出链表长度*/
template <typename DataType>
int LinkList<DataType>::Length() {
Node<DataType> * p = first -> next;
int cnt = 0;
while (p) {
p = p -> next;
cnt ++;
}
return cnt;
}

/*按位查找*/
template <typename DataType>
DataType LinkList<DataType>::Get(int i){
Node<DataType> *p = first -> next;
int pos = 1;
while(p && pos < i){
p = p -> next;
pos ++;
}
if(!p) throw "illage index";
return p -> data;
}

/*按值查找*/
template <typename DataType>
int LinkList<DataType>::Locate(DataType x){
Node<DataType> * p = first -> next;
int pos = 1;
while (p) {
if(p -> data == x){
return pos;
}
p = p -> next;
pos ++;
}
return -1;
}

/*往第i个位置插入值x*/
template <typename DataType>
void LinkList<DataType>::Insert(int i, DataType x){
Node<DataType> *p = first -> next;
int pos = 1;

while (p && pos < i - 1) {
pos++;
p = p -> next;
}

Node<DataType> *newNode = new Node<DataType>;
newNode -> data = x;

newNode -> next = p -> next;
p -> next = newNode;
}

/*删除第i个位置的值x,并展示出来*/
template <typename DataType>
DataType LinkList<DataType>::Delete(int i){
DataType x;
Node<DataType> *p = first -> next;

int pos = 1;

while (p && pos < i - 1) {
p = p -> next;
pos ++;
}

Node<DataType> *del = p -> next;
p -> next = del -> next;
x = del -> data;
delete del;
return x;
}


/*判断是否为空 0 = 为空, 其他数字 = 不为空*/
template <typename DataType>
int LinkList<DataType>::Empty()
{
Node<DataType> *p = first -> next;
if(p){
return 1;
}
return 0;
}
#endif /* LINKLIST_h */
19 changes: 18 additions & 1 deletion ADT/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,26 @@ using namespace std;

int main(int argc, const char * argv[]) {
int a[] = {1,2,3,4,5,6,7,8,9};
LinkList<int>l = LinkList<int>(a,9);
LinkList<int>l;
cout<<"=========链表判断空=========="<<endl;
cout<<l.Empty()<<endl;

l = LinkList<int>(a,9);
//SeqList<int>s = SeqList<int>(a, 7);
//s.Insert(2, 10);
cout<<"=========获得第三个数据========="<<endl;
cout<<l.Get(3)<<endl;
cout<<"=========获得1数据的位置=========="<<endl;
cout<<l.Locate(1)<<endl;

l.Insert(9, 10);
cout<<"=========删除第4个数据=========="<<endl;
cout<<l.Delete(4)<<endl;
cout<<"=========链表的长度=========="<<endl;
cout<<l.Length()<<endl;
cout<<"=========链表判断空=========="<<endl;
cout<<l.Empty()<<endl;
cout<<"=========打印=========="<<endl;
l.PrintList();
cout<<"==================="<<endl;
//s.Delete(1);
Expand Down

0 comments on commit b8f1321

Please sign in to comment.