#include <stdio.h>
#include <stdlib.h>
typedef struct Node{
struct Node *next;
}STNode_def;
typedef struct CicLinklist{
struct Node head;
int size;
}STCicLinklist_def;
#define SUCCESS 0
#define FAILURE -1
STCicLinklist_def* ciclinklist_init()
{
STCicLinklist_def* list_ptr = (STCicLinklist_def*)malloc(sizeof(STCicLinklist_def));
list_ptr->head.next = &(list_ptr->head);
list_ptr->size = 0;
return list_ptr;
}
int ciclinklist_push_back(STCicLinklist_def* list_ptr, STNode_def* data)
{
if (list_ptr == NULL || data == NULL)
{
return FAILURE;
}
STNode_def* p = &(list_ptr->head);
while (p->next != &(list_ptr->head))
{
p = p->next;
}
p->next = data;
data->next = &(list_ptr->head);
list_ptr->size++;
return SUCCESS;
}
int ciclinklist_push_front(STCicLinklist_def* list_ptr, STNode_def* data)
{
if (list_ptr == NULL || data == NULL)
{
return FAILURE;
}
data->next = list_ptr->head.next;
list_ptr->head.next = data;
list_ptr->size++;
return SUCCESS;
}
int ciclinklist_push_bypos(STCicLinklist_def* list_ptr, int pos, STNode_def* data)
{
if (list_ptr == NULL || data == NULL)
{
return FAILURE;
}
if (pos < 0 || pos >= list_ptr->size)
{
pos = list_ptr->size;
printf("pos=%d\n", pos);
}
int i = 0;
STNode_def* p = &(list_ptr->head);
while (p->next != &(list_ptr->head) && i<pos)
{
p = p->next;
i++;
}
data->next = p->next;
p->next = data;
list_ptr->size++;
return SUCCESS;
}
int ciclinklist_pop_back(STCicLinklist_def* list_ptr)
{
if (list_ptr == NULL)
{
return FAILURE;
}
if (list_ptr->size == 0)
{
return FAILURE;
}
int i = 0;
STNode_def* p = &(list_ptr->head);
while (p->next != &(list_ptr->head) && i < list_ptr->size-1)
{
p = p->next;
i++;
}
p->next = &(list_ptr->head);
list_ptr->size--;
return SUCCESS;
}
int ciclinklist_pop_front(STCicLinklist_def* list_ptr)
{
if (list_ptr == NULL)
{
return FAILURE;
}
if (list_ptr->size == 0)
{
return FAILURE;
}
list_ptr->head.next = list_ptr->head.next->next;
list_ptr->size--;
return SUCCESS;
}
int ciclinklist_pop_bypos(STCicLinklist_def* list_ptr, int pos)
{
if (list_ptr == NULL)
{
return FAILURE;
}
if (pos < 0 || pos > list_ptr->size -1 || list_ptr->size == 0)
{
return FAILURE;
}
int i = 0;
STNode_def* p = &(list_ptr->head);
while (p->next != &(list_ptr->head) && i < pos)
{
p = p->next;
i++;
}
p->next = p->next->next;
list_ptr->size--;
return SUCCESS;
}
int ciclinklist_pop_byvalue(STCicLinklist_def* list_ptr, STNode_def* value, int (*CICLINKLIST_COMPARE)(STNode_def*, STNode_def*))
{
if (list_ptr == NULL)
{
return FAILURE;
}
if (list_ptr->size == 0)
{
return FAILURE;
}
STNode_def* p = &(list_ptr->head);
while (p->next != &(list_ptr->head))
{
if (CICLINKLIST_COMPARE(p->next, value))
{
p->next = p->next->next;
list_ptr->size--;
break;
}
p = p->next;
}
return SUCCESS;
}
void ciclinklist_print(STCicLinklist_def* list_ptr, void (*PRINTLIST)(STNode_def*))
{
if (list_ptr == NULL || PRINTLIST == NULL)
{
return;
}
STNode_def* p = &(list_ptr->head);
while (p->next != &(list_ptr->head))
{
p = p->next;
PRINTLIST(p);
}
}
void ciclinklist_ysfprint(STCicLinklist_def* list_ptr, void (*PRINTLIST)(STNode_def*), int (*CICLINKLIST_COMPARE)(STNode_def*, STNode_def*))
{
if (list_ptr == NULL || PRINTLIST == NULL)
{
return;
}
int cnt = 1;
STNode_def* p = list_ptr->head.next;
while (list_ptr->size > 1)
{
if (list_ptr->size == 1)
{
break;
}
if (p == &(list_ptr->head))
{
p = list_ptr->head.next;
}
if (cnt == 2)
{
STNode_def* pnext = p->next;
if (pnext == &(list_ptr->head))
{
pnext = list_ptr->head.next;
}
PRINTLIST(pnext);
ciclinklist_pop_byvalue(list_ptr, pnext, CICLINKLIST_COMPARE);
cnt = 0;
}
p = p->next;
cnt++;
}
}
void list_destory(STCicLinklist_def* list_ptr)
{
if (list_ptr == NULL)
{
return;
}
free(list_ptr);
list_ptr = NULL;
}
typedef struct Student{
STNode_def node;
int num;
}STStudent_def;
void student_info_print(STStudent_def* node)
{
printf("num=%d\n", node->num);
}
int student_compare(STNode_def* var1, STNode_def* var2)
{
STStudent_def* p1 = (STStudent_def*)var1;
STStudent_def* p2 = (STStudent_def*)var2;
if (p1->num == p2->num)
{
printf("num[%d] delete\n", p2->num);
return 1;
}
return 0;
}
int main()
{
STStudent_def p1;
STStudent_def p2;
STStudent_def p3;
STStudent_def p4;
STStudent_def p5;
STStudent_def p6;
STStudent_def p7;
STStudent_def p8;
p1.num = 1;
p2.num = 2;
p3.num = 3;
p4.num = 4;
p5.num = 5;
p6.num = 6;
p7.num = 7;
p8.num = 8;
STCicLinklist_def * list_ptr = ciclinklist_init();
ciclinklist_push_back(list_ptr, &p1);
ciclinklist_push_back(list_ptr, &p2);
ciclinklist_push_back(list_ptr, &p3);
ciclinklist_push_back(list_ptr, &p4);
ciclinklist_push_back(list_ptr, &p5);
ciclinklist_push_back(list_ptr, &p6);
ciclinklist_push_back(list_ptr, &p7);
ciclinklist_push_back(list_ptr, &p8);
ciclinklist_print(list_ptr, student_info_print);
printf("size:%d\n", list_ptr->size);
ciclinklist_ysfprint(list_ptr, student_info_print, student_compare);
ciclinklist_print(list_ptr, student_info_print);
printf("size:%d\n", list_ptr->size);
list_destory(list_ptr);
return 0;
}