结构体10

作者: 时间:2020-07-09 点击数:

#include <stdio.h>
#include <malloc.h>
#include <string.h>
struct node
{
   char name[20];
int score;
struct node *next;
};
struct node *createlist(int n,char *xm[],int cj[])
{
   int i;
struct node *head,*p,*q;
p=(struct node *)malloc(sizeof(struct node));
head=q=p;
for(i=0;i<n;i++)
{
    p=(struct node *)malloc(sizeof(struct node));
 strcpy(p->name,xm[i]);p->score=cj[i];
       q->next=p;q=p;
}
q->next=NULL;
return head;
}
void count(struct node *head,int n)
{
   int i=0;
   struct node *p,*q;
   p=head;q=head->next;
   while (q->next!=NULL) q=q->next;
   q->next=p->next;//数据节点构建成一个环形  
   p=q;q=q->next;
   while (p->next!=p)
   {
      i++;
   if (i==n)
   {
     if (q==head->next) head->next=q->next;  //让原先的第二个数据节点成为新的首节点
  i=0;
  p->next=q->next;
  free(q);
  q=p->next;
   }
   else
   {p=q;q=q->next;}
   }
}
int main()
{
char *xm[]={"zhang","wang","li","zhao","zhu","sun","tan","tao","han","huang","liu","zheng","chen"};
int score[]={77,65,73,89,92,58,87,78,69,84,81,72,93};
struct node *head=NULL;
int k;
head=createlist(13,xm,score);
scanf("%d",&k);
count(head,k);
printf("%s:%d\n",head->next->name,head->next->score);
return 0;
}

上一页 [1] [2]

Copyright© 2020 Anyang Institute of Technology.All rights reserved.

学院地址:河南省安阳市黄河大道西段