#include "malloc.h"
#include "stdio.h"
#define MAX 100
#define ERROR 0
#define OK 1
typedef int ElemType;
typedef struct LNode
{
int num;
ElemType data;
struct LNode *next;
}LNode;
LNode *head,*this,*new;
int str[MAX];
new_code(int a);
delete_code(int a,int b);
main()
{
int m,n,i;
printf("Enter the first code (m):");
scanf("%d",&m);
printf("\nEnter the people number (n):");
scanf("%d",&n);
getchar();
printf("\n");
new_code(n);
if(head!=NULL)
delete_code(n,m);
else
{
printf("list is empty\n");
exit(0);
}
for(i=0;i<n;i++)
printf("%3d",str[i]);
printf("\n");
}
new_code(int a)
{
int i=1;
char numstr[10];
new=(LNode *)malloc(sizeof(LNode));
if(new==NULL)
return ERROR;
if(head==NULL)
head=new;
this=head;
while(--a!=0)
{
this->num=i;
printf("enter the %d code(data):",i);
gets(numstr);
this->data=atoi(numstr);
new=(LNode *)malloc(sizeof(LNode));
this->next=new;
this=new;
i++;
}
this->num=i;
printf("enter the %d code(data):",i);
gets(numstr);
this->data=atoi(numstr);
this->next=head;
return OK;
}
delete_code(int a,int b)
{
int i;
int j=0;
LNode *p;
while((a--)!=1)
{
for(i=1;i<b;i++)
{
p=this;
this=this->next;
}
b=this->data;
str[j]=this->num;
p->next=this->next;
free(this);
j++;
}
str[j]=this->next->num;
return OK;
}
这个程序编译没问题,但运行不了,请教高手指点,谢谢
----------------解决方案--------------------------------------------------------
这个程序的错误太多了,vc++6.0 主要是用来编c++的,而你程序里面很多变量都是c++的关键字,所以
还是自己改吧,要是想看源程序,可以点击这个连接 zoujunjie.blog.com.cn[URL=http://zoujunjie.blog.com.cn]http://zoujunjie.blog.com.cn[/URL]
----------------解决方案--------------------------------------------------------
head=new;
很危险
----------------解决方案--------------------------------------------------------
去www.tiantiansoft.com上面看看那上面是怎么写的,好象比你这个简单啊。
----------------解决方案--------------------------------------------------------
#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFESIBLE -1
#define OVERFLOW -2
typedef int status;
typedef int BOOL;
typedef int ElemType;
typedef struct Node
{
ElemType pasword;
int number;
struct Node *next;
}LNode,*linklist;
linklist Initlinklist(void)
{
linklist p;
ElemType psw;
p=(linklist)malloc(sizeof(LNode));
if(!p)
{
printf("can't init linklist!\n");
exit(0);
}
printf("please enter the first pasword:\n");
scanf("%d",&psw);
p->number=1;
p->next=NULL;
p->pasword=psw;
return p;
}
void Insertlinklist(linklist p,ElemType a[],int n)
{
int i;
linklist q,h;
h=p;
for(i=1;i<n+1;i++)
{
q=(linklist)malloc(sizeof(LNode));
if(!q)
{
printf("can't malloc room\n");
system("pause");
exit(0);
}
q->next=NULL;
q->number=i;
q->pasword=a[i-1];
h->next=q;
h=q;
}
h->next=p;
}
void Dellinklist(linklist p,int m)
{
linklist q,h;
int i;
h=p;
while(h->next!=h)
{
for(i=0;i<m-1;i++)
h=h->next;
q=h->next;
m=q->pasword;
printf("%d, ",q->pasword);
h->next=q->next;
free(q);
}
printf("%d\n",h->pasword);
free(h);
}
int main(void)
{
linklist seqlik;
ElemType a[19];
int i,m;
seqlik=Initlinklist();
for(i=0;i<19;i++)
a[i]=i+1;
Insertlinklist(seqlik,a,19);
printf("please enter the m-ths for out queue:\n");
scanf("%d",&m);
Dellinklist(seqlik,m);
return 0;
}
----------------解决方案--------------------------------------------------------