step3 . day3 数据结构之线性表 单项循环链表和双向循环链表
1. 使用單項循環(huán)鏈表解決約瑟夫問題:
#include <stdio.h>
#include <stdlib.h>
typedef struct looplist{
 int date;
 struct looplist *next;
}looplist;
//創(chuàng)建循環(huán)鏈表
looplist * looplist_creat(){
 looplist *head = NULL;
 head = (looplist*)malloc(sizeof(looplist));
 head->date = 1;
 head->next = head;
 return head;
}
//插入節(jié)點
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;
 while(headtemp->date != start){
 headpre = headtemp;
 headtemp = headtemp->next;
 }//未判斷start在鏈表內,死循環(huán)風險
 while(headtemp!=headtemp->next){
 int counttemp = 1;
 while(counttemp != num){
 headpre = headtemp;
 headtemp = headtemp->next;
 counttemp++;
 }
 looplist_del(head,headpre,headtemp);
 headtemp = headtemp->next;
 }
 printf("%d\n",headtemp->date);
 free(headtemp);
 headtemp = NULL;
 head = NULL;
}
int main(int argc, const char *argv[])
{
 joseph(8,3,4);//8人,從第3個人開始數,數4個數出列
 return 0;
}
2. 雙向循環(huán)鏈表
?
#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;
}
轉載于:https://www.cnblogs.com/huiji12321/p/11233878.html
總結
以上是生活随笔為你收集整理的step3 . day3 数据结构之线性表 单项循环链表和双向循环链表的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: CORS--跨域资源共享
- 下一篇: step3 . day4 数据结构之线性
