Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Data structure implementation with Linked lists.
Could someone explain this part of the coding for data structures & linked list? I actually got this code from a textbook but no matter how I read the textbook, I still don't get the concept & what it means. Thanks :")
#include<stdio.h>
#include<stdlib.h>
struct listNode{
char data;
struct listNode *nextPtr
};
typedef struct listNode ListNode;
typedef ListNode *ListNodePtr;
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){
//some coding
}`
1 answer
struct listNode{
char data;
struct listNode *nextPtr
};
Computer, when I tell you that any region of memory is a struct called listNode
, that means that the region of memory contains a char
, which I will read from and write to using the name data
. The region of memory also contains a pointer that can point to another region of memory matching the same listNode
pattern; I will call this pointer nextPtr
. I'll let you, computer, figure out the details of how to concretely arrange these contents inside the region.
(What's that, computer? I'm missing a semicolon? Well, okay; I guess you understood what I meant anyway, but if it'll make you happier...)
typedef struct listNode ListNode;
typedef ListNode *ListNodePtr;
Computer, I'll use the word ListNode
(note the capitalization!) as an abbreviation for ‘a struct called listNode
’, and I'll use the word ListNodePtr
as an abbreviation for ‘a pointer to a ListNode
’.
void insert(ListNodePtr *sPtr, char value);
Having told you all that, computer, now I'm going to give you a preview of what's to come. I'm going to define a function called insert
that accepts a pointer to a ListNodePtr
(that's a pointer to a pointer, of course!) and a char
. I'm not going to tell you what that function does yet, but a human might recognize these words and imagine that the function needs to replace the ListNodePtr
to which the pointer points with another ListNodePtr
that itself points to a ListNode
containing the char
, in addition to the rest of the original list. I'll tell you all that later though, computer!
char delete(ListNodePtr *sPtr, char value);
Similarly, here's another preview of another function that accepts a pointer to a pointer and a char
; this one also returns a char
. Again, a human might infer, from the fact that this function accepts a pointer to a pointer, that the function needs to replace the ListNodePtr
with a new one somehow—but I'll tell you that later!
int isEmpty(ListNodePtr sPtr);
void printList(ListNodePtr currentPtr);
void instructions(void);
Here are some more previews, computer! That last one is a function that accepts nothing and returns nothing; how mysterious! I must be planning on doing some side effects in there, like printing some information to the screen.
int main (void){
//some coding
}
Finally, here's what you need to actually do when this program is run. I, uh, haven't decided yet. For now, do nothing at all.
Oh yeah, those function previews I gave you? Well, since I haven't told you to use those functions, I guess I don't need to keep my promises to tell you what they do, at least for now! But if I change this program to use them, I'll have to make good on those promises, won't I?
2 comment threads