目录:
- 代码:
- 分析:
- 汇编:
代码:
CircleList.h
#ifndef _CIRCLELIST_H_
#define _CIRCLELIST_H_typedef void CircleList;typedef struct _tag_CircleListNode CircleListNode;struct _tag_CircleListNode{CircleListNode *next;};CircleList * CircleList_Create();void CircleList_Destroy(CircleList* list);void CircleList_Clear(CircleList* list);int CircleList_Length(CircleList* list);int CircleList_Insert(CircleList* list, CircleListNode* node, int pos);CircleListNode* CircleList_Get(CircleList* list, int pos);CircleListNode* CircleList_Delete(CircleList* list, int pos);CircleListNode* CircleList_DeleteNode(CircleList* list, CircleListNode* node);CircleListNode* CircleList_Reset(CircleList* list);CircleListNode* CircleList_Current(CircleList* list);CircleListNode* CircleList_Next(CircleList*list);#endif
CircleList.c
#include<stdio.h>
#include<malloc.h>
#include"CircleList.h"typedef struct _tag_CircleList{CircleListNode header;//头节点CircleListNode* slider;int length;
}TCircleList;CircleList * CircleList_Create(){//定义创建表函数TCircleList * ret = (TCircleList*)malloc(sizeof(TCircleList));if (ret != NULL){ret->length = 0;ret->header.next = NULL;ret->slider = NULL;}return ret;
}void CircleList_Destroy(CircleList * list){//定义销毁表函数free(list);
}void CircleList_Clear(CircleList* list){//定义重设表属性函数TCircleList* sList = (TCircleList*)list;if (sList != NULL){sList->length = 0;sList->header.next = NULL;sList->slider = NULL;}
}int CircleList_Length(CircleList * list){//定义获取表当前长度函数TCircleList *sList = (TCircleList*)list;int ret = -1;if (sList != NULL){ret = sList->length;}return ret;
}int CircleList_Insert(CircleList * list, CircleListNode* node, int pos){//定义插入节点函数TCircleList *sList = (TCircleList*)list;int ret = (sList != NULL) && (pos >= 0) && (node != NULL);//判断表与节点是否为空和pos是否正常int i = 0;if (ret){CircleListNode* current = (CircleListNode*)sList; //取得表的header元素地址//取得插入节点位置的前一个节点//current->next == NULL 表示是第一个插入的节点//注意:这没有对pos判断,pos大于length时会循环for (i = 0; (i < pos) && (current->next != NULL); i++){current = current->next;}//注意:插入第一个节点后,再插入的节点如果不是第一个位置才能正常循环//可以在链表结构加一个尾节点指针变量保持指向尾节点,每次尾节点更改后再更新node->next = current->next;//将原本在pos位置的节点赋给新插入节点的nextcurrent->next = node;//将插入节点赋给插入节点位置的前一个节点的nextif (sList->length == 0){ //如果是第一个插入的节点//注意:插入第一个节点后,再插入的节点如果是第一个位置slider不会更新还是第一次插入的第一个节点sList->slider = node;//将第一个插入节点赋给slider node->next = node;//将第一个插入节点的next指向自身(实现循环) }sList->length++;//长度增加}return ret;
}CircleListNode* CircleList_Get(CircleList* list, int pos){//定义获取节点函数TCircleList * sList = (TCircleList*)list;CircleListNode* ret = NULL;int i = 0;if ((sList != NULL) && (pos >= 0)){CircleListNode* current = (CircleListNode*)sList;for (i = 0; i < pos; i++){ //取得前一个节点//注意:这没有对pos判断,pos大于length时会循环current = current->next;}ret = current->next; //取得要获取的节点}return ret;
}CircleListNode* CircleList_Delete(CircleList* list, int pos){//定义根据下标删除节点函数TCircleList *sList = (TCircleList*)list;CircleListNode * ret = NULL;int i = 0;if ((sList != NULL) && (pos >= 0)){//判断表是否为空与pos是否正常CircleListNode* current = (CircleListNode*)sList;//指向头节点CircleListNode* first = sList->header.next;//指向第一个节点CircleListNode* last = (CircleListNode*)CircleList_Get(sList, sList->length-1);//指向最后一个节点for (i = 0; i < pos; i++){//找到要删除位置的前一个位置current = current->next;}ret = current->next;//指向要删除节点current->next = ret->next;//将要删除节点的next赋给上一节点的nextsList->length--;//长度减少if (first == ret){ //如果是第一个节点//这一步好像多余,上面current就是headersList->header.next = ret->next;//将头节点指向第一个节点的下一个last->next = ret->next;//将最后一个节点的next指向第一个节点的下一个实现循环}if (sList->slider == ret){//如果删除的节点是第一个插入的节点sList->slider = ret->next;//删除节点的下个节点赋给slider}if (sList->length == 0){//如果长度等于0,表示链表没有数据了sList->header.next = NULL; //重置第一个节点的指向sList->slider = NULL;//重置slider}}return ret;//返回删除节点
}CircleListNode* CircleList_DeleteNode(CircleList * list, CircleListNode * node){//定义根据节点删除节点函数TCircleList* sList = (TCircleList*)list;CircleListNode *ret =NULL;int i = 0;if (sList != NULL){//判断表是否为空CircleListNode* current = (CircleListNode*)sList;//指向头节点for (i = 0; i < sList->length; i++){if (current->next == node){//找到删除节点的上一个节点ret = current->next;//指向删除节点break; //找到后跳出循环查找}current = current->next;}if (ret != NULL){//如果有该节点CircleList_Delete(sList, i);//将下标转去执行删除}}return ret;//返回删除节点
}CircleListNode* CircleList_Reset(CircleList* list){//定义重置slider指向的函数TCircleList* sList = (TCircleList*)list;CircleListNode* ret = NULL;if (sList != NULL){//如果表不为空sList->slider = sList->header.next;//将第一个节点赋给sliderret = sList->slider;}return ret;//返回slider}CircleListNode* CircleList_Current(CircleList* list){//定义slider当前指向节点函数TCircleList* sList = (TCircleList*)list;CircleListNode* ret = NULL;if (sList != NULL){ret = sList->slider;}return ret;//返回slider
}CircleListNode* CircleList_Next(CircleList *list){//定义slider下移一个节点函数TCircleList* sList = (TCircleList*)list;CircleListNode* ret = NULL;if ((sList != NULL) && (sList->slider != NULL)){//判断表不为空与slider不为空ret = sList->slider;sList->slider = ret->next;//将slider的next赋给slider}return ret;//返回slider;
}
main.c
#include<stdio.h>
#include<stdlib.h>
#include"CircleList.h"struct Value{CircleListNode header;//必须放在第一个元素int v;
};
int main(int argc, char *argv[]){int i = 0;CircleList* list = CircleList_Create();struct Value v1;struct Value v2;struct Value v3;struct Value v4;struct Value v5;struct Value v6;struct Value v7;struct Value v8;v1.v = 1;v2.v = 2;v3.v = 3;v4.v = 4;v5.v = 5;v6.v = 6;v7.v = 7;v8.v = 8;CircleList_Insert(list, (CircleListNode*)&v1, CircleList_Length(list));CircleList_Insert(list, (CircleListNode*)&v2, CircleList_Length(list));CircleList_Insert(list, (CircleListNode*)&v3, CircleList_Length(list));CircleList_Insert(list, (CircleListNode*)&v4, CircleList_Length(list));CircleList_Insert(list, (CircleListNode*)&v5, 5);CircleList_Delete(list, 0);for (i = 0; i < 2 * CircleList_Length(list); i++){struct Value* pv = (struct Value*)CircleList_Get(list, i);printf("%d\n", pv->v);}printf("\n");while (CircleList_Length(list)>0){struct Value*pv = (struct Value*)CircleList_Delete(list, 0);printf("%d\n", pv->v);}printf("\n");CircleList_Insert(list, (CircleListNode*)&v1, CircleList_Length(list));CircleList_Insert(list, (CircleListNode*)&v2, CircleList_Length(list));CircleList_Insert(list, (CircleListNode*)&v3, CircleList_Length(list));CircleList_Insert(list, (CircleListNode*)&v4, CircleList_Length(list));CircleList_Insert(list, (CircleListNode*)&v5, CircleList_Length(list));CircleList_Insert(list, (CircleListNode*)&v6, CircleList_Length(list));CircleList_Insert(list, (CircleListNode*)&v7, CircleList_Length(list));CircleList_Insert(list, (CircleListNode*)&v8, CircleList_Length(list));for (i = 0; i < CircleList_Length(list); i++){struct Value* pv = (struct Value*)CircleList_Next(list);printf("%d\n", pv->v);}getchar();return 1;
}
分析:
汇编: