1 / 22

Main Index

1. Main Index. Contents. Lecture 4 – Lists. List--Based on array 用数组实现列表 List—Based on vector  用动态数组实现列表 List—Based on linked list  用链表使用列表. 2. Main Index. Contents. List( 列表 ) 概念. 列表: 1. 列表中所有的元素都是同一种类型; 2. 列表具有有限的大小; 3. 其中的元素都是线性排列的: -存在一个首元素和一个末尾元素; -除了末尾元素,每个元素都有一个后继

lyle
Download Presentation

Main Index

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 1 Main Index Contents Lecture 4 – Lists • List--Based on array 用数组实现列表 • List—Based on vector 用动态数组实现列表 • List—Based on linked list 用链表使用列表

  2. 2 Main Index Contents List(列表)概念 列表: 1.列表中所有的元素都是同一种类型; 2.列表具有有限的大小; 3.其中的元素都是线性排列的: -存在一个首元素和一个末尾元素; -除了末尾元素,每个元素都有一个后继 元素;除了首元素,每个元素都有一个 前驱;

  3. 3 Main Index Contents List(列表)ADT 列表ADT: 数据元素集合: 数据元素的一个有限序列,所有元素都是同一类型的。 基本操作: -构造函数:创建空列表; -判空: 检查列表是否为空; -插入: 在列表中增加一项; -删除: 在列表中删除一项; -遍历: 按顺序访问列表,处理元素。 在STL中也称为迭代操作。

  4. 4 Main Index Contents List(列表)ADT 基于数组的列表 const int Capacity=1024; template <typename T> class arrayList { public: arrayList(); bool empty() const; int size() const; void insert(T item, int pos); void erase(int pos); friend ostream& operator<<(ostream& out, const arrayList & aList); private: int mySize; T myArray[Capacity]; };

  5. 5 Main Index Contents List(列表)ADT arrayList::arrayList(): mySize(0) {} bool arrayList::empty() const { return mySize == 0; } intarrayList::size() const { return mySize; }

  6. 6 Main Index Contents List(列表)ADT ostream & operator<< (ostream & out, const List & aList) { for (int i = 0; i < mySize; i++) out << myArray[i] << " "; return out; } ========================================== void List::display(ostream & out) const { for (int i = 0; i < mySize; i++) out << myArray[i] << " "; } ostream & operator<< (ostream & out, const List & aList) { aList.display(out); return out; }

  7. 7 Main Index Contents List(列表)ADT void List::insert(T item, int pos) { if (mySize == Capacity) { cerr << "*** No space for list element -- terminating " "execution ***\n"; exit(1); } if (pos < 0 || pos > mySize) { cerr << "*** Illegal location to insert -- " << pos << ". List unchanged. ***\n"; return; } for(int i = mySize; i > =pos; i--) myArray[i] = myArray[i - 1]; myArray[pos] = item; mySize++; }

  8. 8 Main Index Contents List(列表)ADT void arrayList::erase(int pos) { if (mySize == 0) { cerr << "*** List is empty ***\n"; return; } if (pos < 0 || pos >= mySize) { cerr << "Illegal location to delete -- " << pos << ". List unchanged. ***\n"; return; } for(int i = pos-1; i < mySize-1; i++) myArray[i] = myArray[i+1]; mySize--; }

  9. 9 Main Index Contents List(列表)ADT 基于动态数组的列表 template <typename T> class miniVectorList { public: miniVectorList(); bool empty() const; int size() const; void insert(T item, int pos); void erase(int pos); friend ostream& operator<<(ostream& out, const miniVectorList & aList); private: miniVector<T> miniVectorList; };

  10. lecture 2 – Dynamic array and Vector Container class miniVector { public: miniVector(int size = 0); miniVector(const miniVector& obj); ~miniVector(); miniVector& operator= (const miniVector& rhs); int& back(); const int& back() const; int& operator[] (int i); const int& operator[] (int i) const;

  11. lecture 2 – Dynamic array and Vector Container void push_back(const int& item); void pop_back(); int size() const; bool empty() const; int capacity() const; private: int vCapacity; int vSize; int *vArr; void reserve(int n, bool copy); };

  12. 12 Main Index Contents List(列表)ADT template <typename T> miniVectorList<T>::miniVectorList() {} ================================================== miniVector::miniVector(int size): vSize(0), vCapacity(0), vArr(NULL) { int i; if (size == 0) return; reserve(size, false); vSize = size; for (i=0;i < vSize;i++) vArr[i] = 0; }

  13. 13 Main Index Contents List(列表)ADT bool miniVectorList::empty() const { return miniVectorList.size() == 0; } intminiVectorList::size() const { return miniVector.size(); } ostream & operator<< (ostream & out, const miniVectorList & aList) { for (int i = 0; i < aList.size(); i++) out << miniVectorList[i] << " "; return out; }

  14. 14 Main Index Contents List(列表)ADT bool miniVectorList::empty() const { return miniVectorList.size() == 0; } intminiVectorList::size() const { return miniVector.size(); } ostream & operator<< (ostream & out, const miniVectorList & aList) { for (int i = 0; i < mySize; i++) out << miniVectorList[i] << " "; return out; }

  15. 15 Main Index Contents List(列表)ADT void miniVectorList::erase(int pos) { if (miniVectorList.size() == 0) { cerr << "*** List is empty ***\n"; return; } if (pos < 0 || pos >= miniVectorList.size()) { cerr << "Illegal location to delete -- " << pos << ". List unchanged. ***\n"; return; } miniVectorList.erase(pos); } void List::insert(T item, int pos) { miniVectorList.insert(item, pos); }

  16. 16 Main Index Contents List(列表)ADT 基于链表的列表 template <typename T> class node { public: T nodeValue; // data held by the node node<T> *next; // next node in the list node() : next(NULL) {} node(const T& item, node<T> *nextNode = NULL) : nodeValue(item), next(nextNode) {} };

  17. 17 Main Index Contents List(列表)ADT 基于链表的列表 template <typename T> class linkedList { public: linkedList(); bool empty() const; int size() const; void push_back(const T & item); void pop_back(); void insert(T item, int pos); //思考 void erase(int pos); //思考 friend ostream& operator<<(ostream& out, const linkedList & aList); …… private: node<T> *header; int lsize; };

  18. 18 Main Index Contents List(列表)ADT template <typename T> linkedList<T>::linkedList(node<T> *header =NULL, 0): header(NULL) ,lsize(0) {}

  19. 19 Main Index Contents List(列表)ADT template <typename T> void linkedList<T>::push_back(const T& item) { node<T> *newNode = new node<T> (item); node<T> *tmp=header; if (newNode == NULL) throw memoryAllocationError( "linkedQueue: memory allocation failure"); if (header == NULL) { header = newNode; } else { while(tmp->next!=NULL) tmp=tmp->next; tmp->next=newNode; } lsize++; }

  20. 20 Main Index Contents List(列表)ADT template <typename T> void linkedList<T>::pop_back() { node<T> *tmp=header,tmpptr; if (header == NULL) { exit(0); } else { if(tmp->next==NULL){ tmpptr=header; delete tmpptr; header=NULL; }//只有一个结点 while(tmp->next->next!=NULL) tmp=tmp->next; tmpptr=header->next; delete tmpptr; header->next=NULL; } } lsize--; }

  21. 21 Main Index Contents List(列表)ADT template <typename T>//需将友元函数声明和实现写在一起 friend ostream& operator<<(ostream& out, const node<T> &* header) { node<T> *temp; temp=header; while(temp!=NULL) { out<<temp->dataValue; temp=temp->next; } return out; } 思考:在指定位置插入和删除: void insert(T item, int pos); void erase(int pos);

  22. 实验2: 1.合并两个有序链表 2.P279-7

More Related