getting "undefined reference to postorder" error in this code - c

#include<stdio.h>
struct tree
{
int data;
struct tree *left;
struct tree *right;
};
struct tree *node1[100];
struct tree *node2[100];
int top1;
int top2;
void add(struct tree **root,int item)
{
struct tree *ptr,*temp,*save;
ptr=*root;
if(ptr==NULL)
{
temp=(struct tree *)malloc(sizeof(struct tree));
temp->data=item;
temp->left=NULL;
temp->right=NULL;
*root=temp;
}
else
{
save=ptr;
if((ptr->data)>item)
ptr=ptr->left;
else
ptr=ptr->right;
while(ptr!=NULL)
{
save=ptr;
if((ptr->data)>item)
ptr=ptr->left;
else
ptr=ptr->right;
}
if((save->data)>item)
{
temp=(struct tree *)malloc(sizeof(struct tree));
temp->data=item;
temp->left=NULL;
temp->right=NULL;
save->left=temp;
}
else
{
temp=(struct tree *)malloc(sizeof(struct tree));
temp->data=item;
temp->left=NULL;
temp->right=NULL;
save->right=temp;
}
}
}
void pushright(struct tree *ptr)
{
top2=top2+1;
node2[top2]=ptr;
}
void pushleft(struct tree *ptr)
{
top1=top1+1;
node1[top1]=ptr;
}
struct tree * popleft()
{
struct tree *temp;
temp=node1[top1];
top1=top1-1;
return temp;
};
struct tree * popright()
{
struct tree *temp;
temp=node2[top2];
top2=top2-1;
return temp;
};
void traversal()
{
struct tree *ptr,*temp;
ptr=popleft();
while(ptr!=NULL)
{
printf("%d",ptr->data);
ptr=popleft();
}
if(ptr==NULL)
{
temp=popright();
if(temp!=NULL)
pushing(temp);
}
}
void pushing(struct tree *ptr)
{
while(ptr!=NULL)
{
pushleft(ptr);
if(ptr->right!=NULL)
{
top1=top1+1;
node1[top1]=NULL;
pushright(ptr->right);
}
ptr=ptr->left;
}
traversal();
}
void postoder(struct tree **root)
{
struct tree *ptr;
ptr=*root;
if(ptr==NULL)
printf("Tree is empty\n");
else
{
pushing(ptr);
}
}
int main()
{
int k,m;
struct tree *root;
root=NULL;
x:
printf("Enter which thing u wana do\n1.Insertion\n2.Post Order Traversal\n");
scanf("%d",&k);
switch(k)
{
case 1:
printf("Enter the number u wanna add\n");
scanf("%d",&m);
add(&root,m);
goto x;
case 2:
top1=0;
node1[top1]=NULL;
top2=0;
node2[top2]=NULL;
postorder(&root);
goto x;
}
}

There's a typo in your code
void postoder(struct tree **root)
should be
void postorder(struct tree **root)
// ^
Note that you're also missing an include of <stdlib.h>. Once you add this, you should remove the casts from the return of malloc.

Related

C language:Removing a node from a BST of strings

