链表
分类
- 带头当链表为空时头还在;
- 相同长度带头比不带头少一个元素
- 尾指向NULL为不循环;
- 尾指向链表上元素;
单向不带头不循环链表
定义
typedef int SLDataType
typedef struct SLNode
{
SLDataType value;
struct SLNode* next;
} SLNode;
typedef struct SList
{
SLNode *first;
} SList;
初始化
void SListInit(SList*list)
{
asser(list!=NULL);
list->first = NULL;
}
销毁
void SListDestroy(SList*list)
{
assert(list!=NULL);
SLNode * go = list->first;
SLNode * next;
while(go!=NULL)
{
next = go->next;
free(go);
go = next;
}
list->first = NULL;
}
增加数据
void SListPushFront(SList*list,SLDataType value)
{
assert(list!=NULL);
SListNode *node = (SLiztNode*)malloc(sizeof(SListNode));
assert(node);
node->value = value;
node->next = list->first;
list->first = node;
}
void SListPushBack(SList*list,SLDataType value)
{
assert(list!=NULL);
SLNode* find_last;
if(list->first==NULL)
{
SListPushFront(list,value);
return;
}
for(find_last=list->first;find_last->next!=NULL;find_last = find_last->next){
}
SListNode *node = (SLiztNode*)malloc(sizeof(SListNode));
assert(node);
node->value = value;
find_last->next = node;
node->next = NULL;
}
void SListInsertAfter(SLNode *pos,SLDataType value)
{
SLNode*node = (SLiztNode*)malloc(sizeof(SListNode));
assert(node);
node->value = value;
node->next = pos->next;
pos->next = node;
}
void SListInsertBefore(SList*list,SLNode*pos,SLDataType value)
{
assert(list);
SLNode*node = (SLiztNode*)malloc(sizeof(SListNode));
assert(node);
node->value = value;
for(SLNode*go = list->first;go->next!=pos&&go->next!=NULL;go = go->next)
{
;
}
if(go->next==pos)
{
node->next = pos;
go->next = node;
}
else
{
printf("没有找到该位置\n");
}
}
删除数据
void SListPopFront(SList*list)
{
assert(list!=NULL);
assert(list->first !=NULL );
SLNode*free_frist = list->first;
list->first = list->first->next;
free(free_frist);
}
void SListPopBack(SList*list)
{
assert(list!=NULL);
assert(list->first !=NULL );
if(list->first->next==NULL)
{
SListPopFront(list)
}
SLNode* find_2last;
for(find_2last=list->first;find_2last->next->next!=NULL;find_2last = find_last->next){
}
free(find_2last->next)
find_2last->next = NULL;
}
void SLiztEraseAfter(SLNode*pos)
{
SLNode*next = pos->next;
pos->next = next->next;
free(next);
}
打印
void SListPrint(const SList*list)
{
for(SLNode*go = list->first;go!=NULL;go = go->next)
{
printf("%d->",go->value);
}
printf("NULL\n");
}
查找
SLNode* SListFind(const SList*list,SLDataType value)
{
for(SLNode *go = list->first;go!=NULL;go = go->next)
{
if(go->value==value)
{
return go;
}
}
return NULL;
}
修改
void SListModify(SLNode*nodet,SLDataType value )
{
node->value = value;
}
双向带头循环链表
定义
typedef int DLDataType
typedef struct DLNode
{
DLDataType value;
struct DLNode* prev;
struct DLNode* next;
} DLNode;
typedef struct DList
{
DLNode *head;
} DList;
初始化
DLNode * DLBuyNode(DLDataType value)
{
DLNode*node = (DLNode*)malloc(sizeof(DLNode));
node->value = value;
node->next = node->prev = NULL;
return node;
}
void DListInit(DList*list)
{
asser(list!=NULL);
list->head = DLBuyNode(0);
list->head->next = list->head;
list->head->prev = list->head;
}
销毁
void DListClear(DList*list)
{
DLNode* go,*self ;
go = list->head->next;
while(go!=list->head)
{
self = go;
go = go->next;
free(self);
}
list->head->next = list->head->prev = list->head;
}
void DListDestroy(DList*list)
{
DListClear(list);
free(list->head);
list->head = NULL;
}
增加数据
void DListPushFront(DList*list,DLDataType value)
{
assert(list!=NULL);
#if 0
DListNode *node =DLBuyNode(value);
node->prev = list->head;
node->next = list->head->next;
list->head->next->prev = node;
list->head->next = node;
#endif
DListInsertFront(list->head->next,value);
}
void DListPushBack(DList*list,DLDataType value)
{
assert(list!=NULL);
#if 0
DListNode *node =DLBuyNode(value);
node->prev = list->head->prev;
node->next = list->head;
list->head->prev->next = node;
list->head->prev = node;
#endif
DListInsertFront(list->head,value);
}
void DListInsertFront(DLNode*pos,DLDataType value)
{
DLNode*node = DLBuyNode(value);
node->prev = pos->prev;
node->next = pos;
pos->prev = node;
node->prev->next = node;
}
删除数据
void DListPopFront(DList*list)
{
assert(list!=NULL);
assert(list->head->next !=list->head );
DLNode* go = list->head->next ;
list->head->next = go->next;
go->next->prev = list->head;
free(go);
}
void DListPopBack(DList*list)
{
assert(list!=NULL);
assert(list->head->next !=list->head );
SLNode*get = list->head->prev;
list->head->prev = get->prev;
get->prev->next = list->head;
free(get);
}
void DListErase(DLNode*pos)
{
DLnode*get = pos;
pos->prev->next = pos->next;
pos->next->prev = pos->prev;
free(pos);
}
打印
void DListPrint(const DList*list)
{
for(SLNode*go = list->head->next;go!=list->head;go = go->next)
{
printf("%d->",go->value);
}
printf("\n");
}
查找
DLNode* DListFind(const DList*list,DLDataType value)
{
DLNode* go ;
for(go = list->head->next;go!=list->head;go = go->next)
{
if(go->value == value)
{
return go;
}
}
return NULL:
}
修改
void SListModify(DLNode*node,DLDataType vale )
{
node->vale = vale;
}
- 地址:一段空间的起始位置;
- 指针:是个变量,存放内存单元的地址;制定了内存的长度;
链表的相关题目:
反转链表