无法通过while(1)循环迭代双链表-分段错误

问题描述 投票:0回答:1

我第一次来这里也是初学者,所以请多多包涵。

我得到了一个处理双向链表的作业,并且给了我们两个打印功能,它们可以向前和向后打印链表。无论如何,我们不允许更改这些功能。

我无法通过while(1)传递链表。我做了一个测试函数,在其中用while(pointer != NULL)迭代了列表,并且效果很好。

有什么建议吗?

这是我的主要功能。

FILE *fp;

struct node{
    int data;
    struct node* prev;
    struct node* next;
};
typedef struct node* listPointer;
listPointer headNode = NULL;
listPointer tailNode = NULL;


void delete(listPointer *head, listPointer removalNode);
//int traverseList(listPointer head, int data);
listPointer insertBefore(listPointer *head, listPointer nextNode, listPointer insertionNode);
listPointer findPosition(listPointer *head, int data);
listPointer findNode(listPointer *head, int data);
listPointer insertAtHead(listPointer *head, listPointer insertionNode);
listPointer insertAtBack(listPointer *head, listPointer insertionNode);
listPointer createNode(int data);
void print_forward(listPointer list);
void print_reverse(listPointer list);

void sortedInsert(listPointer *head,listPointer *tail,int data)
{

    listPointer p = malloc(sizeof(listPointer));
    listPointer temp = malloc(sizeof(listPointer));

    p->data = data;
    p->next = NULL;

    if ((*head) == NULL)
    {
        (*head) = p;
        (*tail) = p;
        (*head)->prev = NULL;
        return;
    }

    if ((p->data) < ((*head)->data))
    {
        p->prev = NULL;
        (*head)->prev = p;
        p->next = (*head);
        (*head) = p;
        return;
    }

    if ((p->data) > ((*tail)->data))
    {
        p->prev = (*tail);
        (*tail)->next = p;
        (*tail) = p;
        return;
    }

    temp = (*head)->next;
    while ((temp->data) < (p->data))
        temp = temp->next;


    (temp->prev)->next = p;
    p->prev = temp->prev;
    temp->prev = p;
    p->next = temp;
}

///test print function
void printlist(listPointer head) // this is a test function
{
  listPointer temp = head;
  while(temp != NULL) // works, but seg fault when is set to while(1) like line 282
  {
    printf("%d - ", temp->data);
    temp = temp->next;
  }
  printf("\n");
}

test print function reverse
void printlist2(listPointer head)
{
  listPointer temp = head;
  while( temp != NULL)
  {
    printf("%d - ", temp->data);
    temp = temp->prev;
  }
  printf("\n");
}


void main(int argc, char** argv)
{
    if(argc != 2)
    {
        fprintf(stderr, "usage: ./mp1 input_filename\n");
        exit(1);
    }

    FILE *fp = fopen(argv[1], "r");

    if(fp == NULL)
    {
        fprintf(stderr, "The input file does not exist.\n");
        exit(1);
    }
    char *op;

    listPointer temp;
    listPointer newnode;
    int data=0;
    int flag=0;
    char c;
    int num;
    ///////////////////////////////////////////////////////// TESTING

    data = 10;
    op = "INSERT";
    sortedInsert(&headNode,&tailNode,data);
    sortedInsert(&headNode,&tailNode,6);
    sortedInsert(&headNode,&tailNode,7);
    printlist(headNode);
    printf("\nhead %d\n",headNode->data);
    printf("tail %d\n",tailNode->data);
    printlist2(tailNode);

    print_forward(headNode); // seg fault
    ///////////////////////////////////////////////////////// TESTING


    /*while(!feof(fp)){
        fscanf(fp,"%s",op);
        fscanf(fp,"%d",&data);
        //printf("%s ",op);
        //printf("%d ",data);
        if(strcmp(op,"INSERT")==0){flag=1;}
        else if(strcmp(op,"DELETE")==0){flag=2;}
        else if(strcmp(op,"ASCEND")==0) {flag=3;}
        else if(strcmp(op,"DESCEND")==0) {flag=4;}
        switch(flag)
        {
            case 1:
                newnode = createNode(data);
                if(headNode == NULL) insertAtHead(&headNode,newnode);
                else{
                    temp = findPosition(&headNode,data);
                    insertBefore(&headNode,temp,newnode);
                }
                break;
            case 2:
                temp = findNode(&headNode,data);
                delete(&headNode,temp);
                break;
            case 3:
                print_forward(headNode);
                break;
            case 4:
                print_reverse(headNode);
                break;
            default: break;
        }
    }*/
    fclose(fp);
}

这是给定的打印功能。

void print_forward(listPointer list) {
    listPointer curr;
    FILE *outfile;
    outfile = fopen("mp1_result.txt", "a");
    if(list) {
        curr = list;
        while(1) {
            fprintf(outfile, "%d ", curr->data);
            printf("%d ", curr->data);
            curr = curr->next;
            if(curr == list) break;
        }
    }
    fprintf(outfile, "\n");
    printf("\n");
    fclose(outfile);
}

void print_reverse(listPointer list) {
    listPointer curr;
    FILE *outfile;
    outfile = fopen("mp1_result.txt", "a");
    if(list) {
        curr = list->prev;
        while(curr != list) {
            fprintf(outfile, "%d ", curr->data);
            printf("%d ", curr->data);
            curr = curr->prev;
        }
        fprintf(outfile, "%d ", curr->data);
        printf("%d ", curr->data);
    }
    fprintf(outfile, "\n");
    printf("\n");
    fclose(outfile);

欢迎和赞赏任何建议!

c while-loop linked-list segmentation-fault doubly-linked-list
1个回答
0
投票

给定的函数在假定圆形双向链表的情况下工作,请查看检查if(curr==list)-它正在检查第一个条目,而不是NULL。由于您的链接列表不是循环列表,因此在指向NULL时失败。

© www.soinside.com 2019 - 2024. All rights reserved.