My Data Sructure Templates&Class

二叉树模板   完成于:2012,11,11 光棍节

首先说明,这个模板的通用性比较差。
目前测试阶段仅仅适用于char类型的数据。
限制就在于出入的无效判断上。
我先去看会书,然后再回来好好解决一下。

 1 #include <iostream>
 2 using namespace std;
 3 template<class T>
 4 class BTree
 5 {
 6     private:
 7             class Node
 8             {
 9                 public:
10                         T element;//Can be optimized
11                         Node *LTree;
12                         Node *RTree;
13             };
14             Node *root;
15             bool creat(Node *&root);
16             void show(Node *root)const;//Read Only
17             bool B_Delete(Node *&root);
18     public:
19             BTree();
20             ~BTree();
21             void BTShow();
22             bool BTCreat();
23             void BTDelete();
24 };
25 template<class T>
26 BTree<T>::BTree()
27 {
28     root=NULL;
29 }
30 template<class T>
31 BTree<T>::~BTree()
32 {
33     B_Delete(root);
34 }
35 template<class T>
36 bool BTree<T>::BTCreat()
37 {
38     if(creat(root));
39         return true;
40     return false;
41 }
42 template<class T>
43 bool BTree<T>::creat(Node *&root)
44 {
45     T tempaval;
46     cin>>tempaval;
47     if(tempaval!='#')
48     {
49         root=new Node;
50         root->element=tempaval;
51         if(!creat(root->LTree))
52             root->LTree=NULL;
53         if(!creat(root->RTree))
54             root->RTree=NULL;
55     }
56     else
57         return false;
58     return true;
59 }
60 template<class T>
61 bool BTree<T>::B_Delete(Node *&root)
62 {
63     if(root)
64     {
65         B_Delete(root->LTree);
66         B_Delete(root->RTree);
67         cout<<root->element<<ends;
68         delete root;
69         return true;
70     }
71     else
72     return false;
73 }
74 template<class T>
75 void BTree<T>::show(Node *root)const
76 {
77     if(root)
78     {
79         cout<<root->element<<ends;
80         show(root->LTree);
81         show(root->RTree);
82     }
83     else
84         return;
85 }
86 template<class T>
87 void BTree<T>::BTShow()
88 {
89     show(root);
90 }
91 int main()
92 {
93     BTree<char> temp1;
94     temp1.BTCreat();
95     temp1.BTShow();
96     cout << "Hello world!" << endl;
97     return 0;
98 }

简单的栈模板  完成于:2012,11,13

  1 这个栈模板呢,我也没太多想说的了。
  2  这个栈的是否是空判断有两个地方,一个是deep变量,另外一个就是当前top的front指针的值是不是空的。
  3 别的,就只剩下了一个top变量了……
  4  #include <iostream>
  5 using namespace std;
  6 template<class T>
  7 class Stack
  8 {
  9     private:
 10             class Node
 11             {
 12                 public:
 13                 T member;
 14                 Node *front;
 15                 Node *next;
 16             };
 17             Node *bottom;
 18             Node *top;
 19             int deep;
 20     public:
 21             Stack();
 22             ~Stack();
 23             bool Push(T &valT);
 24             bool Pop(T &valT);
 25             bool Isempty();
 26             void show();
 27             T &GetTop();
 28 };
 29 template<class T>
 30 Stack<T>::Stack()
 31 {
 32     bottom=NULL;
 33     top=NULL;
 34     deep=0;
 35 }
 36 template<class T>
 37 Stack<T>::~Stack()
 38 {
 39     T temp;
 40     while(Pop(temp));
 41 }
 42 template<class T>
 43 bool Stack<T>::Push(T &valT)
 44 {
 45     Node *New;
 46     New=new Node;
 47     if(!New)
 48         return false;
 49     deep++;
 50     New->member=valT;
 51     New->next=NULL;
 52     if(top==NULL)
 53     {
 54         top=New;
 55         top->front=NULL;
 56     }
 57     else
 58     {
 59         New->front=top;
 60         top->next=New;
 61         top=New;
 62     }
 63     return true;
 64 }
 65 template<class T>
 66 bool Stack<T>::Pop(T &valT)
 67 {
 68     Node *valtemp;
 69     if(Isempty())
 70         return false;
 71     deep--;
 72     valT=top->member;
 73     valtemp=top->front;
 74     delete top;
 75     if(valtemp==NULL)
 76         top=NULL;
 77     else
 78     {
 79         valtemp->next=NULL;
 80         top=valtemp;
 81     }
 82     return true;
 83 }
 84 template<class T>
 85 bool Stack<T>::Isempty()
 86 {
 87     if(top==NULL)
 88         return true;
 89     else
 90         return false;
 91 }
 92 template<class T>
 93 T &Stack<T>::GetTop()
 94 {
 95     if(!Isempty())
 96         return top->member;
 97 }
 98 template<class T>
 99 void Stack<T>::show()
