step3. Table linear single bidirectional circular list and the circular linked list data structure day3

1. Use single circular list Joseph solve the problem:

#include <stdio.h>
#include <stdlib.h>


typedef struct looplist{
int date;
struct looplist *next;
}looplist;

// create a circular list
looplist looplist_creat * () {
looplist head * = NULL;
head = (looplist *) the malloc (the sizeof (looplist));

head->date = 1;
head->next = head;
return head;
}

// insert node
void looplist_insert (looplist * head, int value) {

looplist *temp = NULL;
temp = (looplist*)malloc(sizeof(looplist));
temp->date = value;

temp->next = head->next;
head->next = temp;
}

//遍历1
void looplist_show(looplist *head){
looplist* p = NULL;
p = head;

do{
printf("%d ",p->date);
p = p->next;
}while(p != head);
puts("");
}

void looplist_del(looplist* head,looplist* headpre,looplist* headtemp){

printf("%d ",headtemp->date);

looplist *p =NULL;
p = headtemp;
headpre->next = headtemp->next;
printf("\n");
free(p);
p=NULL;

}
looplist * joseph_creat(int person){

int i;
looplist * joseph = NULL;
joseph = looplist_creat();
for(i = person;i >= 2;i--){
looplist_insert(joseph,i);
}
return joseph;
}
void joseph(int person,int start,int num){

looplist * head = NULL;
looplist * headpre = NULL;
head = joseph_creat(person);
looplist_show(head);

looplist* headtemp = head;
while(headtemp->next != head){
headtemp = headtemp->next;
}
headpre = headtemp;

headtemp = head;

the while (headtemp-> DATE = start!) {
headpre = headtemp;
headtemp = headtemp-> Next;
} // start is not determined in the chain, the risk of death cycle
while (headtemp = headtemp-> next! ) {

int counttemp = 1;
while(counttemp != num){
headpre = headtemp;
headtemp = headtemp->next;
counttemp++;
}

looplist_del(head,headpre,headtemp);
headtemp = headtemp->next;
}

the printf ( "% D \ n-", headtemp-> DATE);
Free (headtemp);
headtemp = NULL;
head = NULL;
}
int main (int argc, char const * the argv [])
{
Joseph (8,3,4 ); // 8, beginning from the third number of individuals, the number of the column number 4
return 0;
}

2. The two-way circular list

 

#include <stdio.h>
#include <stdlib.h>

typedef struct node{
int date;
struct node * priv;
struct node * next;
}doublelist;

doublelist * doublelist_creat(){
doublelist * head = NULL;
head = (doublelist*)malloc(sizeof(doublelist));

head->priv = head;
head->next = head;

return head;
}

void doublelist_head_insert(doublelist * head,int value){
doublelist * temp = doublelist_creat();
temp->date = value;

temp->next = head->next;
head->next->priv = temp;

temp->priv = head;
head->next = temp;
}

void doublelist_show(doublelist * head){

doublelist * temp = head->next;
while(temp != head){
printf("%d ",temp->date);
temp = temp->next;
}
printf("\n");
}

int doublelist_is_empty(doublelist * head){
return head->next == head ? 1 : 0;
}

int doublelist_head_del(doublelist * head){
if(doublelist_is_empty(head)){
printf("doublelist is empty\n");
return -1;
}
int value = head->next->date;

doublelist * temp = head->next;
head->next = temp->next;
temp->next->priv = head;

free(temp);
temp = NULL;
return value;
}


int main(int argc, const char *argv[])
{
doublelist * head = NULL;
head = doublelist_creat();

doublelist_head_insert(head,1);
doublelist_head_insert(head,2);
doublelist_head_insert(head,3);
doublelist_head_insert(head,4);
doublelist_head_insert(head,5);

doublelist_show(head);

printf("%d\n",doublelist_head_del(head));
doublelist_show(head);

printf("%d\n",doublelist_head_del(head));
doublelist_show(head);

printf("%d\n",doublelist_head_del(head));
doublelist_show(head);

printf("%d\n",doublelist_head_del(head));
doublelist_show(head);

printf("%d\n",doublelist_head_del(head));
doublelist_show(head);
printf("%d\n",doublelist_head_del(head));
doublelist_show(head);
return 0;
}

Guess you like

Origin www.cnblogs.com/huiji12321/p/11233878.html