Here the direct addressing method is used to go through the list, if the corresponding array position value is 0, then the correction is 1, if the corresponding array is 1, then delete the node. (array initialized to 0)
Some of the operations of the list are simply implemented.
#include <stdio.h>#include<stdlib.h>#include<malloc.h>structnode{intkey; Node*next;};structlist{Node*head;}; typedefstructNode node;typedefstructlist List;voidInit (List *list) {List->head =NULL;} Node*list_search (List *list,intk) {Node*temp = list->Head; while(Temp&&temp->key! =k) Temp= temp->Next; returntemp;}voidInsert (List *list,intkey) {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); returnPrev->Next; } Else{List->head = wanted->Next; Free(Wanted); returnList->Head; }}voidDelete_bykey (List *list,intkey) {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); }}//merging two arraysList *union (list *list1, list *List2) { if(!list1->head)returnList2; Node*temp = list1->Head; while(temp->Next) Temp= temp->Next; Temp->next = list2->Head; returnList1;}voidPrint_list (List *list) {Node*temp = list->Head; while(temp) {printf ("%d", temp->key); Temp= temp->Next; }}typedef Node Vnode;//This assumes that the key is 0 to V-1, which indicates the node (key+1)voidUnique (List *adj,intV) { int*p = (int*)callocVsizeof(int)); for(inti =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; } } //array elements are set to 0temp =Adj[i].head; while(temp) {p[temp->key] =0; Temp= temp->Next; } } Free(P);}voidPrint (List *adj,intV) { for(inti =0; i < V; ++i) {Vnode*temp =Adj[i].head; printf ("node%d", i); while(temp) {printf ("%d", temp->key); Temp= temp->Next; } printf ("\ n"); }}intMain () {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);}
Introduction to Linear time de-weight C code algorithm for adjacency list 22.1-4