I'm trying to write a function, that will remove a node from the BST, which will be == with the word that the user inserts in the program, meaning that I ask the user, which word would he like to remove, he types that in and saves it under the variable key[MAX] in main. But something goes wrong when trying to delete the node, so my program just dies
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 15
typedef struct BST
{
char data[MAX];
struct BST *left;
struct BST *right;
int count;
} node;
node *create();
void insert(node *,node *);
void preorder(node *);
struct BST *minValueNode(struct BST *node)
{
struct BST *current = node;
while (current && current->left != NULL)
current = current->left;
return current;
}
struct BST *deleteNode(struct BST *root, char key[MAX])
{
if (root == NULL)
return root;
int cmp_rezult=strcmp(root->data, key[MAX]);
if (key[MAX] < root->data[MAX])
root->left = deleteNode(root->left, key);
else if (key[MAX] > root->data[MAX])
root->right = deleteNode(root->right, key);
else
{
if (root->left == NULL)//it the node has no children or only 1
{
struct BST *temp = root->right;
free(root);
return temp;
}
else if (root->right == NULL)
{
struct BST *temp = root->left;
free(root);
return temp;
}
//if the node have 2 kids
struct BST *temp = minValueNode(root->right);
//sorting
root->data[MAX] = temp->data[MAX];
//deleting
root->right = deleteNode(root->right, temp->data[MAX]);
}
return root;
}
int main()
{
char key[MAX];
char ch;
node *root=NULL,*temp;
do
{
temp=create();
if(root==NULL)
{
root=temp;
}
else
{
insert(root,temp);
}
printf("\nDo you want to enter more(y/n)?");
ch=getch();
}
while(ch=='y'||ch=='Y');
printf("\nPreorder Traversal: ");
preorder(root);
printf("\nWho shall we delete:");
scanf("%s", &key[MAX]);
deleteNode(root, key[MAX]);
return 0;
}
node *create()
{
node *temp;
printf("\nEnter data:");
temp=(node*)malloc(sizeof(node));
fgets(&temp->data,MAX,stdin);
temp->left=temp->right=NULL;
temp->count=1;
return temp;
}
void insert(node *root,node *temp)
{
int cmp_rezult=strcmp(temp->data,root->data);
if(cmp_rezult<0)
{
if(root->left!=NULL)
insert(root->left,temp);
else
root->left=temp;
}
if(cmp_rezult>0)
{
if(root->right!=NULL)
insert(root->right,temp);
else
root->right=temp;
}
if(cmp_rezult==0)
{
root->count++;
}
}
void preorder(node *root)
{
if(root!=NULL)
{
printf("\n%s Repeats:%d time(s)",root->data, root->count);
preorder(root->left);
preorder(root->right);
}
}
This is basically the same code as above, but, in BST *deleteNode I've updated the code at cmp_result so now it's written correctly, after that I've updated the if statements under the cmp_result, also I've updated the code in the same function, going from root->data[MAX] = temp->data[MAX]; to strcpy(root->data,temp->data);, which is the correct way to do it
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 15
typedef struct BST
{
char data[MAX];
struct BST *left;
struct BST *right;
int count;
} node;
node *create();
void insert(node *,node *);
void preorder(node *);
struct BST *minValueNode(struct BST *node)
{
struct BST *current = node;
while (current && current->left != NULL)
current = current->left;
return current;
}
struct BST *deleteNode(struct BST *root, char key[MAX])
{
if (root == NULL)
return root;
int cmp_result = strcmp(key, root->data);
if (cmp_result < 0)
root->left = deleteNode(root->left, key);
else if (cmp_result > 0)
root->right = deleteNode(root->right, key);
else
{
if (root->left == NULL)
{
struct BST *temp = root->right;
free(root);
return temp;
}
else if (root->right == NULL)
{
struct BST *temp = root->left;
free(root);
return temp;
}
struct BST *temp = minValueNode(root->right);
strcpy(root->data,temp->data);
root->right = deleteNode(root->right, temp->data);
}
return root;
}
int main()
{
char key[MAX];
char ch;
node *root=NULL,*temp;
do
{
temp=create();
if(root==NULL)
{
root=temp;
}
else
{
insert(root,temp);
}
printf("\nDo you want to enter more(y/n)?");
ch=getch();
}
while(ch=='y'||ch=='Y');
printf("\nPreorder Traversal: ");
preorder(root);
printf("\n\nWho shall we delete:");
fgets(key,MAX,stdin);
printf("\n%s", key);
deleteNode(root, key);
printf("\nPreorder Traversal: ");
preorder(root);
return 0;
}
node *create()
{
node *temp;
printf("\nEnter data:");
temp=(node*)malloc(sizeof(node));
fgets(temp->data,MAX,stdin);
temp->left=temp->right=NULL;
temp->count=1;
return temp;
}
void insert(node *root,node *temp)
{
int cmp_rezult=strcmp(temp->data,root->data);
if(cmp_rezult<0)
{
if(root->left!=NULL)
insert(root->left,temp);
else
root->left=temp;
}
if(cmp_rezult>0)
{
if(root->right!=NULL)
insert(root->right,temp);
else
root->right=temp;
}
if(cmp_rezult==0)
{
root->count++;
}
}
void preorder(node *root)
{
if(root!=NULL)
{
printf("\n%s Repeats:%d time(s)",root->data, root->count);
preorder(root->left);
preorder(root->right);
}
}

