C结构体与链表
今 天来总结C语言的学习盲点——结构体,为了不显单一,也为了补足作者链表的编程缺陷,特更此博文,总结近段时间的学习成果。话不多说,先上一段代码 struct none{int item; link next;}; typedef struct none *link; int main(int argc ,char *argv[]) { int i , N = atoi(argv[1]) , M = atoi(argv[2]); link t = malloc(sizeof(*t)) , x=t; t->item = 1; t->next = t; for(i=2 ; i <= N ; i++) { x = x->next = malloc(sizeof(*x)); x->item = i; x->next = t; } while(x != x->next) { for(i=1 ; i < M ; i++) x = x->next; x->next = x->next->next; } printf("%d\n",x->item); return 0; } 上面这段代码是解决约瑟夫问题的一种算法 代 码开头两行定义一个链表的数据节点并为节点结构取一别名link,注意:使用typedef取别名不能定义新的数据结构,只能对现有结构取别名。主函数里获取执行程序时对主函数输入的参数