单向链表循环节点创建、头插、尾插、头删、尾删、遍历、约瑟夫环
main.c
#include"head.h"
int main(int argc, const char *argv[])
{
Linklist L=NULL;
int n,m;
datatype e;
printf("please enter n:");
scanf("%d",&n);
for(int i=0;i<n;i++){
printf("pilease enter e");
scanf("%d",&e);
L=insert_head(e,L);}
printf("weichageshu m:");
scanf("%d",&m);
for(int i=0;i<m;i++){
printf("pilease enter e");
scanf("%d",&e);
L=insert_rear(e,L);
}
L=delete_head(L);
L=delete_rear(L);
output(L);
Linklist L=NULL;
int n;
printf("please people n:");
scanf("%d",&n);
for(int i=0;i<n;i++){
L=insert_rear(i+1,L);}
int m;
printf("please huanshu");
scanf("%d",&m);
yuesefu(n,m,L);
return 0;
test.c
#include"head.h"
Linklist create_node(){
Linklist node=(Linklist)malloc(sizeof(struct Node));
if(node==NULL)
return NULL;
node->data=0;
node->next=node;
return node;
}
Linklist insert_head(datatype e,Linklist L){
Linklist node=create_node();
if(node==NULL)
return NULL;
if(NULL==L){
L=node;
node->data=e;}
else{
node->next=L->next;
L->next=node;
node->data=L->data;
L->data=e;
}
return L;
}
int output(Linklist L){
if(L==NULL)
return -1;
Linklist p=L;
do{
printf("%d ",p->data);
p=p->next;
}while(p!=L);
return 0;
}
Linklist insert_rear(datatype e,Linklist L){
Linklist s=create_node();
s->data=e;
if(L==NULL)
L=s;
else{
Linklist rear=L;
while(rear->next!=L)
rear=rear->next;
rear->next=s;
s->next=L;}
return L;
}
Linklist delete_head(Linklist L){
if(L==NULL)
return L;
if(L->next==L){
free(L);
L=NULL;
}
else{
Linklist q=L->next;
L->data=q->data;
L->next=q->next;
free(q);
q=NULL;
}
return L;
}
Linklist delete_rear(Linklist L){
if(NULL==L)
return L;
else if(L->next==L)
{
free(L);
L=NULL;
}
else{
Linklist second=L;
while(second->next->next!=L)
{
second=second->next;
}
free(second->next);
second->next=L;
return L;
}}
void yuesefu(int n,int m,Linklist L){
Linklist p=L;
for(int i=0;i<n;i++){
for(int j=1;j<m-1;j++){
p=p->next;
}
Linklist q=p->next;
printf("%d ",q->data);
p->next=q->next;
free(q);
q=NULL;
p=p->next;
}
}
head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef int datatype;
typedef struct Node{
datatype data;
struct Node *next;
}*Linklist;
Linklist insert_head(datatype e,Linklist L);
int output();
Linklist insert_rear();
Linklist delete_head();
Linklist delete_rear();
void yuesefu();
#endif