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 3, 2020
1 parent 896e9fa commit 53644c8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
Binary file not shown.
26 changes: 26 additions & 0 deletions ADT/LINKLIST.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,30 @@ LinkList<DataType>::LinkList() {
first -> next = nullptr;
}

/*建立长度为n的c链表*/
template <typename DataType>
LinkList<DataType>::LinkList(DataType a[],int n){
first = new Node<DataType>;
Node<DataType> *ptr = nullptr;//指针
for (int i = 0; i < n; i ++) {
Node<DataType> *node = new Node<DataType>;//新对象
if(i == 0) first -> next = node;
else ptr -> next = node;//上一个对象的下一个对象指向这个
node -> data = a[i];
node -> next = nullptr;
ptr = node;
}
}

/*打印链表*/
template <typename DataType>
void LinkList<DataType>::PrintList() {
Node<DataType> * p = first -> next;
while (p) {
cout<<p -> data <<"\t";
p = p -> next;
}
cout<<endl;
}

#endif /* LINKLIST_h */
14 changes: 7 additions & 7 deletions ADT/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
using namespace std;

int main(int argc, const char * argv[]) {
int a[] = {1,2,3,4,5,6,7};
LinkList<int>l = LinkList<int>();
SeqList<int>s = SeqList<int>(a, 7);
s.Insert(2, 10);
s.PrintList();
int a[] = {1,2,3,4,5,6,7,8,9};
LinkList<int>l = LinkList<int>(a,9);
//SeqList<int>s = SeqList<int>(a, 7);
//s.Insert(2, 10);
l.PrintList();
cout<<"==================="<<endl;
s.Delete(1);
s.PrintList();
//s.Delete(1);
//s.PrintList();
return 0;
}

0 comments on commit 53644c8

Please sign in to comment.