C++ 封装链表类、模板封装链栈、链式队列

1、链表类

#include <iostream>
using namespace std;

struct node
{
    int data;
    struct node* next;
};


class linklist
{
private:
    
    node* head;
public:
    linklist();
    linklist(const linklist &obj);
    ~linklist();
    //判空,求长,增删改查排,倒置,打印
    int linklist_is_empty();
    int get_length_linklist();
    void insert_by_pos_linklist(int pos,int data);
    int delete_by_pos_linklist(int pos);
    int delete_by_data_linklist(int data);
    node* search_by_pos_linklist(int pos);
    node* search_by_data_linklist(int data);
    node* sort_linklist();
    node* reverse_linklist(); 
    void print_linklist();

};

int main()
{
    linklist list;
    // linklist list1(list);
    linklist list1 = list;
    cout<<list1.linklist_is_empty()<<endl;
    cout<<list.linklist_is_empty()<<endl;
    cout<<list.get_length_linklist()<<endl;
    for (int i = 1; i <= 10; i++)
    {
        list.insert_by_pos_linklist(i,i);
    }
    cout<<list.get_length_linklist()<<endl;
    list.print_linklist();
    
    list.insert_by_pos_linklist(5,99);
    list.print_linklist();
    
    list.delete_by_pos_linklist(5);
    list.print_linklist();
    
    list.delete_by_data_linklist(10);
    list.print_linklist();
    
    list.reverse_linklist();
    list.print_linklist();

    cout<<list.search_by_pos_linklist(3)->next->data<<endl;
    cout<<list.search_by_data_linklist(7)->next->data<<endl;
    
    
    return 0;
}

linklist::linklist()//构造函数,在堆区申请空间,初始化头节点
{   
    head = new (nothrow) node;
    head->next = nullptr;
}

linklist::linklist(const linklist &obj)
{
    this->head = new node;
}

linklist::~linklist()//析构函数,清理堆区空间,需要逐个清除
{
    node* p = nullptr;
    while (head != nullptr)
    {
        p = head;
        head = head->next;
        delete p;       
    }
}

int linklist::linklist_is_empty()
{
    return (head->next == nullptr)?1:0;
}

int linklist::get_length_linklist()
{
    node* p = head;
    int count = 0;
    while(p->next != nullptr)
    {
        count++;
        p = p->next;
    }
    return count;
}

void linklist::insert_by_pos_linklist(int pos,int data)
{
    // if(pos < 0 || pos >= get_length_linklist())
    // {
    //     cout<<"pos error"<<endl;
    //     return ;
    // }
    node* p = head;
    while(--pos)
    {
        p = p->next;
    }
    node* new_node = new (nothrow) node;
    if(new_node == nullptr)
    {
        return ;
    }
    new_node->next = p->next;
    new_node->data = data;
    p->next = new_node;
}

int linklist::delete_by_pos_linklist(int pos)
{
    if(linklist_is_empty() == 1)
    {
        return -1;
    }
    if(pos < 0 || pos > get_length_linklist())
    {
        cout<<"pos error"<<endl;
        return -1;
    }
    node* p = head;
    while(--pos)
    {
        p = p->next;
    }
    node* q = p->next;
    p->next = q->next;
    delete q;
    return 0;
}

int linklist::delete_by_data_linklist(int data)
{
    node* p = head;
    while (p->next->data != data)
    {
        p = p->next;
    }
    node* q = p->next;
    p->next = q->next;
    delete q;
    return 0;
}

node* linklist::search_by_pos_linklist(int pos)
{
    if(linklist_is_empty() == 1)
    {
        return nullptr;
    }
    if(pos < 0 || pos > get_length_linklist())
    {
        cout<<"pos error"<<endl;
        return nullptr;
    }
    node* p = head;
    while(pos--)
    {
        p = p->next;
    }
    return p;
}

node* linklist::search_by_data_linklist(int data)
{
    node* p = head->next;
    while(p != nullptr && p->data != data)
    {
        p = p->next;
    }
    return p;
}
node* linklist::sort_linklist()
{
    node* p = head->next;
    head->next = nullptr;
    node *q,*r;
    while (p != nullptr)
    {
        q = p->next;
        r = head;
        while (r != nullptr && r->next->data < p->data)
        {
            r = r->next;
        }     
        p->next = r->next;
        r->next = p->next;
        p = q;    
    }
    return head;
}

node* linklist::reverse_linklist()
{
    node* p = head->next;
    node* q = nullptr;
    head->next = nullptr;
    while(p != nullptr)
    {
        q = p->next;
        p->next = head->next;
        head->next = p;
        p = q;
    }
    return head;
}


void linklist::print_linklist()
{
    node* p = head->next;
    while (p != nullptr)    
    {
        cout<<p->data<<" ";
        p = p->next;
    }
    cout<<endl;  
}

 2、模板链式栈

#include <iostream>
using namespace std;

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


template<typename T>
class Linkstack{
    private:
        Node<T>* top;

    public:
        Linkstack();
        ~Linkstack();

    public:
    bool linkstack_is_empty();
    int get_length_linkstack();
    void linkstack_push(T data);
    T linkstack_pop();
};

