双向约瑟夫(C语言双向循环链表实…
2013-08-23 15:06
288 查看
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct Student)
typedef struct Student{
char name[12];
int num;
char sex[4];
int age;
char Class[6];
char health[10];
struct Student *pre;
struct Student *next;
}student,*studentA;
struct Student *CreatList(student *head,int
n){
struct Student *p1,*p2;
int i;
p1=(student *)malloc(LEN);
head=(student *)malloc(LEN);
scanf("%s %d %s %d %s
%s",p1->name,&p1->num,p1->sex,&p1->age,p1->Class,p1->health);
head=p2=p1;
for(i=0;i<n-1;i++)
{
p1=(struct Student
*)malloc(LEN);
scanf("%s %d %s %d %s
%s",p1->name,&p1->num,p1->sex,&p1->age,p1->Class,p1->health);
p2->next=p1;
p1->pre=p2;
p2=p1;
}
p2->next=head;
head->pre=p2;
return head;
}
void search(student *head,int m,int n,int x)
{
struct Student *p,*q,*p1;
int i=0,k=0;
p=(struct Student *)malloc(LEN);
p=head;
while (n!=0)
{
if(k==0)
{
if(m>1)
{
for(i=0;i<m-2;i++)
p=p->next;
}
q=p->next;
p1=q->next;
p->next=p1;
p1->pre=p;
printf("%s%d%s%d%s%s\n",q->name,q->num,q->sex,q->age,q->Class,q->health);
free(q);
p=p->next;
k++;
}
else
{
if(x>1)
{
for(i=0;i<x-2;i++)
p=p->pre;
}
q=p->pre;
p1=q->pre;
p1->next=p;
p->pre=p1;
printf("%s%d%s%d%s%s\n",q->name,q->num,q->sex,q->age,q->Class,q->health);
free(q);
p=p->pre;
k--;
}
n--;
}
}
int main()
{
struct Student *head;
int m,n,x;
scanf("%d %d
%d",&n,&m,&x);
head=(student *)malloc(LEN);
head=CreatList(head,n);
search(head,m,n,x);
return 0;
}
#include<stdlib.h>
#define LEN sizeof(struct Student)
typedef struct Student{
char name[12];
int num;
char sex[4];
int age;
char Class[6];
char health[10];
struct Student *pre;
struct Student *next;
}student,*studentA;
struct Student *CreatList(student *head,int
n){
struct Student *p1,*p2;
int i;
p1=(student *)malloc(LEN);
head=(student *)malloc(LEN);
scanf("%s %d %s %d %s
%s",p1->name,&p1->num,p1->sex,&p1->age,p1->Class,p1->health);
head=p2=p1;
for(i=0;i<n-1;i++)
{
p1=(struct Student
*)malloc(LEN);
scanf("%s %d %s %d %s
%s",p1->name,&p1->num,p1->sex,&p1->age,p1->Class,p1->health);
p2->next=p1;
p1->pre=p2;
p2=p1;
}
p2->next=head;
head->pre=p2;
return head;
}
void search(student *head,int m,int n,int x)
{
struct Student *p,*q,*p1;
int i=0,k=0;
p=(struct Student *)malloc(LEN);
p=head;
while (n!=0)
{
if(k==0)
{
if(m>1)
{
for(i=0;i<m-2;i++)
p=p->next;
}
q=p->next;
p1=q->next;
p->next=p1;
p1->pre=p;
printf("%s%d%s%d%s%s\n",q->name,q->num,q->sex,q->age,q->Class,q->health);
free(q);
p=p->next;
k++;
}
else
{
if(x>1)
{
for(i=0;i<x-2;i++)
p=p->pre;
}
q=p->pre;
p1=q->pre;
p1->next=p;
p->pre=p1;
printf("%s%d%s%d%s%s\n",q->name,q->num,q->sex,q->age,q->Class,q->health);
free(q);
p=p->pre;
k--;
}
n--;
}
}
int main()
{
struct Student *head;
int m,n,x;
scanf("%d %d
%d",&n,&m,&x);
head=(student *)malloc(LEN);
head=CreatList(head,n);
search(head,m,n,x);
return 0;
}
相关文章推荐
- C语言通过双向循环链表解决Josephus(约瑟夫)问题
- c语言写双向循环链表
- C语言循环链表实现约瑟夫问题
- C语言 双向循环链表的实现和结构 链表 线性表的链式实现
- C语言实现双向循环链表
- C语言实现双向循环链表[下]
- C语言双向循环链表的生成,删除和打印
- 约瑟夫问题 C语言循环链表
- 双向循环链表的C语言实现
- c语言实现双向循环链表
- linux中C语言实现双向循环链表
- 双向循环链表基本操作(C语言)
- C语言实现双向循环链表
- C语言单向循环链表解决约瑟夫问题
- c语言双向循环链表
- 带哨兵的双向循环链表的C语言实现
- C语言实现双向循环链表
- C语言双向循环链表api(源自gluster源码)
- 带哨兵的双向循环链表的C语言实现
- 链表的C语言实现之循环链表及双向链表