Show list in descending order - c

I have this program that does not throw error, but the viewl procedure only shows the first item in the list. How do I for the viewl procedure that printing but in the reverse order to what does the procedure viewr?
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <stdlib.h>
struct list
{
int info;
struct list *nxt,*prv;
}*HEAD=NULL,*AUX=NULL,*P=NULL,*F=NULL,*QD=NULL,*FD=NULL;
main function (ok)
int main()
{
void insertr(void);
void extractr(void);
void viewr(void);
void viewl(void);
void deleten();
void deletep();
char opc;
do
{
system("cls");
printf("___________________________________________________\n");
printf("_______¡¡¡DOUBLE-LINKED CIRCULAR LIST MENU!!!______\n");
printf("___________________________________________________\n");
printf("_________________SELECT AN OPTION__________________\n");
printf("___________________________________________________\n");
printf("___________________________________________________\n");
printf("__________1) INSERT________________________________\n");
printf("__________2) VIEW ASCENDING________________________\n");
printf("__________3) VIEW DESCENDING_______________________\n");
printf("__________4) ENTERING AND ELIMINATE NEXT___________\n");
printf("__________5) ENTERING AND ELIMINATE PREVIOUS_______\n");
printf("__________6) EXIT__________________________________\n");
printf("___________________________________________________\n");
opc=getch();
switch(opc)
{
case '1':
insertr();
break;
case '2':
viewr();
break;
case '3':
viewl();
break;
case '4':
deleten();
break;
case '5':
deletep();
break;
}
}
while(opc!='6');
getch();
return 0;
}
insert right process
void insertr(void)
{
P=HEAD;/* very first execution of this method P=NULL */
AUX=(struct list *)malloc(sizeof(struct list));
system("cls");
printf("ENTER AN ENTIRE NUMBER: ");
scanf("%d",&AUX->info);
AUX->nxt=HEAD;
AUX->prv=HEAD;
F=AUX;
if(HEAD==NULL)
{
HEAD=AUX;
P = AUX;/*first execution of this method P is no longer NULL but P is pointing to AUX */
}
else
{
while (P->nxt!=HEAD)
{
P=P->nxt;
}
}
P->nxt=AUX;
AUX->prv=P;
HEAD->prv=AUX;
}
void deleten()
{
int x;
system("cls");
printf("ENTER A NUMBER TO ELIMINATE THE FOLLOWING: ");
scanf("%d",&x);
FD=HEAD;
QD=HEAD;
while(FD->info!=x&&FD->nxt!=HEAD)
{
FD=FD->nxt;
}
QD=FD->nxt;
if(FD->nxt==HEAD&&FD->info!=x)
{
printf("\nENTERED NUMBER IS NOT LISTED");
}
else
{
if(FD->info==x)
{
FD->nxt=QD->nxt;
(QD->nxt)->prv=FD;
printf("\nDELETED %d",QD->info);
free(QD);
}
}
getch();
}
deletea process (ok)
void deletep()
{
int x;
system("cls");
printf("ENTER A NUMBER TO REMOVE THE PREVIOUS ");
scanf("%d",&x);
FD=HEAD;
QD=HEAD;
while (FD->info!=x&&FD->nxt!=HEAD)
{
FD=FD->nxt;
}
QD=FD->prv;
if(FD->nxt==HEAD&&FD->info!=x)
{
printf("\nENTERED NUMBER IS NOT LISTED");
}
else
{
if(FD->info==x)
{
FD->prv=QD->prv;
(QD->prv)->nxt=FD;
printf("\nDELETED %d",QD->info);
free(QD);
}
}
getch();
}
viewr process (ok):
void viewr(void)
{
system("cls");
if(HEAD==NULL)
{
printf("EMPTY LIST");
getchar();
return;
}
AUX=HEAD;
printf("LIST:\n\n");
while(AUX->nxt!=HEAD)
{
printf("-> %d\n",AUX->info);
AUX=AUX->nxt;
}
if(AUX->nxt==HEAD)
{
printf("-> %d\n",AUX->info);
}
getch();
}
this function fails:
void viewl(void)
{
system("cls");
if(HEAD==NULL)
{
printf("EMPTY LIST");
getchar();
return;
}
AUX=F;
printf("LIST:\n\n");
do
{
printf("-> %d\n",AUX->info);
AUX=AUX->prv;
}
while(AUX->nxt!=HEAD);
getch();
}

