数据结构之单向链表实现
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;
}
//***************************************************
// 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++实现单向链表
- 数据结构——单向链表的顺序存储及实现
- 【数据结构与算法】链表1:单向链表(Java实现)
- 数据结构之单向链表的实现【C++】
- 数据结构——Java实现单向链表
- 单向链表实现多项式加和乘--自己写数据结构
- 数据结构——建立单向链表(java实现)
- 数据结构之单向链表(java实现)
- 数据结构基础 之 单链表 各类操作、思想与实现
- 数据结构:单链表,循环链表,双项链表的实现
- 程序员面试宝典之数据结构基础---⑤单链表逆序的递归与非递归实现
- 数据结构之链表Java实现
- 数据结构之单向链表
- python 数据结构之双向链表的实现
- I学霸官方免费教程三十七:Java数据结构之单向链表结构
- Hrbust-1546-基础数据结构——单链表(1)【链表实现】
- c语言实现通用数据结构(一):通用链表
- 18、数据结构笔记之十八链表实现稀疏矩阵
- 写给过去的自己-No.1-数据结构篇-单向链表C语言的实现
- 【数据结构&算法】数据结构之单向链表(练习)