#include<stdio.h>
#include<stdlib.h>
#define N 3
typedef int datatype;
typedef struct _node_
{
datatype data;
struct _node_ *next;
}linknode,*linklist;
linklist creat_empty_linklist()
{
linklist h;
h = (linklist)malloc(sizeof(linknode));
h->next = NULL;
return h;
}
int empty_linklist(linklist h)
{
return NULL == h->next;
}
int lenth_linklist(linklist h)
{
int len = 0;
linklist p;
p = h->next;
//p = h;
while(p != NULL)
{
len++;
p = p->next;
}
return len;
}
void visit_linklist(linklist h)
{
linklist p;
printf("The linklist is :");
p = h->next;
while(p != NULL)
{
printf("%d->",p->data);
p = p->next;
}
printf("\b\b \n");
return ;
}
int insert_linklist_1(linklist h,datatype x,int pos)
{
int i;
linklist p, q;
p = h;
if((pos < 0) || (pos > lenth_linklist(h))) return -1;
q = (linklist)malloc(sizeof(linknode));
q->data = x;
while(pos--) p = p->next;
q->next = p->next;
p->next = q;
return 0;
}
void insert_linklist_2(linklist h, datatype x)
{
linklist p, q;
p = h;
while((p->next != NULL) && (p->next->data <x)) p = p->next;
q = (linklist)malloc(sizeof(linknode));
q->data = x;
q->next = p->next;
p->next = q;
return ;
}
int delete_linklist_1(linklist h, int pos)
{
linklist p, q;
p = h;
if((pos < 0) || (pos >= lenth_linklist(h))) return -1;
while(pos--) p = p->next;
q = p->next;
p->next = q->next;
free(q);
return 0;
}
int delete_linklist_2(linklist h, datatype x)
{
linklist p, q;
q = h;
p = q->next;
while(p != NULL){
if(p->data == x){
q->next = p->next;
free(p);
p = q->next;
}
else{
q = q->next;
p = q->next;
}
}
}
void clear_linklist(linklist h)
{
linklist p, q;
q = h;
p = q->next;
if(!empty_linklist(h)){
while(p != NULL){
q->next = p->next;
free(p);
p = q->next;
}
}
}
void reverse_linklist(linklist h)
{
linklist p, q;
p = h->next;
h->next = NULL;
while(p != NULL){
q = p;
p = p->next;
q->next = h->next;
h->next = q;
}
}
int main()
{
int i;
linklist h, p;
h = creat_empty_linklist();
for(i=0; i<8; i++)
insert_linklist_1(h, i+1, i);
visit_linklist(h);
p = h->next;
while(h->next != NULL) h = h->next;
h->next = p;
h = p;
printf("MY Josephus : ");
for(i=0; i<10; i++){
printf("%d->", p->data);
p = p->next;
}
printf("\b\b \n");
p = h;
printf("My Josephus out is : ");
while(p != p->next)
{
for(i=0; i<N-2; i++)
{
p = p->next;
}
h = p->next;
p->next = h->next;
printf("%d->", h->data);
free(h);
p = p->next;
}
printf("%d\n", p->data);
return 0;
}
#include<stdlib.h>
#define N 3
typedef int datatype;
typedef struct _node_
{
datatype data;
struct _node_ *next;
}linknode,*linklist;
linklist creat_empty_linklist()
{
linklist h;
h = (linklist)malloc(sizeof(linknode));
h->next = NULL;
return h;
}
int empty_linklist(linklist h)
{
return NULL == h->next;
}
int lenth_linklist(linklist h)
{
int len = 0;
linklist p;
p = h->next;
//p = h;
while(p != NULL)
{
len++;
p = p->next;
}
return len;
}
void visit_linklist(linklist h)
{
linklist p;
printf("The linklist is :");
p = h->next;
while(p != NULL)
{
printf("%d->",p->data);
p = p->next;
}
printf("\b\b \n");
return ;
}
int insert_linklist_1(linklist h,datatype x,int pos)
{
int i;
linklist p, q;
p = h;
if((pos < 0) || (pos > lenth_linklist(h))) return -1;
q = (linklist)malloc(sizeof(linknode));
q->data = x;
while(pos--) p = p->next;
q->next = p->next;
p->next = q;
return 0;
}
void insert_linklist_2(linklist h, datatype x)
{
linklist p, q;
p = h;
while((p->next != NULL) && (p->next->data <x)) p = p->next;
q = (linklist)malloc(sizeof(linknode));
q->data = x;
q->next = p->next;
p->next = q;
return ;
}
int delete_linklist_1(linklist h, int pos)
{
linklist p, q;
p = h;
if((pos < 0) || (pos >= lenth_linklist(h))) return -1;
while(pos--) p = p->next;
q = p->next;
p->next = q->next;
free(q);
return 0;
}
int delete_linklist_2(linklist h, datatype x)
{
linklist p, q;
q = h;
p = q->next;
while(p != NULL){
if(p->data == x){
q->next = p->next;
free(p);
p = q->next;
}
else{
q = q->next;
p = q->next;
}
}
}
void clear_linklist(linklist h)
{
linklist p, q;
q = h;
p = q->next;
if(!empty_linklist(h)){
while(p != NULL){
q->next = p->next;
free(p);
p = q->next;
}
}
}
void reverse_linklist(linklist h)
{
linklist p, q;
p = h->next;
h->next = NULL;
while(p != NULL){
q = p;
p = p->next;
q->next = h->next;
h->next = q;
}
}
int main()
{
int i;
linklist h, p;
h = creat_empty_linklist();
for(i=0; i<8; i++)
insert_linklist_1(h, i+1, i);
visit_linklist(h);
p = h->next;
while(h->next != NULL) h = h->next;
h->next = p;
h = p;
printf("MY Josephus : ");
for(i=0; i<10; i++){
printf("%d->", p->data);
p = p->next;
}
printf("\b\b \n");
p = h;
printf("My Josephus out is : ");
while(p != p->next)
{
for(i=0; i<N-2; i++)
{
p = p->next;
}
h = p->next;
p->next = h->next;
printf("%d->", h->data);
free(h);
p = p->next;
}
printf("%d\n", p->data);
return 0;
}