2015-04-19 74 views
1
Node *orderedInsert(Node *p, int newval) 
/* Allocates a new Node with data value newval 
    and inserts into the ordered list with 
    first node pointer p in such a way that the 
    data values in the modified list are in 
    nondecreasing order as the list is traversed. 
*/ 
{ 
    Node* current = NULL; 
    Node* prev = NULL; 
    Node* newNode = (Node*)malloc(sizeof(Node)); 
    newNode->next = NULL; 
    newNode->data = newval; 

    if(newNode == NULL) 
     printf("Could not allocate memory for new node"); 

    current = p; 

    if(p == NULL){ 

     current = newNode; 
     newNode->next = NULL; 
     newNode->data = newval; 
     return newNode; 
    } 
    else if(newval < p->data){ 
     newNode->next = p; 
     p = newNode; 
     return p; 
    } 
    else{ 
     prev = p; 
     current = current->next; 

     while(newNode->data > current->data && current != NULL){ 

     prev = current; 
     current = current->next; 
     } 
     if(current == NULL){ 
     prev->next = newNode; 
     newNode->next = NULL; 
     } 
     else{ 
     newNode->next = current; 
     prev->next = newNode; 
     } 
    } 

} 

void printList(FILE *outfile, Node *p) 
/* Prints the data values in the list with 
    first node pointer p from first to last, 
    with a space between successive values. 
    Prints a newline at the end of the list. 
*/ 
{ 

    Node* current = p; 

    while(current != NULL){ 
     fprintf(outfile, "%d ",current->data); 
     current = current->next; 
    } 
    fprintf(outfile, "\n"); 

} 

int main(int argc, char * argv[]) 
{ 
    assert(argc == 3); 
    Node * p = NULL; 
    int newval, retval; 
    int op; 

    FILE *in = fopen(argv[1],"r"); 
    assert(in != NULL); 
    FILE *out = fopen(argv[2],"w"); 
    assert(out != NULL); 
    do { 
     op = fgetc(in); 
    } while (op != EOF && isspace(op)); 

    while(op != EOF && op != 'q') { 
     switch(op) { 
    case 'i': 
     fscanf(in,"%d",&newval); 
     p = orderedInsert(p,newval); 
     printList(out,p); 
     printList(stdout,p); 
     break; 
    case 'c': 
     clearList(&p); 
     break; 
    default: 
     fclose(in); 
     fclose(out); 
     return 0; 
     } 
     do 
    op = fgetc(in); 
     while (op != EOF && isspace(op)); 
    } 

    fclose(in); 
    fclose(out); 
    return 0; 
} 

我在调试带有此错误的代码时遇到问题。有没有什么明显的我在我的代码中缺少和/或你有任何提示调试这个错误?我只是觉得自己失去了开始的地方,除了它甚至没有超过第一个列表条目(当列表是空的时候)。排除链接列表中的段错误(核心转储)

感谢

编辑:我都忍了修改后的代码,并输入数字比在列表中的第一大时,我现在只得到了段错误。

+0

线15'如果(newNode = NULL)',应该是:'如果(newNode == NULL)'(比较) – vsnyc

+0

卫生署!不再..谢谢你的帮助。我应该删除这个问题吗?我不知道这个网站的礼仪。 – MS535

+0

它有一个upvote所以你可以保留它,我会添加答案,你可以接受,因为它是有用的。干杯! – vsnyc

回答

1

添加有关调试C/C++程序的一些常规注释。

GDB是一个很好的工具,你必须把它附加到你的可执行文件然后运行程序。没有gui模式,但是人们在它周围构建了gui包装,例如, DDD

我发现很容易,如果我可以使用IDE,这取决于您使用MS Visual Studio中,Netbeans CPPEclipse CDTQtCreator和最新的C++ IDE JetBrains的CLion应该有一些工具比可以帮助你编写更好的环境。

测试一下,它没有遇到我的分段错误。我正在使用g ++版本(Ubuntu 4.8.2-19ubuntu1) 4.8.2在ubuntu 14.04上进行测试。逻辑错误是可能存在的,因为我是不看好的逻辑很清楚,从后面读文件1,然后插入文件2

编译:g++ Node.cpp

运行:./a.out /home/vsnyc/tmp/1.txt /home/vsnyc/tmp/2.txt

File: 1.txt 
i 12 i 4 i 6 i 9 q 

Node.cpp低于:

//Filename: Node.cpp 
#include <iostream> 
#include <stdio.h> 
#include <stdlib.h> 
#include <assert.h> 

struct Node { 
    Node *next; 
    int data; 
}; 

Node *orderedInsert(Node *p, int newval) 
/* Allocates a new Node with data value newval 
    and inserts into the ordered list with 
    first node pointer p in such a way that the 
    data values in the modified list are in 
    nondecreasing order as the list is traversed. 
*/ 
{ 
    Node* current = NULL; 
    Node* prev = NULL; 
    Node* newNode = (Node*)malloc(sizeof(Node)); 
    newNode->next = NULL; 
    newNode->data = newval; 

    if(newNode == NULL) 
     printf("Could not allocate memory for new node"); 

    current = p; 

    if(current == NULL){ 

     current = newNode; 
     newNode->next = NULL; 
     newNode->data = newval; 
     return current; 
    } 
    else if(newval < current->data){ 
     newNode->next = current; 
     current = newNode; 
     return current; 
    } 
    else{ 
     prev = p; 
     current = current->next; 

     while(newNode->data > current->data && current != NULL){ 

     prev = current; 
     current = current->next; 
     } 
     if(current == NULL){ 
     prev->next = newNode; 
     newNode->next = NULL; 
     } 
     else{ 
     newNode->next = current; 
     prev->next = newNode; 
     } 
    } 
} 

void clearList(Node ** p) { 
} 


void printList(FILE *outfile, Node *p) 
/* Prints the data values in the list with 
    first node pointer p from first to last, 
    with a space between successive values. 
    Prints a newline at the end of the list. 
*/ 
{ 

    Node* current = p; 

    while(current != NULL){ 
     fprintf(outfile, "%d ",current->data); 
     current = current->next; 
    } 

} 


int main(int argc, char * argv[]) 
{ 
    assert(argc == 3); 
    Node * p = NULL; 
    int newval, retval; 
    int op; 

    FILE *in = fopen(argv[1],"r"); 
    assert(in != NULL); 
    FILE *out = fopen(argv[2],"w"); 
    assert(out != NULL); 
    do { 
     op = fgetc(in); 
    } while (op != EOF && isspace(op)); 

    std::cout << op << "\n"; 

    while(op != EOF && op != 'q') { 
     switch(op) { 
    case 'i': 
     fscanf(in,"%d",&newval); 
     std::cout << newval << "\n"; 
     p = orderedInsert(p,newval); 
     printList(out,p); 
     printList(stdout,p); 
     break; 
    case 'c': 
     clearList(&p); 
     break; 
    default: 
     fclose(in); 
     fclose(out); 
     return 0; 
     } 
     do 
    op = fgetc(in); 
     while (op != EOF && isspace(op)); 
    } 

    fclose(in); 
    fclose(out); 
    return 0; 
} 
+0

仍然与修复的分段错误。我想还有别的东西。它只适用于列表中的第一个条目。 – MS535