一、双向链表的实现
1.结点定义和头指针的创建
typedef int LTDataType;
typedef struct ListNode
{
LTDataType data;
struct ListNode * next;
struct ListNode * prev;
} LTNode;
2.初始化实现
LTNode* ListInit ( )
{
LTNode* guard = ( LTNode* ) malloc ( sizeof ( LTNode) ) ;
if ( guard == NULL )
{
perror ( "malloc fail" ) ;
exit ( - 1 ) ;
}
guard-> next = guard;
guard-> prev = guard;
return guard;
}
3.打印接口实现
void ListPrint ( LTNode* phead)
{
assert ( phead) ;
printf ( "phead<=>" ) ;
LTNode* cur = phead-> next;
while ( cur != phead)
{
printf ( "%d<=>" , cur-> data) ;
cur = cur-> next;
}
printf ( "\n" ) ;
}
4.开辟新的节点
LTNode* BuyLTNode ( LTDataType x)
{
LTNode* node = ( LTNode* ) malloc ( sizeof ( LTNode) ) ;
if ( node == NULL )
{
perror ( "malloc fail" ) ;
exit ( - 1 ) ;
}
node-> data = x;
node-> next = NULL ;
node-> prev = NULL ;
return node;
}
5.头插接口
void ListPushFront ( LTNode* phead, LTDataType x)
{
assert ( phead) ;
LTNode* newnode = BuyLTNode ( x) ;
LTNode* first = phead-> next;
newnode-> next = first;
first-> prev = newnode;
phead-> next = newnode;
newnode-> prev = phead;
}
6.头删接口
void ListPopFront ( LTNode* phead)
{
assert ( phead) ;
assert ( ! ListEmpty ( phead) ) ;
LTNode* first = phead-> next;
LTNode* second = first-> next;
phead-> next = second;
second-> prev = phead;
free ( first) ;
first = NULL ;
}
7.尾插接口
void ListPushBack ( LTNode* phead, LTDataType x)
{
ListInsert ( phead, x) ;
}
8.尾删接口
void ListPopBack ( LTNode* phead)
{
ListErase ( phead-> prev) ;
}
9.查找接口实现
LTNode* ListFind ( LTNode* phead, LTDataType x)
{
assert ( phead) ;
size_t n = 0 ;
LTNode* cur = phead-> next;
while ( cur != phead)
{
if ( cur-> data = x)
{
return cur;
}
cur = cur-> next;
}
return NULL ;
}
10.在pos位置之前插入
void ListInsert ( LTNode* pos, LTDataType x)
{
assert ( pos) ;
LTNode* prev = pos-> prev;
LTNode* newnode = BuyLTNode ( x) ;
newnode-> next = pos;
pos-> prev = newnode;
prev-> next = newnode;
newnode-> prev = prev;
}
11.删除pos位置
void ListErase ( LTNode* pos)
{
assert ( pos) ;
LTNode* prev = pos-> prev;
LTNode* next = pos-> next;
prev-> next = next;
next-> prev = prev;
free ( pos) ;
pos = NULL ;
}
12.销毁
void ListDestory ( LTNode* phead)
{
assert ( phead) ;
LTNode* cur = phead-> next;
while ( cur!= phead)
{
LTNode* next = cur-> next;
free ( cur) ;
cur = next;
}
free ( phead) ;
}
二、全部代码
1.SList.h
# pragma once
# include <stdio.h>
# include <assert.h>
# include <stdlib.h>
# include <stdbool.h>
typedef int LTDataType;
typedef struct ListNode
{
LTDataType data;
struct ListNode * next;
struct ListNode * prev;
} LTNode;
LTNode* ListInit ( ) ;
LTNode* BuyLTNode ( LTDataType x) ;
void ListPrint ( LTNode* phead) ;
void ListPushFront ( LTNode* phead, LTDataType x) ;
void ListPushBack ( LTNode* phead, LTDataType x) ;
void ListPopBack ( LTNode* phead) ;
void ListPopFront ( LTNode* phead) ;
bool ListEmpty ( LTNode* phead) ;
size_t ListSize ( LTNode* phead) ;
LTNode* ListFind ( LTNode* phead, LTDataType x) ;
void ListInsert ( LTNode* pos, LTDataType x) ;
void ListErase ( LTNode* pos) ;
void ListDestory ( LTNode* phead) ;
2.SList.c
# define _CRT_SECURE_NO_WARNINGS
# include "List.h"
LTNode* ListInit ( )
{
LTNode* guard = ( LTNode* ) malloc ( sizeof ( LTNode) ) ;
if ( guard == NULL )
{
perror ( "malloc fail" ) ;
exit ( - 1 ) ;
}
guard-> next = guard;
guard-> prev = guard;
return guard;
}
LTNode* BuyLTNode ( LTDataType x)
{
LTNode* node = ( LTNode* ) malloc ( sizeof ( LTNode) ) ;
if ( node == NULL )
{
perror ( "malloc fail" ) ;
exit ( - 1 ) ;
}
node-> data = x;
node-> next = NULL ;
node-> prev = NULL ;
return node;
}
void ListPrint ( LTNode* phead)
{
assert ( phead) ;
printf ( "phead<=>" ) ;
LTNode* cur = phead-> next;
while ( cur != phead)
{
printf ( "%d<=>" , cur-> data) ;
cur = cur-> next;
}
printf ( "\n" ) ;
}
void ListPushFront ( LTNode* phead, LTDataType x)
{
assert ( phead) ;
LTNode* newnode = BuyLTNode ( x) ;
LTNode* first = phead-> next;
newnode-> next = first;
first-> prev = newnode;
phead-> next = newnode;
newnode-> prev = phead;
}
void ListPushBack ( LTNode* phead, LTDataType x)
{
ListInsert ( phead, x) ;
}
void ListPopFront ( LTNode* phead)
{
assert ( phead) ;
assert ( ! ListEmpty ( phead) ) ;
LTNode* first = phead-> next;
LTNode* second = first-> next;
phead-> next = second;
second-> prev = phead;
free ( first) ;
first = NULL ;
}
void ListPopBack ( LTNode* phead)
{
ListErase ( phead-> prev) ;
}
bool ListEmpty ( LTNode* phead)
{
assert ( phead) ;
return phead-> next == phead;
}
size_t ListSize ( LTNode* phead)
{
assert ( phead) ;
size_t n= 0 ;
LTNode* cur = phead-> next;
while ( cur!= phead)
{
++ n;
cur = cur-> next;
}
return n;
}
LTNode* ListFind ( LTNode* phead, LTDataType x)
{
assert ( phead) ;
size_t n = 0 ;
LTNode* cur = phead-> next;
while ( cur != phead)
{
if ( cur-> data = x)
{
return cur;
}
cur = cur-> next;
}
return NULL ;
}
void ListInsert ( LTNode* pos, LTDataType x)
{
assert ( pos) ;
LTNode* prev = pos-> prev;
LTNode* newnode = BuyLTNode ( x) ;
newnode-> next = pos;
pos-> prev = newnode;
prev-> next = newnode;
newnode-> prev = prev;
}
void ListErase ( LTNode* pos)
{
assert ( pos) ;
LTNode* prev = pos-> prev;
LTNode* next = pos-> next;
prev-> next = next;
next-> prev = prev;
free ( pos) ;
pos = NULL ;
}
void ListDestory ( LTNode* phead)
{
assert ( phead) ;
LTNode* cur = phead-> next;
while ( cur!= phead)
{
LTNode* next = cur-> next;
free ( cur) ;
cur = next;
}
free ( phead) ;
}
3.Test.c
# define _CRT_SECURE_NO_WARNINGS
# include "List.h"
void TestList1 ( )
{
LTNode* plist = ListInit ( ) ;
ListPushBack ( plist, 1 ) ;
ListPushBack ( plist, 2 ) ;
ListPushBack ( plist, 3 ) ;
ListPushBack ( plist, 4 ) ;
ListPrint ( plist) ;
ListPushFront ( plist, 10 ) ;
ListPushFront ( plist, 20 ) ;
ListPushFront ( plist, 30 ) ;
ListPushFront ( plist, 40 ) ;
ListPrint ( plist) ;
ListPopBack ( plist) ;
ListPopBack ( plist) ;
ListPopBack ( plist) ;
ListPopBack ( plist) ;
ListPrint ( plist) ;
ListPopFront ( plist) ;
ListPopFront ( plist) ;
ListPopFront ( plist) ;
ListPrint ( plist) ;
}
void TestList2 ( )
{
LTNode* plist = ListInit ( ) ;
ListPushBack ( plist, 1 ) ;
ListPushBack ( plist, 2 ) ;
ListPushBack ( plist, 3 ) ;
ListPushBack ( plist, 4 ) ;
ListPrint ( plist) ;
ListPopFront ( plist) ;
ListPopFront ( plist) ;
ListPrint ( plist) ;
ListPopFront ( plist) ;
ListPopFront ( plist) ;
ListPrint ( plist) ;
ListDestory ( plist) ;
plist = NULL ;
}
void TestList3 ( )
{
LTNode* plist = ListInit ( ) ;
ListPushBack ( plist, 1 ) ;
ListPushBack ( plist, 2 ) ;
ListPushBack ( plist, 3 ) ;
ListPushBack ( plist, 4 ) ;
ListPrint ( plist) ;
}
int main ( )
{
TestList2 ( ) ;
return 0 ;
}