#ifndef _LIST_H
#define _LIST_H
#include<stdio.h>
#include<stdlib.h>
typedef int LTDataType;
typedef struct ListNode
{
LTDataType _data;
struct ListNode* _next;
struct ListNode* _prev;
}ListNode;
typedef struct List
{
ListNode* _head;
}List;
void ListInit(List* plist);
void ListDestory(List* plist);
void ListPushBack(List* plist, LTDataType x);
void ListPopBack(List* plist);
void ListPushFront(List* plist, LTDataType x);
void ListPopFront(List* plist);
ListNode* ListFind(List* plist, LTDataType x);
// 在pos的前面进行插入
void ListInsert(ListNode* pos, LTDataType x);
// 删除pos位置的节点
void ListErase(ListNode* pos);
void ListRemove(List* plist, LTDataType x);
void ListPrint(List* plist);
#endif //_LIST_H
#include "List.h"
void ListInit(List* plist)
{
plist->_head = (ListNode *)malloc(sizeof(ListNode));
memset(&plist->_head->_data, 0, sizeof(LTDataType));
plist->_head->_next = plist->_head->_prev = plist->_head;
}
//尾插
void ListPushBack(List* plist, LTDataType x)
{
ListNode *cur = (ListNode *)malloc(sizeof(ListNode));
cur->_data = x;
plist->_head->_prev->_next = cur;
cur->_prev = plist->_head->_prev;
plist->_head->_prev=cur;
cur->_next = plist->_head;
}
//删尾
void ListPopBack(List* plist)
{
ListNode *tmp = plist->_head->_prev;
if (tmp != plist->_head)
{
tmp->_prev->_next = tmp->_next;
tmp->_next->_prev = tmp->_prev;
}
}
void ListPushFront(List* plist, LTDataType x)
{
ListNode *cur = (ListNode *)malloc(sizeof(ListNode));
cur->_data = x;
plist->_head->_next->_prev = cur;
cur->_next = plist->_head->_next;
plist->_head->_next = cur;
cur->_prev = plist->_head;
}
void ListPopFront(List* plist)
{
ListNode *tmp = plist->_head->_next;
if (tmp != plist->_head)
{
tmp->_prev->_next = tmp->_next;
tmp->_next->_prev = tmp->_prev;
}
}
void ListPrint(List* plist)
{
ListNode *cur;
printf("head<-->");
for (cur = plist->_head->_next; cur != plist->_head; cur = cur->_next)
{
printf("%d<-->", cur->_data);
}
printf("head\n");
}
void ListDestory(List* plist)
{
ListNode*tmp = plist->_head->_next;
while (tmp != plist->_head)
{
tmp->_prev->_next = tmp->_next;
tmp->_next->_prev = tmp->_prev;
free(tmp);
tmp = plist->_head->_next;
}
free(plist->_head);
plist->_head = NULL;
}
#include"List.h"
int main()
{
List list;
ListInit(&list);
ListPushFront(&list, 7);
ListPushFront(&list, 6);
ListPushFront(&list, 5);
ListPushFront(&list, 4);
ListPrint(&list);
ListPopBack(&list);
ListPrint(&list);
ListPopFront(&list);
ListPrint(&list);
ListDestory(&list);
system("pause");
return 0;
}