The below code is able to add only one element to tree.
Please check what is wrong with this code.
I tried creating a binary search tree with 8 nodes with values taken from array, and when I print the tree only the first element is printed i.e the first element of array.
May be I am not good with working of structure and pointer. Can you say more about pointers and structure in c.
I thank for your help.
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *left;
struct node *right;
};
struct node *root=NULL;
void add(struct node**,struct node*);
void createtree(struct node **root){
int arr[8]={20,45,17,56,40,32,25,48};
int size=sizeof(arr)/sizeof(int);
struct node *temp=NULL;
for(int i=0;i<size;i++)
{
temp=(struct node *)malloc(sizeof(
struct node));
temp->data=arr[i];
temp->left=NULL;
temp->right=NULL;
add(root,temp);
}
}
void add(struct node **root,struct node *temp){
if(*root==NULL){
*root=temp;
return;
}
struct node *curr=*root;
if(temp->data< curr->data){
struct node *temper=curr->left;
add(&temper,temp);
}
else
{
struct node *temper=curr->right;
add(&temper,temp);
}
}
void print(struct node *root){
if(root!=NULL){
print(root->left);
printf("%d ",root->data);
print(root->right);
}
}
int main(){
createtree(&root);
print(root);
return 0;
}
Related
I'm learning link list. I create structure called Node to store the data and link to the next node. I have two doubts.
I dont understand why we are using the structure we are creating inside the same strucutre as element to store link. Is looks like a nested structure, will the element likn will have link.data and link.link in it? It is confusing
struct Node{
int data;
struct Node* link;
}node;
I created a typedef for the structure Node as node, so that I can declare variables and use as datatype inside the code. But it gives me error. I'm missing some understanding here.
If I use struct Node representation throught my code, it works fine. If I change it to node, i see lot of error. (except when I use it inside sizeof function)
My working code:
#include <stdio.h>
#include <stdlib.h>
struct Node{
int data;
struct Node* link;
}node;
struct Node head;
void Insert(int X);
void Print();
int main(){
head = NULL;
printf("How laby elements you need?\n");
int n,i,x;
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the elements:\n");
scanf("%d",&x);
Insert(x);
Print();
}
}
void Insert(int x)
{
struct Node* temp = (struct Node*)malloc(sizeof(node));
temp->data = x;
temp->link = head;
head = temp;
}
void Print(){
struct Node* temp = head;
while(temp != NULL)
{
printf("The list is: %d\n",temp->data);
temp = temp->link;
}
}
My not working code:
#include <stdio.h>
#include <stdlib.h>
struct Node{
int data;
struct Node* link;
}node;
node* head; // Error
void Insert(int X);
void Print();
int main(){
head = NULL;
printf("How laby elements you need?\n");
int n,i,x;
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the elements:\n");
scanf("%d",&x);
Insert(x);
Print();
}
}
void Insert(int x)
{
node* temp = (node*)malloc(sizeof(node)); // Error
temp->data = x;
temp->link = head;
head = temp;
}
void Print(){
node* temp = head; // Error
while(temp != NULL)
{
printf("The list is: %d\n",temp->data);
temp = temp->link;
}
}
I dont understand why we are using the structure we are creating inside the same strucutre as element to store link. Is looks like a nested structure
The struct Node structure doesn't contain itself. That would be impossible, as it would have infinite size.
Rather, it contains a struct Node * pointer. Specifically, this pointer will be used to locate the next node in the list. But that doesn't make the next node part of the current node.
I created a typedef for the structure Node as node
No, you didn't. There's no typedef in the code you posted.
struct Node {
int data;
struct Node* link;
} node;
is short for
struct Node {
int data;
struct Node* link;
}
struct Node node;
This creates a variable of type struct Node called node.
typedef struct Node {
int data;
struct Node* link;
} node;
is short for
struct Node {
int data;
struct Node* link;
};
typedef struct Node node;
This creates a type called node.
Note that I prefer to use lowercase for variables, and CamelCase for types, so I wouldn't use node for a type; I'd use Node instead.
typedef struct Node {
int data;
struct Node* link;
} Node;
I dont understand why we are using the structure we are creating
inside the same strucutre as element to store link. Is looks like a
nested structure, will the element likn will have link.data and
link.link in it? It is confusing
In this declaration
struct Node{
int data;
struct Node* link;
};
the data member link does not have the type struct Node. It has the pointer type struct Node *. So there is no nested structures. Each node of the list stores a pointer to the next node.
You forgot to place the typedef specifier in this declaration
struct Node{
int data;
struct Node* link;
}node;
As a result the name node in the above declaration means an object of the type struct Node. So the compiler issues an error for the following declaration
node* head;
and other usages of the name node in the program because node in this case is an identifier of an object instead of a type specifier.
You have to write
typedef struct Node{
int data;
struct Node* link;
}node;
node* head;
In this case the name node denotes a type specifier that is a typedef name for the type specifier struct Node..
This statement in main
head = NULL;
is redundant. The pointer head was already initialized as a null pointer in its declaration in the file scope because this declaration
node* head;
is equivalent to
node* head = NULL;
Pay attention to that it is not a good idea when functions depend on global variables as in your program on the pointer head declared in the file scope.
Do not forget at least to write a function that will free all the allocated memory for the list.
I want to create a function that search for a certain value in a linked list and then put the node containing it and the node before the one containing it in two separate nodes.
This is my code and I can't figure out why it does not work:
#include <stdio.h>
#include <stdlib.h>
struct node{
int val;
struct node* next;
};
typedef struct node node_t;
void printlist(node_t *head){
node_t *temp=head;
while (temp!=NULL){
printf("%d -",temp->val);
temp=temp->next;
}
printf("\n");
}
node_t *create_new_node(int value){
node_t *result=malloc(sizeof(node_t));
result->val=value;
result->next=NULL;
return result;
}
void *insert_after_node(node_t *node_to_insert,node_t *newnode){
newnode->next=node_to_insert->next;
node_to_insert->next=newnode;
}
void *find_node(node_t *head,int value,node_t *R,node_t *RA){
node_t *tmp=head;
while (R!=NULL && tmp!=NULL){
if (tmp->val==value)
R=tmp;
else{
RA=tmp;
tmp=tmp->next;
}
}
}
int main(){
node_t *head=NULL;
node_t *tmp;
node_t *R=NULL;
node_t *RA=NULL;
for (int i=0;i<25;i++){
tmp=create_new_node(i);
insert_at_head(&head,tmp);
}
find_node(head,13,R,RA);
printlist(head);
printlist(RA);
printlist(R);
}
Thank you !
You want to pass in the addresses of R and RA to find_node. And declare the params as **
void *find_node(node_t *head,int value,node_t **R,node_t **RA){
and then use (*R/ *RA) in the function
Call with
find_node(head,13,&R,&RA);
In your version, find_node cannot reassign the pointers passed in.
#include<stdio.h>
#include<stdlib.h>
void print();
void insert(int);
struct node
{
int data;
struct node* next;
};
struct node* head;
void insert(int x)
{
struct node* temp=(node*)malloc(sizeof(node));
temp->data=x;
temp->next=head;
head=temp;
}
void print(void)
{
struct node* temp=head;
printf("the linked list is:\n");
while(temp!=NULL)
{
printf("%d ",temp->data);
temp=temp->next;
}
printf("\n");
}
main()
{
head=NULL;
printf("how many numbers?\n");
int n,x,i;
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the number:\n");
scanf("%d",&x);
insert(x);
print();
}
return 0;
}
comiling error says "node undeclared" first use in program,although i made "node" a global one
this code is just for creating a linked list and adding user input values into the front of the linked list
insert(int x) function does the insertion work.
The error message is clear enough. In the function insert you should prepand the name node with the keyword struct.
void insert(int x)
{
struct node* temp=(struct node*)malloc(sizeof(struct node));
^^^^^^ ^^^^^^
Another way to escape the error is to use a typedef. For example
typedef struct node
{
int data;
struct node* next;
} node;
I presume when you say "first use" you mean the first line of insert where you try to allocate a node. But it is not the first; outside of the definition, that is 3 lines above, where you declare head to be a struct node*. (also note that inside the definition you use struct node* as well.)
The problem is that you did not define node; you defined struct node, and that is what you need to use.
You might be confused by C++, in which a struct tag becomes a typedef. Try this:
typedef struct node {
int data;
struct node* next;
} node;
Now you can just use node instead of struct node everywhere. Easier to read and easier to type.
Teacher said write program with four functions (print size or add, remove and print node). In line 17 error comes (Cannot convert int* into node). I can't find other way to represent this line so please help. Since it's my first experience with linked list, you can expect tons of error.
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node{ // Declaring the node (2 way)
int n;
struct node *next;
struct node *prev;
};
int *header; //Declaring header and size counter
int size;
int *finder(int number) //Function that return address of selected node
{
int *adr;
int c;
struct node *temp;
temp=header;
c=0;
while(c==number)
{
temp=temp->next;
c=c+1;
}
return adr;
}
void insert(int data,int number) //insert new node after specified node
{
int *adr;
adr=finder(number);
struct node *current;
struct node *previous;
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
current=adr;
previous=current->prev;
temp->n=data;
temp->next=current;
temp->prev=current->prev;
previous->next=temp;
current->prev=temp;
size=size+1;
}
void remove(int number) //remove node after selected one
{
int *adr;
adr=finder(number);
struct node *current;
struct node *previous;
struct node *neeext;
current=adr;
previous= current->prev;
neeext= current->next;
previous->next= neeext;
neeext->prev= previous;
}
void print(int number) //print data of node
{
int *adr;
adr=finder(number);
struct node *current;
printf("%d",current->n);
}
int main() //main function
{
int i,j,d;
size=0;
for(i=1;i<5;i=i+0)
{
if(i==1)
{
printf("%d",size);
}
scanf("%d",&j);
if(i==2)
{
scanf("%d",&d);
insert(d,j);
printf("inserted");
}
if(i==3)
{
remove(j);
printf("removed");
}
if(i==4)
{
print(j);
}
if(i==5)
{
return 0;
}
scanf("%d",&i);
printf("\n");
}
}
You should declare your header variable as struct node *header;, not as int*
I have to put nodes of binary search tree of every level in a linked list. That is if the height of the tree is 'h' then 'h+1' linked lists would be created and then each linked list would have all the nodes of each level. For this I have thought of creating an array of linked list. But the nodes are not being inserted in the list I guess. The code is as follows:-
struct node{
int data;
struct node *left;
struct node *right;
};
struct linked_list
{
int data;
struct linked_list *next;
};
linkedlistofbst(struct node *new,struct linked_list *n1[], int level)
{
//printf("%d ",new->data);
if(new==NULL)
{
return;
}
if(n1[level]==NULL)
{
struct linked_list *a =(struct linked_list *)malloc(sizeof(struct linked_list));
a->data=new->data;
a->next=NULL;
n1[level]=a;
printf("%d ",a->data);
}
else
{
struct linked_list *b =(struct linked_list *)malloc(sizeof(struct linked_list));
while(n1[level]->next!=NULL)
{
n1[level]=n1[level]->next;
}
b->data=new->data;
b->next=NULL;
n1[level]=b;
}
linkedlistofbst(new->left,n1,level+1);
linkedlistofbst(new->right,n1,level+1);
}
main()
{
struct linked_list *l=(struct linked_list *)malloc((a+1)*sizeof(struct linked_list));//'a' is the height of the tree
linkedlistofbst(new,&l, 0);//new is the pointer to the root node of the tree.
}
You are right there is problem with the second argument, so do the following
Make the following changes in the main:
For defining an array of linked list of size a+1 and initializing them to NULL
struct linked_list **l=(struct linked_list **)malloc((a+1)*sizeof(struct linked_list*));
for(i=0;i<(a+1);++i)
l[i]=NULL;
Then call the method as
linkedlistofbst(new,l, 0);
Therefore your method must look like
linkedlistofbst(struct node *new,struct linked_list **l, int level)
also make the following modification in else as:
else
{
struct linked_list *ptr=n1[level];
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=(struct linked_list *)malloc(sizeof(struct linked_list));
ptr->next->data=new->data;
ptr->next->next=NULL;
}