zoukankan      html  css  js  c++  java
  • 循环链表

      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 
      4 typedef struct CLNode{
      5     int data;
      6     struct CLNode* next;
      7 }CLNode, *CLinkList;
      8 
      9 //产生空的双向循环链表
     10 void InitCL(CLinkList CL){
     11     if(CL != NULL){
     12         CL->next = CL;
     13     }
     14     else{
     15         CL = (CLinkList)malloc(sizeof(CLNode));
     16     }
     17 }
     18 
     19 //在CL的头结点插入元素
     20 void InsertFront(CLinkList CL, int value){
     21     CLinkList p;
     22     p = (CLinkList)malloc(sizeof(CLNode));
     23     p->data = value;
     24     p->next = CL->next;
     25     CL->next = p;
     26 }
     27 
     28 //在CL的尾结点插入元素
     29 void InsertEnd(CLinkList CL, int value){
     30     CLinkList p, q;
     31     p = (CLinkList)malloc(sizeof(CLNode));
     32     p->data = value;
     33     q = CL;
     34     while(q->next != CL){
     35         q = q->next;
     36     }
     37     p->next = q->next;
     38     q->next = p;
     39 }
     40 
     41 //打印CL
     42 void Print(CLinkList CL){
     43     CLinkList p;
     44     p = CL->next;
     45     while(p != CL){
     46         printf("%d ", p->data);
     47         p = p->next;
     48     }
     49     printf(" ");
     50 }
     51 
     52 //删除CL的头结点
     53 void DeleteFront(CLinkList CL, int *val){
     54     CLinkList p;
     55     p = CL->next;
     56     if(p != CL){
     57         CL->next = p->next;
     58         *val = p->data;
     59     }
     60     else{
     61         printf("LinkList is empty! ");
     62     }
     63 }
     64 
     65 //删除CL的尾节点
     66 void DeleteEnd(CLinkList CL, int *val){
     67     CLinkList p, q;
     68     q = CL;
     69     p = CL->next;
     70     if(p != NULL){
     71         while(p->next != CL){
     72             q = p;
     73             p = p->next;
     74         }
     75         *val = p->data;
     76         q->next = p->next;
     77     }
     78     else{
     79         printf("LinkList is empty! ");
     80     }
     81 }
     82 
     83 int main(){
     84     CLinkList ML;
     85     int i, res;
     86     ML = (CLinkList)malloc(sizeof(CLNode));
     87     InitCL(ML);
     88     DeleteFront(ML, &res);
     89     DeleteEnd(ML, &res);
     90     for(i = 1; i <= 8; i++){
     91         InsertFront(ML, i + 38);
     92         InsertEnd(ML, i + 100);
     93     }
     94     Print(ML);
     95     DeleteFront(ML, &res);
     96     printf("%d is deleted from linklist. ", res);
     97     DeleteEnd(ML, &res);
     98     printf("%d is deleted form linklist. ", res);
     99     Print(ML);
    100     printf("hello world! ");
    101     return 0;
    102 }
  • 相关阅读:
    自动化CodeReview
    10个有关RESTful API良好设计的最佳实践
    ASP.NET Core 获取控制器上的自定义属性
    [转] Autofac创建实例的方法总结
    PetaPoco
    LogViewer
    hdoj:2047
    hdoj:2046
    hdoj:2045
    hdoj:2044
  • 原文地址:https://www.cnblogs.com/angle-qqs/p/4026855.html
Copyright © 2011-2022 走看看