Chinaunix首页 | 论坛 | 博客
  • 博客访问: 480996
  • 博文数量: 76
  • 博客积分: 5196
  • 博客等级: 大校
  • 技术积分: 1414
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-10 18:43
个人简介

转了个圈,又回来了

文章分类

全部博文(76)

文章存档

2013年(1)

2011年(8)

2010年(9)

2009年(22)

2008年(36)

我的朋友

分类: 嵌入式

2009-11-20 14:22:57

文件: linklist.rar
大小: 190KB
下载: 下载
在单链表中,每个节点只有一个指针域指向下一个节点的地址,因此每个节点包括数据域和一个节点域。
typedef int datatype;
typedef int* pdatatype;
typedef struct _Linklist
{
 datatype data;
 struct _Linklist *next;
}Linklist, *pLinklist;
 
对链表的操作主要有:
int Length(pLinklist head);   //有效节点的个数
pLinklist FindByIndex(pLinklist head, int i); //按序号查找
int FindByValue(pLinklist head, datatype data); // 按值查找
int ListInsert(pLinklist head, datatype x, int i); // 插入节点
int ListDelete(pLinklist head, int i); //删除节点
 
主要函数的算法以及注意点:
//-----------------------------------------------------------
//Function: FindByIndex
//    find one of the elements'value of the linklist.the index is com from 1;
//
//Parameters:
//    head: the linklist to be dealed with.
//    i: the index of the element; it come from 0;
//
//Returns:
//    if successed, return the point of the element.else return NULL;
//-----------------------------------------------------------
pLinklist FindByIndex(pLinklist head, int i)
{
 int j = 0;
 pLinklist p;
 if (NULL == head)
 {
  printf("\nError: The linklist is invalid\n");
  return NULL;
 }
 if (i <1)
 {
  printf("\nError:The index is invalid\n");
  return NULL;
 }
 p = head->next;
//注意在查找的过程中,变量j和节点p是有关联的,p是查找的当前节点,而j为当前节点的序号(有效节点从1算起)。
 while(p != NULL)
 {
  if ((++j) == i)
  {
   break;
  }
  else
  {
   p = p->next;
  }
 }

 
 if (j == i)
 {
  return p;
 }
 else
 {
  printf("\nError: cannot find the element\n");
  return NULL;
 }
}
 

//----------------------------------------------------------
//Function: ListInsert
//    insert one element into the linklist
//
//Parameters:
//    head: the linklist to be dealed with
//    x: the data to be input.
//    i: the index of the x to be insert in the linklist.
//        and the index come from 1;
//
//Returns:
//    if successed, return 1, else return 0;
//------------------------------------------------------------
int ListInsert(pLinklist head, datatype x, int i)
{
    pLinklist p, node;
 int j = 0;  //j is used to record the location of the current p;
 if (NULL ==head)
 {
  printf("\nThe linklist is invalid");
  return 0;
 }
 p = head;
 
//要插入节点,首先要找到要插入位置之前的那个节点。同样p为当前节点,j为当前节点的序号。 最后判断的条件为得到的j是否为i-1;
 while((j < i-1) && p->next != NULL)
 {
  j++;
  p = p->next;
 }
 if (j != i-1)
 {
  printf("\nCannot insert");
  return 0;
 }
 else

 {
  node = (pLinklist)malloc(sizeof(Linklist));
  node ->data = x;
  node->next = p->next;
  p->next = node;
  return 1;
 }
}
 
//-----------------------------------------------------------
//Function:ListDelete
//    Delete one of the elements of the linklist;
//
//Parameters:
//    head: the linklist to be dealed with;
//    i: the index of the element to be deleted, it come from 1;
//
//Returns:
//    if successed, return 1, else return 0;
//------------------------------------------------------------
int ListDelete(pLinklist head, int i)
{
 int j = 0; //the index of the current selected element.
 pLinklist p; //point to the element before the node that is to be deleted.
 pLinklist s; //the element that is to be deleted.
 if (NULL == head)
 {
  printf("\n The linklist is invalid");
  return 0;
 }
 p = head;
 while(j < i-1 && p->next != NULL)
 {
  j++;
  p = p->next;
 }
 if (j != i-1 || p->next == NULL)
 {
  printf("\n Cannot delete the element");
  return 0;
 }
 s = p->next;
 p->next = s->next;
 free(s);
 
 return 1;
}
阅读(1189) | 评论(0) | 转发(0) |
0

上一篇:顺序表

下一篇:循环链表

给主人留下些什么吧!~~