Chinaunix首页 | 论坛 | 博客
  • 博客访问: 60961
  • 博文数量: 12
  • 博客积分: 232
  • 博客等级: 二等列兵
  • 技术积分: 125
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-22 10:21
个人简介

hello everyone

文章分类

全部博文(12)

文章存档

2014年(1)

2012年(4)

2011年(7)

我的朋友

分类: C/C++

2011-12-28 15:44:06

//头文件

#include

typedef enum Status
{
 StatusFail,
 StatusOk
}Status;

typedef struct ElemType
{
 int m_pid;
 int m_exit;
}ElemType;

typedef struct DuLNode
{
 ElemType data;
 struct DuLNode *pre;
 struct DuLNode *next;
}DuLNode, *DuLinkList, *PNode;

//辅助函数
int CompareElem(ElemType *a, ElemType *b);
int CopyElem(ElemType*dest, ElemType *source);

//public接口
Status InitList(DuLinkList *L);
Status DestroyList(DuLinkList *L);
Status ClearList(DuLinkList L);

Status ListInsert(DuLinkList L, PNode node, ElemType e);
PNode ListDelete(DuLinkList L, PNode node, ElemType *e);
PNode ListFind(DuLinkList L, ElemType e);

void ListTraverse(DuLinkList L, void(*visit)(ElemType *e) );

//private接口

//cpp文件

#include"doubleLinkList.h"
#include

int CompareElem(ElemType *a, ElemType *b)
{
 if(a->m_pid == b->m_pid
  && a->m_exit == b->m_exit)
 {
  return 0;
 }
 else
 {
  return 1;
 }
}

int CopyElem(ElemType*dest, ElemType *source)
{
 dest->m_pid = source->m_pid;
 dest->m_exit = source->m_exit;
}

//public接口
Status InitList(DuLinkList *L)
{
 *L = (DuLinkList)malloc(sizeof(DuLNode));
 if(*L == NULL)
 {
  return StatusFail;
 }
 else
 {
  (*L)->pre = (*L)->next = *L;
  return StatusOk;
 }
}

Status DestroyList(DuLinkList *L)
{
 ClearList(*L);
 free(*L);
 *L=NULL;
 return StatusOk;
}

Status ListInsert(DuLinkList L, PNode node, ElemType e)
{
 PNode pe = (PNode)malloc(sizeof(DuLNode));
 if(pe == NULL)
 {
  return StatusFail;
 }
 
 //pe->data = e;
 CopyElem(&(pe->data), &e);
 pe->pre = node->pre; node->pre->next = pe;
 pe->next = node;  node->pre = pe;
 return StatusOk;
}

//必须保证node不是头结点
PNode ListDelete(DuLinkList L, PNode node, ElemType *e)
{
 //*e = p->data;
 CopyElem(&(node->data), e);
 
 node->pre->next = node->next;
 node->next->pre = node->pre;

 free(node);
 return node->next;
}

PNode ListFind(DuLinkList L, ElemType e)
{
 PNode ret = L->next;
 
 while(ret != L)
 {
  if(CompareElem(&(ret->data), &e) == 0)
  {
   return ret;
  }
  ret = ret->next;
 }
 
 return NULL;
}


Status ClearList(DuLinkList L)
{
 PNode q, p = L->next;
 while(p != L)
 {
  q = p->next;
  free(p);
  p = q;
 }
 L->pre = L->next = L;
 return StatusOk;
}

void ListTraverse(DuLinkList L, void (*visit)(ElemType *e))
{
 PNode p= L->next; // p指向头结点
 while(p != L)
 {
  visit(&(p->data));
  p=p->next;
 }
 printf("\n");
}

//测试代码

#include"DoubleLinkList.c"

void print(ElemType *e)
{
 printf("%d, %d\n", e->m_pid, e->m_exit);
}

int main()
{
 ElemType a = {1, 0};
 ElemType b = {2, 0};
 ElemType c = {3, 0};
 ElemType d = {4, 0};
 
 DuLinkList L;
 InitList(&L);
 
 printf("\nListInsert\n");
 ListInsert(L, L, a);
 ListInsert(L, L, b);
 ListInsert(L, L, c);
 
 ListTraverse(L, print);
 
 printf("\nListFind\n");
 PNode pb = ListFind(L, b);
 pb->data.m_exit = 1;
 printf("%d, %d\n", pb->data.m_pid, pb->data.m_exit);
 
 printf("\nListDelete\n");
 ListDelete(L, pb, &d);
 ListTraverse(L, print);
 
 printf("\nListFind\n");
 PNode pd = ListFind(L, d);
 (pd == NULL)? printf("NULL\n") : printf("\n%d, %d\n", pd->data.m_pid, pd->data.m_exit);
 
 printf("\nClearList\n");
 ClearList(L);
 ListTraverse(L, print);
 
 printf("\nDestroyList\n");
 DestroyList(&L);
 
 system("pause");
 return 0;
}

匆忙完成,如果疏漏请各位指正。

阅读(3298) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~