您的位置:首页 > 其它

新手上路第一天————菜鸟的心得之单链表(循环链表)

2012-07-15 20:04 183 查看
(既然条条大路通罗马,那又何必在乎杨康之道,你说呢?)

今天写一个循环链表,我纠结了很长时间。不知道循环链表怎么定义,最后直接用算法了。

程序实现: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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: