01main.c文件:
#include <stdio.h>
#include "./03doublelist.h"
int main(int argc, const char *argv[])
{
doublelinkList* head = createdoublelinklist();
InsertDoublelinklistByhead(head,111);
InsertDoublelinklistByhead(head,222);
InsertDoublelinklistByhead(head,333);
InsertDoublelinklistByhead(head,444);
InsertDoublelinklistByhead(head,555);
InsertDoublelinklistByhead(head,666);
showDoublelinklist(head);
InsertDoublelinklistBytail(head,100);
InsertDoublelinklistBytail(head,200);
InsertDoublelinklistBytail(head,300);
InsertDoublelinklistBytail(head,400);
InsertDoublelinklistBytail(head,500);
InsertDoublelinklistBytail(head,600);
showDoublelinklist(head);
InsertDoublelinklistBypos(head,1,99999);
showDoublelinklist(head);
deleteDoublelinklistBypos(head,2);
showDoublelinklist(head);
return 0;
}
02doublelist.c文件:
#include <stdio.h>
#include <stdlib.h>
#include "./03doublelist.h"
/*
* function: 创建双向链表
* @param [ in]
* @param [out]
* @return
*/
doublelinkList* createdoublelinklist()
{
doublelinkList* head = (doublelinkList*)malloc(sizeof(doublelinkList));
head->next = NULL;
head->prev = NULL;
head->text.len = 0;
return head;
}
/*
* function: 判断链表是否为空
* @param [ in]
* @param [out]
* @return
*/
int isEmpty(doublelinkList* head)
{
return head->next == NULL?1:0;
}
/*
* function: 头插法插入双向链表
* @param [ in]
* @param [out]
* @return
*/
void InsertDoublelinklistByhead(doublelinkList* head,int num)
{
doublelinkList* temp = (doublelinkList*)malloc(sizeof(doublelinkList));
temp->text.data = num;
temp->next = NULL;
temp->prev = NULL;
doublelinkList* p = head;
if(isEmpty(head) == 1)
{
temp->next = head->next;
head->next = temp;
temp->prev = head;
}
else
{
temp->next = head->next;
head->next = temp;
temp->next->prev = temp;
temp->prev = head;
}
}
/*
* function: 尾插法插入双向链表
* @param [ in]
* @param [out]
* @return
*/
void InsertDoublelinklistBytail(doublelinkList* head,int num)
{
doublelinkList* temp = (doublelinkList*)malloc(sizeof(doublelinkList));
doublelinkList* p = head;
temp->text.data = num;
temp->next = NULL;
temp->prev = NULL;
while(p->next != NULL)
{
p = p->next;
}
temp->next = NULL;
p->next = temp;
temp->prev = p;
}
/*
* function: 按位置插入双向链表
* @param [ in]
* @param [out]
* @return
*/
void InsertDoublelinklistBypos(doublelinkList* head,int pos,int num)
{
doublelinkList* temp = (doublelinkList*)malloc(sizeof(doublelinkList));
doublelinkList* p = head;
temp->text.data = num;
for(int i = 0;i<pos-1;i++)
{
p = p->next;
}
temp->next = p->next;
p->next = temp;
temp->next->prev = temp;
temp->prev = p;
}
/*
* function: 按位置删除
* @param [ in]
* @param [out]
* @return
*/
void deleteDoublelinklistBypos(doublelinkList*head,int pos)
{
doublelinkList* temp = (doublelinkList*)malloc(sizeof(doublelinkList));
doublelinkList* p = head;
for(int i = 0;i<pos;i++)
{
p = p->next;
}
p->prev->next = p->next;
p->next->prev = p->prev;
free(p);
}
/*
* function: 双向链表遍历
* @param [ in]
* @param [out]
* @return
*/
void showDoublelinklist(doublelinkList* head)
{
doublelinkList* p = head;
while(p->next != NULL)
{
p = p->next;
printf("%d\n",p->text.data);
}
printf("-------------------\n");
}
03doublelist.h文件:
#ifndef __DOUBLE__
#define __DOUBLE__
union msg{
int data;
int len;
};
typedef struct node
{
union msg text;
struct node* next;
struct node* prev;
}doublelinkList;
doublelinkList* createdoublelinklist();
void InsertDoublelinklistByhead(doublelinkList* head,int num);
void showDoublelinklist(doublelinkList* head);
void InsertDoublelinklistBytail(doublelinkList* head,int num);
void InsertDoublelinklistBypos(doublelinkList* head,int pos,int num);
void deleteDoublelinklistBypos(doublelinkList*head,int pos);
#endif
关于约瑟夫环,先创建