Free malloc, using Linus method of deletion in a linked list

I been stuck in this problem for hours and I still cannot free the malloc of my program. for some reason it says I have 16 bytes in 1 blocks, I have been trying to free the malloc using a function freeList() in the program but it seems to not be working, if you can catch what is my error I will really appreciate it!
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*head;
void append(int num)
{
struct node *temp,*right;
temp= (struct node *)malloc(sizeof(struct node));
temp->data=num;
right=(struct node *)head;
while(right->next != NULL)
right=right->next;
right->next =temp;
right=temp;
right->next=NULL;
}
void add( int num )
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node)); //*
temp->data=num;
if (head== NULL)
{
head=temp;
head->next=NULL;
}
else
{
temp->next=head;
head=temp;
}
}
void addafter(int num, int loc)
{
int i;
struct node *temp,*left,*right;
right=head;
for(i=1;i<loc;i++)
{
left=right;
right=right->next;
}
temp=(struct node *)malloc(sizeof(struct node));
temp->data=num;
left->next=temp;
left=temp;
left->next=right;
return;
}
void insert(int num)
{
int c=0;
struct node *temp;
temp=head;
if(temp==NULL)
{
add(num);
}
else
{
while(temp!=NULL)
{
if(temp->data<num)
c++;
temp=temp->next;
}
if(c==0)
add(num);
else if(c<count())
addafter(num,++c);
else
append(num);
}
}
int delete(int num)
{
struct node **temp;
temp=&head;
while(*temp!=NULL)
{
if((*temp)->data==num)
{
*temp= (*temp)->next;
return 1;
}
else
{
*temp= &((*temp)->next);
}
}
return 0;
}
void freeList(){
struct node *temp;
int i=0;
while(head!=NULL){
temp=head;
head=head->next;
free(temp->data);
free(temp);
i++;
}
printf("free %d records...\n",i);
}
void display(struct node *r)
{
r=head;
if(r==NULL)
{
return;
}
while(r!=NULL)
{
printf("%d ",r->data);
r=r->next;
}
printf("\n");
}
int count()
{
struct node *n;
int c=0;
n=head;
while(n!=NULL)
{
n=n->next;
c++;
}
return c;
}
int main()
{
int i,num;
struct node *n;
head=NULL;
while(1)
{
printf("\nList Operations\n");
printf("===============\n");
printf("1.Insert\n");
printf("2.Display\n");
printf("3.Size\n");
printf("4.Delete\n");
printf("5.Exit\n");
printf("Enter your choice : ");
if(scanf("%d",&i)<=0){
printf("Enter only an Integer\n");
exit(0);
} else {
switch(i)
{
case 1: printf("Enter the number to insert : ");
scanf("%d",&num);
insert(num);//*
break;
case 2: if(head==NULL)
{
printf("List is Empty\n");
}
else
{
printf("Element(s) in the list are : ");
}
display(n);
break;
case 3: printf("Size of the list is %d\n",count());
break;
case 4: if(head==NULL)
printf("List is Empty\n");
else{
printf("Enter the number to delete : ");
scanf("%d",&num);
if(delete(num))
printf("%d deleted successfully\n",num);
else
printf("%d not found in the list\n",num);
}
break;
case 5: freeList();
return 0;
default: printf("Invalid option\n");
}
}
}
return 0;
}
My main problem is in the function freeList() when running the code freeList does not free the malloc.
void freeList(){
struct node *temp;
int i=0;
while(head!=NULL){
temp=head;
head=head->next;
free(temp->data);
free(temp);
i++;
}
printf("free %d records...\n",i);
}

Inorder traversal for binary search tree, shows some errors