The code had several logical errors, other variables, etc. Here it is working without problems
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
//THIS IS TO CREATE THE LIST
struct list{
int info;
struct list *nxt,*prv;
}*HEAD=NULL,*AUX=NULL,*P=NULL,*FD=NULL,*QD=NULL,*Fd=NULL,*Qd=NULL;
int main(){
void insertr(void);
void viewr(void);
void viewl(void);
void deleten();
void deletep();
char opt;
do{
system("cls");
printf("___________________________________________________\n");
printf("_______¡¡¡DOUBLE-LINKED CIRCULAR LIST MENU!!!______\n");
printf("___________________________________________________\n");
printf("_________________SELECT AN OPTION__________________\n");
printf("___________________________________________________\n");
printf("___________________________________________________\n");
printf("__________1) INSERT________________________________\n");
printf("__________2) VIEW ASCENDING________________________\n");
printf("__________3) VIEW DESCENDING_______________________\n");
printf("__________4) ENTERING AND ELIMINATE NEXT___________\n");
printf("__________5) ENTERING AND ELIMINATE PREVIOUS_______\n");
printf("__________6) EXIT__________________________________\n");
printf("___________________________________________________\n");
opt=getch();
switch(opt) {
case '1':
insertr();
break;
case '2':
viewr();
break;
case '3':
viewl();
break;
case '4':
deleten();
break;
case'5':
deletep();
break;
}
}while(opt!='6');
return 0;
}
void insertr(void){
P=HEAD; /* very first execution of this method P=NULL */
AUX=(struct list *)malloc(sizeof(struct list));
system("cls");
printf("ENTER AN ENTIRE NUMBER: ");
scanf("%d",&AUX->info);
AUX->nxt=HEAD;
AUX->prv=HEAD; // The pointer to the previous node is null (HEAD is null)
if(HEAD==NULL){ //I ask if HEAD is equal to Null
HEAD=AUX;
P=AUX; /* The first execution of this method P is no longer NULL but P is pointing to AUX */
}else{ //THIS YES
while(P->nxt!=HEAD){
P=P->nxt;
}
}
P->nxt=AUX; // The next pointer of P takes the value of AUX
AUX->prv=P;
HEAD->prv=AUX;
}
void deleten(){
int x;
system("cls");
printf("ENTER A NUMBER TO ELIMINATE THE FOLLOWING: ");
scanf("%d",&x);
FD=HEAD;
QD=HEAD;
while(FD->info!=x&&FD->nxt!=HEAD){ // if what I am going to eliminate is different from the number that was entered and if there is another
FD=FD->nxt; //step to the next node
}
QD=FD->nxt;
if(FD->nxt==HEAD&&FD->info==x){
printf("\nIT IS THE HEADBOARD! CAN NOT BE ELIMINATED");
}else
if(FD->nxt==HEAD&&FD->info!=x){
printf("\nTHE NUMBER ENTERED IS NOT IN THE LIST");
}else{
FD->nxt=QD->nxt;
(QD->nxt)->prv=FD;
printf("\nDELETED %d",QD->info);
free(QD); //free space in memory occupied by QD
}
getch();
}
void deletep()
{
int xd;
system("cls");
printf("IENTER A NUMBER TO REMOVE THE PREVIOUS: ");
scanf("%d",&xd);
Fd=HEAD;
Qd=HEAD;
while(Fd->info!=xd){
Fd=Fd->prv;
} Qd=Fd->prv;
if(Fd==HEAD){
printf("\nIT'S THE HEAD! CAN NOT BE ELIMINATED");
}else{
if(Qd==HEAD){
HEAD=HEAD->nxt;
Fd->prv=Qd->prv;
(Fd->prv)->nxt=Fd;
printf("\nDELETED %d",Qd->info);
}else{
Fd->prv=Qd->prv;
(Qd->prv)->nxt=Fd;
printf("\nDELETED %d",Qd->info);
}
free(Qd);
}
getch();
}
void viewr(void)
{
system("cls");
if(HEAD==NULL){
printf("LISTA VACIA");
getchar();
return;
}
AUX=HEAD;
printf("LISTA:\n\n");
while(AUX->nxt!=HEAD){
printf("->%d\n",AUX->info);
AUX=AUX->nxt;
}
if(AUX->nxt==HEAD){
printf("->%d\n",AUX->info);
}
getch();
}
void viewl(void){
system("cls");
if(HEAD==NULL){
printf("EMPTY LIST");
getchar();
return;
} AUX=HEAD->prv;
printf("LIST:\n\n");
do{
printf("->%d\n",AUX->info);
AUX=AUX->prv;
}while(AUX->nxt!=HEAD);
getch();
}

