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.
Related
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;
}
Code for linked list using memory allocation:
#include<stdio.h>
struct node
{
int val;
struct node *point;
};
int main()
{
struct node *head;
head=(struct node* ) malloc(sizeof(struct node ));
void create(struct node* start);
void print(struct node* start);
create(head);
print(head);
return 0;
}
void create(struct node* start)
{
int test,a=1;
struct node* cur;
cur=start;
while(scanf("%d",&test)!=EOF)
{ if(a!=1)
{
cur=cur->point;
}
cur->point=(struct node* ) malloc(sizeof(struct node ));
cur->val=test;
a++;
printf("---------------");
printf("%d\n",cur);
printf("%d\n",cur->val);
printf("%d\n",cur->point);
printf("%d\n",a);
printf("---------------");
}
cur->point=NULL;
return;
}
void print(struct node* start)
{
struct node* cur;
cur=start;
while(cur!=NULL)
{
printf("%d ",cur->val);
cur=cur->point;
}
return;
}
Code for linked list using struct variable:
#include<stdio.h>
struct node
{
int val;
struct node *point;
};
int main()
{
struct node *head,link;
head=&link;
void create(struct node* start);
void print(struct node* start);
create(head);
print(head);
return 0;
}
void create(struct node* start)
{
int test,a=1;
struct node* cur;
cur=start;
while(scanf("%d",&test)!=EOF)
{ if(a!=1)
{
cur=cur->point;
}
struct node link;
cur->point=&link;
cur->val=test;
a++;
printf("---------------");
printf("%d\n",cur);
printf("%d\n",cur->val);
printf("%d\n",cur->point);
printf("%d\n",&link);
printf("%d\n",a);
printf("---------------");
}
cur->point=NULL;
return;
}
void print(struct node* start)
{
struct node* cur;
cur=start;
while(cur!=NULL)
{
printf("%d ",cur->val);
cur=cur->point;
}
return;
}
When I use 2nd code it doesn't work correctly. It prints just two values.
In while loop of create() I use struct node type variable declaration.
That's why it should it would declare a new struct node type link variable and a new memory (struct node link) address should be seen in every execution.
So what's the problem?
I am a newbie learning data structure using C. Please explain in simple language.
I created the following library to insert,delete,search and print nodes in a binary tree.
#include <stdlib.h>
struct NODE
{
int code;
char subject[20];
struct NODE *left;
struct NODE *right;
};
void InOrder(struct NODE *R)
{
if (R==NULL)
return;
InOrder(R->left);
printf("%d %s\n",R->code,R->subject);
InOrder(R->right);
}
void PreOrder(struct NODE *R)
{
if (R==NULL)
return;
printf("%d %s\n",R->code,R->subject);
InOrder(R->left);
InOrder(R->right);
}
void PostOrder(struct NODE *R)
{
if (R==NULL)
return;
InOrder(R->left);
InOrder(R->right);
printf("%d %s\n",R->code,R->subject);
}
struct NODE *Search(struct NODE *R,int CODE,struct NODE **father)
{
if(R==NULL)
return NULL;
if(R->code==CODE)
{
*father=R;
return R;
}
if (CODE<R->code)
return Search(R->left,CODE,father);
else
return Search(R->right,CODE,father);
}
struct NODE * CreateNode(struct NODE T)
{
struct NODE *tmp;
tmp=(struct NODE *)malloc(sizeof(T));
*tmp=T;
tmp->left=tmp->right=NULL;
return tmp;
}
int Insert(struct NODE **R,struct NODE ND)
{
struct NODE *cur,*fath=NULL;
cur=Search(*R,ND.code,&fath);
if (cur)
return 0;
cur=CreateNode(ND);
if(fath==NULL)
*R=cur;
else
if(fath->code>ND.code)
fath->left=cur;
else
fath->right=cur;
return 1;
}
struct NODE *MinOfMax (struct NODE *ND)
{
struct NODE *tmp;
if (ND==NULL)
return NULL;
if(ND->right==NULL)
return NULL;
tmp=ND->right;
while(tmp->left!=NULL)
tmp=tmp->left;
return tmp;
}
struct NODE* Delete(struct NODE *R, int code)
{
if (R==NULL)
return R;
if (code<R->code)
R->left=Delete(R->left,code);
else if (code>R->code)
R->right=Delete(R->right,code);
else
{
if (R->left==NULL)
{
struct NODE *temp=R->right;
free(R);
return temp;
}
else if (R->right==NULL)
{
struct NODE *temp=R->left;
free(R);
return temp;
}
struct NODE *temp=MinOfMax(R->right);
R->code=temp->code;
R->right=Delete(R->right,temp->code);
}
return R;
}
When i try to insert a node in the binary tree,the program crashes.Here is my main:
int main(int argc,char* argv[])
{
typedef struct NODE NODE;
NODE *root=NULL;
NODE tmp;
Insert(&root,tmp);
return 0;
}
I tried to assign static values (for example code=100 and subject="Physics") but still the program crashes.Should i malloc something,change anything in my header file or do something entirely different?I'm stuck here for hours without finding any solution.Most insert functions out there assume that i only have one integer as data in the node,but i need to pass the entire node.
Your code basically does nothing. It seems you copy-pasted it from somewhere. I tried to figure it out and here's a code example. Basically you've to initializate a new node in the main when you try to insert it.
Note that's just an example, i didn't a full test.
int main(int argc,char* argv[])
{
typedef struct NODE NODE;
NODE *root=NULL;
NODE *tmp = malloc(sizeof(struct NODE));
tmp->code = 1; /*Just a number*/
strcpy(tmp->subject,"prova"); /*Put something in it*/
Insert(&root,*tmp); /* Try to insert it*/
PreOrder(root); /*Try to see if it has been inserted*/
return 0;
}
Your tmp node, which is going to be the newly inserted node is used uninitialized in your main(). Your compiler could have warned you for this, if you had used -Wall flag.
So let's take a look in your insert function:
int Insert(struct NODE **R, struct NODE ND)
{
struct NODE *cur,*fath=NULL;
cur = Search(*R, ND.code, &fath); // ND.code is junk, since ND is uninitialized
...
return 1;
}
which likely causes the segmentation fault.
root is too, you could initialize it to NULL in main().
Not the cause of your problem, but Do I cast the result of malloc? No.
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 think I got it wrong in newList. Typedef struct implementations must not be change. This is a lab assignment in my school.. Thanks in advance :)
#include<stdio.h>
typedef struct node *nodeptr;
struct node {
int item;
nodeptr next;
};
typedef nodeptr List;
List newList();
newList creates a header and returns a pointer to the header node
void display(List list);
void addFront(List list, int item);
List newList(){
List list;
list=(nodeptr)malloc(sizeof(List));
list->next=NULL;
return list;
} //I think my new list is incorrect..
void display(List list){
nodeptr ptr=list;
while(ptr!=NULL){
printf("%d",ptr->item);
ptr=ptr->next;
}
printf("\n");
}
void addEnd(List list, int item){
nodeptr temp, ptr;
temp=(List)malloc(sizeof(nodeptr));
temp->item=item;
temp->next=NULL;
if(list->next==NULL)
list=temp;
else{
ptr=list;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=temp;
}
}
I can't seem to add 10 from the list..
int main(void){
List list=newList();
addEnd(list,10);
display(list);
}
There are many ways you can go about this depending on what you actually want (because just creating a node doesn't make a lot of sense by itself). But generally you have three common options — create it on the stack, create that node in the global memory, or allocate it dynamically. Below are some examples.
On stack
#include <stdlib.h>
struct node {
int item;
struct node *next;
};
int main()
{
struct node head;
head.item = 0;
head.next = NULL;
/* Do something with the list now. */
return EXIT_SUCCESS;
}
In Global Memory
#include <stdlib.h>
struct node {
int item;
struct node *next;
};
static struct node head;
int main()
{
/* Do something with the list now. */
return EXIT_SUCCESS;
}
Dynamic Allocation
#include <stdlib.h>
#include <stdio.h>
struct node {
int item;
struct node *next;
};
int main()
{
struct node *head;
head = calloc(1, sizeof(struct node));
if (head == NULL) {
perror("calloc");
return EXIT_FAILURE;
}
/* Do something with the list now. */
return EXIT_SUCCESS;
}
You can read up on any of the above example in any introductory C book.
Hope it helps. Good Luck!