This is the function which I am using for BST inorder Traversal
void inOrder(struct node* root)
{
if(root==NULL)
{
return;
}
inOrder(root->left);
printf("Key:%d,Pointer:%p\n",root->key,root);
inOrder(root->right);
}
The structure for the same is
struct node{
int key;
struct node *left,*right;
};
When I run the function I get two 0 extra elements for the following inserts:
root=insert(root,7);
insert(root,4);
insert(root,10);
insert(root,3);
insert(root,5);
insert(root,1);
insert(root,2);
insert(root,0);
insert(root,8);
insert(root,14);
insert(root,6);
insert(root,9);
insert(root,16);
insert(root,12);
insert(root,15);
insert(root,17);
inOrder(root);printf("\n");
And the insert function is given below:
struct node *insert(struct node * root,int ele)
{
struct node *temp=newNode(ele);
struct node *tree=root;
struct node *saver;
if(root==NULL)
{
root=temp;
}
else
{
while(tree!=NULL)
{
saver=tree;
if(ele<tree->key)
tree=tree->left;
else
tree=tree->right;
}
if(ele<saver->key)
{
saver->left=temp;
}
else
{
saver->right=temp;
}
}
return saver;
}
The output shows two 0s apart from the main elements, which I added. I have recently learned the Binary-Search-Tree, and I tried to implement it, Can someone please help me rectify this error. Thank you in advance
in insert
if(root==NULL)
{root=temp;
}
must be
if(root==NULL)
{
saver=temp;
}
because you return saver (not initialized in your version)
I suppose also in your main the code is not what you give but something like
struct node * root = NULL;
root=insert(root,7);
insert(root,4);
...
If I define newNode like that :
struct node * newNode(int ele)
{
struct node * r = malloc(sizeof(struct node));
r->key = ele;
r->left = r->right = NULL;
return r;
}
and I do the other changes :
pi#raspberrypi:/tmp $ gcc -g -pedantic -Wextra n.c
pi#raspberrypi:/tmp $ ./a.out
Key:0,Pointer:0xf04078
Key:1,Pointer:0xf04058
Key:2,Pointer:0xf04068
Key:3,Pointer:0xf04038
Key:4,Pointer:0xf04018
Key:5,Pointer:0xf04048
Key:6,Pointer:0xf040a8
Key:7,Pointer:0xf04008
Key:8,Pointer:0xf04088
Key:9,Pointer:0xf040b8
Key:10,Pointer:0xf04028
Key:12,Pointer:0xf040d8
Key:14,Pointer:0xf04098
Key:15,Pointer:0xf040e8
Key:16,Pointer:0xf040c8
Key:17,Pointer:0xf040f8
The full code is :
#include <stdio.h>
#include <stdlib.h>
struct node{
int key;
struct node *left,*right;
};
void inOrder(struct node* root)
{
if(root==NULL)
{return;}
inOrder(root->left);
printf("Key:%d,Pointer:%p\n",root->key,root);
inOrder(root->right);
}
struct node * newNode(int ele)
{
struct node * r = malloc(sizeof(struct node));
r->key = ele;
r->left = r->right = NULL;
return r;
}
struct node *insert(struct node * root,int ele)
{
struct node *temp=newNode(ele);
struct node *tree=root;
struct node *saver;
if(root==NULL)
{
saver=temp;
}
else
{
while(tree!=NULL)
{
saver=tree;
if(ele<tree->key)
tree=tree->left;
else
tree=tree->right;
}
if(ele<saver->key)
{
saver->left=temp;
}
else
{
saver->right=temp;
}
}
return saver;
}
int main()
{
struct node * root = NULL;
root=insert(root,7);
insert(root,4);
insert(root,10);
insert(root,3);
insert(root,5);
insert(root,1);
insert(root,2);
insert(root,0);
insert(root,8);
insert(root,14);
insert(root,6);
insert(root,9);
insert(root,16);
insert(root,12);
insert(root,15);
insert(root,17);
inOrder(root);printf("\n");
}

