BST Links and cases not working - c

I've a nested struct like this
typedef struct Node_link {
struct Node_base *parent, *left, *right;
}Node_link;
typedef struct Node_base {
struct Node_link link[2];
}Node_base;
typedef struct Node{
struct Node_base base;
int size;
int *address;
}Node;
Node_base *head[2] ={NULL, NULL};
//head[0] stores int size and head[1] it's corresponding address
The node has right, left and parent link, all are nested e.g node->left->link.parent=node. I've to maintain all links(parent, left and right) and delete nodes.
I've tried a lot of cases and still missing some. Can someone tell me what all cases I need to use? Or refer me to some material? I searched a lot but no success.
My insert function is as follows:
Node_base * insert(Node_base *location, Node_base *n) {
if (head[0]==NULL)
head[0]=n;
else
{
if (location==NULL){
location=n;
return location;
}
else{
if(((Node *)n)->size < ((Node *)location)->size){
if(location->link[0].left==NULL)
{
location->link[0].left=n;
location->link[0].left->link[0].parent=location;
}
else
location->link[0].left=insert(location->link[0].left,n);
return location;
}
}
And I've the same nested insert function for head[1] which stores the size of node inserted in head[0].

It's hard to tell what's going on here. Your code doesn't look remotely like any BST implementation I've ever seen. Why the need for the Node_Link struct? The pointers in the Node structs should define what the links are. Why the parent pointer? That shouldn't be needed in a standard BST implementation. All you should need is:
struct node {
node *left;
node *right;
void *data;
int size;
};
struct bst {
node *root;
};

Related

Allocating memory to structure within structure

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;
}

Random Queue in C

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.

Dereferencing pointer to incomplete type(with well defined structs) in C

I know there are at least 10 questions already about this, but they all point to something I am not doing.
In a header file I have...
typedef struct Node {
struct Node *next;
struct pgmap page;
} Node;
typedef struct linkedlist {
struct Node *head_ptr;
struct Node *tail_ptr;
} LList;
In my c file I have
struct LList mainList;
int main()
{
struct LList *root;
root = &mainList;
root->head_ptr = NULL;
root->tail_ptr = NULL;
...
}
On the root-> lines I get the dereferencing ptr... error. All the threads already on here point to a problem where people accidentally create anonymous structs, such as
typedef struct{
int a;
}; monkey
instead of
typedef struct monkey{
int a;
}; monkey
So what am I missing????
There is no type called "struct LList". The code "typedef struct linkedlist { ... } LList;" creates two type names: one is struct linkedlist, and the other is just LList (without "struct"). You thus need to change "struct LList" to "LList."

How to free a struct that is inside an other struct

I have the following two structs:
typedef struct label{
int id;
double p,*t,q,c;
int V[45];
struct label *next;
struct label *prev;
struct path *tail;
struct path *head;
}label;
typedef struct path{
int i;
struct path *Pperv;
struct path *Pnext;
}path;
void main (){
int i,j;
struct label *Current,*Head,*Tail;
struct path *test1,*path_head,*path_tail;
Head=(struct label*)malloc(1*sizeof(struct label));
Tail=(struct label*)malloc(1*sizeof(struct label));
Head->next=Tail;
Tail->prev=Head;
for (i=0;i<250000;i++)
{
Current=(struct label*)malloc(1*sizeof(struct label));
Current->t=(double*)malloc(15*sizeof(double));
Current->head=(struct path*)malloc(1*sizeof(struct path));
Current->tail=(struct path*)malloc(1*sizeof(struct path));
Current->head->Pnext=Current->tail;
Current->tail->Pperv=Current->head;
for (j=0;j<15;j++)
{
test1=(struct path*)malloc(1*sizeof(struct path));
test1->Pperv=Current->head;
test1->Pnext=Current->head->Pnext;
Current->head->Pnext->Pperv=test1;
Current->head->Pnext=test1;
test1->i=1;
Current->t[j]=23123.4323334;
}
Current->next=Tail;
Current->prev=Tail->prev;
Tail->prev->next=Current;
Tail->prev=Current;
Current->p=54545.323241321;
}
}
I just used an example of filling some of the variables in them so that I can make my question. What I am facing problem with is how to free the struct "Path" that is contained into the the first struct called name "Label".
I would me sth more than greatful if somenone could give me the code of how to correctly free both structs in C.
In general, you just need to be symmetrical with the calls to malloc/calloc:
label *current = malloc(sizeof(*current));
current->head = malloc(sizeof(*current->head));
...
free(current->head);
free(current);

BST with a recursion and given structures

I have to code some methods for a BST and I have some problems, let me explain.
I have the following structures :
struct node {
struct node *lChild;
struct node *rChild;
int value;
};
and
struct tree {
struct node *root;
};
along with the following functions :
struct tree* constructNewTree()
{
struct tree *T=malloc(sizeof(struct tree));
T->root=NULL;
return T;
}
and
struct node* constructNewNode(int i)
{
struct node *N=malloc(sizeof(struct node));
N->value=i;
N->lChild=NULL;
N->rChild=NULL;
return N;
}
And in my main I must call this (for example) :
int main()
{
struct tree *T;
T=constructNewTree();
insertKey(5,T);
insertKey(2,T);
insertKey(9,T);
return 0;
}
What I have to do is to create the function insertKey(int i, struct tree *T) using the recursion.
I wanted to do something like
void insertKey(int i, struct tree *T)
{
if (T->root==NULL) {
T->root=constructNewNode(i);
return;
}
else {
if (i<=T->root->value) {
T->root->lChild=constructNewNode(i);
else if (i>T->root->value) {
T->root->rChild=constructNewNode(i);
}
}
}
But it doesn't get very far, using the recursion would allow me to call insertKey again but I can't seem to use a node and a tree the same way.
Does anyone know how I could do that without altering the given structures?
Thank you very much.
Your insertKey takes a Tree as its argument. A Tree is only a pointer to the very top.
What I recommend you do is write a insertKey function that takes a Node for its argument. Also in this function, you have to check to see if there is another tree on the left/right child.
Currently you just construct a new node regardless of what is there. This will overwrite any previous insertions.

Resources