标题:双链表实现约瑟夫 大家帮忙看看有什么不足的 小白求教
只看楼主
nikobelic
Rank: 2
等 级:论坛游民
帖 子:37
专家分:30
注 册:2012-5-26
结帖率:25%
 问题点数:0 回复次数:0 
双链表实现约瑟夫 大家帮忙看看有什么不足的 小白求教
#include <stdio.h>
#include <stdlib.h>
typedef struct LNode
{
    int num;
    int passwd;
    struct LNode *next;
    struct LNode *prior;
}LNode,*LinkList;

LinkList Creat_L(int n)
{
    LNode *head,*p,*q;
    int i;
    head = NULL;
    for(i = 1;i <= n;i++)
    {
        p = (LNode *)malloc(sizeof(LNode));
        scanf("%d",&p->passwd);
        p->num = i;
        if(head == NULL)
        {
            head = q = p;
        }
        else
        {
            q->next = p;
            p->prior = q;
            q = p;
        }
        p->next = head;
        head->prior = p;
    }
    return head;
}
void Print_L(LinkList L)
{
    LNode *p;
    p = L;
    printf("%d ",p->passwd);
    p = p->next;
    while(p != L)
    {
        printf("%d ",p->passwd);
        p = p->next;
    }
}
void Delete_L(LinkList L,int m,int n)
{
    LNode *p,*q,*head;
    int j,k = 0;    //k为删除次数
    p = q = head = L;
    while(k < n )
    {
        j = 1;
        while(j < m) //指向被删除的节点
        {
            p = p->next;
            j++;
        }
        q = p->next;
        p->prior->next = p->next;
        p->next->prior = p->prior;
        m = p->passwd;
        printf("%d ",p->num);
        free(p);
        p = NULL;
        p = q;
        k++;
        
    }

}

void main()
{
    int n,m;
    LinkList L;
    scanf("%d%d",&m,&n);
    L = Creat_L(n);
    Delete_L(L,m,n);
}
搜索更多相关主题的帖子: 约瑟夫 next include 
2012-10-17 20:21



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-383462-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.344172 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved