Linux C 单向链表2

#include <stdio.h>  
#include <stdlib.h>  
   
/*节点类型结构体*/   
typedef struct node_t {  
    void* data;  //节点的数据域  
    struct node_t *next;  //节点的后继指针域  
}linknode_t, *linklist_t;  
      
linklist_t CreateEmptyLinklist(); //利用头结点来创建一个空链表(头结点)      
void DestroyLinklist(linklist_t list); //删除链表函数
void ClearLinklist(linklist_t list); //删除链表里面节点函数      
int EmptyLinklist(linklist_t list);//判断链表是否为空链表函数      
int LengthLinklist(linklist_t list);//获取链表长度函数       
int GetLinklist(linklist_t list, int at, void **data);//链表的某个节点获取函数      
int SetLinklist(linklist_t list, int at, void *data);//链表的某个节点设置函数         
int InsertLinklist(linklist_t list, int at,void *data); //链表的某个节点插入函数       
int DeleteLinklist(linklist_t list, int at);//删除链表某个节点函数        

linklist_t ReverseLinklist(linklist_t list); //链表反转函数


*
  * 入口参数:
  *                无
  *  返回值:
  *                linknode_t结构体;
  *   函数描述:
  *                利用头结点来创建一个空链表(头结点);
  *  注意事项:
  *               无;
  */
linklist_t CreateEmptyLinklist()  
{   
   linklist_t list;  
   list = (linklist_t)malloc(sizeof(linknode_t)); //结构指针分配空间;

   if (NULL != list) {  
      list->next = NULL; //下一个Node节点为空;
    }  
   return list;  
}  
/*
  * 入口参数:
  *                linklist_t结构指针;
  *  返回值:
  *                无;
  *   函数描述:
  *                删除链表函数;
  *  注意事项:
  *               无;
  */
void DestroyLinklist(linklist_t list)  
{  
  if (NULL != list) {  
     ClearLinklist(list);  
     free(list);  
  }  
}  
/*
  * 入口参数:
  *                linklist_t结构指针;
  *  返回值:
  *                无;
  *   函数描述:
  *                删除链表里面节点函数;
  *  注意事项:
  *                无;
  */
void ClearLinklist(linklist_t list)  
{  
  linknode_t *node; //指向Node节点的时候被移除  
  if (NULL == list) return;  

  while (NULL != list->next) {  
    node = list->next;  
    list->next = node->next;  
    free(node);  
   }  
   return;  
}  
/*
  * 入口参数:
  *                linklist_t结构指针;
  *  返回值:
  *                无;
  *   函数描述:
  *                获取链表长度函数;
  *  注意事项:
  *                无;
  */
int LengthLinklist(linklist_t list)  
{  
  int len = 0;  
  linknode_t *node; //迭代节点  

  if (NULL == list) return -1;  

  node = list->next; // 迭代节点指第一个数据节点
  while (NULL != node) {  
    len++;  
    node = node->next;  
  }  
  return len;  
}  
/*
  * 入口参数:
  *                linklist_t结构指针;
  *  返回值:
  *                无;
  *   函数描述:
  *                判断链表是否为空链表函数;
  *  注意事项:
  *                无;
  */
int EmptyLinklist(linklist_t list)  
{  
  if (NULL != list) {  
    if (NULL == list->next) { //判断链表是否为空,为空返回1
        return 1;  
    } else {    //判断链表是否为空,不为空返回1
        return 0;  
    }  
  } else {  
    return -1;  
  }  
}  
/*
  * 入口参数:
  *                list 结构指针;
  *                at 节点位置;
  *                data 获取数据;
  *  返回值:
  *                无;
  *   函数描述:
  *                链表的某个节点获取函数;  
  *  注意事项:
  *                无;
  */
int GetLinklist(linklist_t list, int at, void **data)  
{  
  linknode_t *node;   //迭代节点   
  int pos;        

  if (NULL == list) return -1;  
  if (at < 0) return -1;  

  node = list->next;  //第一个节点赋值迭代节点
  pos = 0;  
  while (NULL != node) {  
    if (at == pos) {  
        if (NULL != data) {  
            *data = node->data;  
        }  
        return 0;             
    }   
    node = node->next;  //指向下一个节点
    pos++;  
   }  
  return -1;  
}  
/*
  * 入口参数:
  *                list 结构指针;
  *                at  节点位置
  *                data 设置数据
  *  返回值:
  *                无;
  *   函数描述:
  *                链表的某个节点设置函数;  
  *  注意事项:
  *                无;
  */
int SetLinklist(linklist_t list, int at, void *data)  
{  
  linknode_t *node; //迭代节点     
  int pos;  
  int found = 0;  

  if (!list) return -1;    
  if (at < 0) return -1;

  node = list->next;  //初始化迭代节点
  pos = 0;  
  while (NULL != node) {  
    if (at == pos) {   
        found = 1; //插入节点的标志
        node->data = data;  //把数据赋值给节点数据域
        break;            
    }   
    node = node->next;  //指向下一个节点
    pos++;  
  }  
  if (1 == found) {  //如果设置数据成功返回为0;
    return 0;  
   } else {  //如果设置数据失败返回为-1;
    return -1;  
  }  
}  
/*
  * 入口参数:
  *                list 结构指针;
  *                at  节点位置
  *                data 设置数据
  *  返回值:
  *                无;
  *   函数描述:
  *                链表的某个节点插入函数;  
  *  注意事项:
  *                无;
  */
