首页 > 代码库 > 菜鸟之路--线性表__链式存储

菜鸟之路--线性表__链式存储

</pre><pre name="code" class="cpp">#include <stdio.h>
 #include <stdlib.h>
 //链表的定义
 typedef void List;
 typedef void ListNode;

//链式链表的相关定义
 typedef struct _tag_linklist  SqList;
 typedef struct _tag_linkNode SqListNode;
 typedef int Sqdata;
 //创建一个空链表,并返回
 List * List_Create();
 //将链表清空
 void  List_Clear(List * list);
 //销毁链表
 void  List_Destroy(List *list);
 //删除节点的数据为data的数据并返回删除的数据
 Sqdata  List_Delete(List *list,Sqdata data);
 //往链表内插入节点
 int List_Insert(List * list,ListNode * node);
 //得到链表中节点数据为data的节点指针
 ListNode * List_Get(List * list,Sqdata data);
 //返回链表的长度
 int List_Length(List *list);
//定义链式链表的结构
struct _tag_linklist{
            SqListNode * HNode;
            SqListNode * ENode;
  };
  struct _tag_linkNode{
        Sqdata data;
        SqListNode * nNode;
  };

  //线性顺序存储链表具体操作的相关实现
 List * List_Create()
 {
     SqList * ret = (SqList *)malloc(sizeof(SqList));
     if(ret != NULL){
        ret->HNode=ret->ENode = NULL;
     }
     return ret;
 }
 void  List_Clear(List * list)
 {
     SqList * tlist = (SqList *)list;
     if(tlist != NULL){
        tlist->HNode=tlist->ENode = NULL;
     }
 }
void  List_Destroy(List *list)
{
    free(list);
}
int List_Insert(List * list,ListNode * node)
{
    SqList * tlist = (SqList *)list;
    if(tlist !=NULL){
        if(tlist->HNode == NULL){
            tlist->HNode = tlist->ENode = (SqListNode *) node ;
        }else{
            tlist->ENode->nNode = (SqListNode *) node ;
            tlist->ENode = (SqListNode *) node ;
        }
        return 1;
    }

    return 0;


}
Sqdata  List_Delete(List *list,Sqdata data)
{
    SqList * tlist = (SqList *)list;
    SqListNode * dnode = (SqListNode *)List_Get(list,data);
    SqListNode rnode = *dnode;
    SqListNode * tnode = tlist->HNode;
    int i;
    if(tlist !=NULL && tnode != NULL && dnode != NULL){
        while(tnode->nNode != dnode){
            tnode = tnode->nNode;
        }
        tnode->nNode = tnode->nNode->nNode;
        free(tnode->nNode);
    }else{
        printf("ERROR:不存在该节点或链表不存在\n");
    }
    return rnode.data;
}
 ListNode * List_Get(List * list,Sqdata data)
 {
     SqList * tlist = (SqList *)list;
     SqListNode * tnode = tlist->HNode;
     if(tlist != NULL && tnode !=NULL)
     {
        while(tnode != tlist->ENode->nNode){
            if(tnode->data=http://www.mamicode.com/=data){>

菜鸟之路--线性表__链式存储