How to fix my assign program with runtime error

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct listnode
{
int data;
struct listnode *nextptr;
struct listnode *prevptr;
}*head,*tail;
void print_linked_list(){
struct listnode *temp;
temp=head;
while(temp!=NULL){
printf("%d ",temp->data);
temp = temp->nextptr;
}
}
void InsertAtHead(int x){
struct listnode *var,*temp;
var=(struct listnode *)malloc(sizeof(struct listnode));
var->data=x;
if(head==NULL){
head = var;
head->prevptr=NULL;
head->nextptr=NULL;
tail=head;
}
else{
temp=var;
temp->prevptr=NULL;
temp->nextptr=head;
head->prevptr=temp;
head=temp;
}
}
void InsertAtTail(int x) {
struct listnode *var,*temp;
var=(struct listnode *)malloc(sizeof(struct listnode));
var->data=x;
if(head==NULL){
head=var;
head->prevptr=NULL;
head->nextptr=NULL;
tail=head;
}
else{
tail=head;
while(tail!=NULL){
temp=tail;
tail=tail->nextptr;
}
tail=var;
temp->nextptr=tail;
tail->prevptr=temp;
tail->nextptr=NULL;
}
}
int DeleteAtHead(){
struct listnode* temp;
temp=head;
if(temp->nextptr==NULL){
free(temp);
head=NULL;
tail=NULL;
return 0;
}
head=temp->nextptr;
head->prevptr=NULL;
free(temp);
return 0;
}
int DeleteAtTail(){
struct listnode* temp;
temp=tail;
if(temp->prevptr==NULL){
free(temp);
head=NULL;
tail=NULL;
return 0;
}
tail=temp->prevptr;
tail->nextptr=NULL;
free(temp);
return 0;
}
int main(){
head=NULL;
char operation[10];
char place[5];
int element;
/** write code here ^-^ya **/
while(scanf("%s ",operation)!=EOF){
if(strcmp(operation,"insert")==0){
scanf("%s",place);
if(strcmp(place,"head")==0){
scanf("%d",&element);
InsertAtHead(element);
}
else if(strcmp(place,"tail")==0){
scanf("%d",&element);
InsertAtTail(element);
}
}
else if(strcmp(operation,"delete")==0){
scanf("%s",place);
if(strcmp(place,"head")==0){
DeleteAtHead();
}
else if(strcmp(place,"tail")==0){
DeleteAtTail();
}
}
}
print_linked_list();
printf ("\n");
return 0;
}
When I submit my code,it always appears Runtime Error(RE) and Time limit Exceeded(TLE).
I try to free some memory but useless. How can I solve this?

Inorder traversal is printing in wrong order

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<string.h>
struct node{
char *name;
struct node *lchild;
struct node *rchild;
};
void find(char *str,struct node **root,struct node **loc,struct node **par)
{
struct node *ptr,*ptrsave;
*loc=NULL;
*par=NULL;
if(*root==NULL)
{
return;
}
if(!strcmp((*root)->name,str))
{
*loc=*root;
return;
}
ptrsave=NULL;
ptr=*root;
while(ptr!=NULL)
{
if(!strcmp(ptr->name,str)) break;
ptrsave=ptr;
if(strcmp(ptr->name,str)>0)
ptr=ptr->lchild;
else
ptr=ptr->rchild;
}
*loc=ptr;
*par=ptrsave;
}
void insert(struct node **p,char *str)
{
struct node *location,*parent,*temp;
find(str,&(*p),&location,&parent);
if(location!=NULL)
{
printf("Element already exists\n");
return;
}
temp=(struct node *)malloc(sizeof(struct node));
temp->name=strdup(str);
temp->lchild=NULL;
temp->rchild=NULL;
if(parent==NULL)
{
*p=temp;
return;
}
else
{
if(strcmp(parent->name,str)>0)
parent->lchild=temp;
else
parent->rchild=temp;
}
}
void inorder(struct node *root)
{
if(root!=NULL)
{
preorder(root->lchild);
printf("[%30s]\n",root->name);
preorder(root->rchild);
}
}
int main()
{
struct node *root=NULL;
insert(&root,"Crocin");
insert(&root,"Acetyl");
insert(&root,"Colchichine");
insert(&root,"Diclofenac_50mg");
insert(&root,"Diclofenac_25mg");
insert(&root,"Morphine Sulphate");
insert(&root,"Fentanyl");
insert(&root,"Dolo");
insert(&root,"Ibuprofen");
insert(&root,"Tramadol");
insert(&root,"Paracetamol");
inorder(root);
getchar();
return 0;
}
This is the code i am using for creating and inserting nodes in a binary search tree. After that i created recursive inorder function. but this is not giving the right ouput. I am not able to find error that is creating discrepancy. Can someone suggest where i am going wrong?
Change
void inorder(struct node *root)
{
if(root!=NULL)
{
preorder(root->lchild);
printf("[%30s]\n",root->name);
preorder(root->rchild);
}
to
void inorder(struct node *root)
{
if(root!=NULL)
{
inorder(root->lchild);
printf("[%30s]\n",root->name);
inorder(root->rchild);
}
You are using another function (preorder) (not listed in your code)

Resources