码迷,mamicode.com
首页 > 其他好文 > 详细

List

时间:2014-12-03 22:55:13      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   ar   color   os   sp   

实在是受不了了,作业被我不小心格式化了,所以把作业存到了博客上,哎

VirtualListClass

bubuko.com,布布扣
 1 #ifndef VIRTUALLISTCLASS
 2 #define VIRTUALLISTCLASS
 3 template <typename T> class VirtualListClass  { // VirtualListClass ADT
 4 private:
 5     void operator = (const VirtualListClass&) {}
 6     // Protect assignment
 7     VirtualListClass(const VirtualListClass&) {}
 8     // Protect copy constructor
 9 public:
10     VirtualListClass() {}
11     // Default constructor
12 
13     virtual ~VirtualListClass() {} // Base destructor
14 
15     // Clear contents from the VirtualListClass, to make it empty.
16     virtual void clear() = 0;
17 
18     // put an element at the total location.
19     // item: The element to be inserted
20     virtual void push_back(const T& item) = 0;
21 
22     // Remove and return the element at pos.
23     // pos is the position
24     // Return: the element that was removed.
25     virtual T remove(int pos) = 0;
26 
27     // insert a element at some position
28     // The position to be insert
29     virtual void insert(const T& item,int pos) = 0;
30 
31     // Return: The number of elements in the VirtualListClass.
32     virtual int length() const = 0;
33 
34     // Return a element.
35     // pos is the position of the element.
36     virtual const T& getValue(int pos) const = 0;
37 };
38 
39 #endif
View Code

ListClass

bubuko.com,布布扣
 1 #include "VirtualListClass.h"
 2 
 3 #ifndef LISTCLASS
 4 #define LISTCLASS
 5 
 6 const int MAXSIZE = 0xfffffe;
 7 
 8 template <typename T> // Array-based list implementation
 9 class ListClass : public VirtualListClass<T> {
10 private:
11     int maxSize;
12     // Maximum size of list
13 
14     int listSizeNow;
15     // Number of list items now
16 
17     int curr;
18     // Position of current element
19 
20     T* listArray;
21     // Array holding list elements
22 public:
23     ListClass(int size = MAXSIZE) { // Constructor
24         maxSize = size;
25         listSizeNow = curr = 0;
26         listArray = new T[maxSize];
27     }
28     ~ListClass() { delete [] listArray; } // Destructor
29 
30     void clear() {
31         delete [] listArray;
32         listSizeNow = curr = 0;
33         listArray = new T[maxSize];
34     }
35 
36     void push_back(const T& item) {
37         listArray[curr++] = item;
38         listSizeNow++;
39         // Increase list size
40     }
41 
42     // Remove and return the current element.
43     T remove(int pos) {
44         T it = listArray[pos];
45         // Copy the element
46         for(int i=pos; i<listSizeNow-1; i++) // Shift them down
47         listArray[i] = listArray[i+1];
48         listSizeNow--;
49         curr--;
50         // Decrease list size
51         return it;
52     }
53 
54     void insert(const T& item,int pos) {
55         for (int i = listSizeNow;i > pos;i--) {
56             listArray[i] = listArray[i - 1];
57         }
58         listArray[pos] = item;
59         curr ++;
60         listSizeNow++;
61         // Increase list size
62     }
63 
64     int length() const { return listSizeNow; }
65 
66     const T& getValue(int pos) const { // Return current element
67         return listArray[pos];
68     }
69 
70     T operator [] (const int pos) const {
71         return listArray[pos];
72     }
73 
74 };
75 
76 #endif
View Code

 

List

标签:des   style   blog   http   io   ar   color   os   sp   

原文地址:http://www.cnblogs.com/xiaoshanshan/p/4141365.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!