Data Structure
Data Structure
/*************************************************************
The use of User defined Data Type i.e. enum
*************************************************************/
#include<stdio.h>
#include<conio.h>
main()
{
int roll1,roll2;
enum standard {FIRST,SECOND,THIRD,FOURTH};
enum standard s1,s2;
clrscr();
printf(“\n Enter the roll numbers for two students”);
scanf(“%d%d”,&roll1,&roll2);
s1=FIRST;
s2=FOURTH; /*assigning the standards*/
printf(“\nThe Roll Number %d is admitted to
%d st Standard”,roll1,s1+1);
printf(“\nThe Roll Number %d is admitted to
%d th Standard”,roll2,s2+1);
getch();
}
//Output
Enter the roll numbers for two students: 1456
4567
/*************************************************************
Program to perform addition two matrices.
The matices are nothing but the two dimensional arrays.
*************************************************************/
#include<stdio.h>
#include<conio.h>
#define size 3
int A[size][size],B[size][size],C[size][size],n;
void main()
{
int i,j;
clrscr();
printf(“\n Enter The order of the matrix”);
scanf(“%d”,&n);
printf(“\n Enter The Elements For The First Matrix”);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf(“%d”,&A[i][j]);
printf(“\n Enter The Elements For The Second Matrix”);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf(“%d”,&B[i][j]);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
C[i][j]=A[i][j]+B[i][j];
printf(“\n The Addition Is\n”);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf(“%d”,C[i][j]);
}
printf(“\n”);
}
getch();
}
//Output
The Addition Is
12 9 11
7 12 14
11 12 9
3) ‘C’ program
/*************************************************************
Program for implementing a stack using arrays.It involves
various operations such as push,pop,stack empty,stack full and
display.
*************************************************************/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define size 5
/* stack structure*/
struct stack {
int s[size];
int top;
}st;
/*
The stfull Function
Input:none
Output:returns 1 or 0 for stack full or not
Called By:main
Calls:none
*/
int stfull()
{
if(st.top>=size-1)
return 1;
else
return 0;
}
/*
The push Function
Input:item which is to be pushed
Output:none-simply pushes the item onto the stck
Called By:main
Calls:none
*/
void push(int item)
{
st.top++;
st.s[st.top] =item;
}
/*
The stempty Function
Input:none
Output:returns 1 or 0 for stack empty or not
Called By:main
Calls:none
*/
int stempty()
{
if(st.top==-1)
return 1;
else
return 0;
}
/*
The pop Function
Input:none
Output:returns the item which is popped from the stack
Called By:main
Calls:none
*/
int pop()
{
int item;
item=st.s[st.top];
st.top – –;
return(item);
}
/*
The display Function
Input:none
Output:none-displys the contents of the stack
Called By:main
Calls:none
*/
void display()
{
int i;
if(stempty())
printf(“\n Stack Is Empty!”);
else
{
for(i=st.top;i>=0;i– –)
printf(“\n%d”,st.s[i]);
}
}
/*
The main Function
Input:none
Output:none
Called By:O.S.
Calls:push,pop,stempty,stfull,display
*/
void main(void)
{
int item,choice;
char ans;
st.top=-1;
clrscr();
printf(“\n\t\t Implementation Of Stack”);
do
{
printf(“\n Main Menu”);
printf(“\n1.Push\n2.Pop\n3.Display\n4.exit”);
printf(“\n Enter Your Choice”);
scanf(“%d”,&choice);
switch(choice)
{
case 1:printf(“\n Enter The item to be pushed”);
scanf(“%d”,&item);
if(stfull())
printf(“\n Stack is Full!”);
else
push(item);
break;
case 2:if(stempty())
printf(“\n Empty stack!Underflow !!”);
else
{
item=pop();
printf(“\n The popped element is %d”,item);
}
break;
case 3:display();
break;
case 4:exit(0);
}
printf(“\n Do You want To Continue?”);
ans=getche();
}while(ans ==’Y’ ||ans ==’y’);
getch();
}
/******************** End Of Program ************************/
//Output
Implementation Of Stack
Main Menu
1.Push
2.Pop
3.Display
4.exit
Enter Your Choice
1
//Output
Main Menu
1.Insert
2.Delete
3.Display
Enter Your Choice1
/* stack structure*/
struct stack {
char s[size];
int top;
}st;
/*
The push Function
Input:item which is to be pushed
Output:none-simply pushes the item onto the stck
Called By:main
Calls:none
*/
void push(char item)
{
st.top++;
st.s[st.top] =item;
}
/*
The stempty Function
Input:none
Output:returns 1 or 0 for stack empty or not
Called By:main
Calls:none
*/
int stempty()
{
if(st.top==-1)
return 1;
else
return 0;
}
/*
The pop Function
Input:none
Output:returns the item which is popped from the stack
Called By:main
Calls:none
*/
char pop()
{
char item;
item=st.s[st.top];
st.top—;
return(item);
}
/*
The main Function
Input:none
Output:none
Called By:O.S.
Calls:push,pop,stempty
*/
void main(void)
{
char item,string[10];
int i;
st.top=-1;
clrscr();
printf(“\n Program For reversing a given string”);
printf(“\n\t\t Enter The string:”);
scanf(“%s”,&string);
i=0;
while(string[i]!=’\0’)
{
push(string[i]);
i++;
}
printf(“\t\t\t”);
while(!stempty())
{
item=pop();
printf(“%c”,item);
}
getch();
}