码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-09-10 00:29:29      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:线性表__链式存储   线性表   

</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==data){
                return tnode;
            }
            tnode = tnode->nNode;
        }
     }
     return NULL;
 }
 int List_Length(List *list)
 {
     int count=-1;
     SqList * tlist = (SqList *)list;
     SqListNode * tnode = tlist->HNode;
     if(tlist !=NULL) {
            count++;
            while(tnode != tlist->ENode->nNode){
                count++;
                tnode = tnode->nNode;
            }

     }
     return count;
 }

 //添加输出函数用于具体测试
 void List_Print(List *list)
 {
    int count =0;
    SqList * tlist = (SqList *)list;
    SqListNode * tnode = tlist->HNode;
    if(tlist != NULL){
        while(tnode != tlist->ENode->nNode){
            printf("第%d个元素:%d\n",count,tnode->data);
            count++;
            tnode = tnode->nNode;
        }
    }
 }
 int main()
 {
     SqListNode a[5];
     int i;
     List * list = List_Create();
     for(i=0;i<5;i++){
        
        a[i].data = i+1;
        a[i].nNode = NULL;
     }
      for(i=0;i<5;i++){
        List_Insert(list,&a[i]);
     }
     List_Print(list);
     printf("链表的长度:%d\n",List_Length(list));
     printf("被删除的数据是:%d\n",List_Delete(list,3));
     List_Print(list);
     printf("链表的长度:%d\n",List_Length(list));
     printf("%d\n",((SqListNode *)List_Get(list,2))->data);
     return 0;
 }


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

标签:线性表__链式存储   线性表   

原文地址:http://blog.csdn.net/u013303425/article/details/39164221

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!