100 {
101     Node *current=top;
102     if(top==NULL)
103         return;
104     while(current!=NULL)
105     {
106         cout<<current->member;
107         current=current->front;
108     }
109     cout<<"\nDeep:"<<deep<<endl;
110 }
111 int main()
112 {
113     cout << "Hello world!" << endl;
114     int temp;
115     Stack<int> temp1;
116     for(int i=0;i<5;i++)
117         temp1.Push(i);
118     temp1.show();
119     for(int i=0;i<5;i++)
120     {
121         cout<<temp1.GetTop();
122         temp1.Pop(temp);
123     }
124     return 0;
125 }

队列模板   完成于2012,11,10

  1 这个是随手写的,刚才测试了一下,没发现什么问题,就顺手贴上来啦。
  2 如果想添加一个自己的类,一定要设置好输入和输出的操作符重载啊。
  3 #include <iostream>
  4 using namespace std;
  5 template<class T>
  6 class Queue
  7 {
  8     private:
  9             T *queuehead;
 10             int maxsize;
 11             int front;
 12             int rear;
 13     public:
 14             Queue(int max=10);
 15             ~Queue();
 16             bool Isempty();
 17             bool Isfull();
 18             bool addin(T & add);
 19             bool pop(T & out);
 20             void show();
 21 };
 22 template<class T>
 23 Queue<T>::Queue(int max)
 24 {
 25     if(max>0)
 26         maxsize=max;
 27     else
 28         maxsize=10;
 29     queuehead=new T [max];
 30     if(queuehead==NULL)
 31         abort();
 32     front=0;
 33     rear=0;
 34 }
 35 template<class T>
 36 Queue<T>::~Queue()
 37 {
 38     delete [] queuehead;
 39 }
 40 template<class T>
 41 bool Queue<T>::Isempty()
 42 {
 43     if(front==rear)
 44         return true;
 45     else
 46         return false;
 47 }
 48 template<class T>
 49 bool Queue<T>::Isfull()
 50 {
 51     if((rear+1)%maxsize==front)
 52         return true;
 53     else
 54         return false;
 55 }
 56 template<class T>
 57 bool Queue<T>::addin(T & add)
 58 {
 59     if(!Isfull())
 60     {
 61         queuehead[rear%=maxsize]=add;
 62         rear++;
 63         return true;
 64     }
 65     else
 66         return false;
 67 }
 68 template<class T>
 69 bool Queue<T>::pop(T & out)
 70 {
 71     if(!Isempty())
 72     {
 73         out=queuehead[front%=maxsize];
 74         front++;
 75         return true;
 76     }
 77     else
 78     return false;
 79 }
 80 template<class T>
 81 void Queue<T>::show()
 82 {
 83     cout<<"Data in line:\n";
 84     for(int i=0;i<maxsize;i++)
 85         cout<<queuehead[i]<<ends;
 86     cout<<endl;
 87     cout<<"rear:"<<rear<<endl;
 88     cout<<"front:"<<front<<endl;
 89 }
 90 int main()
 91 {
 92     cout << "Hello world!" << endl;
 93     int a;
 94     Queue<int> temp1;
 95     cin>>a;
 96     while(!cin.fail())
 97     {
 98         temp1.addin(a);
 99         cin>>a;
100     }
101     while(!temp1.Isempty())
102     {
103         temp1.pop(a);
104         cout<<a<<ends;
105     }
106     cout<<endl;
107     temp1.show();
108     return 0;
109 }

