您的位置:首页 > 理论基础 > 数据结构算法

数据结构之单向链表实现

2013-06-18 09:56 537 查看
       在单向链表中,数据被存储在一个一个的“结点”中,这些“结点”在内存中并非是处于相邻的位置,而是“见缝插针”,由系统分配在一小块一小块的内存中。在进行插入数据的操作时,你需要告诉系统你需要一块内存,系统会根据你设定的大小在堆中寻找空闲的内存用来存储新的“结点”;而在进行删除操作时,你需要手动将要删除的“结点”的空间释放掉,同时注意将其置空(NULL)。这样,链表的存储结构克服了顺序表的插入与删除数据需要移动结点的操作,更加简便,同时也提高了空间的利用率。

//***************************************************
// function:单向链表
//
//
//*************************************************

#include <stdio.h>

//#define datatype int;

typedef struct LNode
{
int data;
struct LNode *next_node;
}LNode,*Linklist;

//1.创建空链表
int initlist(Linklist L)
{
L = (Linklist)malloc(sizeof(struct LNode));
if(L == NULL)
{
printf("over flow!\n");
return -1;
}

L->next_node = NULL;

return 0;
}

//2.判断是否为空链表
int isempty(Linklist L)
{
if(L->next_node == NULL)
return 0;
else
return -1;
}

//3.销毁链表
int destorylist(Linklist L)
{
Linklist ptmp;
while(L)
{
ptmp = L->next_node;
free(L);
L = NULL;//此处必须置空
L = ptmp;
}

return 0;
}

//4.清除链表中的所有元素,即将链表置为空
int clearlist(Linklist L)
{
Linklist p,q;
p = L->next_node;
while(p)
{
q = p->next_node;
free(p);
p = NULL;
p = q;
}
//L->data = 0;
L->next_node = NULL;

return 0;
}

//5.获取链表中的元素的个数
int getlength(Linklist L)
{
int len = 0;
if(!isempty(L))
return -1;

while(L->next_node != NULL)
{
len ++;
L = L->next_node;
}

return len;
}

//6.取链表中位置location处的元素
int getvalue(Linklist L,int location,int value)
{
Linklist tmp;
int i;
tmp = L->next_node;
while((tmp != NULL) && (location < i))
{
i++;
tmp = tmp->next_node;
}
if((tmp == NULL) || (location > i))
return -1;
value = tmp->data;

return value;
}

//7.定位元素value在表中的位置
int locationlist(Linklist L,int value)
{
Linklist p;
int location = 1;
p = L->next_node;
while(p != NULL)
{
p = p->next_node;
location++;
if(p->data == value)
break;
}
if(p == NULL)
{
printf("is not here!\n");
return -1;
}

return location;
}

//8.取元素cur_e的前驱元素
int prevalue(Linklist L,int cur_e,int pre_e)
{
Linklist p,q;
p = L->next_node;
while(p)
{
q = p->next_node;
if(cur_e == q->data)
{
pre_e = p->data;
return 0;
}
p = q;
}
return -1;
}

//9.取元素cur_e的后继元素
int nextvalue(Linklist L,int cur_e,int next_e)
{
Linklist p,q;
p = L->next_node;
while(p)
{
q = p->next_node;
if(cur_e == p->data)
{
next_e = q->data;
return 0;
}
p = q;
}
}

//10.在位置location处插入元素value
int insertvalue(Linklist L,int location,int value)
{
Linklist p,q,new;
int i = 1;

while(p && (i<location))
{
i++;
p = p->next_node;
}

if((p == NULL) || (i > location))
return -1;

new = (Linklist)malloc(sizeof(struct LNode));
if(new == NULL)
{
printf("overflow!\n");
return -1;
}
new->data = value;
new->next_node = p->next_node;
p->next_node = new;

return 0;
}

//11.删除位置location处的元素
int deletevalue(Linklist L,int location)
{
Linklist p,q;
int i = 1;
p = L->next_node;
while(p && (i<location))
{
i++;
p = p->next_node;
}
if((p == NULL) || (i > location))
return -1;
//p->next_node = p->next_node->next_node;
q = p->next_node;
p->next_node = q->next_node;
free(q);
q = NULL;

return 0;
}

//12.遍历输出链表中的所有元素
void travellist(Linklist L)
{
int i = 0;
Linklist p;
p = L->next_node;
while(p)
{
i++;
printf("data[i]=%d\n",p->data);
}
}

int main(void)
{
printf("signallist!\n");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据结构 链表 存储 C