C将元素插入升序链表中

时间:2015-11-19 16:25:17

标签: c linked-list

我的插入方法有问题,因为某些原因我最终得到了无限循环。这是我的结构:

mysql> SELECT LPAD('hi',4,'??');
        -> '??hi'
mysql> SELECT LPAD(123, 5,'0');
        -> 00123

这是我的插入方法:

#Define SW_HIDE             0
#Define SW_NORMAL           1
#Define SW_SHOWMINIMIZED    2
#Define SW_SHOWMAXIMIZED    3

Local lcResult
lcResult = ShellExec('C:\Windows\System32\restart.bat', '', 'C:\Windows\System32', SW_NORMAL)
If !Empty(m.lcResult) && error
    Messagebox(m.lcResult)
Endif

Function ShellExec
    Lparameters tcExecutable,tcParams,tcWorkingDir,tnShowType,tcOperation

    Declare Long ShellExecute In "shell32.dll" ;
        long HWnd, String lpszOp, ;
        string lpszFile, String lpszParams, ;
        string lpszDir, Long nShowCmd
    tcOperation  = Iif(Empty(m.tcOperation), 'Open', m.tcOperation)
    tcExecutable = Iif(Empty(m.tcExecutable), '', m.tcExecutable)
    tcParams     = Iif(Empty(m.tcParams), '', m.tcParams)
    tcWorkingDir = Iif(Empty(m.tcWorkingDir), '', m.tcWorkingDir)
    tnShowType   = Iif(Type('m.tnShowType') # 'N', SW_SHOWNORMAL, m.tnShowType)
    Local lnResult, lcError
    lcError = ''
    lnResult = ShellExecute(0,m.tcOperation,m.tcExecutable,m.tcParams,m.tcWorkingDir,m.tnShowType)
    If !( m.lnResult > 32 ) && Error
        lcError = GetShExecErrorMsg(m.lnResult)
    Endif
    Return m.lcError
Endfunc

Function GetShExecErrorMsg
    Lparameters tnErrNum
    Local Array aErrors[1]
    Local lcMessage, lcErrors,lnErrors,ix

    TEXT to m.lcErrors noshow
0,The operating system is out of memory or resources. \n
2,The specified file was not found. \n
3,The specified path was not found. \n
11,The .exe file is invalid (non-Win32® .exe or error in .exe image). \n
5,The operating system denied access to the specified file.  \n
27,The file name association is incomplete or invalid. \n
30,The DDE transaction could not be completed because
other DDE transactions were being processed. \n
29,The DDE transaction failed. \n
28,The DDE transaction could not be completed because the request timed out. \n
32,The specified dynamic-link library was not found.  \n
31,There is no application associated with the given file name extension.
This error will also be returned if you attempt to print a file that is not printable. \n
8,There was not enough memory to complete the operation. \n
26,A sharing violation occurred. \n
    ENDTEXT
    Clear
    lnErrors = Alines(aErrors,m.lcErrors,.T.,'\n')
    For ix=1 To m.lnErrors
        If ( Val(Chrtran(Left(aErrors[m.ix],;
                At(',',aErrors[m.ix])-1),Chr(13)+Chr(10),'')) = m.tnErrNum )
            lcMessage = Substr(aErrors[m.ix],At(',',aErrors[m.ix])+1)
            Exit
        Endif
    Endfor
    If Empty(m.lcMessage)
        lcMessage = 'An unspecified error occurred.'
    Endif
    Return m.lcMessage
Endfunc

我在这里发现了类似的问题:C - Inserting into linked list in ascending order但它并没有真正帮助我。

1 个答案:

答案 0 :(得分:3)

insert的第一个参数是指针。但是你需要一个指针指针(struct List **first)。

如果列表为空,则将 VALUE NULL传递给函数(方法内的变量first的值为NULL)。然后为其分配一个新的malloced值并返回。呼叫方的变量没有改变,你的记忆泄露了。

当您传递指针的指针时,变量first保存调用方法的变量的地址。这样,您可以重新分配它的价值。

指针,指针指针,返回函数指针的函数数组的指针......这就是C的有趣部分;)