所以我的代码几乎是完整的,在用于搜索重复项的 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 位数字作为The List after removing duplicate digits is: 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]);
}