| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2197 人关注过本帖
标题:链表的值是另一个链表
只看楼主 加入收藏
renkejun1942
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:不是这样
等 级:贵宾
威 望:33
帖 子:1645
专家分:5297
注 册:2016-12-1
结帖率:95.65%
收藏
已结贴  问题点数:100 回复次数:14 
链表的值是另一个链表
简单来说就是一个索引表,以前做题的时候,完成了的。
这不……前几天完成通用链表,就想着,用这组函数来重新写。
结果卡住了,打印出来的值是是乱码(好吧,也不是太乱):


21:00
问题已经被修正,现在可以正常运行了。
这并不是一个完善的索引表,因为并不检查输入的数据是否是合法的单词。
List.h 的声明和实现文件在下面的连接里可以找到。
https://bbs.bccn.net/viewthread.php?tid=476405&page=1&extra=page%3D1#pid2625770


程序代码:
/*打印效果
if
ok
and
jack
witch
where
balck
andy
way
for
of
zippo
oxford
english
chinese
dictionary
cell
element
new
learner


a:
and
andy

b:
balck

c:
cell
chinese

d:
dictionary

e:
element
english

f:
for

i:
if

j:
jack

l:
learner

n:
new

o:
of
ok
oxford

w:
way
where
witch

z:
zippo
*/


程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "d:\mylib\list.h"
#include "d:\mylib\getstring.h"
#define MAXLINE 101
int
com1( void *, void * );
int
com2( void *, void * );
void
Print1( void * );
void
Print2( void * );


int
main( void )
{
    List_T_P Root;
    List_T_P C_Root;
    List_T_P Temp, Head;
    List_T Value;
    char *Line;
    char ch;

    Root = NULL;

    while( NULL != ( Line = getword( stdin ) ) )
    {
        ch = tolower( Line[ 0 ] );
        if( NULL == ( Value.Element = malloc( sizeof( char ) ) ) )
            exit( EXIT_FAILURE );
        Value.Link = NULL;
        memmove( Value.Element, &ch, sizeof( char ) );

        if( NULL == ( C_Root = Find( Root, &Value, com1 ) ) )
        {
            Insert( &Root, &Value, sizeof( List_T ), com1 );
            C_Root = Find( Root, &Value, com1 ); 
        }
        else
            free( Value.Element );

        Temp = ( List_T_P )( C_Root->Element );
        if( NULL == Find( Temp->Link, Line, com2 ) )
            Insert( &Temp->Link, Line, strlen( Line ) + 1, com2 );
        free( Line );
    }

    Print( Root, Print1 );

    while( NULL != ( Head = First( Root ) ) )
    {
        Temp = ( List_T_P )Head->Element;
        Delete( &Temp->Link );
        DelFirst( &Root );
    }

    return 0;
}

int
com1( void *a, void *b )
{
    List_T_P A, B;
    char *ch1, *ch2;

    A = ( List_T_P )a;
    B = ( List_T_P )b;
    
    ch1 = ( char * )(A->Element);
    ch2 = ( char * )(B->Element);

    return *ch2 - *ch1;
}

int
com2( void *a, void *b )
{
    return strcmp( ( char * )b, ( char * )a );
}

void
Print1( void *a )
{
    List_T_P A;

    A = ( List_T_P )a;
    printf( "\n%c:\n",*( ( char * )( A->Element ) ) );
    Print( A->Link, Print2 );
}

void
Print2( void *a )
{
    printf( "%s\n", ( char * )a );
}




[此贴子已经被作者于2017-4-29 18:27编辑过]

搜索更多相关主题的帖子: 单词 通用 
2017-04-28 20:47
书生牛犊
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:星夜征程
等 级:贵宾
威 望:10
帖 子:1101
专家分:5265
注 册:2015-10-27
收藏
得分:50 
#include "d:\mylib\list.h"

这个文件显然只有提前下载并放到指定位置这个程序才能用。。。建议楼主可以把那个文件的内容也直接复制上来,然后用/*list.h注释*/ /*main.c*/备注区分

