用c++做数据结构的作业题:线性表与栈


线性表的链式实现


class Node {
public:
    int data;
    Node *next;

};

class listlk{
public:
    Node *head;



    listlk() {
        head = new Node;
        head->data = 0;
        head->next = 0;

    }
    bool isempty() {
        return head->next == 0;
    }
    bool Delete(int k) {                
        if (isempty())return false;
        else {
            Node *ptr = head;
            for (int i = 0; i < k; i++)
            {
                ptr = ptr->next;
            }
            Node *temp = ptr->next;
            ptr->next = ptr->next->next;
            delete temp;
            return true;
        }
    }
    int insert(int k, int e) {
        if (k == 0)return false;
        else {
            Node temp;
            temp.data = e;
            Node *ptr = head;
            for (int i = 0; i < k; i++)
            {
                ptr = ptr->next;
            }
            ptr->next = temp.next;
            temp.next = ptr;
            return true;
        }
    }
    int length() {
        int i = 0;
        Node *ptr = head;
        while (ptr != 0) {
            i++;
        }
        return i;
    }

    int find(int k) {
        Node *ptr = head;
        for (int i = 0; i < k; i++)
        {
            ptr = ptr->next;
        }
        int e = ptr->data;
        return e;
    }

    ~listlk() {
        int i = 0;
        while (!isempty())
        {
            Delete(i);
            i++;
        }
    }

};

线性表的顺序实现

class list_se
{
public:
    list_se() {
        length = 0;
        ele = new int[inisize];
    }
    bool isempty() {
        return length == 0;
    }
    //寻找第K个数
    int find(int k) {
        int x;
        x = ele[k];
        return x;

    }
    bool Delete(int k) {
        if (length == 0)return false;
        else {
            for (int i = k; i < length; i++)

                ele[i - 1] = ele[i];
            length--;
            return true;
        }

    }
    int Delete_num(int k) {
        int x = ele[k];
        Delete(k);
        return x;
    }
    bool insert(int k, int e) {
        if (length >= inisize)return false;
        else {
            for (int i = length; i >=k; i--)
            {
                ele[i + 1] = ele[i];
                ele[k] = e;

            }
            length++;
            return true;
        }
    }


    ~list_se() {
        delete[]ele;
    }

private:

    int length;
    int inisize = 30;
    int *ele;
};

栈的顺序实现

class stackse {
public:
    int *base, *top;
    int size;//栈的现在的最大容量
    int length;
    //构造函数
    stackse(int inisize = 30) {
        base = new int[inisize];
        top = base;
        size = inisize;
        length = 0;
    }
    //入栈
    bool push(int e) {
        if (length < size) {
            top = &e;
            top++;
            length++;
            return true;
        }
        else
        {
            return false;
        }
    }
    //出栈不返回值;
    bool pop() {
        if (top == base)
            return false;
        else
        {
            top--;
            return true;
        }
    }
    //出栈返回值
    int popE(int e) {
        e = *top;
        pop();
        return e;

    }
    //栈的长度
    int length(){
        return length;
    }
    //析构
    ~stackse()
    {
        clear();
        delete base;

    }
    //置空
    bool clear(){
        if (top==base)
        {
            return false;

        }
        else {
            while (top != base)
            {
                pop();
            }
            return  true;
        }
    }
    //空否
    bool isempty() {
        return top == base;
    }
    //栈顶
    int gettop() {
        if (!isempty())
        {
            int e = *(top - 1);
            return e;
        }
        else
            return -100;
    }
};

栈的链式实现

class Node {
public:
    int data;
    Node *next;
};
class stacklk
{
public:
    Node *top;
    stacklk() {
        top = new Node;
        top->data = -88;
        top->next = 0;

    }

    bool push(int e) {

        Node *temp;
        if (temp = new Node) {
            temp->data = e;
            temp->next = top->next;
            top = temp;
            return true;
        }
        else {
            return false;
        }

    }

    bool isempty() { return top->next == 0; }
    bool pop() {
        if (!isempty())
        {
            Node *tem;
            tem = top;
            top = top->next;
            delete tem;

        }
        else { return false; }
    }

    bool popE(int e) {
        e = top->data;
        pop();
        return e;

    }


    bool clear() {
        if (top->next == 0)
        {
            return false;

        }
        else {
            while (top->next == 0)
            {
                pop();
            }
            return  true;
        }
    }

    int length() {
        if (isempty())
            return 0;
        else {
            Node *ptr;
            ptr = top;
            int i = 0;
            while (ptr->next != 0)
                i++;
            return i;
        }

    }

    ~stacklk() {
        clear();
        delete top;
    }



};

你可能感兴趣的:(用c++做数据结构的作业题:线性表与栈)