新手上路第一天————菜鸟的心得之单链表(循环链表)
2012-07-15 20:04
183 查看
(既然条条大路通罗马,那又何必在乎杨康之道,你说呢?)
今天写一个循环链表,我纠结了很长时间。不知道循环链表怎么定义,最后直接用算法了。
程序实现:1~10个人围成一圈,对其进行1~10编号。随便输入一个大于0的整数n。第一个人开始数数从1开始,数到n的人退出。得
今天写一个循环链表,我纠结了很长时间。不知道循环链表怎么定义,最后直接用算法了。
程序实现:1~10个人围成一圈,对其进行1~10编号。随便输入一个大于0的整数n。第一个人开始数数从1开始,数到n的人退出。得
#include <stdio.h> #include <stdlib.h> struct num { int num; struct num *next; }; struct num *head; void creat_num() { head=(struct num*)malloc(sizeof(struct num)); head=NULL; } void in_num(int num) { struct num *p=(struct num *)malloc(sizeof(struct num)); p->num=num; p->next=head; head=p; } void rm_num(int n) { struct num *p=NULL; struct num *temp=NULL; int i=10; int count=0; while(i>1) { if(p==NULL) { p=head; } count++; if(count==n) { if(head==p) { temp=head; head=head->next; p=head; free(temp); temp=NULL; } else { temp->next=p->next; temp=p; p=p->next; free(temp); temp=NULL; } i--; count=0; } else { temp=p; p=p->next; } } } void display_num() { struct num *p=head; while(p) { printf("%d\t",p->num); p=p->next; } printf("\n"); } int main() { int i=0; int delete; printf("please input a num:"); scanf("%d",&delete); if(delete<0) { printf("input error!\n"); } printf("delete=%d\n",delete); creat_num(); for(i=10;i>0;i--) { in_num(i); } display_num(); rm_num(delete); printf("output:"); display_num(); return 0; }
相关文章推荐
- 新手上路第一天————菜鸟的心得之单链表
- 新手上路第一天————菜鸟的心得之双向循环链表
- 第一天——新手上路
- VS2010 C# ReportViewer控件新手上路心得
- 新手开车上路心得
- 转载:VS2010 C# ReportViewer控件新手上路心得
- 新手上路之图像处理学习心得
- 菜鸟Z学java的第一天
- 软件项目经理新手上路(3) - 这不是份简单的工作
- Ubuntu 6.06新手上路
- PHP新手上路(十一)
- 使用GAE(Google App Engine)一步一步开发自己的博客[1]:新手上路
- 软件项目经理新手上路11 - 找不到自己,看不见别人 推荐
- 菜鸟上路 Linux命令
- 堆栈溢出的异常问题,本人新手菜鸟,请各位高手指点一下啊!
- Java------网络通信编程 之 菜鸟新手的学习总结
- 新手入门 Linux菜鸟必学的60个命令--安装和登录命令
- 线性表链式存储及其实现(单链表,循环链表,双向链表)
- 新手上路系列2:表达式与流程控制
- PHP新手上路(五)