2015-04-19 63 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