我尝试了structur中的结构列表编程和函数中的pointers.passing参数以及访问结构成员,我得到了以下错误请帮忙。
#include<stdio.h>
#include<stdlib.h>
enter code here
struct node {
int32_t data;
struct node *next;
};
void
SLL_insert_beg( struct node *head_lst
){
int32_t num_lst;
struct node *temp_lst,*new_lst; //intialising local variable.
new_lst = ( struct node* )malloc( sizeof(struct node) ); //Allocating dynamic memory for node new.
printf(" Enter data: ");
scanf("%d", &num_lst );
new_lst->data = num_lst; //inserting data into the datafield in node new.
if(head_lst == NULL) { //Condition to check list is empty in SLL_insert_beg() function.
new_lst->next = NULL; //Making pointer field to point null in node new.
head_lst = new_lst; //Making new node as head node.
} else {
new_lst->next = head_lst; //pointer field in new node points to the head node.
head_lst = new_lst; //Making new node as head node.
}
}
void SLL_display( struct node *head_lst
) {
struct node *temp_lst,*new_lst;
int32_t i_lst = 1 ; //intialising local variable.
if( head_lst == NULL ) { //Condition to check list is empty in SLL_display() function.
printf( " List is empty!! " );
} else {
temp_lst = head_lst; //Making head node as temp node.
printf( "\nThe linked list is:\n " );
printf( " Elements: " );
/*Below loop used to print all elements
in single linked list*/
while( temp_lst != NULL ) {
printf( "%d-> ", temp_lst->data );
temp_lst = temp_lst->next; //Making temp next node as temp node.
}
printf( "NULL\n " );
printf( " Position: " );
temp_lst = head_lst; //Making head node as temp node.
/*Below loop used to print no.of positions
in single linked list*/
while( temp_lst != NULL ) {
printf( "%d " , i_lst );
i_lst = i_lst + 1; //Incrementing variable 'i_lst' once.
temp_lst = temp_lst->next; //Making temp next node as temp node.
}
}
}
void main(void) { //sll_program(void) {
int32_t data;
int32_t SL_ch; //Intialised a variable used to choose the option in the below menu.
struct node *head_lst=NULL;
struct node *temp_lst,*new_lst;
while( 1 ) {
printf(" \n\nSingly Linked List(SLL)");
printf(" \n1.Insert beg \
\n2.diplay \
\n3.exit " );
printf("\nEnter your choice(1-4):");
scanf("%d",&SL_ch);
switch(SL_ch) {
case 1: //If choice is 1 calls insert at beginning function.
SLL_insert_beg( &head_lst
);
break;
case 2:
SLL_display( &head_lst
); //If choice is 2 calls the display function.
break;
case 3:
return;
default:
printf( "Please give the right choice!!" );
}
}
}
编译后这是我的代码的错误
main.c:在函数'main'中:
main.c:91:63:警告:传递'SLL_insert_beg'的参数1 不兼容的指针类型[默认启用]
main.c:12:6:注意:预期的'struct node *'但是参数是类型的 'struct node **' SLL_insert_beg(struct node * head_lst main.c:98:62:警告:从'SLL_display'传递参数1 不兼容的指针类型[默认启用]
); //如果选择2则调用显示功能。main.c:34:6:注意:预期的'struct node *'但是参数是类型的 'struct node **' void SLL_display(struct node * head_lst
答案 0 :(得分:1)
问题是声明是SLL_insert_beg(struct node *head_lst);
,调用是SLL_insert_beg(&head_lst);
- 所有这一切都被head_lst
声明为指向struct node
的指针(struct node *head_lst=NULL
)。这会导致&head_lst
成为指向struct node
的指针。
换句话说,正如编译器警告:expected 'struct node *' but argument is of type 'struct node **'
更改函数的声明,或更改调用。