首页 > 代码库 > C语言双向链表

C语言双向链表

原文:C语言双向链表

  今天写了点双向链表的各种操作,写插入的时候费了点时间,不过,现在看来还是值得耗费那点时间去写的,这种小东西应该能信手拈来才行啊。

  1 /*双向链表*/  2 #include <stdio.h>  3 #include <string.h>    /*strcmp(const char *,const char *) return 0 is equal*/  4    5  typedef struct dulnode  6  {  7     char name[20];  8     struct dulnode *prior,*next;  9  }stud; 10  /*创建,返回链表头指针,参数n-节点个数*/ 11  stud * create(n); 12  /*查找,返回节点地址,参数h-链表头指针,name为查询条件*/ 13  stud * search(stud *h,char *name); 14  /*删除*/ 15  void del(stud *p); 16  /*显示,参数-h,链表头节点,num-显示节点个数*/ 17  void show(stud *h,int num); 18  /*求链表长度*/ 19  int length(stud *head); 20  /*插入节点,参数head-链表头节点,name-节点,n-节点位置*/ 21  stud *insert(stud *head,char *name,int n);  22  int main() 23  { 24     stud *head,*re,*in; 25     head = create(5); 26     show(head,5); 27     re = search(head,"Accipiter"); 28     show(re,1); 29     del(re); 30     re = head->next; 31     show(re ,4); 32     in = insert(re , "cnblogs",3); 33     show(in,5); 34     return 0; 35  } 36  void show(stud *head,int num) 37  { 38     stud *sp; 39     int cnt = 1; 40     if(NULL == head) 41     { 42         printf("It‘s NULL!\n"); 43         return; 44     } 45     if(num>length(head)) 46      { 47         printf("It‘s Full\n"); 48         return ; 49     } 50     if(strcmp(head->name,"") == 0) 51     { 52         sp = head->next; 53     } 54     else 55     { 56         sp = head; 57     } 58     while(sp) 59     { 60         printf("name %d:%s\n",cnt,sp->name); 61         if(cnt == num) 62         { 63             sp = NULL; 64         }else 65         { 66             sp = sp->next; 67             cnt++; 68         } 69     } 70  } 71   72 stud * create(n) 73 { 74     stud *p,*h,*s;/*p-前指针,h-头指针,s-后指针*/ 75     int i; 76     /*构建头指针*/ 77     h = (stud *)malloc(sizeof(stud)); 78     if(h == NULL) 79     { 80         return NULL; 81     } 82     h->name[0] = \0; 83     h->prior = NULL; 84     h->next = NULL; 85     /*插入节点*/ 86     p = h; 87     for(i=0;i<n;i++) 88     { 89         s = (stud *)malloc(sizeof(stud)); 90         if(NULL == s) 91         { 92             printf("node malloc error!\n"); 93             break; 94         } 95         p->next = s; 96         printf("%d‘s name:\n",i+1); 97         gets(s->name); 98         s->prior = p; 99         s->next = NULL;/*末尾插入*/100         p = s;101     }102     p->next = NULL;103     return (h);104 }105 106 stud * search(stud *head , char *name)107 {108     stud *p,*re=NULL;109     p=head->next;110     while(p)111     {112         if(strcmp(p->name,name) == 0)113         {114             re = p;115             p = NULL;116         }117         else118         {119             p = p->next;120         }121     }122     if(re ==  NULL)123     {124         printf("Can‘t Find!\n");125     }126     return (re);127 }128 129 void del(stud *p)130 {131     if(p == NULL|| (strcmp(p->name,"")==0))132     {133         printf("is Null or head!\n");134         return;135     }136     else137     {138         p->next->prior = p->prior;139         p->prior->next = p->next;140         free(p);141     }142 }143 144 int length(stud *head)145 {146     int len=0;147     stud *sp;148     if(head != NULL)149     {150         len = 1;151         sp = head->next;152         while(sp)153         {154             sp = sp->next;155             len++;156         }    157     }158     return len;159 }160 161 stud *insert(stud *head , char *name , int n)162 {163     stud *p,*nhead;164     int cnt=0;165     if(strcmp(head->name,"") != 0)166     {167         cnt=1;168     }169     nhead = head;170     if(n>length(head)||n<0)171     {172         printf("It‘s Full or Error!\n");173         return NULL;174     }175     /*插入节点*/176     while(cnt<n)177     {178         head = head->next;179         cnt++;180     }181     p = (stud *)malloc(sizeof(stud));182     if(p != NULL)183     {184          strcpy(p->name,name);185          p->next = head->next;186          p->prior = head;187     188          if(head->next)/*双向链表操作这里很重要*/189              head->next->prior=p;190             head->next=p;191     }192     193     return nhead;194 }

 

C语言双向链表