C Foundation--The construction of doubly linked list

Source: Internet
Author: User
Tags define null

#include <stdio.h>#include"link.h"voidprint_item (link p) {printf ("%d\n", p->item);}intMainvoid) {link head, tail, p; //struct node *head;Link_init (&head, &tail);//head = NULLP= Make_node (3); Link_insert (&head, &tail, p);//Head Insertion Methodp = Make_node (5); Link_insert (&head, &tail, p);//Head Insertion Methodp = Make_node (1); Link_insert (&head, &tail, p);//Head Insertion Methodp = Make_node (8); Link_insert (&head, &tail, p);//Head Insertion MethodLink_travel_head (&head, Print_item);//traversing the Print List numeric fieldsprintf"***************\n"); Link_travel_tail (&tail, Print_item);//traversing the Print List numeric fieldsprintf"***************\n"); P= Link_search (&head,1); if(P! =NULL) {Link_delete (&head, &tail, p);    Free_node (P); } link_travel_tail (&tail, Print_item);//traversing the Print List numeric fieldsLink_destory (&head, &tail); return 0;}
#ifndef _link_h_#define_link_h_typedefstructNode *link;structNode {intitem;        Link next; //struct node *next; successorLink pre;//struct node *pre; precursor};voidLink_init (link *head, link *tail); link Make_node (intitem);voidLink_insert (link *head, link *tail, link p); Link link_search (Link*head,intkey);voidLink_delete (link *head, link *tail, link p);voidfree_node (link p);voidLink_modfie (link p,intkey);voidLink_destory (link *head, link *tail);voidLink_travel_head (Link *head,void(*vist) (link));voidLink_travel_tail (Link *tail,void(*vist) (link));#endif
#include <stdio.h>#include<stdlib.h>#include"link.h"voidLink_init (link *head, link *tail)//struct Node **head = &head{    //head = NULL;*head = *tail =NULL;} Link Make_node (intItem) {    //link p = (link *) malloc (sizeof (struct node));Link p = (link)malloc(sizeof(*p)); P->item = Item;//(*p). Itme = Item;P->next = NULL;//#define NULL (void *) 0P->pre = NULL;//#define NULL (void *) 0    returnp;}voidLink_insert (link *head, link *tail, link p)//Head Insertion Method{link q; if(*head = =NULL) {        *head =p; *tail =p; return; } P->next = *Head; (*head)->pre =p; *head =p;} Link link_search (link*head,intkey)    {link p;  for(p = *head; p! = NULL; p = p->next)if(P->item = =key)returnp; returnNULL;}voidLink_delete (link *head, link *tail, link q)    {link p; if(q = = *head) {        *head = q->Next; (*head)->pre =NULL; return; }    if(q = = *tail) {        *tail = q->Pre; (*tail)->next =NULL; return; } q->pre->next = q->Next; Q->next->pre = q->pre;}voidfree_node (link p) { Free(P);}voidLink_modfie (link p,intkey) {P->item =key;}voidLink_destory (link *head, link *tail) {Link P= *Head, q;  while(P! =NULL) {Q= p->Next;  Free(P); P=Q; }    *head = NULL; *tail =NULL;}voidLink_travel_head (Link *head,void(*vist) (link))    {link p;  for(p = *head; p! = NULL; p = p->next) vist (p);}voidLink_travel_tail (Link *tail,void(*vist) (link))    {link p;  for(p = *tail; p! = NULL; p = p->pre) vist (p);}

C Foundation--The construction of doubly linked list

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.