• 邻接链表线性时间去重 C代码 算法导论 22.1-4


    这里利用直接寻址法去重,遍历链表,如果对应数组位置值为0,则修正为1,如果对应数组为1,则删除该节点。(数组初始化为0)

    链表的一些操作都简单的实现了一下。

    #include <stdio.h>
    #include <stdlib.h>
    #include <malloc.h>
    
    struct Node{
        int key;
        Node *next;
    };
    
    struct List{
        Node *head;
    };
    typedef struct Node Node;
    typedef struct List List;
    
    void Init(List *list){
        list->head = NULL;
    }
    Node *List_Search(List *list, int k){
        Node *temp = list->head;
        while (temp&&temp->key != k)
            temp = temp->next;
        return temp;
    }
    
    void Insert(List *list, int key){
        Node *p = (Node*)malloc(sizeof (Node));
        p->key = key;
        p->next = list->head;
        list->head = p;
    }
    
    Node *Delete_Byptr(List *list,Node *prev,Node *wanted){
        if (prev){
            prev->next = wanted->next; 
            free(wanted);
            return prev->next;
        }
        else {
            list->head = wanted->next;
            free(wanted);
            return list->head;
        }
    }
    
    void Delete_Bykey(List *list, int key){
        Node *temp = list->head;
        Node *prev = 0;
        while (temp&&temp->key != key){
            prev = temp;
            temp = temp->next;
        }
        if (temp){
            prev->next = temp->next;
            free(temp);
        }
    }
    
    //合并两个数组
    List *Union(List *list1, List *list2){
        if (!list1->head)
            return list2;
        Node *temp = list1->head;
        while (temp->next)
            temp = temp->next;
        temp->next = list2->head;
        return list1;
    }
    
    void Print_List(List *list){
        Node *temp = list->head;
        while (temp){
            printf("%d ", temp->key);
            temp = temp->next;
        }
    }
    
    typedef Node Vnode;
    
    //这里假设key为0到V-1,表示的是第(key+1)个节点
    void Unique(List *Adj, int V){
        int *p = (int *) calloc(V,sizeof(int));
        for (int i = 0; i < V; ++i){
            Vnode *temp = Adj[i].head;
            Vnode *prev = 0;
            while (temp){
                if (p[temp->key] == 1 || temp->key == i)
                    temp=Delete_Byptr(&Adj[i], prev, temp);
                else {
                    p[temp->key] = 1;
                    prev = temp;
                temp = temp->next;
                }
            }
            //数组元素都置为0
            temp = Adj[i].head;
            while (temp){
                p[temp->key] = 0;
                temp = temp->next;
            }
        }
        free(p);
    }
    
    void Print(List *Adj, int V){
        for (int i = 0; i < V; ++i){
            Vnode *temp = Adj[i].head;
            printf("Node%d ", i);
            while (temp){
                printf("%d ", temp->key);
                temp = temp->next;
            }
            printf("
    ");
        }
    }
    
    int main(){
        List list1,list2,list3;
        Init(&list1); Init(&list2); Init(&list3);
        Insert(&list1, 0);
        Insert(&list1, 1); Insert(&list1, 1); Insert(&list1, 1);
        Insert(&list1, 2); Insert(&list1, 2);
        Insert(&list2, 2); Insert(&list2, 2);
        Insert(&list3, 1); Insert(&list3, 1);
        List Adj[3] = { list1,list2,list3 };
        Print(Adj, 3);
        Unique(Adj, 3);
        Print(Adj, 3);
    }
  • 相关阅读:
    css的盒子模型由什么组成?
    div盒子水平、垂直居中
    display:none和visibility:hidden的区别
    创建一个多选框,且和文本关联起来(单击文本就像单击该选框一样)
    Canvas和SVG的不同
    js两个页面之间通过URL传参数
    css a标签去除下划线
    css 设置文本垂直居中
    css 边框圆角的方法
    html 文本框css设置边框圆角
  • 原文地址:https://www.cnblogs.com/Nastukashii/p/4424584.html
Copyright © 2020-2023  润新知