码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-04-14 14:26:36      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:

这里利用直接寻址法去重,遍历链表,如果对应数组位置值为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("\n");
    }
}

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);
}

 

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

标签:

原文地址:http://www.cnblogs.com/Nastukashii/p/4424584.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!