2015-10-24 134 views
0

我有一个程序,其中三个值插入到链接列表中。当我尝试遍历列表时,我只能得到打印的第一个值。对不起,如果函数名称是令人困惑的我仍然对c新手,我试图使用函数和变量名称给我只是为了让我的生活更容易进行测试。我对调试器也很不熟悉,以及我将如何使用它来了解这里发生了什么。提前致谢。链接列表只打印第一个列表的第一个值

void program_header(char i[]); 
Node *allocateNode(int iNewInfo); 
Node *searchLL(Node *pHead, int iMatch, Node **ppPrecedes); 
Node *insertLL(Node **ppHead, int iNewInfo); 
void printLL(Node *pHead); 

int main(int argc, char *argv[]) 
{ 
    program_header(argv[0]); 
    insertLL(&pHead, 84); 
    insertLL(&pHead, 45); 
    insertLL(&pHead, 81); 
    printLL(pHead); 

    return 0; 
} 

    #include <stdlib.h> 
#include <string.h> 
#include <stdio.h> 

typedef struct Node 
{ 
    int iInfo; 
    struct Node *pNext; 
}Node; 

Node *pHead = NULL; 
Node *pNew = NULL; 
Node *pPrecedes = NULL; 

Node *allocateNode(int iNewInfo) 
{ 
    // to allocate a new node 
    pNew = malloc(sizeof(Node)); 
    if (pNew == NULL) 
     printf("Memory allocation error"); 
     pNew->iInfo = iNewInfo; 
     pNew->pNext = NULL; 
    return pNew; 
} 

Node *searchLL(Node *pHead, int iMatch, Node **ppPrecedes) 
{ 
Node *p; 
for (p = pHead; p != NULL; p = p->pNext) 
{ 
if (iMatch == p->iInfo) 
    printf("Found! %d\n", iMatch); 
    return p; 
    if (iMatch < p->iInfo) 
     return NULL; 
    *ppPrecedes = p; 
} 
return NULL; 
} 

Node *insertLL(Node **ppHead, int iNewInfo) 
{ 

Node *pFind;  
// see if it already exists 
pFind = searchLL(*ppHead, iNewInfo, &pPrecedes); 
if(pFind != NULL) 
    return pFind; 

// Doesn't already exist. Allocate a node and insert it 
pNew = allocateNode(iNewInfo); 
if(pPrecedes == NULL) 
{  //insert at head 
     pNew->pNext = *ppHead; 
     *ppHead = pNew; 
} 
else 
{  //insert after a node 
     pNew->pNext = pPrecedes->pNext; 
     pPrecedes->pNext = pNew; 
} 
return pNew; 
} 

void printLL(Node *pHead) 
{ 
    Node *p; 
    printf("iInfo Values\n"); 
    for (p = pHead; p != NULL; p = p->pNext) 
    { 
     printf("%d\n", p->iInfo); 
    } 
     p = pHead; 
} 

void program_header(char i[]) 
{ 

    int j, n = strlen(&i[2]); 
    char *name = &i[2], border[n], dash = '-'; 

    // loads dashes into array 
    for(j = 0; j < n; j++) 
     border[j] = dash; 
    border[j] = '\0'; 

    // print header 
    printf("\n~%s~\n~%s~\n~%s~\n\n" 
     , border, name, border); 
} 
+0

获取与调试器的更多familar。有下/近投票。 –

回答

1

我相信你的问题是在你的搜索方法。我已将其重新格式化以显示它目前的行为方式。为了帮助提高代码的可读性并避免这些类型的错误,您应该习惯于在if语句中使用大括号,即使它们只有一行。

这是你当前的代码

Node *searchLL(Node *pHead, int iMatch, Node **ppPrecedes) 
{ 
    Node *p; 
    for (p = pHead; p != NULL; p = p->pNext) 
    { 
     if (iMatch == p->iInfo) 
     { 
      printf("Found! %d\n", iMatch); 
     } 

     return p; 

     if (iMatch < p->iInfo) 
     { 
      return NULL; 
     } 
     *ppPrecedes = p; 
    } 
    return NULL; 
} 

请注意,在for循环,不管是否找到匹配与否,你总是返回p这实在是pHead。然后在您的插入代码中检查是否在列表中找到该项目。既然你总是回头,它认为该项目是在列表中,并从来没有添加一个新的项目。

我还没有测试过这个,但我相信这是你需要做的改变。如果该值已在列表中,您希望搜索返回节点。所以,你想回到p如果有匹配,否则要返回NULL

Node *searchLL(Node *pHead, int iMatch, Node **ppPrecedes) 
{ 
    Node *p; 
    for (p = pHead; p != NULL; p = p->pNext) 
    { 
     if (iMatch == p->iInfo) 
     { 
      printf("Found! %d\n", iMatch); 
      return p; 
     } 

     if (iMatch < p->iInfo) 
     { 
      return NULL; 
     } 
     *ppPrecedes = p; 
    } 
    return NULL; 
} 
+0

谢谢,代码使括号更有意义。也感谢您的解释。 –

0

纠正这种

Node *allocateNode(int iNewInfo) 
{ 
    // to allocate a new node 
    pNew = malloc(sizeof(Node)); 
    if (pNew == NULL) 
     printf("Memory allocation error"); 
     pNew->iInfo = iNewInfo; 
     pNew->pNext = NULL; 
    return pNew; 
} 

这个

Node *allocateNode(int iNewInfo) 
{ 
    // to allocate a new node 
    pNew = malloc(sizeof(Node)); 
    if (pNew){ 
     pNew->iInfo = iNewInfo; 
     pNew->pNext = NULL; 
    }else{ 
     printf("Memory allocation error"); 
    } 
    return pNew; 
} 
+0

编译,但仍然只打印第一个值。 –

+0

尽管从代码格式和清晰度的角度来看,这是很好的(并且解决了在解引用NULL指针时会发生段错误的问题),但它不能解决OP询问的问题。 – pstrjds

相关问题