简单的选择排序类  完成于2012,11,12

 1 这个代码,我也实在是没什么想说的了……
 2  #include <iostream>
 3 #include <cstdlib>
 4 using namespace std;
 5 class SelectSort
 6 {
 7     private:
 8             int *Array;
 9             int maxsize;
10             int preposition;
11             int maxval;
12             int maxposition;
13             void initial();
14     public:
15             SelectSort(int *array=NULL,int size=0);
16             ~SelectSort();
17             bool Sort(int *array,int size);
18             void show();
19 };
20 SelectSort::SelectSort(int *array,int size)
21 {
22     if(Array==NULL||size<=0)
23     {
24         Array=NULL;
25         maxsize=0;
26     }
27     else
28         Sort(array,size);
29 }
30 SelectSort::~SelectSort()
31 {
32     ;
33 }
34 void SelectSort::initial()
35 {
36     preposition=0;
37     maxposition=0;
38 }
39 bool SelectSort::Sort(int *array,int size)
40 {
41     initial();
42     Array=array;
43     if(!Array||size==0)
44         return false;
45     maxsize=size;
46     for(int i=0;i<maxsize;i++)
47     {
48         maxval=Array[i];
49         for(int j=i+1;j<maxsize;j++)
50         {
51             if(maxval<Array[j])
52             {
53                 maxval=Array[j];
54                 maxposition=j;
55             }
56         }
57         //Do Exchange
58         Array[maxposition]=Array[i];
59         Array[i]=maxval;
60         //Used to initial the wait to compare position
61         maxposition=i+1;
62     }
63     return true;
64 }
65 void SelectSort::show()
66 {
67     cout<<"\nArray Information\n";
68     if(Array)
69         for(int i=0;i<maxsize;i++)
70         {
71             cout<<Array[i]<<ends;
72         }
73     else
74         cout<<"The Array Is Invaled\n";
75     cout<<"\nInside Variable:\n"\
76         <<"Array Address:"<<Array<<endl
77         <<"Maxsize:"<<maxsize<<endl
78         <<"Maxval:"<<maxval<<endl
79         <<"Maxposition"<<maxposition<<endl;
80 }
81 int main()
82 {
83     cout << "Hello world!" << endl;
84     int a[]={4,5,2,6,8,1,2,5};
85     cout<<endl;
86     SelectSort temp2;//(a,sizeof(a)/sizeof(int));
87     temp2.Sort(a,sizeof(a)/sizeof(int));
88     temp2.show();
89     SelectSort temp1(a,sizeof(a)/sizeof(int));
90     temp1.show();
91     return 0;
92 }

插入排序类(非模板)  完成于2012,11,13

 1 今天早上计算方法,老师又不点名,一开心就睡到了8点半,然后洗个头,随便吃个饭,就开机写代码了~~~
 2 期间还水了一会睿思,看了会dota视频,有点汗颜啊。
 3 不过最后算法还是实现了,10点8分左右。
 4 这个算法需要注意的就是要对进行交换的值预先存储,初期我就会忘了这点,导致调了半天……
 5 代码:
 6  #include <iostream>
 7 
 8 using namespace std;
 9 class InsertSort