in your code:
do
{
printf("-> %d\n",AUX->info);
AUX=AUX->prv;
}
while(AUX->nxt!=HEAD);
It can be happening something like this.
As you are going to the previous node and checking if next is head, next will be head if you built your list to be double-linked and circular.
try like this:
do
{
AUX=AUX->prv;
printf("-> %d\n",AUX->info);
}
while(AUX!=HEAD);
and you should avoid using all variables as global.

Related

Implementation of stack using linked list

I am getting a "Function should return a value" error at the 91st line of the code in Turbo C++, please help me as I have to submit my project, I know that Turbo C++ is a very old compiler but that's what our University Teacher recommends so I cant do nothing in that
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#include <stdio.h>
#include <conio.h>
struct stack
{
int element;
struct stack *next;
} * top;
void push(int);
int pop();
void display();
void main()
{
int num1, num2, choice;
while (1)
{
clrscr();
printf("Select a choice from the following:");
printf("\n[1] Push an element into the stack");
printf("\n[2] Pop out an element from the stack");
printf("\n[3] Display the stack elements");
printf("\n[4] Exit\n");
printf("\n\tYour choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
{
printf("\n\tEnter the element to be pushed into the stack: ");
scanf("%d", &num1);
push(num1);
break;
}
case 2:
{
num2 = pop();
printf("\n\t%d element popped out of the stack\n\t", num2);
getch();
break;
}
case 3:
{
display();
getch();
break;
}
case 4:
exit(1);
break;
default:
printf("\nInvalid choice !\n");
break;
}
}
}
void push(int value)
{
struct stack *ptr;
ptr = (struct stack *)malloc(sizeof(struct stack));
ptr->element = value;
ptr->next = top;
top = ptr;
return;
}
int pop()
{
if (top == NULL)
{
printf("\n\STACK is Empty.");
getch();
exit(1);
}
else
{
int temp = top->element;
top = top->next;
return (temp);
}
}
void display()
{
struct stack *ptr1 = NULL;
ptr1 = top;
printf("\nThe various stack elements are:\n");
while (ptr1 != NULL)
{
printf("%d\t", ptr1->element);
ptr1 = ptr1->next;
}
}
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
The compiler is complaining because you don’t have a return statement outside of the if statement. Even though you call exit in the if branch, syntactically speaking that’s just another function call; structurally, the compiler sees a pathway where you reach the closing } of the function body without a return statement.
You want to make sure the return is reachable outside the body of the if-else statement, and the best way to do it is take the else branch out of the statement entirely:
int pop( void )
{
int temp;
if ( !top )
{
fputs( "Stack is empty", stderr );
exit( 1 );
}
temp = top->element;
top = top->next;
return temp;
}
you can change your pop function as below ( assuming you are not storing -1 as an element in the stack)
int pop()
{
if (top == NULL)
{
printf("\n\STACK is Empty.");
getch();
return -1;// or other invalid value which indicates stack empty
}
else
{
int temp = top->element;
top = top->next;
return (temp);
}
}
and at the place you are calling modify as following
case 2:
{
num2 = pop();
if(num2 != -1) {
printf("\n\t%d element popped out of the stack\n\t", num2);
getch();
}else{
printf("Stack is Empty\n");
exit(1);
}
break;
}

Double Linked List query

