无头单向不循环链表
#include <stdio.h>
#include <stdlib.h>
typedef int SLTDateType;
typedef struct SListNode
{
SLTDateType data;
struct SListNode* next;
}SListNode;
SListNode* BuySListNode(SLTDateType x)
{
SListNode* node = (SListNode*)malloc(sizeof(SListNode));
node->data = x;
node->next = NULL;
return node;
}
void SListPrint(SListNode* plist)
{
SListNode* cur = plist;
while (cur) {
printf("%d ", cur->data);
cur = cur->next;
}
printf("\n");
}
void SListPushBack(SListNode** pplist, SLTDateType x)
{
SListNode* node = (SListNode*)malloc(sizeof(SListNode));
node->data = x;
SListNode* cur = *pplist;
if (*pplist == NULL) (*pplist) = node;
else {
while (cur->next) {
cur = cur->next;
}
cur->next = node;
cur = cur->next;
cur->next = NULL;
}
}
void SListPushFront(SListNode** pplist, SLTDateType x)
{
SListNode* node = (SListNode*)malloc(sizeof(SListNode));
node->data = x;
if (*pplist == NULL) *pplist = node;
else {
node->next = *pplist;
*pplist = node;
}
}
void SListPopBack(SListNode** pplist)
{
SListNode* cur = *pplist;
SListNode* prev = NULL;
if (cur == NULL) return;
else {
if (cur->next == NULL) {
free(*pplist);
*pplist = NULL;
}
else {
while (cur->next) {
prev = cur;
cur = cur->next;
}
free(cur);
prev->next = NULL;
}
}
}
void SListPopFront(SListNode** pplist)
{
if (*pplist == NULL) return;
SListNode* next = (*pplist)->next;
free(*pplist);
*pplist = next;
}
SListNode* SListFind(SListNode* plist, SLTDateType x)
{
SListNode* cur = plist;
while (cur) {
if (cur->data == x) return cur;
cur = cur->next;
}
return NULL;
}
void SListInsertAfter(SListNode* pos, SLTDateType x)
{
if (pos != NULL) {
SListNode* node = (SListNode*)malloc(sizeof(SListNode));
node->data = x;
node->next = pos->next;
pos->next = node;
}
}
void SListEraseAfter(SListNode* pos)
{
if (pos != NULL && pos->next != NULL) {
SListNode* next = pos->next->next;
free(pos->next);
pos->next = next;
}
}
void SListDestory(SListNode** plist)
{
while (*plist) {
if ((*plist)->next == NULL) {
free(*plist);
*plist = NULL;
}
else {
SListNode* next = (*plist)->next;
free(*plist);
*plist = NULL;
*plist = next;
next = (*plist)->next;
}
}
}
int main()
{
SListNode* head = BuySListNode(5);
SListPrint(head);
SListPushBack(&head, 6);
SListPushBack(&head, 7);
SListPushBack(&head, 8);
SListPrint(head);
SListPushFront(&head, 4);
SListPushFront(&head, 3);
SListPushFront(&head, 2);
SListPrint(head);
SListPopBack(&head);
SListPopBack(&head);
SListPrint(head);
SListPopFront(&head);
SListPopFront(&head);
SListPrint(head);
SListNode* find = SListFind(head, 5);
SListInsertAfter(find, 10);
SListPrint(head);
find = SListFind(head, 5);
SListEraseAfter(find);
SListPrint(head);
SListEraseAfter(find);
SListPrint(head);
SListEraseAfter(find);
SListPrint(head);
SListEraseAfter(find);
SListPrint(head);
SListDestory(&head);
SListPrint(head);
system("pause");
return 0;
}
带头双向循环链表
typedef int LTDataType;
typedef struct ListNode
{
LTDataType _data;
struct ListNode* _next;
struct ListNode* _prev;
}ListNode;
ListNode* ListCreate()
{
ListNode* head = (ListNode*)malloc(sizeof(ListNode));
head->_next = head;
head->_prev = head;
return head;
}
void ListDestory(ListNode* pHead)
{
ListNode* cur = pHead->_next;
while (cur != pHead) {
ListNode* next = cur->_next;
free(cur);
cur = next;
}
pHead->_next = pHead;
pHead->_prev = pHead;
}
void ListPrint(ListNode* pHead)
{
ListNode* cur = pHead->_next;
while (cur != pHead) {
printf("%d ", cur->_data);
cur = cur->_next;
}
putchar('\n');
}
void ListPushBack(ListNode* pHead, LTDataType x)
{
ListNode* node = (ListNode*)malloc(sizeof(ListNode));
node->_data = x;
ListNode* prev = pHead->_prev;
node->_prev = prev;
node->_next = pHead;
prev->_next = node;
pHead->_prev = node;
}
void ListPopBack(ListNode* pHead)
{
if (pHead->_prev != pHead) {
ListNode* cur = pHead->_prev;
cur->_prev->_next = pHead;
pHead->_prev = cur->_prev;
free(cur);
cur = NULL;
}
}
void ListPushFront(ListNode* pHead, LTDataType x)
{
ListNode* node = (ListNode*)malloc(sizeof(ListNode));
node->_data = x;
ListNode* next = pHead->_next;
node->_prev = pHead;
node->_next = next;
next->_prev = node;
pHead->_next = node;
}
void ListPopFront(ListNode* pHead)
{
if (pHead->_next != pHead) {
ListNode* cur = pHead->_next;
pHead->_next = cur->_next;
cur->_next->_prev = pHead;
free(cur);
cur = NULL;
}
}
ListNode* ListFind(ListNode* pHead, LTDataType x)
{
ListNode* cur = pHead->_next;
while (cur != pHead) {
if (cur->_data == x)
return cur;
cur = cur->_next;
}
return NULL;
}
void ListInsert(ListNode* pos, LTDataType x)
{
ListNode* node = (ListNode*)malloc(sizeof(ListNode));
ListNode* prev = pos->_prev;
node->_data = x;
node->_next = pos;
node->_prev = prev;
prev->_next = node;
pos->_prev = node;
}
void ListErase(ListNode* pos)
{
if (pos->_next != pos) {
ListNode* prev = pos->_prev;
ListNode* next = pos->_next;
prev->_next = next;
next->_prev = prev;
free(pos);
pos = NULL;
}
}