10 {
11     private:
12             int *Array;
13             int maxsize;
14     public:
15             InsertSort(int *a=NULL,int size=0);
16             void movf(int position,int last);
17             bool Sort(int *a,int size);
18 };
19 int main()
20 {
21     cout << "Hello world!" << endl;
22     int a[5]={7,3,9,5,6};
23     int b=5;
24     InsertSort temp1(a,b);
25     temp1.Sort(a,b);
26     for(int i=0;i<5;i++)
27         cout<<a[i]<<ends;
28     return 0;
29 }
30 InsertSort::InsertSort(int *a,int size)
31 {
32     Array=a;
33     maxsize=size;
34 }
35 void InsertSort::movf(int position,int last)
36 {
37     for(int i=last;i>position;i--)
38         Array[i]=Array[i-1];
39 }
40 bool InsertSort::Sort(int *a,int size)
41 {
42     int valtemp;
43     if(a!=NULL||size>0)
44     {
45          Array=a;
46          maxsize=size;
47     }
48     else
49         return false;
50     for(int i=1;i<maxsize;i++)
51     {
52         for(int j=0;j<i;j++)//前部扫描
53         {
54             if(Array[i]>Array[j])
55                 continue;
56             else
57             {
58                 valtemp=Array[i];
59                 movf(j,i);
60                 Array[j]=valtemp;
61             }
62         }
63     }
64 
65 }

 线性队列类  完成时间:2012,10,16

  1 首先,这个代码存在缺陷。
  2 最后一位无法完全读取,每次满队列都会有一个空余元素,但是并不影响使用。
  3 codeblocks编译通过且运行正常。
  4 以前写的队列是用链表做的,这个用的数组,原因就是软基老师今天好像讲这个了,晚上就写了一下。
  5 最用用C++比较顺手,就直接贴出来了,上次我在空间贴C++代码已经是好久以前的事了~~~
  6 刚才看了好长时间的Jin书,搞得现在才开始贴代码,唉……
  7 #include <iostream>
  8 #include <cstdlib>
  9 #include <ctime>
 10 using namespace std;
 11 class LineQueue
 12 {
 13     private:
 14     int *queue;
 15     int front;
 16     int rear;
 17     int maxsize;
 18     void Linitial(){front=rear=0;}
 19     void ShowSize(){cout<<"The size of the Queue is"<<maxsize<<endl;}
 20     public:
 21     LineQueue(int ms)
 22     {
 23         maxsize=ms;
 24         queue=new int [maxsize];
 25         Linitial();
 26     }
 27     LineQueue()
 28     {
 29         maxsize=10;
 30         queue=new int [maxsize];
 31         Linitial();
 32 
 33     }
 34     virtual ~LineQueue()
 35     {
 36         delete [] queue;
 37     }
 38     bool InsertMem(const int & num);
 39     bool GetMem(int & num);
 40     bool Isempty();
 41     bool Isfull();
 42     void show();
 43 };
 44 //inplementation
 45 bool LineQueue::Isempty()
 46 {
 47     if(front==rear)
 48         return true;
 49     else
 50         return false;
 51 }
 52 bool LineQueue::Isfull()
 53 {
 54     if(front%maxsize==(rear+1)%maxsize)
 55         return true;
 56     else
 57         return false;
 58 }
 59 bool LineQueue::InsertMem(const int & num)
 60 {
 61     if(Isfull())
 62         return false;
 63     queue[(rear++)%maxsize]=num;
 64     return true;
 65 }
 66 bool LineQueue::GetMem(int &num)
 67 {
 68     if(Isempty())
 69         return false;
 70     num=queue[front%maxsize];
 71     queue[front++%maxsize]=0;
 72     return true;
 73 }
 74 void LineQueue::show()
 75 {
 76     cout<<"The present information of the Queue\n";
 77     cout<<"front is "<<front<<";  "<<"rear is "<<rear<<endl;
 78     for(int i=0;i<maxsize;i++)
 79     {
 80         cout<<queue[i]<<ends;
 81         if((i+1)%5==0)
 82         cout<<endl;
 83     }
 84     cout<<endl;
 85 }
 86 int main()
 87 {
 88     srand(time(NULL));
 89     cout << "Hello world!" << endl;
 90     LineQueue Q1;
 91     int temp;
 92     while(!Q1.Isfull())
 93     {
 94         Q1.InsertMem(rand()%10);
 95     }
 96     Q1.show();
 97     while(!Q1.Isempty())
 98     {
 99         Q1.GetMem(temp);
100     }
101     Q1.show();
102     while(!Q1.Isfull())
103     {
104         Q1.InsertMem(rand()%10);
105     }
106     //cout<<temp<<endl;
107     Q1.show();
108     return 0;
109 }
110 就是随手写的,如果觉得有错误,请指正。