so i have created a program which uses double linked list and
performs some operations on it . The problem is that it displays garbage
values
at the end every time i try to create a linked list and then display it.
whats wrong in my code?(sorry! for the bad indentations)
if the linked list i created has elements say 15 and 16, it displays it as
15 16 25710 0 0
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct dll
{
int data;
struct dll *llink;
struct dll *rlink;
};
typedef struct dll *node;
node head=NULL;
void create();
int search(int u);
void insert(int num1);
void Delete(int num2);
void display();
node getnode();
int main()
{
int i,num,o;
while(1)
{
printf("\n1.create a list\n 2. insert before a search node\n 3. delete a node\n 4.display\n 5.exit\n");
scanf("%d",&num);
switch(num)
{
case 1 :
create();
break;
case 2 :
printf("enter the value before which you want to enter the node\n");
scanf("%d",&i);
insert(i);
break;
case 3 :
printf("enter the value to be deleted\n");
scanf("%d",&o);
Delete(o);
break;
case 4 :
printf("the linked list has :\n");
display();
break;
case 5 :
getch();
exit(1);
default :
printf("enter the correct option\n");
break;
}
}
}
node getnode()
{
node temp1;
temp1=(node)malloc(sizeof(node));
temp1->llink=NULL;
temp1->rlink=NULL;
return temp1;
}
void create()
{
node nn;
int num,y;
if(head==NULL)
head=getnode();
while(1)
{
printf("enter the data for the node");
scanf("%d",&num);
head->data=num;
printf("do you want to create another node(1/0):\n");
scanf("%d",&y);
if(y==1)
{
nn=getnode();
nn->rlink=head;
head->llink=nn;
head=nn;
nn=NULL;
}
else
break;
}
}
void insert (int num1)
{
int i,n,k;
node temp=head,nn;
n=search(num1);
if(n==0)
{
printf("element not present in the linked list");
}
if(n==1)
{
nn=getnode();
printf("enter the data for the node");
scanf("%d",&k);
nn->data=k;
nn->rlink=head;
head->llink=nn;
head=nn;
nn=NULL;
}
else
{
for(i=2; i<=n; i++)
temp=temp->rlink;
nn=getnode();
temp->llink->rlink=nn;
nn->llink=temp->llink;
nn->rlink=temp;
temp->llink=nn;
}
}
void Delete(int num2)
{
node temp=head;
int p,i;
p=search(num2);
if(p==0)
{
printf("no element is found");
}
if(p==1)
{
printf("the deleted element is %d",head->data);
head=head->rlink;
head->llink=NULL;
free(temp);
}
else
{
for(i=2; i<=p; i++)
{
temp=temp->rlink;
}
temp->llink->rlink=temp->rlink;
temp->rlink->llink=temp->llink;
free(temp);
temp=temp->rlink;
}
}
int search(int u)
{
node temp=head;
int pos=0;
if(u==head->data)
return 1;
else
{
while(temp!=NULL)
{
pos++;
if(temp->data==u)
{
printf("element found\n");
return(pos);
}
temp=temp->rlink;
}
}
if(temp==NULL)
{
return 0;
}
return -1;
}
void display()
{
node temp=head;
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->rlink;
}
}
This:
temp1=(node)malloc(sizeof(node));
is a major error. Since you're "hiding a star", and node is a typedef for a pointer type, you're not allocating enough memory. It should be:
node temp1 = malloc(sizeof *temp1);
But I really recommend against typedefing a pointer away, it just makes things confusing.

C fifo linked list char push

I'm currently trying to understand fifo linked list and found example here Example , and I'm trying to input char instead of int
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct Node
{
char Data;
struct Node* next;
}*rear, *front;
void delQueue()
{
struct Node *temp, *var=rear;
if(var==rear)
{
rear = rear->next;
free(var);
}
else
printf("\nQueue Empty");
}
void push(char *value)
{
struct Node *temp;
temp=(struct Node *)malloc(sizeof(struct Node));
temp->Data=value;
if (front == NULL)
{
front=temp;
front->next=NULL;
rear=front;
}
else
{
front->next=temp;
front=temp;
front->next=NULL;
}
}
void display()
{
struct Node *var=rear;
if(var!=NULL)
{
printf("\nElements are as: ");
while(var!=NULL)
{
printf("\t%d",var->Data);
var=var->next;
}
printf("\n");
}
else
printf("\nQueue is Empty");
}
int main()
{
int i=0;
char ch;
front=NULL;
printf(" \n1. Push to Queue");
printf(" \n2. Pop from Queue");
printf(" \n3. Display Data of Queue");
printf(" \n4. Exit\n");
while(1)
{
printf(" \nChoose Option: ");
//scanf("%d",&i);
ch = getchar();
switch(ch)
{
case '+':
{
char value[20];
printf("\nEnter a valueber to push into Queue : ");
scanf("%s", value);
push(value);
printf("%s",value);
display();
break;
}
case '-':
{
delQueue();
display();
break;
}
case '*':
{
display();
break;
}
case '$':
{
exit(0);
}
default:
{
printf("\nwrong choice for operation");
}
}
}
}
I can't understand this warning on line 26: warning: assignment makes integer from pointer without a cast
I can input text, for example: "Hello world", but when I want to display it, it is showed as "-9".
I am really confused.
Data is defined as char, but your assigning a char * (char pointer) to it. That creates the warning, and surely won't work as you expect.
In your Node struct, the value is of type char, but you're assigning a char* to it instead. This is why you get the warning, and why printing things doesn't come out right.

