日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

step3 . day3 数据结构之线性表 单项循环链表和双向循环链表

發(fā)布時間:2024/4/17 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 step3 . day3 数据结构之线性表 单项循环链表和双向循环链表 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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個人開始數(shù),數(shù)4個數(shù)出列
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 数据结构之线性表 单项循环链表和双向循环链表的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。