list的常见操作以及算法的时间复杂度

链表操作,时间复杂度为O(n),重在思维和设计,如下给出了一个链表的常见操作的实例


(1) 维护一个数组,来标记链表的元素是否存在

(2)数组下标和值得反应反映射,例如 a ={10,20,30,20}
t[256] = {-1}
t[10] =1;t[20] =1;t[30] =1;
a数组的最后一个元素为20,t[20]已经为1,记录20存在元素的个数,t[20]++;即可
/*
************************************************************************ > File Name: list_op.h > Author: zhoulin > Mail: [email protected] > Created Time: Sat 23 Apr 2016 09:23:40 AM CST ************************************************************************/ #ifndef _LIST_OP_H #define listSize 256 #include <pthread.h> typedef struct _listNode { int v; struct _listNode *next; }listNode; typedef struct _listMgr { unsigned int len; int vmax; int vmin; struct _listNode *head; int flag[listSize]; pthread_mutex_t lock; }listMgr; #define listMgrInit(mgr,max) \ { \ mgr->len = 0; \ mgr->vmax = max; \ mgr->vmin = max; \ mgr->head = NULL; \ memset(mgr->flag,-1,sizeof(int)*max); \ } #define rebuild(p,h,t) \ { \ if(h == NULL) \ { \ h = t = p; \ }else{ \ t->next = p; \ t = p; \ } \ t->next = NULL; \ } //打印链表,时间复杂度O(n) void listPrt(listNode *p); //链表初始化,时间负责度O(n) listNode *listInit(listMgr *mgr,listNode *a,int n,int max); //删除链表P中有过重复的元素 listNode *listDRepeat(listMgr *mgr); //删除链表中重复元素 listNode *listNRepeat(listMgr *mgr); //给定一个值,要求链表左边小于该值,链表右边大于该值,且链表的相对位置不改变 listNode *listQuick(listMgr *mgr,int v); //链表排序 listNode *listOrder(listMgr *mgr); #define _LIST_OP_H #endif

你可能感兴趣的:(list的常见操作以及算法的时间复杂度)