我会尽可能地保持清醒。
我在一个名为“esercizio.h”的文件中有这个结构(我不能因为我的老师而改变它):
#ifndef ESERCIZIO_H
#define ESERCIZIO_H
struct ElemSCL{
int info;
struct ElemSCL* next;
};
typedef struct ElemSCL NodoSCL;
typedef NodoSCL* TipoSCL;
void accoda(TipoSCL* pscl, int i);
#endif
函数“accoda”必须将一个元素(int i)添加到循环链表的末尾(由TipoSCL * pscl指向)。我试图在一个名为“esercizio.c”的文件中编写函数体:
#include <stdio.h>
#include <stdlib.h>
#include "esercizio.h"
void accoda(TipoSCL* pscl, int i){
NodoSCL* temp = (NodoSCL*) malloc(sizeof(NodoSCL));
temp->info = i;
temp->next = temp;
if (pscl == NULL){
return;}
while ((*pscl)->next != *pscl){
*pscl = (*pscl)->next;}
(*temp)->next = (*pscl)->next; //Problems starts here
(*pscl)->next = *temp;
}
正如我在代码中注意到的那样,如果我不添加两行,那么一切都会好的。如果在函数中没有TypeSCL *但NodeSCL *我将使用:
temp->next = pscl->next;
pscl->next = temp;}
但我的老师决定使用TypeSCL * pscl而不是NodeSCL * pscl。
我有一个“test.h”文件......
#include "esercizio.h"
#ifndef TEST_H
#define TEST_H
char* toString(TipoSCL scl);
#endif
...和一个“test.c”文件,带有main()函数和所有输入,让我检查我的代码是否正常工作:
#include <stdlib.h>
#include <string.h>
#include "../libtest/libtest.h"
#include "test.h"
#include "esercizio.h"
const int NTEST=5;
TipoSCL input[5];
int add[5] = {1,2,3,4,5};
TipoSCL expected[5];
int main(int argc, char** argv){
input[0] = NULL;
input[1] = (TipoSCL) malloc(sizeof(NodoSCL));
input[1] -> info = 1;
input[1] -> next = input[1];
input[2] = (TipoSCL) malloc(sizeof(NodoSCL));
input[2] -> info = 1;
input[2] -> next = (TipoSCL) malloc(sizeof(NodoSCL));
input[2] -> next -> info = 2;
input[2] -> next -> next = input[2];
input[3] = (TipoSCL) malloc(sizeof(NodoSCL));
input[3] -> info = 1;
input[3] -> next = (TipoSCL) malloc(sizeof(NodoSCL));
input[3] -> next -> info = 2;
input[3] -> next -> next = (TipoSCL) malloc(sizeof(NodoSCL));
input[3] -> next -> next -> info = 3;
input[3] -> next -> next -> next = input[3];
input[4] = (TipoSCL) malloc(sizeof(NodoSCL));
input[4] -> info = 1;
input[4] -> next = (TipoSCL) malloc(sizeof(NodoSCL));
input[4] -> next -> info = 2;
input[4] -> next -> next = (TipoSCL) malloc(sizeof(NodoSCL));
input[4] -> next -> next -> info = 3;
input[4] -> next -> next -> next = (TipoSCL) malloc(sizeof(NodoSCL));
input[4] -> next -> next -> next -> info = 4;
input[4] -> next -> next -> next -> next = input[4];
expected[0] = (TipoSCL) malloc(sizeof(NodoSCL));
expected[0] -> info = 1;
expected[0] -> next = expected[0];
expected[1] = (TipoSCL) malloc(sizeof(NodoSCL));
expected[1] -> info = 1;
expected[1] -> next = (TipoSCL) malloc(sizeof(NodoSCL));
expected[1] -> next -> info = 2;
expected[1] -> next -> next = expected[1];
expected[2] = (TipoSCL) malloc(sizeof(NodoSCL));
expected[2] -> info = 1;
expected[2] -> next = (TipoSCL) malloc(sizeof(NodoSCL));
expected[2] -> next -> info = 2;
expected[2] -> next -> next = (TipoSCL) malloc(sizeof(NodoSCL));
expected[2] -> next -> next -> info = 3;
expected[2] -> next -> next -> next = expected[2];
expected[3] = (TipoSCL) malloc(sizeof(NodoSCL));
expected[3] -> info = 1;
expected[3] -> next = (TipoSCL) malloc(sizeof(NodoSCL));
expected[3] -> next -> info = 2;
expected[3] -> next -> next = (TipoSCL) malloc(sizeof(NodoSCL));
expected[3] -> next -> next -> info = 3;
expected[3] -> next -> next -> next = (TipoSCL) malloc(sizeof(NodoSCL));
expected[3] -> next -> next -> next -> info = 4;
expected[3] -> next -> next -> next -> next = expected[3];
expected[4] = (TipoSCL) malloc(sizeof(NodoSCL));
expected[4] -> info = 1;
expected[4] -> next = (TipoSCL) malloc(sizeof(NodoSCL));
expected[4] -> next -> info = 2;
expected[4] -> next -> next = (TipoSCL) malloc(sizeof(NodoSCL));
expected[4] -> next -> next -> info = 3;
expected[4] -> next -> next -> next = (TipoSCL) malloc(sizeof(NodoSCL));
expected[4] -> next -> next -> next -> info = 4;
expected[4] -> next -> next -> next -> next = (TipoSCL) malloc(sizeof(NodoSCL));
expected[4] -> next -> next -> next -> next -> info = 5;
expected[4] -> next -> next -> next -> next -> next = expected[4];
test_reset();
for (int i = 0; i < NTEST; i++) {
print_test_start(i+1);
printf("Funzione: accoda\n");
printf("Input: %s\n", toString(input[i]));
accoda(&input[i],add[i]);
test_compare_strings(toString(expected[i]),toString(input[i]));
print_test_end();
print_n_success("#Test superati: ");
}
print_test_result("Percentuale test corretti:");
}
char* toString(TipoSCL scl){
char* res = (char*) malloc(200*sizeof(char));
res[0] = '[';
res[1] = '\0';
TipoSCL aux = scl;
if (aux != NULL) {
char buf[10];
do{
sprintf(buf,"%d->",aux -> info);
strcat(res,buf);
aux = aux -> next;
}
while(aux != scl);
sprintf(buf,"|%d",aux -> info);
strcat(res,buf);
aux = aux -> next;
}
strcat(res,"]");
return res;
}
我的意思是“如果我不在我的代码中添加两行”,那么“一切正常” 当我运行我的程序时(感谢终端和 cd 和 make )没有
(*temp)->next = (*pscl)->next; //Problems starts here
(*pscl)->next = *temp;
测试运行没有问题(当然,它告诉我我没有正确的结果。但是如果我将这两行添加到我的代码中,我得到了“Segmentation fault:11”。
答案 0 :(得分:0)
我认为你的pscl指针必须一直到linklist的结尾,然后才能插入temp元素。 你能做的是
void add(TypeSCL* pscl, int i){
NodeSCL* temp = (NodeSCL*) malloc(sizeof(NodeSCL));
temp->info = i;
temp->next = temp;
if (pscl == NULL){
return;
while(pscl->next != pscl)
pscl = pscl->next;
temp->next = (*pscl)->next; //THINGS GO WRONG HERE...
(*pscl)->next = temp; //...AND HERE
}
&#39;
答案 1 :(得分:0)
尝试将添加功能更改为此::
void add(TypeSCL* pscl, int i){
NodeSCL* temp = (NodeSCL*) malloc(sizeof(NodeSCL));
temp->info = i;
temp->next = temp;
if (*pscl == NULL){
*pscl = temp;
return;}
NodeSCL *tempCheck = *pscl;
while(tempCheck->next != *pscl) {
tempCheck = tempCheck->next;
}
tempCheck->next = temp;
temp->next = (*pscl);
}
你做错的事情::
您需要意识到C中的所有内容都是按值传递的,因此如果传递指针,该指针也会按值传递。因此,当您的老师告诉您使用TypeSCL* pscl
时,这意味着NodeSCL **pscl
,而她是对的,因为您无法使用NodeSCL *pscl
,这可能会帮助您理解我为什么这样说:: What is the reason for using a double pointer when adding a node in a linked list?
此外,在您pscl == NULL
首先应该*pscl == NULL
的情况下,您需要将*pscl
设置为新节点。
接下来,如果你想在最后添加一个新节点,你应该使用while循环,如@Neha Chauhan所述。
接下来,您使用的最后两个语句::
(*temp)->next = (*pscl)->next;
(*pscl)->next = *temp;
您正在将新添加的节点的next
设置为链接列表的第2个元素,这没有任何意义。
我使用变量NodeSCL *tempCheck
移动到链表的最后一个节点!
所以,最后两行看起来像
tempCheck->next = temp;
temp->next = (*pscl);
最后,你需要用指针来研究你的基础知识。 (不要介意!)
修改:: 强>
为什么你的测试在没有最后两行的情况下工作::因为,使用你编写的代码,你的链表总是大小为1,在第一个元素之后没有添加任何元素!所以,测试运行,但它失败了!