φ(゜▽゜*)♪
2017-04-28 22:02
renkejun1942
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:不是这样
等 级:贵宾
威 望:33
帖 子:1645
专家分:5297
注 册:2016-12-1
收藏
得分:0 
回复 2楼 书生牛犊
这个,我发了啊,在置顶贴里。
通用链表。

09:30 05/21 种下琵琶种子,能种活么?等待中……
21:50 05/27 没有发芽。
20:51 05/28 没有发芽。
23:03 05/29 没有发芽。
23:30 06/09 我有预感,要发芽了。
2017-04-28 22:10
书生牛犊
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:星夜征程
等 级:贵宾
威 望:10
帖 子:1101
专家分:5265
注 册:2015-10-27
收藏
得分:0 
回复 3楼 renkejun1942
嗯,代码放一块不是更好用唦

φ(゜▽゜*)♪
2017-04-28 22:15
renkejun1942
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:不是这样
等 级:贵宾
威 望:33
帖 子:1645
专家分:5297
注 册:2016-12-1
收藏
得分:0 
回复 4楼 书生牛犊
所有东西都放在一起,当然不好啊,毫无疑问。

最简单的例子,某些函数会管理一些私有函数,或者私有的全局变量。
如果都放在一共文件里,那么就需要处理函数名或变量名的重复的问题。
还有其他的。
实现和借口分离,方便对实现进行修改。

09:30 05/21 种下琵琶种子,能种活么?等待中……
21:50 05/27 没有发芽。
20:51 05/28 没有发芽。
23:03 05/29 没有发芽。
23:30 06/09 我有预感,要发芽了。
2017-04-28 22:17
九转星河
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:长长久久
等 级:贵宾
威 望:52
帖 子:5023
专家分:14003
注 册:2016-10-22
收藏
得分:50 
我对  "链表的值是另一个链表"所理解的意思是~现在有若干个链表~然后我想通过一个接口来获取其中一个链表头结点的方法~就通过一个值来做索引~这样感觉可以应用在图的邻接表上~~这种方法可以考虑写一个抽象的邻接表了~~~

[code]/*~个性签名:bug是什么意思?bug是看上去没有可能的东西实际上是有可能做到的 就是这样~2018-08-08更~*/[/code]
2017-04-28 23:34
renkejun1942
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:不是这样
等 级:贵宾
威 望:33
帖 子:1645
专家分:5297
注 册:2016-12-1
收藏
得分:0 
回复 6楼 九转星河
百度了一下邻接表,好复杂。

我发在主题贴里的代码是索引表,比喻来说,就是中文字典的拼音检索页。

[此贴子已经被作者于2017-4-28 23:50编辑过]


09:30 05/21 种下琵琶种子,能种活么?等待中……
21:50 05/27 没有发芽。
20:51 05/28 没有发芽。
23:03 05/29 没有发芽。
23:30 06/09 我有预感,要发芽了。
2017-04-28 23:47
九转星河
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:长长久久
等 级:贵宾
威 望:52
帖 子:5023
专家分:14003
注 册:2016-10-22
收藏
得分:0 
想不到我无意包含头文件list.h里面竟然是这个东东~~原来这种事情在之前已经有人做了~

程序代码:
//=============================================================================
//  Microsoft (R) Bloodhound (tm). Copyright (C) 1991-1993.
//
//  MODULE: list.h
//
//  Modification History
//
//  raypa           03/17/93    Created.
//=============================================================================

#if !defined(_LIST_)

#define _LIST_
#pragma pack(1)

//=============================================================================
//  The LINK structure is used to chain structures together into a list.
//=============================================================================

typedef struct _LINK *LPLINK;

typedef struct _LINK
{
    LPLINK     PrevLink;                    //... Previous or back pointer.
    LPLINK     NextLink;                    //... Next or forward pointer.
} LINK;

//=============================================================================
//  The LIST data structure.
//=============================================================================

typedef struct _LIST
{
    LPLINK      Tail;                       //... List Tail pointer.
    LPLINK      Head;                       //... List Head pointer.
    DWORD       Length;                     //... List Length.
} LIST;

