主题:小弟的约瑟夫环大家帮忙看看错在哪里谢谢
qikangkang
[专家分:0] 发布于 2006-03-20 16:02:00
#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef struct LNode{
int num;
int key;
struct LNode *next;
}LNode,*LinkList;
void CreateList_L(LinkList &L,int n){
L=(LinkList)malloc(sizeof(LNode));
LinkList Li=L->next;
for(int i=n;i>0;--i){
LinkList p=(LinkList)malloc(sizeof(LNode));
p->num=n;
printf("Please input key:\n");
scanf("%d",&p->key);
p->next=Li;L->next=p;
}
}//CreateList_L
void main()
{
int m,n;
printf("Please input m,n:\n");
scanf("%d%d",&m,&n);
LinkList L;
CreateList_L(L,n);
while(n>=1)
{
for(int i=1;i<m;++i)
{
LinkList L_last=L;L=L->next;
printf("%3d",L->num);
L_last->next=L->next;
m=L->key;
L=L->next;
--n;
}
}
}
回复列表 (共3个回复)
沙发
冰封星云 [专家分:2260] 发布于 2006-04-04 20:56:00
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node* next;
}linklist;
linklist *link_create(linklist *L,int m)
{
int i=1;
linklist *head,*rear,*p;
head=(linklist *)malloc(sizeof(linklist));
L=head;
rear=head;
head->next=rear;
while(i<=m)
{
p=(linklist *)malloc(sizeof(linklist));
p->data=m-i+1;
p->next=head->next;
head->next=p;
i++;
}
return L;
}
linklist *joseph(linklist *L,int x)
{
int k=0;
linklist *head,*p,*q;
head=L;
p=head;
q=p->next;
while(head->next!=head)
{
for(k=1;k<x;k++)
{
p=q;
q=q->next;
if(q==head)
k--;
}
if(q==head)
{
p=q;
q=q->next;
}
printf("%3d",q->data);
x=q->data;
p->next=q->next;
free(q);
q=p->next;
}
return L;
}
print(linklist *L)
{
linklist *p,*head;
head=L;
p=head->next;
while(p!=head)
{
printf("%3d",p->data);
p=p->next;
}
printf("\n");
}
main()
{
int m,n;
linklist *L=0;
printf("please input the numbers and guess:\n");
scanf("%d,%d",&m,&n);
L=link_create(L,m);
print(L);
printf("joseph:\n");
joseph(L,n);
printf("\n");
}
板凳
wudden2008 [专家分:20] 发布于 2006-04-06 21:24:00
上面的斑竹的程序不能达到预期效果,我就针对他的修改了一下,有错的地方望指教:
#include <stdio.h>
#include <stdlib.h>
#include"iostream.h"
typedef struct node
{
int data;
struct node* next;
}linklist;
int array[100];
linklist *link_create(linklist *L,int m)
{
int i=1;
linklist *head,*p;
head=(linklist *)malloc(sizeof(linklist));
L=head;
head->data=NULL;
p=L;
p->next=head;
while(i<=m)
{
linklist *q=(linklist *)malloc(sizeof(node));
q->data=m-i+1;
q->next=p->next;
p->next=q;
p=q;
i++;
}
return L;
}
void print(linklist *L)
{
linklist *p,*head;
head=L;
p=head->next;
while(p!=head)
{
printf("%5d",p->data);
p=p->next;
}
printf("\n");
}
linklist *joseph(linklist *L,int n)
{
int k,i=0;
linklist *head,*p,*q,*r;
p=head=L;
q=head->next;
while(head->next!=head)
{
for(k=0;k<n;++k)
{
p=q;
q=q->next;
if(q==head)
--k;
}
if(q==head)
{
r=head->next;
L=head=head->next->next;
array[i]=r->data;
free(r);
q=q->next;
}
else
{
array[i]=q->data;
p->next=q->next;
free(q);
q=p->next;
}
++i;
print(L);
}
return L;
}
void main()
{
int m,n;
linklist *L=0;
printf("please input the numbers and guess:\n");
//scanf("%d,%d",&m,&n);
scanf("%d",&m);
scanf("%d",&n);
L=link_create(L,m);
print(L);
printf("joseph:\n");
joseph(L,n);
printf("\n");
printf("依次删除的对象是:\n");
for(int j=0;j<m;++j)
printf("%5d",array[j]);
cin.get();
cin.get();
}
3 楼
zhoul [专家分:260] 发布于 2006-04-22 10:11:00
简单哇`
#include<iostream.h>
struct person{
int num;
int code;
};
enum error_code {success,underflow,overflow,ranger_error};
typedef person list_entry;
typedef person node_entry;
template<class node_entry>
struct node{
node_entry entry;
node<node_entry> *next;
node();
node(node_entry,node<node_entry>*link=NULL);
};
template<class node_entry>
node<node_entry>::node()
{
next=NULL;
}
template<class node_entry>
node<node_entry>::node(node_entry item,node<node_entry> *add_on)
{
entry=item;
next=add_on;
}
template<class list_entry>
class list{
public:
list();
// int size()const;
// bool full()const;
// bool empty()const;
// void clear();
// void traverse(void(*visit)(list_entry &));
error_code retrieve(int position,list_entry &x)const;
error_code replace(int posotion,const list_entry &x);
error_code remove(int position,list_entry &x);
error_code insert(int position,const list_entry &x);
protected:
int count;
mutable int current_position;
node<list_entry>*head;
mutable node<list_entry>*current;
mutable int current_position0;
mutable node<list_entry>*current0;
void set_position(int position)const;
void set_position0(int position)const;
};
template<class list_entry>
list<list_entry>::list()
{
head=NULL;
count=0;
current_position0=1;
current_position=1;
current=head;
current0=head;
}
template<class list_entry>
void list<list_entry>::set_position0(int position)const
{
/*if(position<current_position){
current_position=0;
current=head;
}*/
current_position0=1;
for(;current_position0!=position;current_position0++)
current0=current0->next;
}
template<class list_entry>
void list<list_entry>::set_position(int position)const
{
if(position<current_position){
current_position=0;
current=head;
}
for(;current_position!=position;current_position++)
current=current->next;
current0=head;
}
template<class list_entry>
error_code list<list_entry>::insert(int position,const list_entry &x)
{
if(position<0||position>count)
return ranger_error;
node<list_entry>*new_node,*previous,*following;
if(position>0){
set_position(position-1);
previous=current;
following=previous->next;
}
else following=head;//possition==0;
new_node=new node<list_entry>(x,following);
if(new_node==NULL)return overflow;
if(position==0)
head=new_node;
else
previous->next=new_node;
if(count==position)
new_node->next=head;
count ++;
return success;
}
template<class list_entry>
error_code list<list_entry>::remove(int position,list_entry &x)
{
node<list_entry>* node,*previous,*following;
if(position<=0)
return ranger_error;
if(position==1)
set_position0(count);
else
set_position0(position-1);
previous=current0;
node=previous->next;
x=node->entry;
following=node->next;
previous->next=following;
current0=following;
delete node;
count--;
return success;
}
template<class list_entry>
error_code list<list_entry>::retrieve(int position,list_entry &x)const
{
node<list_entry>* node;
if(position<=0||position>count)
return ranger_error;
set_position(position);
node=current;
x=node->entry;
return success;
}
template<class list_entry>
error_code list<list_entry>::replace(int position,const list_entry &x)
{
node<list_entry>* node,*previous,*following;
if(position<=0||position>count)
return ranger_error;
set_position(position);
node=current;
node->entry=x;
return success;
}
main()
{
int m,j;
list <list_entry>circle;
list_entry x,y;
for(int i=1;i<=4;i++)
{
x.num=i;
cout<<"请输入"<<i<<"号的密码"<<endl;
cin>>x.code;
if(x.code<=0){
cout<<"输入错误,请再输入一遍"<<endl;break;
}
circle.insert(i-1,x);
}
cout<<"请输入第一个要出来的人号码"<<endl;
cin>>m;
circle.remove(m,x);
cout<<"从圈中出来的人的号码顺序为:"<<endl;
cout<<x.num<<",";
for(j=1;j<i-1;j++)
{
circle.remove(x.code,y);
cout<<y.num<<",";
x=y;
}
cout<<endl;
return 0;
}
我来回复