Introduction to Linear time de-weight C code algorithm for adjacency list 22.1-4

Source: Internet
Author: User

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 (&AMP;LIST1); Init (&AMP;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 (&AMP;LIST3,1); Insert (&AMP;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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.