(C++)利用双向链表基本实现从邻接矩阵到邻接链表的转换

#include <iostream>
#include <vector>

using namespace std;

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

class Linkedlist
{
public:

    Linkedlist(): head(NULL), last(NULL){}

    bool isEmpty() { return isEmpty(head); };
    void insertEnd(int data) { return insertEnd(head,last, data); }
    void insertFirst(int data) { return insertFirst(head, last, data); };
    void removeFirst() { removeFirst(head, last); };
    void removeEnd() { removeEnd(head, last); };
    void Preshow() { Preshow(head); };
    void Aftershow() { Aftershow(last); }
    node* Get_head() { return head; }
    
    //输入数组创建链表
    void createListfromArr(int arr[], int n) {
        for (int i = 0; i < n; i++) {
            insertEnd(head, last, arr[i]);
        }
        return;
    }


private:
    node* head;
    node* last;

    //判断是否为空
    bool isEmpty(node* head) {
        if (head == NULL)
            return true;
        else
            return false;
    }

    void insertAsFistElement(node*& head, node*& last, int data) {
        node* tmp = new node;
        tmp->data = data;
        tmp->next = NULL;
        tmp->parent = NULL;
        head = tmp;
        last = tmp;

    }
    //在末尾添加数据

    void insertEnd(node*& head, node*& last, int data) {
        if (isEmpty(head))
            insertAsFistElement(head, last, data);
        else {
            node* tmp = new node;
            tmp->data = data;
            tmp->next = NULL;
            last->next = tmp;
            tmp->parent = last;
            last = tmp;
        }
    }

    //在开始添加数据
    void insertFirst(node*& head, node*& last, int data) {
        if (isEmpty(head))
            insertAsFistElement(head, last, data);
        else {
            node* tmp = new node;
            tmp->data = data;
            tmp->parent = NULL;
            head->parent = tmp;
            tmp->next = head;
            head = tmp;
        }
    }


    //删除最后一个数据。
    void removeEnd(node*& head, node*& last) {
        if (isEmpty(head))
            cout << "Empty link list!" << endl;
        else if (head == last) {
            delete head;
            head = NULL;
            last = NULL;
        }
        else {
            node* tmp = last;
            last = last->parent;
            last->next = NULL;
            delete tmp;
        }

    }

    //删除第一个数据。
    void removeFirst(node*& head, node*& last) {
        if (isEmpty(head))
            cout << "Empty link list!" << endl;
        else if (head == last) {
            delete head;
            head = NULL;
            last = NULL;
        }
        else {
            node* tmp = head;
            head = head->next;
            head->parent = NULL;
            delete tmp;
        }

    }

    //从头开始遍历
    void Preshow(node* current) {
        if (isEmpty(current))
            cout << "the list is empty!" << endl;
        else {
            while (current != NULL) {
                cout << current->data << "->";
                current = current->next;
            }
            cout << "NULL" << endl;
        }
    }
    
    //从尾开始遍历
    void Aftershow(node* current) {
        if (isEmpty(current))
            cout << "the list is empty!" << endl;
        else {
            while (current != NULL) {
                cout << current->data << "->";
                current = current->parent;
            }
            cout << "NULL" << endl;
        }
    }
};

//输入邻接矩阵创建邻接链表:
void test03()
{
    vector<vector<int>> Matrix; //接收输入的邻接矩阵
    vector<node*> MyVector;       //目标得到的邻接链表

    //接受维数:
    int dim; cin >> dim;

    //接收邻接矩阵:
    for (int i = 0; i < dim; i++)
    {
        vector<int> matrix;         //邻接矩阵的内部矩阵
        for (int j = 0; j < dim; j++)
        {
            int num; cin >> num;
            matrix.push_back(num);
        }
        Matrix.push_back(matrix);
    }

    //检测邻接矩阵
    for (int i = 0; i < dim; i++)
    {
        for (int j = 0; j < dim; j++)
        {
            cout << Matrix[i][j];
        }
        cout << endl;
    }

    //建立并遍历邻接链表
    for (int i = 0; i < dim; i++)
    {
        Linkedlist Mylist;
        for (int j = 0; j < dim; j++)
        {
            if (Matrix[i][j] == 1)
            {
                Mylist.insertEnd(j + 1);
            }
        }
        MyVector.push_back(Mylist.Get_head());
        //遍历:
        cout << i+1 << ": ";
        Mylist.Preshow();
        cout<< endl;
    }
}

int main() {
    test03();
    return 0;
}


 

输入:

9
0 1 1 1 0 0 0 0 0
1 0 0 0 1 1 0 0 0
1 0 0 1 0 0 0 1 0
1 0 1 0 0 0 0 1 1
0 1 0 0 0 0 1 0 0
0 1 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0
0 0 1 1 0 0 0 0 0
0 0 0 1 0 0 0 0 0

输出:

011100000
100011000
100100010
101000011
010000100
010000000
000010000
001100000
000100000
1: 2->3->4->NULL

2: 1->5->6->NULL

3: 1->4->8->NULL

4: 1->3->8->9->NULL

5: 2->7->NULL

6: 2->NULL

7: 5->NULL

8: 3->4->NULL

9: 4->NULL

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值