zoukankan      html  css  js  c++  java
  • 邻接链表线性时间去重 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);
    }
  • 相关阅读:
    Checking Types Against the Real World in TypeScript
    nexus pip proxy config
    go.rice 强大灵活的golang 静态资源嵌入包
    几个golang 静态资源嵌入包
    rpm 子包创建学习
    Rpm Creating Subpackages
    ava 类似jest snapshot 功能试用
    ava js 测试框架基本试用
    The Architectural Principles Behind Vrbo’s GraphQL Implementation
    graphql-compose graphql schema 生成工具集
  • 原文地址:https://www.cnblogs.com/Nastukashii/p/4424584.html
Copyright © 2011-2022 走看看