I have this assignment where I have to implement a stack to work with the following struct given by my professor. As you know I don't know how to implement a stack using the following struct.
struct elements{
char word;
struct elements *next;
}
I do know how to do that using the struct below...
struct elements{
struct student data;
struct elements *next;
}
To insert the data into the stack I'd use... I know dat the code below is correct. I do understand what is happening there. But I cannot see how I can do that using char word instead of struct student data; Someone could explain it to me? I'm not getting the idea.
int pushStack(Stack* ptr, struct student info){
Elemn* node = (Elemn*)malloc(sizeof(Elemn);
if(node == NULL && ptr == NULL){
printf("error.");
return 0;
}
node->student = info;
node->next = (*ptr);
*ptr = node;
return 1;
}
Have you tried to replace struct student by char?
Of course in the function header, but also in the Elemn class.
Related
I'm trying to allocate memory for the code,of which i've only included excerpts from the actual program, that follows below, the problem I am having is that i don't know how to allocate memory to the type Key that lies within BStree_node this leads to the issue of segmentation errors when i try to assign values to variables within Key.
typedef int Data_Item;
typedef char* Sub_Key;
typedef struct {Sub_Key key1; Sub_Key key2;} Key;
struct BStree_node{
Key key;
Data_Item data;
struct BStree_node *left, *right;
}
typedef struct BStree_node BStree_node;
typedef BStree_node** BStree;
BStree bs_tree_ini(void){
BStree tempTreePointer;
tempTreePointer = malloc(sizeof(BStree_node*));
BStree_node *tempNode;
tempNode = malloc(sizeof(BStree_node));
tempNode = NULL;
tempTreePointer = &tempNode;
return tempTreePointer;
}
You could initialize your node like this, using calloc to zero the memory to initialize all the fields properly:
BStree_node *init_node()
{
BStree_node *rval = calloc(1,sizeof(BStree_node)); // so all data & pointers are zeroed
return rval;
}
use it like this: init main, and only left. right stays zeroed: no right node for that main node.
int main()
{
BStree_node *head = init_node();
head->left = init_node();
...
return 0;
}
For an assignment I need to write code that takes a string as input and counts what word is the most used in that string.
We need to implement this using a Binary Search Tree of a structure called "WordCount" that contains a character array and the count of how many times the word appears.
This is how the structure is defined.
struct wordcount {
char word[80];
int count;
};
typedef struct wordcount WordCount;
A binary search tree must have a way to create nodes, this is the code:
BSTnode* createNode(void* item) {
BSTnode* newNode = (BSTnode*) malloc(sizeof(BSTnode));
newNode->item = item;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
When I store the WordCount structure inside of the Binary Search Tree and attempt to access the item, and then access the word, I get a Segmentation Fault.
If I just try to access the item of the tree, I get the char word array. This doesn't make sense because I'm storing the wordCount structure, so I should have to dereference it twice.
int main(int argc, char* argv[]) {
if (argc >= 1) {
WordCount* firstWord = (WordCount*) malloc(sizeof(WordCount));
strcpy(firstWord->word,argv[1]);
firstWord->count = 0;
BSTnode* BST = createNode(firstWord);
printf("%s", BST->item); // should be BST->item->word...but this does not work and says that it cannot find "word" which is apart of
}
/*int i;
char string[80];
for(i = 1; i < argc; i++) {
sscanf(argv[i], "%s", string);
//printf("%s ", string);
//insert(main, argv[i], wordCountCompare);
}*/
}
Any help is greatly appreciated. Let me know if my explanation was entirely vague or incomplete or if I'm overlooking something entirely.
I also want to clarify that the printf statements are merely for debugging only they won't be apart of the actual program...however the point still stands.
Definition of BSTnode:
struct bstnode {
void *item;
struct bstnode *left;
struct bstnode *right;
};
typedef struct bstnode BSTnode;
Your item should be of wordcount type.
struct bstnode {
wordcount *item;
struct bstnode *left;
struct bstnode *right;
};
typedef struct bstnode BSTnode;
You can't be dereferencing void pointers (which lead to the compilation error)
You're likely getting these errors because you're trying to dereference a pointer to NULL. Learn how to use the debugger!. Hint: break in main and step thru the function to see where the segmentation fault occurs
printf("%s", BST->item);
Printf's "%s" expects a string, and item is a structure. Try:
printf("%s" BST->item->word);
I've written this code with all correct understandings i have. please check my problems.
#include<stdio.h>
#include<stdlib.h>
// Define a structure for the dequeue elements
This structure is all good, with data, next, previous pointers.
typedef struct RanElmt_ {
void *data;
struct DeqElmt_ *prev;
struct DeqElmt_ *next;
void (*destroy)(void *data);
//Your Code here
} RanElmt;
THis is ok too, acording to what i think is correct.
typedef struct RandQ_{
int size;
struct RanElmt *head;
struct RanElmt *tail;
}RandQ;
RandQ * RandomizedQueue(void (*destroy)(void *data)){
RandQ *relmt = (RandQ*)malloc(sizeof(RandQ));
} // construct an empty randomized queue
int isREmpty(RandQ *rQ){
if ( rQ->size == 0)
return 1;
return 0;
} // is the queue empty?
int rsize(RandQ *rQ){
return rQ->size;
}
// return the number of items on the queue
ACtually this is only one function,(enqueue) I'm going to get the idea and code other functions(dequeue, sample etc..)
int enqueue(RandQ *rQ, const void *data){
RanElmt *relmt = (RanElmt*)malloc(sizeof(RanElmt));
relmt->data = (void*)data;
if (rQ->head == NULL){
relmt = rQ->head;
relmt = rQ->tail;
relmt->prev = NULL;
relmt->next = NULL;
}
else{
rQ->head = relmt;
}
(rQ->head)->prev = relmt;
relmt->prev = rQ->head;
rQ->head = relmt;
} // add the item
main(){
Deque(free);
printf(" okk \n");
}
THis program is giving these errors:
Errors i'm getting
In C struct tags and type names live in different name spaces. That is struct RanElmt and RanElmt are two different types, in addition struct RanElmt is not completely defined.
Your RandQ should be defined something like
typedef struct RandQ_{
int size;
struct RanElmt_ *head; // or RanElmt* head;
struct RanElmt_ *tail; // or RanElmt* tail;
}RandQ;
in addition your RanElmt is probably not what you want, maybe you meant:
typedef struct RanElmt_ {
void *data;
struct RanElmt_ *prev; // pointer to a struct of the same type
struct RanElmt_ *next; // pointer to a struct of the same type
void (*destroy)(void *data);
// You cannot put code here in C (or even a function definition AFAIK).
} RanElmt;
You have confused the struct tag and the typedeffed alias for the queue elements in the definition of the queue:
typedef struct RandQ_{
int size;
struct RanElmt *head;
struct RanElmt *tail;
} RandQ;
Here, the head and tail are of the type struct RanElmt. This struct doesn't exist in your program. You have a struct RanElmt_ (with trailing underscore) that you can also call ´RanElmtwithout thestructkeyword, because you have combined the struct definition with atypedef`.
The compiler still generates the code, because pointers to unknown structs are okay, unless you try to get at their data. Obviously the compiler can't access the struct fields if it doesn't know them.
There's no need for the underscore. The names of structs are in a separate namespace, so you can have both a struct called RandQ and a type (in global namespace) called RanQ. I recommend to use the same name for struct tag and aliassed type.
You can also get rid of the need to use the struct keyword inside the struct defnition if you separate the typedef from the struct definition:
typedef struct RanElmt RanElmt; // use just RanElmt from now on
struct RanElmt {
void *data;
RanElmt *prev;
RanElmt *next;
} RanElmt;
Your code has several other problems, but I think he program is in an early state, so I don't address them here.
I'm new to C and trying to compile this simple code, but it's not working and I'm not sure why. Can anyone help me?
int main(int argc, const char * argv[])
{
struct Node{
int value;
struct Node *next;
};
struct Node* x;
struct Node* y;
struct Node* z;
x = malloc(sizeof(Node));
y = malloc(sizeof(Node));
z = malloc(sizeof(Node));
return 0;
}
The compiler is complaining about the use of an undeclared identifier ‘Node’:
x = malloc(sizeof(Node));
y = malloc(sizeof(Node));
z = malloc(sizeof(Node));
Welcome to SO and the wonderful world of C!
A few pointers for you:
Syntax-ically there's no problem with defining a struct inside a function, but typically it's defined outside so that it can be used in other functions. For example:
main(){
struct nodedef{vars};
add_to_node(node var);
}
add_to_node(node var)
{
// How can I add a to a node when I don't know what that is?
}
The main problem with your code is that you aren't correctly referencing your node later on, if I declaire:
struct me {
int i;
};
Then anytime I reference this type of struct, I have to explicitly say struct again:
struct me myself;
myself = malloc(sizeof(struct me));
myself.i = 5;
The way to avoid this reuse of the struct keyword is to use the typedef:
typedef struct me {
int i;
}m;
m myself;
myself = malloc(sizeof(m));
myself.i = 5;
Last point is anytime you allocate some memory via malloc() make sure you call free() to release that memory:
free(myself);
Or else you'll have a memory leak.
Try sizeof(struct Node) instead.
struct Node should be used to refer to the structure. If you want the code above works, an alternative is typedef-ing the struct Node structure as
typedef struct Node {
int value;
struct Node *next;
} Node;
I'm trying to insert a node into the proper place (in order) in a linked list that is sorted in ascending order. I keep getting the GCC error "Error: dereferencing pointer to incomplete type". I've been working off of the code in this stackoverflow post. Below is my code:
typedef struct sNode {
int sid;
struct sNode *next;
}sNode;
sNode* addsNode (struct sNode *headPtr, int pSid)
{
struct sNode *ptr, *root = headPtr;
ptr = malloc (sizeof(struct sNode));
if(headPtr == NULL){ //In other code I've already check for a NULL list, pointer
ptr->sid = pSid;
}
else{
while(headPtr->next != NULL && headPtr->next->sid < pSid){
//while(headPtr->next != NULL){ --> Compiles when uncommented
headPtr = headPtr->next;
}//while
ptr->sid = pSid;
}//else
return root;
}//addsNode
I'm trying to return a pointer to the front of the list so other linked list manipulation can happen once returned. I've been doing some research on StackOverflow and it sounds like it's referring to the struct sNode that is causing the problem. I've seen different posts on using typedef to declare the struct. So I've tried it with and without using typedef to declare it. I've also seen posts suggesting #including & but that didn't work either. I'm using GCC 4.6.3 Any help is appreciated thanks!
typedef struct sNode {
int sid;
struct sNode *next;
};
You must typedef the struct to some name,
typedef struct sNode {
int sid;
struct sNode *next;
} sNode;
for example. Without the name you typedef it to, it's invalid anyway, and the type still must be referred to with the struct keyword.