I'm trying to create a dynamic list, which should read an input string and save every string into a new node of the lis. The program will print every node, after that i want to add a function to delete a node, but i'm getting a lot of errors, mostly because i'm not so good at using pointers and i'm new to data structures, can someone please help me?
EDIT: I corrected the code, now i'm not getting the errors anymore but the program seems to print for each node a single character, instead a string for each node, this is the current output:
// Inserting and deleting nodes in a list
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// self-referential structure
struct listNode {
char *data; // each listNode contains a character
struct listNode *nextPtr; // pointer to next node
};
typedef struct listNode ListNode; // synonym for struct listNode
typedef ListNode *ListNodePtr; // synonym for ListNode*
// prototypes
void insert(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
char item; // char entered by user
instructions(); // display the menu
printf("%s", "? ");
unsigned int choice; // user's choice
scanf("%u", &choice);
// loop while user does not choose 3
while (choice != 3) {
switch (choice) {
case 1:
printf("%s", "Enter a character: ");
scanf("%c", &item);
insert(&startPtr, &item); // insert item in list
printList(startPtr);
break;
default:
puts("Invalid choice.\n");
instructions();
break;
} // end switch
printf("%s", "? ");
scanf("%u", &choice);
}
puts("End of run.");
}
// display program instructions to user
void instructions(void)
{
puts("Enter your choice:\n"
" 1 to insert an element into the list.\n"
" 2 to delete an element from the list.\n"
" 3 to end.");
}
// insert a new value into the list in sorted order
void insert(ListNodePtr *sPtr, char *value)
{
ListNodePtr newPtr = malloc(sizeof(ListNode)); // create node
if (newPtr != NULL) { // is space available
newPtr->data= malloc(strlen(value)+1);
strcpy(newPtr->data, value);
newPtr->nextPtr = NULL; // node does not link to another node
ListNodePtr previousPtr = NULL;
ListNodePtr currentPtr = *sPtr;
// loop to find the correct location in the list
while (currentPtr != NULL && value > currentPtr->data) {
previousPtr = currentPtr; // walk to ...
currentPtr = currentPtr->nextPtr; // ... next node
}
// insert new node at beginning of list
if (previousPtr == NULL) {
newPtr->nextPtr = *sPtr;
*sPtr = newPtr;
}
else { // insert new node between previousPtr and currentPtr
previousPtr->nextPtr = newPtr;
newPtr->nextPtr = currentPtr;
}
}
else {
printf("%s not inserted. No memory available.\n", value);
}
}
int isEmpty(ListNodePtr sPtr)
{
return sPtr == NULL;
}
// print the list
void printList(ListNodePtr currentPtr)
{
// if list is empty
if (isEmpty(currentPtr)) {
puts("List is empty.\n");
}
else {
puts("The list is:");
// while not the end of the list
while (currentPtr != NULL) {
printf("%s --> ", currentPtr->data);
currentPtr = currentPtr->nextPtr;
}
puts("NULL\n");
}
}
Here is a sample of the output currently:
./test
Enter your choice:
1 to insert an element into the list.
2 to delete an element from the list.
3 to end.
? 1
Enter a character: The list is:
--> NULL
? Test
Enter a character: The list is:
--> T --> NULL
? Enter a character: The list is:
--> T --> e --> NULL
? Enter a character: The list is:
--> T --> e --> s --> NULL
? Enter a character: The list is:
--> T --> e --> s --> t --> NULL
?
Where i want it to be, for example: Word One -> Word Two etc.