Professional Documents
Culture Documents
//below is c code
#include <stdio.h>
#include<string.h>
#include <stdlib.h>
struct student
{
char name[50];
int id;
int age;
double score;
};
struct Stack {
int top;
int sizeOfStack;
struct student* array;
};
stack->array[++stack->top] = item;
printf(" push in stack %s %d %d %.2f \n",item.name,item.id,item.age,item.score);
}
void pop(struct Stack* stack)
{
if (isEmpty(stack))
{
int main()
{
struct Stack* stack = createStack(100);
struct student stud1,stud2,stud3;
return 0;
}
//output
LINK 3:
#include<stdio.h>
struct student
{
int ID;
char fname[20];
char lname[20];
float score;
char Zip_code[20];
struct student *right,*left;
};
struct student*savetemp=NULL;
struct student* del(struct student*,int);
struct student* insert(struct student*,struct student* );
struct student* travel(struct student*,struct student*);
void printAll(struct student *);
int height(struct student*);
int noOfNode(struct student *);
int isFound(struct student *root,int n)
{
if (root != NULL)
{
if((*root).ID==n)
return 0;
isFound(root->left,n);
isFound(root->right,n);
}
return 0;
}
struct student* addRecord(struct student *root)
{
struct student s;
do{
printf("Enter ID (1000 - 1100):");
scanf("%d",&s.ID);
}while(isFound(root,s.ID)==1 || s.ID>1100 || s.ID<1000);
printf("Enter FNAME :");
scanf("%s",&s.fname);
printf("Enter LNAME :");
scanf("%s",&s.lname);
printf("Enter Score :");
scanf("%f",&s.score);
printf("Enter ZIPCode :");
scanf("%s",&s.Zip_code);
return insert(root,&s);
}
void main()
{
struct student* root=NULL;
int item,choice;
do
{
printf("Enter your choice\n");
printf("1.Add new Record \n2.Delete Record\n 3.Display Records\n4.no of nodes 5.height 6.exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
root=addRecord(root);
break;
case 2:{
printf("Enter element to delete\n");
scanf("%d",&item);
root=del(root,item);
if(savetemp!=NULL)//condition for any break link is present to insert or not
travel(root,savetemp);//to insert break link during insertion
}
break;
case 3:{
printf("printAll travel\n");
printAll(root);
}
break;
case 4:{
printf("No of nodes are: %d\n",noOfNode(root));
}
break;
case 5:
printf("height of tree %d",height(root));
break;
case 6:
printf("Exiting");
break;
default : printf("wrong choice\n");
}
}while(choice!=6);
getch();
}
void print(struct student s)
{
printf("ID ::%d\n",s.ID);
printf("FNAME ::%s\n",s.fname);
printf("LNAME ::%s\n",s.lname);
printf("Score ::%.2f\n",s.score);
printf("ZIPCode ::%s\n\n",s.Zip_code);
}
void printAll(struct student *root)
{
if (root != NULL)
{
printAll(root->left);
print(*root);
printAll(root->right);
}
}
struct student* insert(struct student*root,struct student* temp)
{
if(root==NULL)
{
temp->left=NULL;
temp->right=NULL;
root=temp;
}
else
{
if(root->ID < temp->ID)
{
root->right=insert(root->right,temp);
}
else
{
root->left=insert(root->left,temp);
}
}
return(root);
}
struct student* travel(struct student*root,struct student*savetemp)
{
if (savetemp != NULL)
{
insert(root,savetemp);
travel(root,savetemp->left);
travel(root,savetemp->right);
}
return(root);
}
struct student* del(struct student*root,int item)
{
if(item==root->ID)
{
if(root->left==NULL&&root->right==NULL) //no child
{
root=NULL;
}
else if(root->left==NULL||root->right==NULL) //one child
{
if(root->left!=NULL) //left child
{
root=root->left;
}
else //right child
{
root=root->right;
}
}
else if(root->left!=NULL&&root->right!=NULL) //both child
{
struct student* temp;
savetemp=root->right->left;
temp=root;
root=root->right;
root->left=temp->left;
}
}
else
{
if(root->ID<item)
{
root->right=del(root->right,item);
}
else
{
root->left=del(root->left,item);
}
}
return(root);
}
LINK 4: