数据结构之单向链表实现

       在单向链表中,数据被存储在一个一个的“结点”中,这些“结点”在内存中并非是处于相邻的位置,而是“见缝插针”,由系统分配在一小块一小块的内存中。在进行插入数据的操作时,你需要告诉系统你需要一块内存,系统会根据你设定的大小在堆中寻找空闲的内存用来存储新的“结点”;而在进行删除操作时,你需要手动将要删除的“结点”的空间释放掉,同时注意将其置空(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;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值