Estructura de datos del lenguaje C (lenguaje tipo C) -lista circular unidireccional-anillo de Joseph

Área de código

Joseph Ring: ¡Implementado en lenguaje C-like! ! ! Se puede ejecutar con éxito! ! ! No solo un algoritmo, sino una verdadera clase C

#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct LNode
{
 ElemType data;
 struct LNode *next;
}LNode,*LinkList;

enum Status{ERROR,OK}; //此处使用枚举类型
Status InitList(LinkList &L,int n)//初始化
{
 LinkList p,q;
 
 L=(LinkList)malloc(sizeof(LNode));//申请头结点的空间
 L->next=NULL;    
 p=L;
 for(int i=1;i<=n;i++) //输入数据域
 {
 q=(LinkList)malloc(sizeof(LNode));
 q->data=i;  
 p->next=q;
 p=q;
 }
 p->next=L->next; //首尾连接(注意,因为头结点没有真实数据域,所以是L->next)
 return OK;
}

Status cycle_delete(LinkList L,int n)
{
 LinkList p=L,q;
 printf("the number that leaves in order are:\n");   //依次出局的数字为
 while(p->next!=p)  //当链表只有一个结点的时候(不包括头结点)
 {
  for(int i=1;i<n;i++)  //n为循环次数
  {
    p=p->next;
  }
  q=p->next;
  p->next=q->next;
  printf("%d ",q->data);
  free(q);  
 }
 printf("\n");
 printf("the last one is %d\n",p->data);
 free(p);
 return OK;
}

Status show(LinkList L)
{
  LinkList p;
  p=L->next;
  printf("look these datas\n");
  printf("%d ",p->data);
  p=p->next;
  while(p!=L->next)
  {
  printf("%d ",p->data);
  p=p->next;
  }
  printf("\n");
  return OK;
}

int main()
{
 LinkList L;
 int n,m;
 printf("enter number you want to cycle\n");
 scanf("%d",&n);
 InitList(L,n);
 show(L);

printf("enter the num you want to cycle to delete\n");
scanf("%d",&m);
cycle_delete(L,m);
return 0;
 }

Área de prueba

Inserte la descripción de la imagen aquí

Publicado 56 artículos originales · elogiado 53 · visitas 2324

Supongo que te gusta

Origin blog.csdn.net/September_C/article/details/104873432
Recomendado
Clasificación