数据布局练手01 线性表实现   
               添加时间:2013-5-22 点击量: 
 
              C++的常识点很零散繁琐,只是总结没啥感化,更多的应当是在实践中找题目。今天我去编写《数据布局,算法与应用 C++说话描述》中线性表的实现代码,感触感染写代码才能陌生了很多多少。
编写过程中,我发了然几个题目,很值得存眷:
1、我的代码是经由过程类模板编写的。很多人写代码的时辰,都把声明和实现放在一个头文件里,primer中说要借用export才干实现分别编译,对于这点,我也不是很懂得,然则我取巧了下,经由过程两个 #ifndef....#endif 达到了声明放在.h, 实现放在了.cpp中。
2、模板类中关于<<的重载,须要在声明中参加 <> ,要不然编译不经由过程。
3、cout 的刷新题目。 如 fun(int& x), 若我们这么用  cout << fun(y) << y ;  屏幕上显示的不是y更新后的值,而是调用y前的值。而将它分成两个cout来输出,或者刷新后输出,那么将是正确的。
好的,贴代码:  实现景象 vs2010
// linearlist.h
#ifndef MY_LINEARLIST_H    // 在头文件中做保护头
#define MY_LINEARLIST_H
#include <ostream>
using namespace std;
template<class T>
class LinearList{
public:
    LinearList(int cap = 100);
    ~LinearList();
    bool isEmpty();
    int length();
    bool find(int pos, T& hold) const;
    int search(const T& x) const;
    LinearList<T>& drop(int pos, T& hold);
    LinearList<T>& (int pos, const T& x);
    void printt(ostream& os)const;
    friend ostream& operator << <>(ostream& os, const LinearList<T>& x);    // 题目二的失足,模板不好把握呀
private:
    int size;
    int capacity;
    T element;
};
#endif
#include LinearList.cpp           // 这里包含实现文件,这个技能其实在 《C++ template》中有显现过。欲深切懂得模板,此书必读!
// linearlist.cpp实现文件
#ifndef MY_LINEARLIST_CPP   //  这里又来个实现文件的保护头,如许就避免了多重定义,同时经由过程.h最后的#include实现了 实际上的头文件和实现文件在一个文件中。
#define MY_LINEARLIST_CPP
#include LinearList.h
#include <cstdlib>
#include <ostream>
template<class T>
LinearList<T>::LinearList(int cap)
{
    size = 0;
    capacity = cap;
    element = new T[capacity];
}
template<class T>
LinearList<T>::~LinearList(void)
{
    if (element != NULL) {
        [] element;
    }
}
template<class T>
bool LinearList<T>::isEmpty ()
{
    return size == 0;
}
template<class T>
int LinearList<T>::length()
{
    return size;
}
template<class T>
bool LinearList<T>::find(int pos,T& hold) const
{
    if (pos > size || pos < 1) return false;
    hold = element[pos-1];
    return true;
}
template<class T>
int LinearList<T>::search(const T& x) const
{
    for (int i=0; i<size; ++i) {
        if (element[i] = x) {
            return i+1;
        }
    }
    return 0;
}
template<class T>
LinearList<T>& LinearList<T>::drop(int pos, T& hold)
{
    if (pos > size || pos < 1) {
        exit(1);
    }
    hold = element[pos-1];
    for(int i=pos; i<size; ++i){
        element[i-1] = element[i];
    }
    --size;
    return this;
}
template<class T>
LinearList<T>& LinearList<T>::(int pos, const T& x)
{
    if(size == capacity){
        T newElement = new T[capacity2];
        capacity <<= 1;
        for (int i=1; i<pos; ++i) {
            newElement[i-1] = element[i-1];
        }
        newElement[pos] = x;
        for(int i=pos; i<size; ++i){
            newElement[i+1] = element[i];
        }
        ++size;
        [] element;
        element = newElement;
    }else{
        for(int i=size; i>=pos; --i){
            element[i] = element[i-1];
        }
        element[pos-1] = x;
        ++size;
    }
    return this;
}
template<class T>
void LinearList<T>::printt(ostream& out) const
{
    for (int i=0; i<size; ++i) {
        out << element[i] <<  ;
    }
}
template<class T>
ostream& operator<< <>(ostream& out, const LinearList<T>& x)   // 题目2
{
    x.printt(out); return out;
}
#endif
 1 int main()
 2 {
 3     LinearList<int> L(5);
 4 
 5     int y=0;
 6     L. (1,1);
 7         cout << L << endl;
 8     L. (2,2);
 9         cout << L << endl;
10     L. (3,3);
11         cout << L << endl;
12     cout << y before:  << y << endl;
13     cout << Find  << L.find(2,y) <<  << y << endl;   // 题目3
14     cout << y after  << y << endl;
15 
16 }

