赞
踩
- 带头当链表为空时头还在;
- 相同长度带头比不带头少一个元素
- 尾指向NULL为不循环;
- 尾指向链表上元素;
typedef int SLDataType
typedef struct SLNode
{
SLDataType value;
struct SLNode* next;//指向下一个这样的结构体;
} SLNode;
typedef struct SList//链表的首节点
{
SLNode *first;//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;//注意只剩一个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;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。