linked list implementation in C (debugging - incorrect output)

血红的双手。 提交于 2019-12-13 04:23:53

问题


I created this linked list program in C. It reads data in from a3data.txt (The contents of this text file are pasted after the output, below). In the text file, INSERT and REMOVE are commands. I want to be able to read the command INSERT, and insert the next integer (next line) into the list. The REMOVE command should remove the last node from the list. As you can see, my remove function is not working properly, and I don't understand why. Will someone please help me debug this?

OUTPUT

linux@computer ~/Documents/Data Structures/a3/code $ gcc exercise4.3.3.c
linux@computer ~/Documents/Data Structures/a3/code $ ./a.out

INSERT            0 5
INSERT            0 5 3
INSERT            0 5 3 19
         REMOVE   0 5 3 19
INSERT            1
         REMOVE   0
         REMOVE   0
         REMOVE   0
INSERT            4
INSERT            4 25
INSERT            4 25 5
         REMOVE   0 25 5
INSERT            4
INSERT            4 874
         REMOVE   0 874
         REMOVE   0 874
INSERT            8
INSERT            8 75
INSERT            8 75 22
INSERT            8 75 22 6
         REMOVE   0 75 22 6
INSERT            9
INSERT            9 31
INSERT            9 31 1
         REMOVE   0 31 1
         REMOVE   0 31 1
INSERT            419
INSERT            419 55
         REMOVE   0 55
INSERT            5

TEXT FILE

INSERT
5
INSERT
3
INSERT
19
REMOVE
INSERT
1
REMOVE
REMOVE
REMOVE
INSERT
4
INSERT
25
INSERT
5
REMOVE
INSERT
4
INSERT
874
REMOVE
REMOVE
INSERT
8
INSERT
75
INSERT
22
INSERT
6
REMOVE
INSERT
9
INSERT
31
INSERT
1
REMOVE
REMOVE
INSERT
419
INSERT
55
REMOVE
INSERT
5

CODE

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

struct node {
    int number;
    struct node *next;
};

/* prototypes */

void ins(struct node *llist, int number);
void rem(struct node *llist);
void sho(struct node *llist);

int main(void)
{
    int number;
    char command[6];
    struct node *llist;

    llist = (struct node *)malloc(sizeof(struct node));
    llist->number = 0;
    llist->next = NULL;

    FILE *file;
    file = fopen("a3data.txt", "r");

    if (file == NULL)
    {
        printf("\n----------------------------------------\n");
        printf("| Error.  Did not read file.  Exiting. |\n");
        printf("----------------------------------------\n\n");
        exit(1);
    }
    else
    {
        while ((fscanf(file, "%s", command)) != EOF)
        {
            if((strcmp(command, "INSERT"))==0)
                {
                    fscanf(file, "%d", &number);
                    printf("\nINSERT            ", number);
                    ins(llist, number);
                    sho(llist);
                }
            else if((strcmp(command, "REMOVE"))==0)
                {
                    printf("\n         REMOVE   ");                
                    rem(llist);
                    sho(llist);
                }
        }
    }

    printf("\n");
    free(llist);
    return(0);
}

void ins(struct node *llist, int number) 
{
    while(llist->next != NULL)
    {
        llist = llist->next;
    }

    llist->next = (struct node *)malloc(sizeof(struct node));
    llist->next->number = number;
    llist->next->next = NULL;
}

void rem(struct node *llist)
{
    struct node *temp;
    temp = (struct node *)malloc(sizeof(struct node));

    /* remove the node */
    temp = llist->next;
    free(llist);
    llist = temp; 

}

void sho(struct node *llist)
{
    while(llist->next != NULL)
    {
        printf("%d ", llist->number);
        llist = llist->next;
    }

    printf("%d", llist->number);
}

ATTEMPT AT SOLUTION (I THINK IT WORKS FOR LIFO)

void rem(struct node *llist)
{
    while(llist->next->next != NULL)
    {
        llist = llist->next;
    }

    llist->next = NULL;
}

来源:https://stackoverflow.com/questions/17711998/linked-list-implementation-in-c-debugging-incorrect-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!