一个链表类  完成于2012,10,21

  1 指针值得可能有点乱,还有不少定义了没用变量,有兴趣的就自己贴着调试一下吧
  2 另外注释有点太简单了,不过内容基本差不多,没什么技术含量。
  3 代码在code::blocks调试运行通过
  4 /********************START*********************/ 
  5  #include <iostream>
  6 #include <cstdlib>
  7 #define badswap(a,b) {(a)+=(b);(b)=(a)-(b);(a)=(a)-(b);}
  8 #define swap(a,b) if(&a==&b);else{(a)+=(b);(b)=(a)-(b);(a)=(a)-(b);}
  9   
 10 using namespace std;
 11 
 12 typedef struct Item
 13 {
 14     int num;
 15     Item *next;
 16     //wait to be added in new elements
 17 }item;
 18 class List
 19 {
 20     private:
 21             item *head,*last,*pointer;
 22             int maxsize;
 23             int presize;
 24             int prelocation;//present location
 25             //
 26     public:
 27             List(int max=10);
 28             List(const List &);
 29             bool CreatList();
 30             bool insert(const item &it,int position);//two arguments is needed
 31             item* search(/*???*/)const;//finished
 32             bool sort();//no implementation
 33             void show()const;
 34             ~List();
 35             //friend istream &operator>>(istream & is,List )
 36             //It seems to be a little difficult to do this,so canceled temporary
 37 };
 38 List::List(int max)
 39 {
 40     head=last=pointer=NULL;
 41     maxsize=max;
 42     presize=0;
 43     prelocation=0;
 44 }
 45 List::List(const List &Another_L)//There is some problem in it~~~
 46 {
 47     cout<<"working~"<<endl;
 48     if(Another_L.head==NULL)
 49         abort();
 50     item *current,*pre,*Ahead=Another_L.head;
 51     head=NULL;
 52     while(Ahead!=NULL)
 53     {
 54         if(head==NULL)
 55         {
 56             head=new item;
 57             if(head==NULL)
 58                 abort();
 59             head->num=Ahead->num;
 60             pre=head;
 61             Ahead=Ahead->next;
 62             presize++;
 63             continue;
 64         }
 65         current=new item;
 66         if(current==NULL)
 67             abort();
 68         //checked that it's OK
 69         current->next=NULL;//next initial
 70         pre->next=current;//lingking~~~
 71         pre=current;//save present pointer
 72         //I think I am right to do so when there is no pointer in Item
 73         *current=*Ahead;
 74         //~~~~~~~~~~~~~~
 75         Ahead=Ahead->next;
 76         //finished
 77     }
 78     cout<<"Copy constructor is finished\n";
 79 }
 80 bool List::CreatList()
 81 {
 82     item *current,*pre;
 83     cout<<"This List support int Only now~~\n";
 84     head=new item;
 85     if(head==NULL)
 86         return false;
 87     pre=head;
 88     pre->next=NULL;
 89     cin>>pre->num;
 90     presize++;
 91     //
 92     while(!cin.fail()&&presize<maxsize)
 93     {
 94         current=new item;
 95         if(current==NULL)
 96             return false;
 97         pre->next=current;
 98         pre=current;
 99         current->next=NULL;
100         presize++;
101         cin>>current->num;
102     }
103     if(presize==maxsize)
104         current->next=NULL;
105     else
106         {
107             current->num=-1;
108             current->next=NULL;
109         }
110     return true;
111 }
112 bool List::insert(const item &it,int position)//insert function
113 {
114     item *temp=head,*pre;
115     if(position>presize)
116     {
117         cout<<"there is no position to add"<<endl;
118         return false;
119     }
120     for(int i=1;i<position;i++)
121     {
122         pre=temp;
123         temp=temp->next;
124     }
125     pre->next=new item;
126     if(pre->next==NULL)
127     {
128         pre->next=temp;
129         return false;
130     }
131     *(pre->next)=it;
132     pre->next->next=temp;
133     return true;
134 }
135 void List::show()const
136 {
137     item *temp;
138     temp=head;
139     cout<<"Present size of the List: "<<presize<<endl;
140     while(temp!=NULL)
141     {
142         cout<<temp->num<<ends;
143         temp=temp->next;
144     }
145     cout<<endl;
146 }
147 bool List::sort()
148 {
149     item *pointer=head;//initial
150     item *swappointer,*waittoswap;
151     if(pointer==NULL)
152         return false;
153     if(presize==1)
154     {
155         cout<<"there is olny one valid value in the list\n";
156         return true;
157     }
158     //temp data
159     int max;//used to sign the max number in the list
160     //save the present infomation
161     waittoswap=pointer;
162     max=pointer->num;
163     swappointer=pointer;
164     //used for swap
165     //the uper part has no problem;
166     //this part can find the maxnumber in the list
167     while(waittoswap->next!=NULL)
168     {
169         max=pointer->num;
170         while(pointer->next!=NULL)
171         {
172             if(max<pointer->next->num)
173             {
174                 max=pointer->next->num;
175                 swappointer=pointer->next;
176             }
177             pointer=pointer->next;//tempswap and pointer must be the same position at the beginning
178         }
179         //show();
180         cout<<max<<endl;
181         swap(waittoswap->num,swappointer->num);
182         waittoswap=waittoswap->next;
183         //this is used to initial the present pointer
184         //it's very important
185         pointer=waittoswap;
186         swappointer=waittoswap;
187     }
188     return true;
189     //
190 } 
191 List::~List()
192 {
193     item *temp;
194     temp=head;
195     while(temp!=NULL)
196     {
197         //cout<<temp->num<<ends;
198         head=temp->next;
199         delete temp;
200         temp=head;
201     }
202     //abort();
203     cout<<"\ndestructor is finished\n";
204 }
205 int main()
206 {
207     cout << "Hello world!" << endl;
208     List temp;
209     item asd={99,NULL};
210     temp.CreatList();
211     temp.show();
212     temp.insert(asd,10);
213     temp.show();
214     return 0;
215 }
216 今天下午写的~见错莫怪请指正~~

 

 

posted @ 2013-03-29 09:17  Matrix_R  阅读(368)  评论(0编辑  收藏  举报