C语言约瑟夫环链表
2016-05-19 20:01
627 查看
#include <stdio.h>
#include <windows.h>
#include <string.h>
struct link * creat1(int n);
void out(struct link * head);
void free_(struct link * head);
struct link * jsf(struct link * head,int m);
struct link
{
int data;
struct link * next;
};
void main()
{
struct link * head;
struct link * p;
int n,m;
printf("input n , m:"); //读入个数与约定删除数
scanf("%d %d",&n,&m);
head = creat1(n);
out(head);
p = jsf(head,m);
printf("\nlast: %d \n",p->data);
free(p);
}
struct link * jsf(struct link * head,int m)
{
int i;
struct link * p = malloc(sizeof(struct link )); //给无头结点链表添加辅助头结点,但循环链表的尾巴仍指向第一个元素点,而不是头结点
struct link * q;
p->next = head;
head = p;
while(p->next != p) //只剩下一个节点时,即为结果
{
for(i = 0; i < m; i++) //数到M删除一次
{
q = p;
p = p->next;
}
q->next = p->next;
free(p);
p = q;
}
free(head);//删除头结点
return p;
}
struct link * creat1(int n) //创建循环链表
{
struct link * head;
struct link * p;
struct link * q;
int i = 1;
head = (struct link *)malloc(sizeof(struct link));
p = q = (struct link *)malloc(sizeof(struct link));
p->data = i;
head->next=p;
while (i < n)
{
i++;
p = (struct link *)malloc(sizeof(struct link));
p->data = i;
q->next = p;
q = p;
}
p = head->next; //删除头结点。使之为无头结点链表,多此一举,懒得改
q->next = p;
free(head);
return p;
}
void out(struct link * head) //输出无头结点链表
{
struct link * p = head->next;
printf("%d ",head->data);
while(p != head)
{
printf("%d ",p->data);
p = p->next;
}
putchar(10);
}
void free_(struct link * head) //删除无头结点链表,未使用
{
struct link * p = head->next;
struct link * q = head;
while(head != p)
{
q = p->next;
free(p);
p = q;
}
free(head);
}
#include <windows.h>
#include <string.h>
struct link * creat1(int n);
void out(struct link * head);
void free_(struct link * head);
struct link * jsf(struct link * head,int m);
struct link
{
int data;
struct link * next;
};
void main()
{
struct link * head;
struct link * p;
int n,m;
printf("input n , m:"); //读入个数与约定删除数
scanf("%d %d",&n,&m);
head = creat1(n);
out(head);
p = jsf(head,m);
printf("\nlast: %d \n",p->data);
free(p);
}
struct link * jsf(struct link * head,int m)
{
int i;
struct link * p = malloc(sizeof(struct link )); //给无头结点链表添加辅助头结点,但循环链表的尾巴仍指向第一个元素点,而不是头结点
struct link * q;
p->next = head;
head = p;
while(p->next != p) //只剩下一个节点时,即为结果
{
for(i = 0; i < m; i++) //数到M删除一次
{
q = p;
p = p->next;
}
q->next = p->next;
free(p);
p = q;
}
free(head);//删除头结点
return p;
}
struct link * creat1(int n) //创建循环链表
{
struct link * head;
struct link * p;
struct link * q;
int i = 1;
head = (struct link *)malloc(sizeof(struct link));
p = q = (struct link *)malloc(sizeof(struct link));
p->data = i;
head->next=p;
while (i < n)
{
i++;
p = (struct link *)malloc(sizeof(struct link));
p->data = i;
q->next = p;
q = p;
}
p = head->next; //删除头结点。使之为无头结点链表,多此一举,懒得改
q->next = p;
free(head);
return p;
}
void out(struct link * head) //输出无头结点链表
{
struct link * p = head->next;
printf("%d ",head->data);
while(p != head)
{
printf("%d ",p->data);
p = p->next;
}
putchar(10);
}
void free_(struct link * head) //删除无头结点链表,未使用
{
struct link * p = head->next;
struct link * q = head;
while(head != p)
{
q = p->next;
free(p);
p = q;
}
free(head);
}
相关文章推荐
- java jni jbyteArray传参给c++
- 抽象工厂 C++实现
- c++学习书籍汇总
- C++ 中 ZeroMemory、memset 危险需慎用
- QFile和C语言对文件操作的性能比较.--读取double型二进制数据文件
- C语言判断分支循环函数杂记
- 常量指针与指向常量的指针
- C语言数据类型杂记
- c++新人,结构化程序设计与面向对象程序设计的比较(1)
- LeetCode 47
- C++冒号表达式
- SM2算法第十五篇:ECDSA数字签名算法的C语言实现
- typedef C/C++
- L1-002. 打印沙漏(C++)
- C/C++的字符串操作(二)
- C语言随记(一)—函数
- c/c++原子操作 接口函数 自查文档
- C/C++的字符串操作(一)
- C++第六次作业 项目1 项目4
- C语言中memset函数详解