ListBuyNode ————————————申请一个新节点并赋值
ListInit ———————————————计算链表的长度
ListDestory —————————————销毁链表
ListPushBack ————————————尾插
ListPushFront ————————————头插
ListPopBack —————————————尾删
ListPopFront ————————————头删
ListFindByVal ————————————按值查找链表
ListInsertAfter ————————————任意位置插入
ListErase ——————————————任意位置删除
ListPrint ——————————————打印链表
#pragma once#define _CRT_SECURE_NO_WARNINGS 1#include
#include
#include
#includetypedef int ListDataType;typedef struct ListNode
{struct ListNode* preview;struct ListNode* next;ListDataType val;
}ListNode;extern ListNode* ListBuyNode(ListDataType x);//创建一个节点
extern ListNode* ListInit();//链表初始化,创造表头
extern void DListDestory(ListNode** pphead);//销毁
extern void ListPrint(ListNode* phead);//打印
extern void ListPushBack(ListNode* phead, ListDataType x);//尾插
extern void ListPopBack(ListNode* phead);//尾删
extern void ListPushFront(ListNode* phead, ListDataType x);//头插
extern void ListPopFront(ListNode* phead);//头删
extern ListNode* ListFindVal(ListNode* phead, ListDataType x);//按值查找
extern void ListInsertAfter(ListNode* pos, ListDataType x);//在任意位置插入
extern void ListErase(ListNode* pos);//在任意位置删除
#include "DList.h"ListNode* ListInit()
{ListNode* phead = ListBuyNode(0);phead->preview = phead;phead->next = phead;return phead;
}void DListDestory(ListNode** pphead)
{ListNode* cur = (*pphead)->next;while (cur != (*pphead)){cur = cur->next;free(cur->preview);}free(cur);*pphead = NULL;
}ListNode* ListBuyNode(ListDataType x)
{ListNode* newnode = malloc(sizeof(ListNode));assert(newnode);newnode->val = x;newnode->preview = NULL;newnode->next = NULL;return newnode;
}void ListPushBack(ListNode* phead, ListDataType x)
{assert(phead);ListNode* newnode = ListBuyNode(x);assert(newnode);phead->preview->next = newnode;newnode->preview = phead->preview;newnode->next = phead;phead->preview = newnode;
}bool ListEmpty(ListNode* phead)
{return (phead->next == phead) ? true : false;
}
void ListPrint(ListNode* phead)
{assert(phead);if (ListEmpty(phead))printf("List is empty!");for (ListNode* cur = phead->next; cur != phead; cur = cur->next)printf("%d ", cur->val);printf("\n");
}void ListPopBack(ListNode* phead)
{assert(phead && phead->next != phead);ListNode* tail = phead->preview;phead->preview = tail->preview;tail->preview->next = phead;free(tail);
}void ListPushFront(ListNode* phead, ListDataType x)
{assert(phead);ListNode* newnode = ListBuyNode(x);phead->next->preview = newnode;newnode->next = phead->next;phead->next = newnode;newnode->preview = phead;
}void ListPopFront(ListNode* phead)
{assert(phead && phead->next != phead);ListNode* cur = phead->next;phead->next = cur->next;cur->next->preview = phead;free(cur);
}ListNode* ListFindVal(ListNode* phead, ListDataType x)
{assert(phead);ListNode* cur = phead->next;while (cur != phead){if (cur->val == x)return cur;cur = cur->next;}return NULL;
}void ListInsertAfter(ListNode* pos, ListDataType x)
{assert(pos);ListNode* newnode = ListBuyNode(x);newnode->preview = pos;newnode->next = pos->next;pos->next->preview = newnode;pos->next = newnode;
}void ListErase(ListNode* pos)
{assert(pos && pos->next != pos);ListNode* prev = pos->preview;ListNode* next = pos->next;prev->next = next;next->preview = prev;free(pos);
}
#include "DList.h"void test1()
{ListNode* pList = ListInit();ListPushFront(pList, 4);ListPushFront(pList, 3);ListPushFront(pList, 2);ListPushFront(pList, 1);ListPrint(pList);DListDestory(&pList);
}void test2()
{ListNode* pList = ListInit();ListPushBack(pList, 5);ListPushFront(pList, 4);ListPushFront(pList, 3);ListPushFront(pList, 2);ListPushFront(pList, 1);ListPrint(pList);ListPopBack(pList);ListPopBack(pList);ListPopBack(pList);ListPopBack(pList);ListPopBack(pList);ListPrint(pList);
}void test3()
{ListNode* pList = ListInit();ListPushBack(pList, 5);ListPushFront(pList, 4);ListPushFront(pList, 3);ListPushFront(pList, 2);ListPushFront(pList, 1);ListPrint(pList);ListPopFront(pList);ListPopFront(pList);ListPopFront(pList);ListPopFront(pList);ListPrint(pList);ListPopFront(pList);ListPrint(pList);
}void test4()
{ListNode* pList = ListInit();ListInsertAfter(pList, 5);ListInsertAfter(pList, 4);ListInsertAfter(pList, 1);ListInsertAfter(pList->next, 3);ListInsertAfter(pList->next, 2);ListPrint(pList);ListErase(pList->next->next->next->next);ListNode* cur = ListFindVal(pList, 4);if (cur != NULL)ListErase(cur);elseprintf("The node with this value does not exist!\n");ListPrint(pList);ListErase(pList->next);ListErase(pList->next);ListErase(pList->next);ListPrint(pList);
}void main()
{printf("---------------------test1----------------------\n");test1();printf("---------------------test2----------------------\n");test2();printf("---------------------test3----------------------\n");test3();printf("---------------------test4----------------------\n");test4();
}
上一篇:xxe盲注