typedef LIST *LPLIST;


#ifndef NO_INLINE

#ifndef INLINE
#define INLINE __inline
#endif

//=============================================================================
//  FUNCTIONS.
//=============================================================================

INLINE LPLINK WINAPI GetPrevLink(LPLINK Link)
{
    return Link->PrevLink;
}

INLINE LPLINK WINAPI GetNextLink(LPLINK Link)
{
    return Link->NextLink;
}

INLINE LPLINK WINAPI GetHeadOfList(LPLIST List)
{
    return List->Head;
}

INLINE LPLINK WINAPI GetTailOfList(LPLIST List)
{
    return List->Tail;
}

INLINE DWORD WINAPI GetListLength(LPLIST List)
{
    return List->Length;
}

//=============================================================================
//  FUNCTION: InitializeList()
//
//  Modification History
//                               
//  raypa           04/15/93        Created
//=============================================================================

INLINE LPLIST WINAPI InitializeList(LPLIST List)
{
    List->Head    = (LPLINK) 0L;
    List->Tail    = (LPLINK) 0L;
    List->Length  = 0;

    return List;
}

//=============================================================================
//  FUNCTION: AddLinkToLink()
//
//  Modification History
//                               
//  raypa           04/15/93        Created
//=============================================================================

INLINE VOID WINAPI AddLinkToLink(LPLINK DstLink, LPLINK SrcLink)
{
    //=========================================================================
    //  Make the source link point at the destination link.
    //=========================================================================

    SrcLink->PrevLink = DstLink;
    SrcLink->NextLink = DstLink->NextLink;

    //=========================================================================
    //  Make the destination link point at the source link.
    //=========================================================================

    DstLink->NextLink->PrevLink = SrcLink;
    DstLink->NextLink = SrcLink;
}

//=============================================================================
//  FUNCTION: AddToList()
//
//  Modification History
//                               
//  raypa           04/15/93        Created
//=============================================================================

INLINE LPLINK WINAPI AddToList(LPLIST List, LPLINK DstLink, LPLINK SrcLink)
{
    //=========================================================================
    //  Grow the list length by one.
    //=========================================================================

    List->Length++;

    //=========================================================================
    //  If SrcLink is NULL then add DstLink to the end of the list.
    //=========================================================================

    if ( SrcLink == (LPLINK) 0L )
    {
        //=====================================================================
        //  If the tail pointer is NULL then the list is empty.
        //=====================================================================

        if ( List->Tail != (LPLINK) 0L )
        {
            AddLinkToLink(List->Tail, DstLink);
        }
        else
        {
            DstLink->PrevLink = DstLink;
            DstLink->NextLink = DstLink;

            List->Head = DstLink;
        }

        return (List->Tail = DstLink);
    }

    //=========================================================================
    //  If DstLink is NULL then add SrcLink to the front of the list.
    //=========================================================================

    if ( DstLink == (LPLINK) 0L )
    {
        //=====================================================================
        //  If the head pointer is NULL then the list is empty.
        //=====================================================================

        if ( List->Head != (LPLINK) 0L )
        {
            AddLinkToLink(List->Head, SrcLink);
        }
        else
        {
            SrcLink->PrevLink = SrcLink;
            SrcLink->NextLink = SrcLink;

            List->Tail = SrcLink;
        }

        return (List->Head = SrcLink);
    }

    //=========================================================================
    //  Neither DstLink nor SrcLink is NULL so link them together.
    //=========================================================================

    AddLinkToLink(DstLink, SrcLink);

    return SrcLink;
}

//=============================================================================
//  FUNCTION: DeleteFromList()
//
//  Modification History
//                               
//  raypa           04/15/93        Created
//=============================================================================

