关于约瑟夫的小问题
自己编了一下约瑟夫问题,可是每次输出完就会弹出已停止工作,不知为何,求解答~程序如下:
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int data;
struct Node *next;
}NODE,*PNODE;
PNODE create();
//void traverse(PNODE);
void traverse2(PNODE);
int yue(PNODE);
//int delete_p(PNODE,int);
void delete_p2(PNODE,int);
void main()
{
PNODE p=NULL;
p=create();
delete_p2(p,3);
}
PNODE create()
{
PNODE pHead=(PNODE)malloc(sizeof(NODE));
PNODE pTail=pHead;
int len,i;
printf("请输入人数:");
scanf("%d",&len);
pTail->next=NULL;
for(i=1;i<=len;i++)
{
PNODE pNew=(PNODE)malloc(sizeof(NODE));
pNew->data=i;
pTail->next=pNew;
pNew->next=NULL;
pTail=pNew;
}
pTail->next=pHead->next;
return pHead;
}
void traverse2(PNODE ps)
{
PNODE p=ps;
p=p->next;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
void delete_p2(PNODE pHead,int pos)
{
int num=0;
int val,i;
PNODE p=pHead;
PNODE pp;
while(p->next!=NULL){
for(i=1;i<pos;i++)
{
p=p->next;
}
pp=p->next;
val=p->next->data;
p->next=p->next->next;
free(pp);
printf("%d\n",val);
// return val;
}
} 要学会调试
人造人 发表于 2017-3-7 16:37
要学会调试
好厉害{:5_92:}那该怎么改进阿? 我改了半天也不行... sunmenmian 发表于 2017-3-7 16:43
好厉害那该怎么改进阿? 我改了半天也不行...
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int data;
struct Node *next;
}NODE, *PNODE;
PNODE create();
//void traverse(PNODE);
void traverse2(PNODE);
int yue(PNODE);
//int delete_p(PNODE,int);
void delete_p2(PNODE, int);
void main()
{
PNODE p = NULL;
p = create();
delete_p2(p, 3);
}
PNODE create()
{
PNODE pHead = (PNODE)malloc(sizeof(NODE));
PNODE pTail = pHead;
int len, i;
printf("请输入人数:");
scanf("%d", &len);
pTail->next = NULL;
for(i = 1; i <= len; i++)
{
PNODE pNew = (PNODE)malloc(sizeof(NODE));
pNew->data = i;
pTail->next = pNew;
pNew->next = NULL;
pTail = pNew;
}
pTail->next = pHead->next;
return pHead;
}
void traverse2(PNODE ps)
{
PNODE p = ps;
p = p->next;
while(p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
void delete_p2(PNODE pHead, int pos)
{
int num = 0;
int val, i;
PNODE p = pHead;
PNODE pp;
while(p->next != p) //这里
{
for(i = 1; i<pos; i++)
{
p = p->next;
}
pp = p->next;
val = p->next->data;
p->next = p->next->next;
free(pp);
printf("%d\n", val);
// return val;
}
//这里
printf("%d\n", p->data);
free(p);
}
人造人 发表于 2017-3-7 17:14
我天,大神阿{:10_275:} 路过看看 顶一顶
页:
[1]