Я только начинающий, и у меня все еще есть небольшие проблемы, когда речь идет о массивах, структурах и прочем. Я только что изучил связанные списки и получил этот код шаблона. Элемент данных в структуре ранее был просто char data;
.
. Я хочу знать, как должен создаваться код, если я изменю его на char data[10];
. Я получаю эти ошибки, но я нене понимаю, что на самом деле означает ошибка и какие изменения я должен сделать.
linkedlist.c: In function 'insert':
linkedlist.c:87:16: error: assignment to expression with array type
newPtr->data = value; //place value in node
^
linkedlist.c:92:37: warning: comparison between pointer and integer
while(currentPtr != NULL && value > currentPtr ->data){ //loop to find the correct location in the list
^
linkedlist.c: In function 'delete':
linkedlist.c:117:12: warning: comparison between pointer and integer
if (value == (*sPtr)-> data){
^~
linkedlist.c:126:50: warning: comparison between pointer and integer
while(currentPtr != NULL && currentPtr -> data != value){ //loop to find the correct location in the list
^~
был бы очень признателен за помощь.
#include <stdio.h>
#include <stdlib.h>
struct listNode{
char data[10];
struct listNode *nextPtr;
};
typedef struct listNode ListNode;
typedef ListNode *ListNodePtr;
/* prototypes*/
void insert(ListNodePtr *sPtr, char value);
char delete(ListNodePtr *sPtr, char value);
int isEmpty(ListNodePtr sPtr);
void printList(ListNodePtr currentPtr);
void instructions(void);
int main(void){
ListNodePtr startPtr = NULL; //initially there are no nodes
unsigned int choice; //user's choice
char item; //char entered by user
instructions(); //display the menu
printf("%s", "?");
scanf("%u", &choice);
while(choice != 3){ //loop while user does not choose 3
switch(choice){
case 1:
printf("%s", "Enter a character:");
scanf ("\n%c", &item);
insert(&startPtr, item); //insert item in list
printList( startPtr );
break;
case 2: //delete an element
if(!isEmpty(startPtr)){ //if list is not empty
printf("%s", "Enter character to be deleted: ");
scanf("\n%c", &item);
if (delete(&startPtr, item)){ /* if character is found, remove it*/ /* remove item*/
printf("%c deleted.\n", item);
printList(startPtr);
}else{
printf("%c not found.\n\n", item);
}
}else{
puts("List is empty.\n");
}
break;
default:
puts("Invalid choice.\n");
instructions();
break;
} // end switch
printf("%s", "? ");
scanf("%u", &choice);
}
puts("End of run.");
}
void instructions(void){
puts("Enter your choice:\n"
" 1 insert an element.\n"
" 2 delete an element.\n"
" 3 end.");
}
/* insert a new value into the list in sorted order*/
void insert(ListNodePtr *sPtr, char value){
ListNodePtr newPtr; //pointer to new node
ListNodePtr previousPtr; //pointer to previous node in list
ListNodePtr currentPtr; //pointer to current node in list
newPtr = malloc(sizeof(ListNode)); //create node
if(newPtr != NULL){ //is space available*/
newPtr->data = value; //place value in node
newPtr->nextPtr = NULL; //node does not link to another node
previousPtr = NULL;
currentPtr = *sPtr;
while(currentPtr != NULL && value > currentPtr ->data){ //loop to find the correct location in the list
previousPtr = currentPtr; //walk to ...
currentPtr = currentPtr->nextPtr; //... next node
} /* end while*/
if(previousPtr == NULL){ //insert new node at beginning of list
newPtr->nextPtr = *sPtr;
*sPtr = newPtr;
}else{ // insert new node between previousPtr and currentPtr
previousPtr->nextPtr = newPtr;
newPtr->nextPtr = currentPtr;
}
}else{
printf("%c not inserted. No memory available.\n", value);
}
}
char delete(ListNodePtr *sPtr, char value){
ListNodePtr previousPtr; //pointer to previous node in list
ListNodePtr currentPtr; //pointer to current node in list
ListNodePtr tempPtr; //temporary node pointer
/* delete first node*/
if (value == (*sPtr)-> data){
tempPtr = *sPtr; //hold onto node being removed
*sPtr = (*sPtr)->nextPtr; //de-thread the node
free(tempPtr); //free the de-threaded node
return value;
}else{
previousPtr = *sPtr;
currentPtr = (*sPtr)->nextPtr;
while(currentPtr != NULL && currentPtr -> data != value){ //loop to find the correct location in the list
previousPtr = currentPtr; //walk to ...
currentPtr = currentPtr -> nextPtr; //... next node
}
if (currentPtr != NULL){ //delete node at currentPtr
tempPtr = currentPtr;
previousPtr->nextPtr = currentPtr->nextPtr;
free(tempPtr);
return value;
}
}
return '\0'; //we are returning a character that's why its in brackets
}
/* return 1 if the list is empty, 0 otherwise*/
int isEmpty(ListNodePtr sPtr){
return sPtr == NULL;
}
void printList(ListNodePtr currentPtr){
if (isEmpty(currentPtr)){ /* if list is empty*/
puts("List is empty.\n");
}else{
puts("The list is: ");
while (currentPtr != NULL){ //while not the end of the list
printf( "%c --> ", currentPtr->data );
currentPtr = currentPtr ->nextPtr;
}
puts("NULL\n");
}
}