int main()
{
    Linkstack<char> stack;
    cout << stack.linkstack_is_empty() << endl;
    for (int i = 0; i < 10; i++)
    {
        stack.linkstack_push('A' + i);
    }
    cout << stack.get_length_linkstack() << endl;

    for (int  i = 0; i < 10; i++)
    {
        cout << stack.linkstack_pop() << " ";
    }
    cout << endl;
    cout << stack.linkstack_is_empty() << endl;
    return 0;
}


template<typename T>
bool Linkstack<T>::linkstack_is_empty()
{
    // cout << __func__ <<":"<< __LINE__ << endl; 
    return ((top->next == nullptr)?true:false);
}

template<typename T>
int Linkstack<T>::get_length_linkstack()
{
    // cout << __func__ <<":"<< __LINE__ << endl; 
    int count = 0;
    Node<T>* p = top->next;
    while(p != nullptr)
    {
        count++;
        p = p->next;
    }
    return count;
}


template<typename T>
void Linkstack<T>::linkstack_push(T data)
{
    // cout << __func__ <<":"<< __LINE__ << endl; 
    Node<T>* newptr = new Node<T>;
    newptr->data = data;
    newptr->next = top->next;
    top->next = newptr;
}

template<typename T>
T Linkstack<T>::linkstack_pop()
{
    // cout << __func__ <<":"<< __LINE__ << endl; 
    T temp;
    Node<T>* p = top->next;
    // if(p == nullptr)
    // {
    //     return 0;
    // }
    top->next = p->next;
    temp = p->data;
    delete p;
    return temp;
}

template<typename T>
Linkstack<T>::Linkstack()
{
    // cout << __func__ <<":"<< __LINE__ << endl; 
    top = new Node<T>;
    top->next = nullptr;
}

template<typename T>
Linkstack<T>::~Linkstack()
{
    // cout << __func__ <<":"<< __LINE__ << endl; 
    while (this->linkstack_is_empty() != 1)
    {
        this->linkstack_pop();
    }  
    delete top;
}

 3、模板链式队列

#include <iostream>
using namespace std;


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

// template<typename T>
// struct Queue
// {
//     Node<T>* front;
//     Node<T>* rear;
// };


template<typename T>
class Linkqueue{
    private:
        Node<T>* head;
        Node<T>* front;
        Node<T>* rear;
    public:
        Linkqueue();
        ~Linkqueue();

    public:
        int linkqueue_is_empty();
        int get_length_linkqueue();
        void print_linkqueue();
        void linkqueue_en(T data);
        T linkqueue_de();
};


int main()
{
    Linkqueue<char> lqueue;

    cout << lqueue.linkqueue_is_empty() << endl;
    lqueue.get_length_linkqueue();
    for (int i = 0; i < 10; i++)
    {
        lqueue.linkqueue_en('A'+i);
    }
    // lqueue.print_linkqueue();
    cout << lqueue.get_length_linkqueue() << endl;
    for (int i = 0; i < 10; i++)
    {
        cout << lqueue.linkqueue_de() <<" ";
    }
    cout << endl;
    cout << lqueue.get_length_linkqueue() << endl;

    return 0;
}


template<typename T>
int Linkqueue<T>::linkqueue_is_empty()
{
    // cout << __func__ <<":"<< __LINE__ << endl; 
    return front == rear;
}

template<typename T>
int Linkqueue<T>::get_length_linkqueue()
{
    cout << __func__ <<":"<< __LINE__ << endl;
    Node<T>* p = front;
    int count = 0;
    while(p->next != nullptr)
    {
        count++;
        p = p->next;
    }
    return count;
}


template<typename T>
void Linkqueue<T>::print_linkqueue()
{
    cout << __func__ <<":"<< __LINE__ << endl;
    Node<T>* p = front;
    while (p->next != nullptr)
    {
        cout << p->next->data <<" ";
    }
    cout << endl;
    
}


template<typename T>
void Linkqueue<T>::linkqueue_en(T data)
{
    cout << __func__ <<":"<< __LINE__ << endl;
    Node<T>* newnode = new Node<T>;
    newnode->data = data;
    newnode->next = NULL;
    rear->next = newnode;
    rear = newnode;
    return;
}

template<typename T>
T Linkqueue<T>::linkqueue_de()
{
    // cout << __func__ <<":"<< __LINE__ << endl;
    if(this->linkqueue_is_empty() == 1)
    {
        return 0;
    }
    T temp = front->next->data;
    Node<T>* p = front;
    front = p->next;
    delete p;
    return temp;
}


template<typename T>
Linkqueue<T>::Linkqueue()
{
    cout << __func__ <<":"<< __LINE__ << endl;
    Node<T>* head = new Node<T>;
    head->next = nullptr;
    front = head;
    rear = head;
}

template<typename T>
Linkqueue<T>::~Linkqueue()
{
    cout << __func__ <<":"<< __LINE__ << endl;
    // while(this->linkqueue_is_empty() != 1)
    // {
    //     this->linkqueue_de();
    // }
    // delete head;
    // delete front;
    // delete rear;
}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值