Я новичок в структурах данных в C. Я хочу реализовать словарную структуру данных через связанный список.
Я ожидаю следующий вывод:
Pankaj = 10
Панкадж = 20
Кумар = 30
Кумар = 30
Кумар = 30
Но вместо этого я получаю некоторые предупрежденияи ошибки, которые я не могу исправить:
justprint.c: In function ‘push’:
justprint.c:19:15: warning: assignment to ‘int’ from ‘void *’ makes integer from pointer without a cast [-Wint-conversion]
node->phone = malloc(sizeof(int));
^
justprint.c: In function ‘main’:
justprint.c:50:12: warning: passing argument 1 of ‘push’ from incompatible pointer type [-Wincompatible-pointer-types]
push(&start, &phone[i], *name[i]);
^~~~~~
justprint.c:13:28: note: expected ‘struct nodetype *’ but argument is of type ‘struct nodetype **’
void push(struct nodetype *head, int *phone, char *name[]) {
~~~~~~~~~~~~~~~~~^~~~
justprint.c:50:31: warning: passing argument 3 of ‘push’ makes pointer from integer without a cast [-Wint-conversion]
push(&start, &phone[i], *name[i]);
^~~~~~~~
justprint.c:13:52: note: expected ‘char **’ but argument is of type ‘char’
void push(struct nodetype *head, int *phone, char *name[]) {
~~~~~~^~~~~~
Segmentation fault
Вот мой полный код на C:
Обратите внимание, что я добавляю новые данные в заголовок связанного списка.Я сталкиваюсь с проблемой печати строк и избавления от ошибок и предупреждений.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct nodetype{
int phone;
char **name;
struct nodetype * next;
};
struct nodetype *head = NULL;
void push(struct nodetype *head, int *phone, char *name[]) {
struct nodetype* node = malloc(sizeof(struct nodetype));
size_t size=strlen(*name);
node->name = malloc(size);
node->next = head;
int i;
for (i=0; i<size; i++)
*(node->name + i) = *(name + i);
(node->phone)=*phone;
*head = *node;
}
void print(struct nodetype *node)
{
while (node != NULL)
{
printf("%s=%d\n", *node->name, node->phone);
node = node->next;
}
}
int main()
{
struct nodetype *start = NULL;
int phone[] = {10, 20, 30, 40, 50}, i;
char *name[] = {"Pankaj","Pankaj","Kumar","Kumar","Kumar"};
for (i=0; i<5; i++)
push(start, &phone[i], &name[i]);
print(start);
return 0;
}
Я прочитал много онлайн-статей по связанному списку, словарю, указателям и т. Д., Каждая из которых имеет свой собственный метод реализации, оставляяменя смутило.Я прочитал столько, сколько мог.