I'm pretty new to C, and just started learning about linked lists. I'm trying to design a linked list structure in c, and was wondering how I could be able to add letters to my LL. In the past for ints, I simply used a for loop with i and added the number i into the LL as it increments. How would I be able to do the same with letters, such as A, B, C? Does it involve using ASCII? I searched online and found that the ASCII for A is 65 and was wondering if I could just print the ASCII somehow...Below is an attempt.
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
typedef struct node
{
void* dataPtr;
struct node* next;
} NODE;
typedef struct
{
NODE* head;
int count;
} LIST;
NODE* createNode (void* itemPtr)
{
NODE* nodePtr;
nodePtr = (NODE*) malloc (sizeof (NODE));
nodePtr->dataPtr = itemPtr;
nodePtr->next = NULL;
return nodePtr;
}
LIST* createList(void)
{
LIST* list;
list= (LIST*) malloc (sizeof (LIST));
if (list)
{
list->head = NULL;
list->count = 0;
}
return list;
}
bool insertList (LIST* list, void* itemPtr)
{
NODE* newPtr;
if (!(newPtr = (NODE*)malloc(sizeof(NODE)))) return false;
newPtr->dataPtr = itemPtr;
newPtr->next = list->head;
(list->count) ;
list->head = newPtr;
return true;
}
int main (void)
{
char* newDataP;
LIST* sList;
sList = createList();
for (int i = 1; i<=26; i ){
newDataP = (char*) malloc (sizeof(int));
*newDataP = 64 i;
insertList (sList, newDataP);
}
//print out the chars, I'm trying to do the 26 capitalized letters, which is why
//I started at 64
return 0;
}
CodePudding user response:
You code seems to be storing the letters correctly into the linked list. So you're problably printing the variable with printf("%d", var). In C, char are integers, so if you want to print a char you should use printf("%c", var)
If you want to improve your code you should use (char*)malloc(sizeof(char))
. And instead of writing 64
you can write directly 'A'
, because 'A'
is the value of A in ASCII that is 64
CodePudding user response:
Your use of void
pointers leads me to assume that you are trying to make some generic linked lists. You can do that, but it is usually more hassle than it is worth in C. So, if you just want a list of a specific type, you are generally better off making a specific data structure for that type. If you use void *
, you have to use pointers, that involves a lot of memory management, you lose almost all type checking, and I generally find it not worth doing in 95% of cases.
If you just want char
as the data in your links, use char
. Then, there is very little to it.
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
struct node
{
char data; // Use an actual char for the data
struct node *next;
};
// Unless you really need the count (it is rare that you do),
// then you don't need a list structure. You can use a link as
// the head of a list and get all of the same benefits. But here
// is a list.
struct list
{
struct node *data;
int count;
};
// For initialising a list, we just need to set data to NULL
// and count to zero, so we might as well return a struct.
// notice: doesn't return a pointer
struct list
newList(void)
{
return (struct list){.data = NULL, .count = 0};
}
struct node *
createNode(char data, struct node *next)
{
struct node *node = malloc(sizeof *node);
assert(node); // handle alloc failures
*node = (struct node){.data = data, .next = next};
return node;
}
bool insertList(struct list *list, char data)
{
list->data = createNode(data, list->data);
list->count ;
return true;
}
int main(void)
{
struct list list = newList();
const char *some_data = "abcdABCD";
for (const char *c = some_data; *c; c )
{
insertList(&list, *c);
}
// insert prepends, so the data is reversed compared
// to some_data.
for (struct node *n = list.data; n; n = n->next)
{
printf("looking at %c\n", n->data);
}
return 0;
}