INLINE LPLINK WINAPI DeleteFromList(LPLIST List, LPLINK Link)
{
    //=========================================================================
    //  If the list is empty then return NULL.
    //=========================================================================

    if ( List->Length != 0 )
    {
        //=====================================================================
        //  If the list length is not zero then we may need to fixup head and
        //  tail pointers in the event we delete the first or last link,
        //  respectively.
        //=====================================================================

        if ( --List->Length != 0 )
        {
            //=================================================================
            //  If we are deleting the front link then fixup the head pointer.
            //=================================================================

            if ( List->Head == Link )
            {
                List->Head = List->Head->NextLink;
            }

            //=================================================================
            //  If we are deleting the end link then fixup the tail pointer.
            //=================================================================

            if ( List->Tail == Link )
            {
                List->Tail = List->Tail->PrevLink;
            }

            //=================================================================
            //  Now we can unlink this link from the list.
            //=================================================================

            Link->NextLink->PrevLink = Link->PrevLink;
            Link->PrevLink->NextLink = Link->NextLink;
        }
        else
        {
            //=================================================================
            //  There is only one link on the list and we just deleted it.
            //=================================================================

            List->Head = (LPLINK) 0L;
            List->Tail = (LPLINK) 0L;
        }

        return Link;
    }

    return (LPLINK) 0L;
}

//=============================================================================
//  FUNCTION: AddToFrontOfList()
//
//  Modification History
//                               
//  raypa           04/15/93        Created
//=============================================================================

INLINE LPLINK WINAPI AddToFrontOfList(LPLIST List, LPLINK Link)
{
    return AddToList(List, (LPLINK) 0L, Link);
}

//=============================================================================
//  FUNCTION: AddToEndOfList()
//
//  Modification History
//                               
//  raypa           04/15/93        Created
//=============================================================================

INLINE LPLINK WINAPI AddToEndOfList(LPLIST List, LPLINK Link)
{
    return AddToList(List, Link, (LPLINK) 0L);
}

//=============================================================================
//  FUNCTION: DeleteFromFrontOfList()
//
//  Modification History
//                               
//  raypa           04/15/93        Created
//=============================================================================

INLINE LPLINK WINAPI DeleteFromFrontOfList(LPLIST List)
{
    return DeleteFromList(List, GetHeadOfList(List));
}

//=============================================================================
//  FUNCTION: DeleteFromEndOfList()
//
//  Modification History
//                               
//  raypa           04/15/93        Created
//=============================================================================

INLINE LPLINK WINAPI DeleteFromEndOfList(LPLIST List)
{
    return DeleteFromList(List, GetTailOfList(List));
}

#endif

#pragma pack()
#endif

[code]/*~个性签名:bug是什么意思?bug是看上去没有可能的东西实际上是有可能做到的 就是这样~2018-08-08更~*/[/code]
2017-04-28 23:47
renkejun1942
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:不是这样
等 级:贵宾
威 望:33
帖 子:1645
专家分:5297
注 册:2016-12-1
收藏
得分:0 
回复 8楼 九转星河
肯定啊,链表是很有用的数据结构。
别说链表,栈和队列的通用或者说泛型,也早就有人写了。

你发的那些代码,我扫了一眼,找时间仔细来看看。
我靠,这还是卖咖啡写的,一定要找时间来学习下。

我跟你,不会是第一个,也永远不会是最后一个。只要还有人学C,就会有人尝试将数据结构通用化。

[此贴子已经被作者于2017-4-28 23:55编辑过]


09:30 05/21 种下琵琶种子,能种活么?等待中……
21:50 05/27 没有发芽。
20:51 05/28 没有发芽。
23:03 05/29 没有发芽。
23:30 06/09 我有预感,要发芽了。
2017-04-28 23:53
九转星河
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:长长久久
等 级:贵宾
威 望:52
帖 子:5023
专家分:14003
注 册:2016-10-22
收藏
得分:0 
看到刚刚的一个回帖~忍不住顶上来~~这个程序只实现了一级查找~其实还可以实现二级查找三级查找……~~~
到底这不是就字典树么~

[code]/*~个性签名:bug是什么意思?bug是看上去没有可能的东西实际上是有可能做到的 就是这样~2018-08-08更~*/[/code]
2017-05-31 15:49
快速回复:链表的值是另一个链表
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021657 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved