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

  • Post author:
  • Post category:其他


1. 使用单项循环链表解决约瑟夫问题:

#include <stdio.h>

#include <stdlib.h>


typedef struct looplist{


int date;

struct looplist *next;

}looplist;

//创建循环链表

looplist * looplist_creat(){


looplist *head = NULL;

head = (looplist*)malloc(sizeof(looplist));

head->date = 1;

head->next = head;

return head;

}

//插入节点

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在链表内,死循环风险

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. 双向循环链表

#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