c - 在我的 c 代码中删除重复项时出现错误

标签 c struct linked-list structure singly-linked-list

所以我的代码几乎完成了,在用于搜索重复项的 for 循环中,我的代码有一个小的逻辑错误。我能得到一些关于我哪里出错的提示吗?

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


struct studentID{
    int value;          //a data member which is an integer
    struct studentID *next;         //a data member which is a pointer to next node
};


typedef struct studentID STUDENTID;     //creating a nickname for struct studentID as STUDENTID
typedef STUDENTID *STUDENTIDPtr;        //creating a nickname for STUDENTID as STUDENTIDPtr


//Global variables
STUDENTIDPtr previousPtr;           //pointer to previous node in list
STUDENTIDPtr currentPtr;            //pointer to current node in list


int main(){
    int checker[5];
    int removeDuplicate[5];


    STUDENTIDPtr newPtr1;           //creating a pointer to create a new node
    STUDENTIDPtr newPtr2;           //creating a pointer to create a new node
    STUDENTIDPtr newPtr3;           //creating a pointer to create a new node
    STUDENTIDPtr newPtr4;           //creating a pointer to create a new node
    STUDENTIDPtr newPtr5;           //creating a pointer to create a new node


    //creation of the first node
    newPtr1 = malloc(sizeof(STUDENTID));            //This is when a node1 is created
    newPtr2 = malloc(sizeof(STUDENTID));            //This is when a node2 is created
    newPtr3 = malloc(sizeof(STUDENTID));            //This is when a node3 is created
    newPtr4 = malloc(sizeof(STUDENTID));            //This is when a node4 is created
    newPtr5 = malloc(sizeof(STUDENTID));            //This is when a node5 is created


    newPtr1 -> value = 5; // assign data in node 01
    newPtr1 -> next = newPtr2;


    newPtr2 -> value = 2; // assign data in node 02 
    newPtr2 -> next = newPtr3;


    newPtr3 -> value = 4; // assign data in node 03 
    newPtr3 -> next = newPtr4;


    newPtr4 -> value = 4; // assign data in node 04
    newPtr4 -> next = newPtr5;


    newPtr5 -> value = 1; // assign data in node 05
    newPtr5 -> next = NULL;


    currentPtr = newPtr1;

    printf("\n");
    printf("\n");

    /*Loop to print the last 5 digits in my student ID*/
    printf("The last 05 digits of my Student ID are,\n");
    while (currentPtr != NULL){         //while not the end of the list
        printf("%d - ", currentPtr->value);
        currentPtr = currentPtr ->next;
    }

    printf("\n");
    printf("\n");

    /*Assigning the 5 digits into an array*/
    checker[0] = newPtr1 -> value;
    checker[1] = newPtr2 -> value;
    checker[2] = newPtr3 -> value;
    checker[3] = newPtr4 -> value;
    checker[4] = newPtr5 -> value;


    /*This is a loop just to show that the values have successfully been addded to the array */
    printf("The List before removing duplicate digits is: ");
    for(int i=0; i<5; i++){
        printf("%d", checker[i]);
    }


    /*This would be the loop to remove duplicates*/
    for(int i=0; i<=5; i++){
        int temp;
        temp = checker[i];

        for(int j=0; j<=5; j++){
            if(temp != checker[j]){
                removeDuplicate[j] = temp;
            }
        }
    }

    printf("\n");
    printf("\n");

    /*This would be the loop to print the finalized list without duplicates*/
    printf("The List after removing duplicate digits is: ");
    for(int i=0; i<5; i++){
        printf("%d", removeDuplicate[i]);
    }

    printf("\n");

    return 0;
}

现在一切看起来都很好,但我确信我的 for 循环搜索重复项是我出错的地方。我的方法是取一位数字并将其与其他数字进行比较。如果没有任何重复项,则会将其放入数组中并移至下一个数字。

作为最终列表,它只能包含 4 位数字,删除重复数字后的列表为:5241

最佳答案

对于初学者来说,数组与列表没有任何共同点,除了它们存储数据成员 value 的值。列表的节点数。因此,您不会删除列表中的重复项。

数组 removeDuplicate未初始化

int removeDuplicate[5];

所以这个循环

    for(int j=0; j<=5; j++){
        if(temp != checker[j]){
            removeDuplicate[j] = temp;
        }
    }

有未定义的行为。而且循环中的这个条件j<=5提供数组之外的访问。

循环可以如下所示

int n = 0;

for ( int i = 0; i < 5; i++ )
{
    int j = 0;

    while ( j < n && checker[i] != removeDuplicate[j] ) j++; 

    if ( j == n ) removeDuplicate[n++] = checker[i];
}

然后

printf("The List after removing duplicate digits is: ");
for( int i = 0; i < n; i++){
    printf("%d", removeDuplicate[i]);
}

关于c - 在我的 c 代码中删除重复项时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58992307/

相关文章:

c - 将混合类型参数作为指针传递给函数,我认为

java - Java循环链表节点插入

java - 使用迭代器将对象添加到链表

c - 为什么预定义 gtk 函数会出现隐式声明错误?

c++ - "#define FOO(template)"是做什么的?

c++ - 将 C++ 中的结构转换为 Ruby

c - 链表哨兵节点以避免将指针重新分配给第一个节点

c++ - 使用 libusb-win32 与 USB 转 DMX512 接口(interface)通信,找不到任何端点

c - C 中使用共享变量和互斥锁进行线程同步

c - 为 C 结构编写 init 函数