单向链表类模板和string类简易操作

单向链表类模板

/*1  设计一个单向链表类模板,节点数据域中数据从小到大排列,并设计插入、删除节点的成员函数。*/
#include <iostream>

using namespace std;

template < typename T>
class People
{
public:
    void setX(T x);
    T getX(void);
    friend ostream & operator<<<>(ostream & out, const People & x);
    friend istream & operator>><>(istream & in, People & x);
private:
    T m_x;
};

template < typename T>
struct  Node
{
    People<T> peo;
    struct Node<T>* next;
};

template < typename T>
void People<T>::setX(T x)
{
    m_x = x;
}

template < typename T>
istream& operator>><>(istream & in, People<T> & x)
{
    in >> x.m_x;
    return in;
}

template < typename T>
ostream & operator<<<>(ostream & out, const People<T> & x)
{
    out << x.m_x << " ";
    return out;
}

template < typename T>
T People<T>::getX(void)
{
    return m_x;
}

template < typename T>
Node<T>* creatNode(Node<T>* head)//Node<T>* head
{
    head = new struct Node<T>;
    head->peo.setX(0);
    //head->peo.setX(0);
    head->next = NULL;
    return head;
}

template < typename T>
void insertNode(const People<T> & man, Node <T>* head)
{
    Node <T>* p = head;
    while (p->next)
    {
        p = p->next;
    }
    Node <T>* newNode = new Node<T>;
    newNode->peo = man;
    p->next = newNode;
    newNode->next = NULL;
}

template<typename T>
void deleteNode(Node<T>* head,const T x)
{
    Node<T>* pR;
    Node<T>* pN;
    bool flag = false;
    pN = head->next;
    pR = head;
    while (pN)
    {
        if (pN->peo.getX() == x)
        {
            flag = true;
            pR->next = pN->next;
            delete  pN;
            pN = pR->next; 
            continue;           
        }
        pR = pN;
        pN = pN->next;
    }
    if (!flag)
    {
        cout << "no such num" << endl;
    }
}

template<typename T>
void sort(Node<T>* head)//冒泡排序
{
    Node<T>* pR = head;
    Node<T>* p = pR->next;
    Node<T>* pTail = NULL;
    while (head->next->next != pTail)
    {
        pR = head;
        p = head->next;
        while (p->next != pTail)
        {
            if (p->peo.getX() > p->next->peo.getX())
            {
                pR->next = p->next;
                p->next = p->next->next;
                pR->next->next = p;
                p = pR->next;
            }
            p = p->next;
            pR = pR->next;
        }
        pTail = p;
    }
}

template < typename T>
void displayNode(Node<T>* head)
{
    while (head->next)
    {
        head = head->next;//.的优先级比*高
        cout << head->peo;
    }
}

template < typename T>
void deleteSpace(Node<T>* head)
{
    Node<T>* p = head, *pR = NULL;
    while (p)
    {
        pR = p->next;
        delete p;
        p = pR;
    }
}

int main()
{
    Node <int> *head = NULL;

    head = creatNode(head);

    People<int> man;
    cin >> man;
    while (man.getX() != -1)
    {
        insertNode(man, head);
        cin >> man;
    }

    sort(head);
    displayNode(head);

    cout << "\ninput which one you want to delete :";
    int x;
    cin >> x;
    deleteNode(head, x);

    displayNode(head);
    deleteSpace(head);//删除堆分配

    system("pause");
    return 0;
}

类String的原型

/*2 已知类String的原型为:
class String
{
public:
     String(const char *str = NULL);// 普通构造函数
     String(const String &other);    // 拷贝构造函数
     ~ String(void);    // 析构函数
     String & operate =(const String &other);// 赋值函数
private:
     char *m_data;// 用于保存字符串
}; 
请编写String的上述4个函数。*/

#include <iostream>

using namespace std;

class String
{
public:
    String(const char *str = NULL);
    String(const String &other);    
    ~String(void);
    String & operator =  (const String &other);
    friend ostream & operator<<(ostream & out,const String & s);//只写一个参数也可以,因为写在类中其第二个参数默认为this,如果不在类中,比如friend就得加全参数了
    String & operator+(const String &s);
private:
    char *m_data;// 用于保存字符串
};

String::String(const char *str)
{
    m_data = new char[strlen(str) + 1];//因为sizeof一个指针永远只会是4,因为它只是个指针变量而不是里面的字符串数组的大小
    int i = 0;
    while (str[i])
    {
        m_data[i] = str[i];
        i++;
    }
    m_data[i] = 0;
}

String::String(const String &other)
{
    m_data = new char[strlen(other.m_data) + 1];//因为sizeof一个char型指针永远只会是4,因为它只是个指针变量而不是里面的字符串数组的大小
    int i = 0;
    while (other.m_data[i])
    {
        m_data[i] = other.m_data[i];
        i++;
    }
    m_data[i] = 0;
}

String & String::operator= (const String &other)//赋值运算符重载
{
    int i = 0;
    while (other.m_data[i])
    {
        this->m_data[i] = other.m_data[i];//这边一个一个赋
        i++;
    }
    this->m_data[i] = 0;
    return *this;
}

ostream & operator<<(ostream & out, const String & s)//友元函数输出流
{
    int i = 0;
    while (s.m_data[i])
    {
        out << s.m_data[i];
        i++;
    }
    return out;
}

String & String::operator+(const String &s)//+运算符重载
{
    int len = strlen(s.m_data) + 1 + strlen(this->m_data) + 1;
    int i = 0;
    char* spy1 = new char[strlen(s.m_data) + 1];//这边两个spy实现+号左右的string
    while (s.m_data[i])
    {
        spy1[i] = s.m_data[i];
        i++;
    }
    spy1[i] = 0;

    int j = 0;
    char* spy2 = new char[strlen(this->m_data) + 1];//第二个在此
    while (this->m_data[j])
    {
        spy2[j] = this->m_data[j];
        j++;
    }
    spy2[j] = 0;

    this->m_data = new char[len];
    int k = 0;
    while (spy1[k])//此处实现两个string链接既形似加
    {
        this->m_data[k] = spy1[k];
        k++;
    }
    int t = 0;
    while (spy2[t])
    {
        this->m_data[k] = spy2[t];
        t++;
        k++;
    }
    this->m_data[k] = 0;

    delete[] spy1;
    delete[] spy2;

    return *this;
}

String::~String(void)//析构
{
    delete[] m_data;
}

int main()
{
    String s1("zhang");
    cout << s1<<endl;

    String s2("chen");
    cout << s2 << endl;

    String s3(s1 = s2);
    cout << s3 << endl;

    String s5("wang");
    String s6(s1 + s5);

    cout << s6 << endl;

    system("pause");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/kuimzzs/article/details/79982307