链表实现动态数组C源代码

来源:互联网 发布:win7开机网络转圈假死 编辑:程序博客网 时间:2024/05/16 15:58

 

       用链表结构实现的动态数组,数据元素是整数类型。当然,也很容易修改为其他数据类型。

实现很简单,但是由于插入、取出数据都要顺序查找,时间复杂度为O (Length(LArray)),

所以性能比C中的数组要差。

       准备下次再发表一个更快速的动态数组的实现。

// array.h

/*------------------------------------------------------------------------------
* Copyright (C) 2007-2008 HaiTao email:guht2000@126.com
*
* 本代码可以随意发布、修改和使用。
*  
------------------------------------------------------------------------------*/

#define TRUE 1
#define FALSE 0

#define NULL 0

//链表节点的数据结构
typedef struct _LNod{
 int elem;
 _LNod* next;
 
} LNOD, *LARRAY;

//创造空的动态数组p
bool createNullLArray(LARRAY& p);
//创造动态数组,长度为n,元素初值为数组elem前n个元素
bool createLArray(LARRAY& p, int n, int elem[]);

//删除动态数组

bool deltLArray(LARRAY& p);

//向动态数组尾部加入元素elem
bool addElem(LARRAY p, int elem );

//向动态数组尾部增加n个元素,值初始化为elem
bool appendElem(LARRAY p, int n, int elem);

//取得位置pos处的元素值,注:数组第一个元素的位置pos为0
bool getElem(LARRAY p, int pos,  int& elem);

//在位置pos之前插入元素elem。注意:不要和setElem()函数混淆
bool insertElem(LARRAY p, int pos, int elem);

//删除位置pos处的元素
bool deltElem(LARRAY p, int pos);

//把位置pos处的元素值设置为elem
bool setElem(LARRAY p, int pos,  int elem);

//////////////////////////////////////////////////////////

//array.c

/*------------------------------------------------------------------------------
* Copyright (C) 2007-2008 HaiTao email:guht2000@126.com
*
* 本代码可以随意发布、修改和使用。
*  
------------------------------------------------------------------------------*/


#include "array.h"


bool createNullLArray(LARRAY& p)
{

 p= new LNOD;    //本方案的动态数组有一个elem值为空的头节点,头节点的next指向第一个数组元素
 if (!p) return FALSE;
 p->next=NULL; //现在,第一个数组元素为空
 return TRUE;
}


bool createLArray(LARRAY& p, int n, int elem[])
{
 LNOD* t;
 if (!(p=new LNOD)) return FALSE;
 p->next=NULL;     //倒序方法创建链表
 while (--n>=0) {
  if(!(t=new LNOD)) return FALSE;
  t->elem=elem[n];
  t->next=p->next;
  p->next=t;
 }
 return TRUE;
}

bool deltLArray(LARRAY& p)
{
 LNOD* temp;
 if (!p) return FALSE;
 
 while (p)
 {
  temp=p;
  p=p->next;
  delete temp;
 }

 p=NULL;
 return TRUE;
}

bool addElem(LARRAY p, int elem )
{
 while ( p->next!= NULL) p=p->next;
 
 LNOD* temp= new LNOD;
 if (!temp) return FALSE;

 p->next = temp;
 
 p=p->next;
 p->next=NULL;
 p->elem=elem;
 return TRUE;
}

bool appendElem(LARRAY p, int n, int elem)
{
    LNOD* temp;
    while ( p->next!= NULL) p=p->next;
    while (n--) {   
        if (!(temp=new LNOD)) return FALSE;
        temp->elem=elem;
        p->next=temp;
        p=temp;
    }
    p->next=NULL;
    return TRUE;

}

bool insertElem(LARRAY p, int pos, int elem)
{
 LNOD* t;
 if (pos<0|| !p) return FALSE;
 while(pos-->0)
  if ((p=p->next) == NULL ) { return FALSE;}
 
 if (!(t=new LNOD)) return FALSE;
 t->elem=elem;
 t->next=p->next;
 p->next=t;
 return TRUE;

}

bool deltElem(LARRAY p, int pos)
{
 LNOD* t;
 if (pos<0|| !p) return FALSE;
 while (pos-->0)
  if ((p=p->next) == NULL ) { return FALSE;}
 if (!(t=p->next)) return FALSE;
 p->next=t->next;
 delete t;
 return TRUE;
}

bool getElem(LARRAY p, int pos,  int& elem)
{
 if (pos<0||!p||!(p=p->next)) return FALSE;
 
 while (pos-->0)
  if ((p=p->next) == NULL ) { return FALSE;}
 
 elem=p->elem;
 return TRUE;

}

bool setElem(LARRAY p, int pos,  int elem)
{
    if (pos<0||!p||!(p=p->next)) return FALSE;
   
    while (pos-->0)
        if ((p=p->next) == NULL ) { return FALSE;}
   
    p->elem=elem;
    return TRUE;
}

 

 

原创粉丝点击