所有随风而逝的都属于昨天的,所有历经风雨留下来的才是面向未来的。—— 玛格丽特·米切尔 《飘》
                     
                  
     
  
 
    
    
C++的常识点很零散繁琐,只是总结没啥感化,更多的应当是在实践中找题目。今天我去编写《数据布局,算法与应用 C++说话描述》中线性表的实现代码,感触感染写代码才能陌生了很多多少。
编写过程中,我发了然几个题目,很值得存眷:
1、我的代码是经由过程类模板编写的。很多人写代码的时辰,都把声明和实现放在一个头文件里,primer中说要借用export才干实现分别编译,对于这点,我也不是很懂得,然则我取巧了下,经由过程两个 #ifndef....#endif 达到了声明放在.h, 实现放在了.cpp中。
2、模板类中关于<<的重载,须要在声明中参加 <> ,要不然编译不经由过程。
3、cout 的刷新题目。 如 fun(int& x), 若我们这么用 cout << fun(y) << y ; 屏幕上显示的不是y更新后的值,而是调用y前的值。而将它分成两个cout来输出,或者刷新后输出,那么将是正确的。
好的,贴代码: 实现景象 vs2010
// linearlist.h
#ifndef MY_LINEARLIST_H // 在头文件中做保护头
#define MY_LINEARLIST_H
#include <ostream>
using namespace std;
template<class T>
class LinearList{
public:
LinearList(int cap = 100);
~LinearList();
bool isEmpty();
int length();
bool find(int pos, T& hold) const;
int search(const T& x) const;
LinearList<T>& drop(int pos, T& hold);
LinearList<T>& (int pos, const T& x);
void printt(ostream& os)const;
friend ostream& operator << <>(ostream& os, const LinearList<T>& x); // 题目二的失足,模板不好把握呀
private:
int size;
int capacity;
T element;
};
#endif
#include LinearList.cpp // 这里包含实现文件,这个技能其实在 《C++ template》中有显现过。欲深切懂得模板,此书必读!
// linearlist.cpp实现文件
#ifndef MY_LINEARLIST_CPP // 这里又来个实现文件的保护头,如许就避免了多重定义,同时经由过程.h最后的#include实现了 实际上的头文件和实现文件在一个文件中。
#define MY_LINEARLIST_CPP
#include LinearList.h
#include <cstdlib>
#include <ostream>
template<class T>
LinearList<T>::LinearList(int cap)
{
size = 0;
capacity = cap;
element = new T[capacity];
}
template<class T>
LinearList<T>::~LinearList(void)
{
if (element != NULL) {
[] element;
}
}
template<class T>
bool LinearList<T>::isEmpty ()
{
return size == 0;
}
template<class T>
int LinearList<T>::length()
{
return size;
}
template<class T>
bool LinearList<T>::find(int pos,T& hold) const
{
if (pos > size || pos < 1) return false;
hold = element[pos-1];
return true;
}
template<class T>
int LinearList<T>::search(const T& x) const
{
for (int i=0; i<size; ++i) {
if (element[i] = x) {
return i+1;
}
}
return 0;
}
template<class T>
LinearList<T>& LinearList<T>::drop(int pos, T& hold)
{
if (pos > size || pos < 1) {
exit(1);
}
hold = element[pos-1];
for(int i=pos; i<size; ++i){
element[i-1] = element[i];
}
--size;
return this;
}
template<class T>
LinearList<T>& LinearList<T>::(int pos, const T& x)
{
if(size == capacity){
T newElement = new T[capacity2];
capacity <<= 1;
for (int i=1; i<pos; ++i) {
newElement[i-1] = element[i-1];
}
newElement[pos] = x;
for(int i=pos; i<size; ++i){
newElement[i+1] = element[i];
}
++size;
[] element;
element = newElement;
}else{
for(int i=size; i>=pos; --i){
element[i] = element[i-1];
}
element[pos-1] = x;
++size;
}
return this;
}
template<class T>
void LinearList<T>::printt(ostream& out) const
{
for (int i=0; i<size; ++i) {
out << element[i] << ;
}
}
template<class T>
ostream& operator<< <>(ostream& out, const LinearList<T>& x) // 题目2
{
x.printt(out); return out;
}
#endif
1 int main()
2 {
3 LinearList<int> L(5);
4
5 int y=0;
6 L. (1,1);
7 cout << L << endl;
8 L. (2,2);
9 cout << L << endl;
10 L. (3,3);
11 cout << L << endl;
12 cout << y before: << y << endl;
13 cout << Find << L.find(2,y) << << y << endl; // 题目3
14 cout << y after << y << endl;
15
16 }

所有随风而逝的都属于昨天的,所有历经风雨留下来的才是面向未来的。—— 玛格丽特·米切尔 《飘》




