single_linked_list_int

来源:互联网 发布:网络下单系统 编辑:程序博客网 时间:2024/06/06 03:09

单向存整数的链表,代码如下:

//****************************Test_main.cpp****************************#include "single_linked_list_int.h"#include <iostream>using namespace std;void test_single_linked_list_int() {    SLLI_list l1; //for a test for empty list    cout << l1.num_in_list(9) << endl;    cout << l1.is_in_list(9) << endl;    cout << l1.is_empty() << endl;    cout << l1.node_num() << endl;    cout << l1.list_num() << endl;    cout << endl;        SLLI_list l2; //for a test for list containing only one int    cout << l2.num_in_list(9) << endl;    cout << l2.is_in_list(9) << endl;    cout << l2.is_empty() << endl;    cout << l2.node_num() << endl;    cout << l2.list_num() << endl;    l2.add_to_head(9);    cout << l2.data_in_head() << ' ' << l2.data_in_tail() << endl;    l2.delete_from_head();    cout << l2.data_in_head() << ' ' << l2.data_in_tail() << endl;    l2.add_to_tail(9);    cout << l2.data_in_head() << ' ' << l2.data_in_tail() << endl;    l2.delete_from_tail();    cout << l2.data_in_head() << ' ' << l2.data_in_tail() << endl;    l2.add_to_tail(9);    cout << l2.num_in_list(9) << endl;    cout << l2.is_in_list(9) << endl;    cout << l2.is_empty() << endl;    cout << l2.node_num() << endl;    cout << endl;        SLLI_list l3; //for a test for list containing more than one int    cout << l3.num_in_list(9) << endl;    cout << l3.is_in_list(9) << endl;    cout << l3.is_empty() << endl;    cout << l3.node_num() << endl;    cout << l3.list_num() << endl;    l3.add_to_head(9);    l3.add_to_tail(1);    l3.add_to_tail(2);    l3.add_nodes(9, 9, 9);    l3.add_nodes(1, 1, 9);    l3.delete_from_head();    l3.delete_from_tail();    cout << l3 << endl;    cout << l3.delete_node(4) << endl;    cout << l3 << endl;    cout << l3.delete_node(9) << endl;    cout << l3 << endl;    cout << l3.delete_node(9) << endl;    cout << l3 << endl;    cout << l3.delete_nodes(1) << endl;    cout << l3 << endl;    cout << l3.num_in_list(9) << endl;    cout << l3.is_in_list(9) << endl;    cout << l3.is_empty() << endl;    cout << l3.node_num() << endl;    cout << l3.list_num() << endl;    cout << l3.data_in_head() << " " << l3.data_in_tail() << endl;    cout << endl;        //test for destructor and clear function    l3.clear();    cout << l3 << endl;    cout << l3.node_num() << endl;    cout << l3.list_num() << endl;        SLLI_list *pointer_3 = &l3;    delete pointer_3;    cout << l2.list_num() << endl;}int main() {    test_single_linked_list_int();    return 0;}//************************single_linked_list_int.h*********************//single linked list to store integer#ifndef _SINGLE_LINKED_LIST_INT_H#define _SINGLE_LINKED_LIST_INT_H#include <iostream>using std::ostream;class intNode { //a node for the list  public:    int data;    intNode *next;    intNode(int in_data, intNode *in_next = 0) {        data = in_data;        next = in_next;    }};class SLLI_list { //the list  public:    SLLI_list(); //constructor    ~SLLI_list(); //destructor    void clear(); //empty the list    void add_to_head(int);    void add_to_tail(int);    bool add_nodes(int target, int pos, int num); //find the first int(==pos) and add targets of quantity of num    void delete_from_head();    void delete_from_tail();    int data_in_head() const;    int data_in_tail() const;    bool delete_node(int); //find the first target and delete it, return weather this operation is successful    int delete_nodes(int); //find all the targets and delete them, return the number of the deleted number    bool is_in_list(int) const; //weather the target is in the list    int num_in_list(int) const; //return the number of apparence of the target in the lis    bool is_empty() const;    int node_num() const; //the number of integer     int list_num() const; //the number of list        friend ostream& operator << (ostream &out, const SLLI_list &l); //output a list in the form "[ ]"(for empty list) or "[ a1 a2 a3 a4 ]"(for nonempty list)      private:    intNode *head, *tail; //the head node and the tail node    int nodes; //the number of integer    static int SLLI_list_num; //the number of list    static int error_sign; //sign integer for error time};#endif//***************************single_linked_list_int.cpp****************#include "single_linked_list_int.h"int SLLI_list::SLLI_list_num = 0;int SLLI_list::error_sign = 404040404;SLLI_list::SLLI_list() {    head = tail = 0;    SLLI_list_num++;    nodes = 0;}SLLI_list::~SLLI_list() {    SLLI_list_num--;    clear();}void SLLI_list::clear() {    intNode *temp = head;    while (head) {        temp = head;        head = head->next;        delete temp;    }    head = tail = 0;    nodes = 0;}void SLLI_list::add_to_head(int in_data) {    if (head == 0) {        head = tail = new intNode(in_data, 0);    } else {        intNode *temp = new intNode(in_data, head);        head = temp;    }    nodes++;}void SLLI_list::add_to_tail(int in_data) {    if (head == 0) {        head = tail = new intNode(in_data, 0);    } else {        intNode *temp = head;        while (temp->next != 0) {            temp = temp->next;        }        intNode *temp_2 = new intNode(in_data, 0);        tail->next = temp_2;        tail = temp_2;    }    nodes++;}bool SLLI_list::add_nodes(int target, int pos, int num) {    if (!is_in_list(pos)) {        return false;    } else {        nodes += num;        intNode *temp = head;        while (temp->data != pos) {            temp = temp->next;        }        if (temp == tail) {            for (int i = 0; i < num; i++) {                add_to_tail(target);            }        } else {            intNode *next_node = temp->next;            for (int i = 0; i < num; i++) { //add from back                intNode *temp_2 = new intNode(target, next_node);                next_node = temp_2;            }            temp->next = next_node;        }    }    return true;}void SLLI_list::delete_from_head() {    if (head == 0) {        return;    } else {        if (head == tail) {            delete head;            head = tail = 0;        } else {            intNode *temp = head;            head = head->next;            delete temp;        }    }    nodes--;}void SLLI_list::delete_from_tail() {    if (head == 0) {        return;    } else if (head == tail) {        clear();    } else {        intNode *temp = head->next;        intNode *temp_prev = head;        while (temp->next != 0) {            temp = temp->next;            temp_prev = temp_prev->next;        }        delete temp;        temp_prev->next = 0;        tail = temp_prev;    }    nodes--;}int SLLI_list::data_in_head() const {    if (head == 0) {        return error_sign;    }    return head->data;}int SLLI_list::data_in_tail() const {    if (head == 0) {        return error_sign;    }    return tail->data;}bool SLLI_list::delete_node(int target) {    if (head == 0) { //empty list        return false;    } else if (head == tail) { //only one node        if (head->data == target) {            delete_from_head();            return true;        } else {            return false;        }    } else {        if (head->data == target) { //check the head            delete_from_head();            return true;        }        intNode *temp = head->next;        intNode *temp_prev = head;        while (temp->next != 0) { //notice that the tail node won't be checked            if (temp->data == target) {                temp_prev->next = temp->next;                nodes--;                delete temp;                return true;            } else {                temp = temp->next;                temp_prev = temp_prev->next;            }        }        if (temp->data == target) { //check the tail            nodes--;            delete_from_tail();            return true;        }    }    return false; //not found}int SLLI_list::delete_nodes(int target) { //if call the delete_node function, it will take a lot of time    int deleted_num = 0;    if (head == 0) { //empty list        return 0;    } else if (head == tail) { //only one node        if (head->data == target) {            delete_from_head();            return 1;        } else {            return 0;        }    } else {        while (head != 0 && head->data == target) { //check the head, there may be a lot of same head to be deleted            delete_from_head();            deleted_num++;        }        if (head == 0) { //nodes are all deleted            return deleted_num;        }        intNode *temp = head->next;        intNode *temp_prev = head;        while (temp->next != 0) { //notice that the tail node won't be checked            if (temp->data == target) {                temp_prev->next = temp->next;                delete temp;                nodes--;                temp = temp_prev->next;                deleted_num++;                continue;            }            temp = temp->next;            temp_prev = temp_prev->next;        }        if (temp->data == target) { //check the tail            delete_from_tail();            deleted_num++;        }    }    return deleted_num;}bool SLLI_list::is_in_list(int target) const {    if (head == 0) {        return false;    }    intNode *temp = head;    while (temp != 0) {        if (temp->data == target) {            return true;        }        temp = temp->next;    }    return false;}int SLLI_list::num_in_list(int target) const {    if (head == 0) {        return 0;    }    int counter = 0;    intNode *temp = head;    while (temp != 0) {        if (temp->data == target) {            counter++;        }        temp = temp->next;    }    return counter;}bool SLLI_list::is_empty() const {    return nodes == 0;}int SLLI_list::node_num() const {    return nodes;}int SLLI_list::list_num() const {    return SLLI_list_num;}ostream& operator << (ostream &out, const SLLI_list &l) {    if (l.is_empty()) {        out << "[ ]";        return out;    } else {        out << "[ ";        intNode *temp = l.head;        while (temp != 0) {            out << temp->data << " ";            temp = temp->next;        }        out << "]";    }    return out;}

0 0
原创粉丝点击