双向链表

void listInit(List * lst)
{
//´´½¨Ò»¸öÍ·½áµã£¬¹¹³ÉÑ­»·½á¹¹
lst->_header = creatNode(0);
lst->_header->_next = lst->_header;
lst->_header->_prev = lst->_header;
}

Node* creatNode(Type data)
{
Node* node = (Node*)malloc(sizeof(Node));
node->_prev = node->_next = NULL;
node->_data = data;
return node;
}

void printList(List* lst)
{
Node* cur = lst->_header->_next;
while (cur != lst->_header)
{
printf("%d “, cur->_data);
cur = cur->_next;
}
printf(”\n");
}

void listPushBack(List * lst, Type data)
{
Node* node = creatNode(data);
Node* last = lst->_header->_prev;
last->_next = node;
node->_next = lst->_header;
lst->_header->_prev = node;
}

void listPopBack(List* lst)
{
if (lst ->_header->_next == lst->_header)
{
return;
}
Node* last = lst->_header->_prev;
Node* prev = last->_prev;
//prev _header
prev->_next = lst->_header;
lst->_header->_prev = prev;
free(last);
}

void listPushFront(List* lst, Type data)
{
Node* front = lst->_header;
Node* node = creatNode(data);

node->_next = front;
front->_prev = node;

node->_prev = lst->_header;
lst->_header->_next = node;

}

void listPopFront(List* lst)
{
if (lst->_header->_next == lst->_header)
return;
Node* front = lst->_header->_next;
Node* next = front->_next;
free(front);
lst->_header->_next = next;
next->_prev = lst->_header;
}

void listInsert(Node* pos, Type data)
{
Node* prev = pos->_prev;
Node* node = creatNode(data);
//prev node pos
prev->_next = node;
node->_prev = prev;
node->_next = pos;
pos->_prev = node;
}

void listErase(Node* pos, Type data)
{
if (pos->_next == pos)
{
return;
}
Node* prev = pos->_prev;
Node* next = pos->_next;
free(pos);
prev->_next = next;
next->_prev = prev;
}

void listDestroy(List* lst)
{
Node* cur = lst->_header->_next;
while(cur!=lst->_header)
{
Node* next = cur->_next;
free(cur);
cur = next;
}
free(lst->_header);
lst->_header = NULL;
}

你可能感兴趣的:(C)