Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
h>
#include<stdlib.h>
typedef struct bnode{
int key;
struct bnode* left;
struct bnode* right;
}BNODE;
void printKeysReverse(BNODE* current);
void inorder(BNODE* current);
void insert(BNODE **root,int key);
int main(void){
BNODE* root=NULL;
insert(&root,27);
insert(&root,59);
insert(&root,21);
insert(&root,38);
insert(&root,54);
insert(&root,63);
insert(&root,8);
insert(&root,70);
insert(&root,15);
}
void insert(BNODE **root, int val){
BNODE *newnode;
newnode=(BNODE*)malloc(sizeof(BNODE));
newnode->right=NULL;
newnode->left=NULL;
if ((*root)==NULL){
*root=newnode;
(*root)->key=val;
return;
}
if (val<(*root)->key) insert((&root)->left,val);
else insert((&root)->right,val);
}//end
void inorder(BNODE *root){
if (root==NULL) return ;
inorder(root->left);
printf("%d ",root->key);
inorder(root->right);
}//end inorder
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
------------------
# include<stdio.h>
# include<conio.h>
# include "malloc.h"
struct node
{
int data;
struct node *link;
};
void main()
{
int a=111,b=2,c=3,will,wish,num;
struct node *ptr,*ptr2,*result,*temp;
void add(struct node **,int );
struct node * search(struct node *);
void display(struct node *);
void invert(struct node *);
void del(struct node *,int);
struct node * concat(struct node *,struct node *);
ptr=NULL;
ptr2=NULL;
result=NULL; //result for storing the result of concatenation
clrscr();
will=1;
while(will==1)
{
printf("
Main Menu
1. Add element
2.Delete element
3.Search element
4Linked List concatenation
5.Invert linked list
6. Display elements
Please enter the choice");
scanf("%d",&wish);
switch(wish)
{
case 1:
printf("
Enter the element you want to add ");
scanf("%d",&num);
add(&ptr,num);
display(ptr);
break;
case 2:
printf("
Enter the element to delete ");
scanf("%d",&num);
del(ptr,num);
break;
case 3:
printf("
Now demonstrating search ");
temp = search(ptr);
printf("
Address of first occurence is %u ",temp);
break;
case 4:
/* Inputs given internally for demo only */
printf(" Now demonstrating linked list concatenation
Press any key to continue...");
add(&ptr2,2);
add(&ptr2,4);
add(&ptr2,6);
getch();
printf("
Displaying second Linked List
");
display(ptr2);
getch();
result = concat(ptr,ptr2);
clrscr();
printf("
if (p==NULL)
r=q;
if (q==NULL)
r=p;
else
{
x=p;
r=x;
while(x->link!=NULL)
x=x->link;
x->link=q;
}
return(r);
}