2007-07-15
c实现的double linked list
关键字: 数据结构,listdouble_list.h
c 代码
- #ifndef __DOUBLE_LIST_H__
- #define __DOUBLE_LIST_H__
- ///////////////////////////////////////////////////
- //用户可以任意修改结构的定义,除了结构名称。
- ///////////////////////////////////////////////////
- typedef struct dllElem{
- int n;
- }dllElem;
- int dllElem_equal(const dllElem *,const dllElem*);
- ///////////////////////////////////////////////////
- typedef void * dlist_t;
- typedef const void *const_dlist_t;
- dlist_t dlist_new();
- int dlist_size(const_dlist_t pl);
- const dllElem *dlist_index(const_dlist_t pl,int n);
- int dlist_is_empty(const dlist_t pl);
- int dlist_insert(dlist_t pl, int n,const dllElem *element);
- int dlist_remove(dlist_t pl,int n);
- int dlist_equal(const_dlist_t first, const_dlist_t second);
- const dllElem *dlist_front(const_dlist_t pl);
- const dllElem *dlist_back(const_dlist_t pl);
- int dlist_push_back(dlist_t pdlist, const dllElem *element);
- int dlist_pop_back(dlist_t pdlist);
- void dlist_make_empty(dlist_t pl);
- void dlist_destroy(dlist_t pl);
- void dlist_print(const_dlist_t pl);
- #endif
double_list.c
c 代码
- #include "double_list.h"
- #include <stdlib.h>
- #include <stdio.h>
- /////////////////////////////////
- //实现了数据隐藏
- ////////////////////////////////
- typedef struct Node{
- dllElem element;
- struct Node *prev;
- struct Node *next;
- }Node;
- typedef struct _dlist_t{
- Node *head;
- Node *tail;
- int size;
- }_dlist_t;
- int dllElem_equal(const dllElem *lelem,const dllElem *relem)
- {
- if(lelem->n == relem->n)
- return 1;
- else
- return 0;
- }
- void dllElem_print(const dllElem *elem)
- {
- printf("%d",elem->n);
- }
- ///////////////////////////////////////////////////////////////////////////////////
- //以下是私有函数--static--只在文件内部可见
- ///////////////////////////////////////////////////////////////////////////////////
- //这个函数看起来可能会非常晦涩,他返回的是一个Node指针,而不是
- //dllElem 指针。
- static Node *dlist_at_n(const_dlist_t pl,int n)
- {
- const _dlist_t *pdlist = (const _dlist_t *)pl;
- Node *cursor;
- int i=0;
- if(n > pdlist->size || n < 1)
- return 0;
- if(n <= pdlist->size/2)
- {
- cursor = pdlist->head;
- while(i++<n)
- cursor = cursor->next;
- }else
- {
- cursor = pdlist->tail;
- n = pdlist->size - n + 1;
- while(i++<n)
- cursor = cursor->prev;
- }
- return cursor;
- }
- //////////////////////////////////////////////////////////////////////////////////
- dlist_t dlist_new()
- {
- _dlist_t * pdlist = (_dlist_t *)malloc(sizeof(_dlist_t));
- pdlist -> size = 0;
- pdlist -> head = (Node *)malloc(sizeof(Node));
- if (pdlist->head==NULL)
- return NULL;
- pdlist -> tail = (Node *)malloc(sizeof(Node));
- if(pdlist -> tail == NULL)
- {
- free(pdlist);
- return NULL;
- }
- pdlist -> head -> prev = NULL;
- pdlist -> head -> next = pdlist -> tail;
- pdlist -> tail -> next = NULL;
- pdlist -> tail -> prev = pdlist -> head;
- return (dlist_t)pdlist;
- }
- int dlist_size(const_dlist_t pl)
- {
- const _dlist_t *pdlist = (const _dlist_t *)pl;
- return pdlist->size;
- }
- const dllElem *dlist_index(const_dlist_t pl,int n)
- {
- const _dlist_t *pdlist = (const _dlist_t *)pl;
- Node * node = dlist_at_n(pdlist,n);
- if(node == NULL)
- return NULL;
- else
- return &(node->element);
- }
- int dlist_is_empty(const dlist_t pl)
- {
- const _dlist_t *pdlist = (const _dlist_t *)pl;
- if(pdlist->size == 0)
- return 1;
- else
- return 0;
- }
- int dlist_insert(dlist_t pl, int n,const dllElem *element)
- {
- _dlist_t *pdlist = (_dlist_t *)pl;
- Node *latter;
- Node *former;
- Node *current;
- if(n != pdlist->size + 1)
- {
- latter = dlist_at_n(pdlist,n);
- if(latter == NULL)
- return 0;
- }else
- {
- latter = pdlist->tail;
- }
- former = latter->prev;
- current= (Node *)malloc(sizeof(Node));
- if(current ==NULL)
- return 0;
- current->element = *element;
- current->next=latter;
- current->prev=former;
- former->next = current;
- latter->prev = current;
- pdlist->size++;
- return 1;
- }
- int dlist_remove(dlist_t pl,int n)
- {
- _dlist_t *pdlist = (_dlist_t *)pl;
- Node * node = dlist_at_n(pdlist,n);
- if(node == NULL)
- return 0;
- node->prev->next=node->next;
- node->next->prev=node->prev;
- free(node);
- pdlist->size--;
- return 1;
- }
- int dlist_equal(const_dlist_t firstpl,const_dlist_t secondpl)
- {
- int i=0;
- const _dlist_t *first = (const _dlist_t *)firstpl;
- const _dlist_t *second = (const _dlist_t *)secondpl;
- Node * firstp = first->head;
- Node * secondp = second->head;
- if(first->size != second->size)
- return 0;
- while(i++ < first->size)
- {
- firstp = firstp->next;
- secondp = secondp->next;
- if (!dllElem_equal(&(firstp->element), &(secondp->element)))
- return 0;
- }
- return 1;
- }
- const dllElem *dlist_front(const_dlist_t pl)
- {
- const _dlist_t *pdlist = (const _dlist_t *)pl;
- return dlist_index(pl,1);
- }
- const dllElem *dlist_back(const_dlist_t pl)
- {
- const _dlist_t *pdlist = (const _dlist_t *)pl;
- return dlist_index(pl,pdlist->size);
- }
- int dlist_push_back(dlist_t pl,const dllElem *element)
- {
- _dlist_t *pdlist = (_dlist_t *)pl;
- return dlist_insert(pdlist,pdlist->size + 1,element);
- }
- int dlist_pop_back(dlist_t pl)
- {
- _dlist_t *pdlist = (_dlist_t *)pl;
- return dlist_remove(pdlist,pdlist->size);
- }
- void dlist_make_empty(dlist_t pl)
- {
- _dlist_t *pdlist = (_dlist_t *)pl;
- Node * cursor = pdlist->head->next;
- Node * next = cursor->next;
- while(cursor != pdlist->tail)
- {
- free(cursor);
- cursor = next;
- next = cursor->next;
- }
- pdlist->size =0;
- }
- void dlist_destroy(dlist_t pl)
- {
- _dlist_t *pdlist = (_dlist_t *)pl;
- dlist_make_empty(pdlist);
- free(pdlist->head);
- free(pdlist->tail);
- }
- void dlist_print(const_dlist_t pl)
- {
- const _dlist_t *pdlist = (const _dlist_t *)pl;
- Node * cursor = pdlist->head ->next;
- while(cursor != pdlist->tail)
- {
- dllElem_print(&(cursor->element));
- printf("\n");
- cursor = cursor->next;
- }
- }
发表评论
- 浏览: 55828 次
- 性别:

- 来自: 乌托邦

- 详细资料
搜索本博客
最近加入圈子
链接
最新评论
-
【团队管理】大家Have A ...
sg552 写道 今天白天要出去,晚上回来,或者明天的时候,好好帮你分析一下。 ...
-- by rj045wq -
【团队管理】大家Have A ...
那就试试每天早上花个20分钟开个早茶会,让每人介绍下自己在干的活,以及需要什么帮 ...
-- by xiaolin0105 -
【团队管理】大家Have A ...
早上起来看到回帖,呵呵,楼主别激动。我知道我的帖子写的非常概括。因为之前只是把自 ...
-- by sg552 -
【团队管理】大家Have A ...
sg552 写道不是我故意跟你作对。 我觉得你确实很需要提高。犯了很多大忌。 也 ...
-- by xombat -
【团队管理】大家Have A ...
抛出异常的爱 写道 你管的太多。 放下手中的事 让他们自己决定要干什么不要干什么 ...
-- by xombat






评论排行榜