"search" linked list in c

I added a "search" function for my linked list menu and I don't know what is wrong in my code. When I enter a search key which is a name that is not in the list, instead of printing the "Search Key: %s Not Found!", the program will just stop. How to fix it? Any suggestion on how can I improve my program?
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
struct list
{
char name[20];
int age;
char gender[10];
struct list *next;
};
void main(void)
{
struct list *HEAD = NULL;
struct list *temp,*current, *trav;
struct list *prev,*temp1,*temp2;
char choice;
while(1)
{
clrscr();
printf("MENU\n");
printf("A) ADD\n");
printf("B) DISPLAY\n");
printf("C) DELETE\n");
printf("D) SEARCH\n");
printf("X) EXIT\n");
scanf("%c", &choice);
switch(toupper(choice))
{
case 'A':
temp= (struct list*)malloc(sizeof(struct list));
temp->next=NULL;
printf("Fill-Up the following:\n");
printf("Name:");
fflush(stdin);
gets(temp->name);
printf("Age:");
fflush(stdin);
scanf("%d",&temp->age);
printf("Gender:");
fflush(stdin);
gets(temp->gender);
if(HEAD == NULL)
{
HEAD = temp;
}
else if(HEAD!=NULL)
{
for(trav=HEAD; trav->next != NULL; trav= trav->next);
trav->next=temp;
}
else
{
printf("Not Enough Memory!\n");
}
break;
case 'B':
if(HEAD==NULL)
{
printf("Linked List is Empty!\n");
getch();
}
else{
for(trav=HEAD; trav != NULL; trav=trav->next )
{
printf("\nName: %s\n", trav->name);
printf("Age: %d\n", trav->age);
printf("Gender: %s\n\n", trav->gender);
}
getch();
}
break;
case 'C' :
temp1=( struct list*)malloc(sizeof(struct list));
temp1->next=NULL;
if(HEAD==NULL)
{
printf("No item to be delete. List is Empty!\n");
getch();
}
else {
printf("Enter The Name of the item you want to Delete: ");
fflush(stdin);
gets(temp1->name);
current=HEAD;
if(strcmp(temp1->name,current->name)== 0)
{
HEAD=HEAD->next;
free(current);
printf("Item has been successfully deleted from the list.\n");
getch();
}
else
{
for(prev=HEAD,trav=HEAD->next; strcmp(trav->name,temp1->name) == 1 ; trav=trav->next,prev=prev->next);
if(trav==NULL)
{
printf("Name: %s not found!", temp1->name);
getch();
}
else{
prev->next=trav->next;
free(trav);
printf("Item has been successfully deleted from the list.\n");
getch();
}
}
}
break;
case 'D':
temp2=( struct list*)malloc(sizeof(struct list));
temp2->next=NULL;
if(HEAD==NULL)
{
printf("No item to search. List is Empty.\n");
getch();
}
else{
printf("Enter Name (Search Key): ");
fflush(stdin);
gets(temp2->name);
int count=0;
struct list *trav2=HEAD;
while( trav2 !=NULL)
{
for(struct list *trav1=trav2; strcmp(trav1->name,temp2->name)!=0;trav1=trav1->next);
if(trav1!=NULL)
{
printf("\nName: %s\n", trav1->name);
printf("Age: %d\n", trav1->age);
printf("Gender: %s\n",trav1->gender);
trav2=trav1->next;
count++;
}
else {
trav2=NULL;
}
}
getch();
if(count==0)
{
printf("Search Key: %s Not Found!\n", temp2->name);
getch();
}
}
break;
case 'X':
if(HEAD!=NULL){free(HEAD); }
if(trav!=NULL){ free(trav); }
if(trav1!=NULL){ free(trav1); }
if(trav2!=NULL){ free(trav2); }
if(temp!=NULL){ free(temp); }
if(temp1!=NULL){ free(temp1); }
exit(1);
break;
}
}
}
In your search routine..
for(struct list *trav1=trav2; strcmp(trav1->name,temp2->name)!=0;trav1=trav1->next);
here trav1 will be null at the end of list but you are still going ahead and derefrencing it.
Add a check temp1 in your for loop like this:
for(struct list *trav1=trav2; trav1 && (strcmp(trav1->name,temp2->name)!=0);trav1=trav1->next);
Or, for better readability:
for (struct list *trav1 = trav2; trav1 != NULL; trav1 = trav1->next)
{
if (strcmp(trav1->name, temp2->name) !=0 )
break;
}
A few other comments on your code:
Do not use fflush(stdin) use something like this since that is not guaranteed by the standard to work.
int c;
while ((c = getchar()) != EOF && c != '\n')
;
Stop using gets(). It is impossible to use gets() safely. Use fgets() instead (but be aware it keeps the newline where gets() discards it).
Your loop continues while while( trav2 !=NULL). But where do you set trav2 = trav2->next? Without that, you loop will just continue forever.
But your code seems to have more problems than just that. Why are you allocating a new list item? Just declare a pointer to a list item (struct list*) and then point it to the head of your list.
for (temp = HEAD; temp != NULL; temp = temp->next)
{
// Examine temp for the value you are looking for
}
I suspect you are still new to understanding pointers. That is key to getting this code working right.

