您的位置:首页 > 编程语言 > C语言/C++

双向约瑟夫(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;

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