int InsertLinklist(linklist_t list, int at, void *data)  
{  
  linknode_t  *node_prev, *node_at, *node_new;  //初始化迭代节点
  int     pos_at;  
  int     found = 0;  

  if (NULL == list) return -1;  

  /* at must >= 0 */  
  if (at < 0) return -1;  

  node_new = malloc(sizeof(linknode_t));   //分配空间
  if (NULL == node_new) {  
     return -1;  
   }  
  node_new->data = data; /* assigned value */  
  node_new->next = NULL; //节点如果插入超过链表长度的位置,会接到尾节点后面,这样,node_new成了尾节点,node_new->next = NULL

  node_prev = list;  //跟随指针,帮助我们更好的定位  
  node_at = list->next; //遍历指针   
  pos_at = 0;  
  while (NULL != node_at) {  
    if (pos_at == at) {  
        found = 1;  //找到正确的位置,跳出循环  
        break;            
    }    
    node_prev = node_at;  //跟随指针先跳到遍历指针的位置  
    node_at = node_at->next; //遍历指针跳到下一个节点的位置
    pos_at++;  
  }  
 
  if (found) {  
    node_new->next = node_at;  //插入的节点next指向node_at
    node_prev->next = node_new; //插入节点的前一个节点   
   } else {  
    /*若是没找到正确的位置,即所插入位置超越了链表的长度,则接到尾节点的后面,
    同样,这样适用于空链表,这样我们可以建立一个空链表,利用这个函数,实现链表的初始化*/
    node_prev->next = node_new;  
   }  
   return 0;  
 }  
/*
  * 入口参数:
  *                list 结构指针;
  *                at  节点位置
  *  返回值:
  *                无;
  *   函数描述:
  *                删除链表某个节点函数;  
  *  注意事项:
  *                无;
  */
int DeleteLinklist(linklist_t list, int at)  
{  
  linknode_t  *node_prev, *node_at;  
  int     pos_at;  
  int         found = 0;  

  if (!list) return -1;  
  /* at must >= 0 */  
  if (at < 0) return -1;  

  node_prev = list;  
  node_at = list->next;  
  pos_at = 0;   

  while (NULL != node_at) {  
    if (pos_at == at) {  
        found = 1;  //找到对应的位置,跳出循环
        break;            
    }    
    node_prev = node_at;   //跟随指针,帮助我们更好的定位
    node_at = node_at->next;  //遍历指针跳到下一个节点的位置
    pos_at++;  
  }  
  if (found) {  
    node_prev->next = node_at->next;  //移除对应的节点
    free(node_at);  
    return  0;  
  } else {  
    return -1;  
  }  
}  
/*
  * 入口参数:
  *                list 结构指针;
  *  返回值:
  *                无;
  *   函数描述:
  *                链表反转函数;  
  *  注意事项:
  *                无;
  */
linklist_t ReverseLinklist(linklist_t list)  
{  
  linknode_t *node;   //迭代节点
  linknode_t *node_prev;  //迭代之前节点  
  linknode_t *node_next;  //迭代之后节点
            
  if (NULL == list) return NULL;  
  node_prev = NULL;  
  node = list->next;  
  while (NULL != node) {  
    node_next = node->next;  //迭代节点指向下一个节点
    /*当迭代节点指向最后一个节点,链表头节点指向最后一个节点,
     链表最后一个节点成为头节点*/  
    if (NULL == node_next) {  
        list->next = node;  
    }  
    /*反转链表直接的节点,确保当前节点指向先前的节点不是下一个节点*/      
    node->next = node_prev;        
   
    /*反转链表位置*/
    node_prev = node;  
    node = node_next;  
  }  
  return list;  


/**********************************测试程序******************************************/


typedef struct{
   char a[10];
   char b;
}data_t;
      

int main()  
{  
  int i;

  data_t *data;
 
  linklist_t p;

  data_t *c;
 
  p = CreateEmptyLinklist();

  for(i = 0;i < 5;i++)  
    {  
      c =(data_t *)malloc(sizeof(data_t));
      sprintf(c->a,"a%d",i);
      c->b=i;
      InsertLinklist(p,i,(void*)c);
    }

//ReverseLinklist(p);  
printf("The length of the list is:%d\n",LengthLinklist(p));  
 
 for(i = 0;i < 5;i++)  
    {
     GetLinklist(p,i,&data);
     printf("The NO of this list is:%s\n",data->a);
     printf("The NO of this list is:%d\n",data->b);
    }
 
return 0;
}






猜你喜欢

转载自blog.csdn.net/wuyu92877/article/details/78603621