Implementing BinarySearchTree using Single Linked List

I have the below code which I am using to implement BinarySearchTree. Somehow it doesn't build the binarytree.Can anybody help what is the issue ?
typedef struct BinarySearchTree
{
struct BinarySearchTree *left;
int nodeval;
struct BinarySearchTree *right;
}
BST;
BST *root=NULL;
void addrootnode(BST *,int );
void addnode(BST *,int );
void deletenode(int );
void printnodes();
main()
{
int nodeval;
char choice;
printf("\n r->rootnode \n a->add \n d->delete \n p->print \n e->exit\n");
while (1)
{
printf("\nEnter your choice:");
scanf("%c",&choice);
switch (choice)
{
case 'r':
printf("\nEnter the root node value to add: ");
scanf("%d",&nodeval);
addrootnode(root,nodeval);
break;
case 'a':
printf("\nEnter the node value to add: ");
scanf("%d",&nodeval);
addnode(root,nodeval);
break;
case 'd':
printf("\nEnter the node value to delete: ");
scanf("%d",&nodeval);
break;
case 'p':
//printf("\n");
printnodes(root);
break;
case 'e':
exit(0);
default:
break;
}
}
getchar();
}//end of main
//addrootnode
void addrootnode(BST *ptr,int num)
{
if(ptr==NULL)
{
ptr=(BST *) malloc(sizeof(BST));
ptr->left=NULL;
ptr->nodeval=num;
ptr->right=NULL;
root=ptr;
}
}
//add node
void addnode(BST *ptr,int num)
{
if(ptr==NULL)
{
ptr=(BST *) malloc(sizeof(BST));
ptr->left=NULL;
ptr->nodeval=num;
ptr->right=NULL;
}
else if(num < ptr->nodeval )
{
addnode(ptr->left,num);
}
else
{
addnode(ptr->right,num);
}
}
//delete functionality
void deletenode(int nodeval)
{
}
//print all nodes
void printnodes(BST *ptr)
{
if (ptr)
{
printnodes(ptr->left);
printf("%d\t",ptr->nodeval);
printnodes(ptr->right);
}
}
You should not use BST *ptr as argument to addnode. You try to assign a value to it but at this time ptr is a local variable to the function addnode an does not alter the intended left or right pointer of the parent node.
Try to think about using BST **ptr instead.

Resources