zoukankan      html  css  js  c++  java
  • 双向循环链表示例

      





    1
    #include <stdio.h> 2 #include <malloc.h> 3 #include <stdlib.h> 4 #include <memory.h> 5 6 //定义学生信息节点 7 typedef struct student_info_node_def 8 { 9 char num[32];//学号 10 char name[32]; //姓名 11 struct student_info_node_def *last; //前指针 12 struct student_info_node_def *next; //后指针 13 }student; 14 15 16 17 int list_length(student *plink_header) 18 { 19 int length=0; 20 student *plink_length=plink_header->last; //采用前指针计算 21 while(plink_length !=plink_header) 22 { 23 length++; 24 plink_length=plink_length->last; 25 } 26 printf("length=%d ",length); 27 return length; 28 } 29 30 void list_insert(student *plink_header) 31 { 32 int length=list_length(plink_header); //链表长度 33 34 /*1.在那个节点处插入节点*/ 35 int i,n=0; 36 printf("插入的位置:"); 37 scanf("%d",&n); 38 39 /*2.新节点内容*/ 40 student *pnew=(student*)malloc(sizeof(student)); 41 memset(pnew,0,sizeof(student)); 42 sprintf(pnew->num,"学号%03d",n+1); 43 sprintf(pnew->name,"姓名%03d",n+1); 44 pnew->next=NULL; 45 pnew->last=NULL; 46 47 /*3.插入到链表指定位置*/ 48 student *plink=plink_header; 49 if(n<length) 50 { 51 for(i=0;i<n;i++) 52 { 53 plink=plink->next; 54 } 55 pnew->next=plink->next; 56 pnew->last=plink; 57 plink->next=pnew; 58 pnew->next->last=pnew; 59 } 60 if(n>=length) 61 { 62 for(i=0;i<length;i++) 63 { 64 plink=plink->next; 65 } 66 pnew->next=plink->next; 67 pnew->last=plink; 68 plink->next=pnew; 69 pnew->next->last=pnew; 70 } 71 } 72 73 74 void plink_view(student *plink_header) 75 { 76 student *plink_std=plink_header->last; 77 while(plink_std !=plink_header) 78 { 79 printf("%s,%s ",plink_std->num,plink_std->name); 80 plink_std=plink_std->last; 81 } 82 83 printf("/*********************/ "); 84 85 student *plink_std2=plink_header->next; 86 while(plink_std2 !=plink_header) 87 { 88 printf("%s,%s ",plink_std2->num,plink_std2->name); 89 plink_std2=plink_std2->next; 90 } 91 } 92 93 void list_cancel(student* plink_header) 94 { 95 int length=list_length(plink_header); 96 student *plink=plink_header; 97 //提供删除节点 98 int i,n=0; 99 printf("删除的节点号(小于%d):",length); 100 scanf("%d",&n); 101 102 //删除操作处理 103 if(n<length) 104 { 105 for(i=0;i<n;i++) 106 { 107 plink=plink->next; 108 } 109 plink->last->next=plink->next; 110 plink->next->last=plink->last; 111 } 112 113 } 114 115 int main() 116 { 117 /***********************************/ 118 //创建头节点 119 int i=0; 120 student *plink_header; 121 plink_header=(student*)malloc(sizeof(student)); //为节点分配空间 122 plink_header->next=NULL; //初始化节点指针 123 plink_header->last=NULL; 124 125 /*********************************/ 126 //创建新节点 127 student *plink=plink_header; 128 for(;i<8;i++) 129 { 130 student *pnew=NULL; 131 pnew=(student*)malloc(sizeof(student)); 132 memset(pnew,0,sizeof(student)); 133 sprintf(pnew->num,"学号%03d",i+1); 134 sprintf(pnew->name,"姓名%03d",i+1); 135 pnew->next=NULL; 136 pnew->last=NULL; 137 //新节点连接前一个节点 138 plink->next=pnew; 139 pnew->last=plink; 140 plink=pnew; 141 } 142 //链表尾节点指向头节点,从而构成双向循环链表 143 plink->next=plink_header; 144 plink_header->last=plink; 145 146 /*********************************/ 147 148 //使用前指针和后指针遍历链表: 149 plink_view(plink_header); 150 151 152 /*********************************/ 153 //求链表的长度 154 list_length(plink_header); 155 156 //双向链表的插入操作 157 list_insert(plink_header); 158 159 //遍历链表 160 plink_view(plink_header); 161 162 //计算链表长度 163 list_length(plink_header); 164 165 //删除操作 166 list_cancel(plink_header); 167 168 //遍历链表 169 plink_view(plink_header); 170 171 //计算链表长度 172 list_length(plink_header); 173 return 0; 174 }
    内在的趣味,表面的繁琐
  • 相关阅读:
    Lucene学习总结之七:Lucene搜索过程解析
    Lucene学习总结之六:Lucene打分公式的数学推导
    Lucene学习总结之五:Lucene段合并(merge)过程分析
    Lucene学习总结之四:Lucene索引过程分析
    Lucene学习总结之三:Lucene的索引文件格式(1)
    Lucene学习总结之二:Lucene的总体架构
    Lucene学习总结之一:全文检索的基本原理
    解决Eclipse中文乱码
    【Lucene4.8教程之五】Luke
    【Tika基础教程之一】Tika基础教程
  • 原文地址:https://www.cnblogs.com/data1213/p/4888200.html
Copyright © 2011-2022 走看看