You are on page 1of 8

LINK 1:

//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;
};

struct Stack* createStack(int sizeOfStack)


{
   struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
   stack->sizeOfStack = sizeOfStack;
   stack->top = -1;

   stack->array = (struct student*)malloc(stack->sizeOfStack * sizeof(struct student));


   return stack;
}

int isEmpty(struct Stack* stack)


{
   return stack->top == -1;
}

int isFull(struct Stack* stack)


{
   return stack->top == stack->sizeOfStack - 1;
}

void push(struct Stack* stack, struct student item)


{
   if (isFull(stack))
{
return;
}

   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))
       {

       printf("stack is empty");


       }
else
{
struct student remove= stack->array[stack->top];
stack->top--;
printf(" pop in stack %s %d %d %.2f \n", remove.name, remove.id, remove.age, remove.score);
}
}

int main()
{
   struct Stack* stack = createStack(100);
struct student stud1,stud2,stud3;

strcpy( stud1.name, "rahul" );


stud1.id=1;
stud1.age=25;
stud1.score=24.5;
strcpy( stud2.name, "shivraj" );
stud2.id=2;
stud2.age=20;
stud2.score=19;
strcpy( stud3.name, "priya" );
stud3.id=3;
stud3.age=19;
stud3.score=16;
   push(stack, stud1);
   push(stack, stud2);
   push(stack, stud3);
   pop(stack);

  

   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);
}

int height(struct student*root)


{
int lheight,rheight;
if(root==NULL)
{
return(-1);
}
else
{
lheight=height(root->left);
rheight=height(root->right);
}
if(lheight>rheight)
{
return(lheight+1);
}
else
{
return(rheight+1);
}
}
int noOfNode(struct student *root)
{
static int count=0;
if (root != NULL)
{
noOfNode(root->left);
count=count+1;
noOfNode(root->right);
}
return(count);
}

LINK 4:

Program: please follow these program as shown in below


#include<stdio.h>
#include<conio.h>
#include<ctype.h> char stack[30],str[30]; int tos=0;
void push(char c)
{
if(tos>=29)
{
printf(“stack is full\n”); return;
}
else stack[tos++]=c;
}
char pop()
{
tos– –; if(tos<0)
{
printf(“stack is empty\n”); return 0;
}
else
return(stack[tos]);
}
main()
{
int i,check_stack=0; char x;
clrscr();
printf(“enter the prefix expression\n”); gets(str);
for(i=0;str[i]!=’\0’;i++)
{
if(!isalpha(str[i]))
{
if(check_stack>1)
printf(“%c”,pop());
push(str[i]);
check_stack=0;
}
else
{
printf(“%c”,str[i]);
check_stack++;
if(check_stack==2)
{
printf(“%c”,pop());
check_stack=0;
}
}
}
while(tos!=0)
printf(“%c”,pop());
getch();
}

You might also like