0% found this document useful (0 votes)
78 views17 pages

Data Structure

Uploaded by

Kashif Mohd
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views17 pages

Data Structure

Uploaded by

Kashif Mohd
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

1) ‘C’ program

/*************************************************************
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

The Roll Number 1456 is admitted to 1st Standard


The Roll Number 4567 is admitted to 4th Standard
2) ‘C’ program

/*************************************************************
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

Enter The order of the matrix:3

Enter The Elements For The First Matrix:7


5
6
4
6
7
5
4
3

Enter The Elements For The Second Matrix:5


4
5
3
6
7
6
8
6

The Addition Is
12 9 11
7 12 14
11 12 9
3) ‘C’ program
/*************************************************************
Program for implementing a stack using [Link] 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([Link]>=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)
{
[Link]++;
st.s[[Link]] =item;
}
/*
The stempty Function
Input:none
Output:returns 1 or 0 for stack empty or not
Called By:main
Calls:none
*/
int stempty()
{
if([Link]==-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[[Link]];
[Link] – –;
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=[Link];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;
[Link]=-1;
clrscr();
printf(“\n\t\t Implementation Of Stack”);
do
{
printf(“\n Main Menu”);
printf(“\[Link]\[Link]\[Link]\[Link]”);
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
[Link]
[Link]
[Link]
[Link]
Enter Your Choice
1

Enter The item to be pushed:3

Do You want To Continue?y


Main Menu
[Link]
[Link]
[Link]
[Link]
Enter Your Choice1

Enter The item to be pushed:7

Do You want To Continue?y


Main Menu
[Link]
[Link]
[Link]
[Link]
Enter Your Choice1

Enter The item to be pushed:8

Do You want To Continue?y


Main Menu
[Link]
[Link]
[Link]
[Link]
Enter Your Choice
2

The popped element is 8


Do You want To Continue?y
Main Menu
[Link]
[Link]
[Link]
[Link]
Enter Your Choice4
4) ‘C’ program
/*************************************************************
Program for implementing the Queue using arrays
*************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define size 5
struct queue
{
int que[size];
int front,rear;
}Q;
/*
The Qfull Function
Input:none
Output:1 or 0 for q full or not
Called By:main
Calls:none
*/
Qfull()
{
if([Link] >=size–1)
return 1;
else
return 0;
}
/*
The insert Function
Input:item -which is to be inserted in the Q
Output:rear value
Called By:main
Calls:none
*/
int insert(int item)
{
if([Link] == –1)
[Link]++;
[Link][++[Link]] = item;
return [Link];
}
int Qempty()
{
if(([Link] == – 1) || ([Link] > [Link]))
return 1;
else
return 0;
}
/*
The delete Function
Input:none
Output:front value
Called By:main
Calls:none
*/
int delete()
{
int item;
item = [Link][[Link]];
[Link]++;
printf(“\n The deleted item is %d”,item);
return [Link];
}
/*
The display Function
Input:none
Output:none
Called By:main
Calls:none
*/
void display()
{
int i;
for(i=[Link];i<=[Link];i++)
printf(“ %d”,[Link][i]);
}
void main(void)
{
int choice,item;
char ans;
clrscr();
[Link] = –1;
[Link] = –1;
do
{
printf(“\n Main Menu”);
printf(“\[Link]\[Link]\[Link]”);
printf(“\n Enter Your Choice”);
scanf(“%d”,&choice);
switch(choice)
{
case 1:if(Qfull()) //checking for Queue overflow
printf(“\n Can not insert the element”);
else
{
printf(“\n Enter The number to be inserted”);
scanf(“%d”,&item);
insert(item);
}
break;
case 2:if(Qempty())
printf(“\n Queue Underflow!!”);
else
delete();
break;
case 3:if(Qempty())
printf(“\nQueue Is Empty!”);
else
display();
break;
default:printf(“\n Wrong choice!”);
break;
}
printf(“\n Do You Want to continue?”);
ans =getche();
}while(ans ==’Y’||ans ==’y’);
}

//Output

Main Menu
[Link]
[Link]
[Link]
Enter Your Choice1

Enter The number to be inserted4

Do You Want to continue?y


Main Menu
[Link]
[Link]
[Link]
Enter Your Choice1

Enter The number to be inserted6

Do You Want to continue?y


Main Menu
[Link]
[Link]
[Link]
5) ‘C’ program
/*************************************************************
Program to evaluate a given postfix expression. The program
handles postfix expressions with only binary arithmatic
operators +,-,*,/ and ^. Operands are single digit numbers
between 0-9.
*************************************************************/
/* List of include files */
#include <stdio.h>
#include <conio.h>
#include <process.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
/* List of defined constants */
#define size 80
/* Global declarations */
struct stack
{ double s[size];
int top;
}st;
enum Type { operand, oprtor };
/* The Push function
Input : A value to be pushed on global stack
Output: None, modifies global stack and its top
Parameter Passing Method :By Value
Called By : Post()
Calls :none
*/
void Push(double Val)
{
if ( [Link]+1 >= size )
printf(“Error: Stack is Full\n”);
[Link]++;
st.s[[Link]] = Val;
}
/* The Pop function
Input : None, uses global stack and top
Output: Returns the value on top of stack
Parameter Passing Method :None
Called By : post()
Calls :none
*/
double Pop()
{
double Val;
if ( [Link] == -1 )
printf(“Error: Stack is Empty\n”);
Val = st.s[[Link]];
[Link]—;
return(Val);
}
/* The gettokn function
Input : Given infix expression
Output: Returns the next character from the expression
Parameter Passing Method : By reference
Called By : post()
Calls : None
*/
char gettokn( char exp[])
{
static int i = 0;
char ch;
ch = exp[i];
i++;
return ch;
}
/* The Gettype function
Input : A character from infix expression
Output: Returns the token type as operand or oprtor
Parameter Passing Method : By value
Called by : post()
Calls : none
*/
enum Type Gettype(char ch)
{
ch = toupper(ch); /* convert to upper case */
if ( ch >= ‘0’ && ch <= ‘9’)
return operand;
if ( ch == ‘+’|| ch == ‘-’||
ch == ‘*’|| ch == ‘/’ ||
ch == ‘^’ )
return oprtor;
/* Error */
printf(“Invalid operator\n”);
}
/* The post function which contains major logic of
the program.
Input : A post Expression of single digit operand
Output: Resultant value after evaluating the expression
Parameter Passing Method :by reference
Called By : main()
Calls :gettoken(), Push(), Pop() and
Gettype()
*/
double post( char exp[])
{
char ch,type;
double result, Val, Op1, Op2;
[Link] = 0;
ch = gettokn(exp);
while ( ch != ‘$’ )
{
type = Gettype(ch);
if( type ==operand)
{
Val = ch - 48;
Push( Val);
}
else
if ( type == oprtor)
{
Op2 = Pop();
Op1 = Pop();
switch(ch)
{
case ‘+’ : result = OP1 + Op2;
break;
case ‘–’ : result = Op1 – Op2;
break;
case ‘*’ : result = Op1 * Op2;
break;
case ‘/’ : result = Op1 / Op2;
break;
case ‘^’ : result = pow (Op1 – Op2;
break;
}/* switch */
Push (result);
}
ch = gettokn(exp);
}/* while */
result = Pop();
return(result);
}
/* The main function
Input : None
Output : None
Parameter Passing Method : None
called By : OS
Calls : post()
*/
void main ()
{
/* Local declarations */
char exp[size];
int len;
double Result;
clrscr();
printf(“”Enter the postfix Expression\n");
scanf(“%S”,exp);
len = strlen(exp);
exp[len] = ‘’; /* Append at the end */
Result = post(exp);
printf(“The Value of the expression is %f\n”, Result);
getch();
exit(0);
}
/ ************** End of the Program ***************** /
6) ‘C’ Program
/ **********************************************************
Program for conversion of Infix expression to Postfix form.
************************************************************/
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
char inf[40],post[40];
int top=0,st[20];
void postfix();
void push(int);
char pop();
/*
The main Function
Input:none
Output:none
Called BY:O.S.
Calls:postfix
*/
void main(void)
{
clrscr();
printf(“n\tEnter the infix expression :: \n\n\t\t”);
scanf(“%s”, inf);
postfix();
getch();
}
/*
The postfix Function
Input:none
Output:none
Called By:main
Calls:push,pop
*/
void postfix()
{
int i,j=0;
for(i=0;inf[i]!=’\0’;i++)
{
switch(inf[i])
{
case ‘+’ : while(st[top] > = 1)
post[j++] = pop();
push(1)
break;
case ‘–’ : while(st[top] >=1)
post[j++] = pop();
push(2);
break;
case ‘*’ : while(st[top] >=3)
pos[j++] = pop();
push(3)
break;
case ‘/’ : while(st[top] > = 3)
post[j++] = pop();
push(4);
break;
case ‘^’ : while(st[top] >=4)
post[j++] = pop();
push(5);
break;
case ‘(‘ : push(0);
break;
case ‘)’ : while(st[top] ! = 0
post[j++] = pop();
top —;
break;
default : post[j++] = inf[i];
}
}
while(top>0)
post[j++] = pop();
print(“\n\tPostfix expression is = > \n\n\t\t %s”, post);
}
/*
The push Function
Input:ele-the element which is to be pushed onto the stack
Output:none
Called By:postfix
Calls:none
*/
void push(int ele)
{
top++;
st[top] = ele;
}
/*
The pop Function
Input:none
Output:e-the popped element
Called By:postfix
Calls:none
*/
char pop()
{
int el;
char e;
el = st[top];
top—;
switch(el)
{
case 1 : e = ‘+’;
break;
case 2 : e = ‘–’;
break;
case 3 : e = ‘*’;
break;
case 4 : e = ‘/’;
break;
case 5 : e = ‘^’;
break;
}
return(e);
}
7) ‘C’ program
/*************************************************************
Program for reversing the given string using stack
*************************************************************/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#define size 10

/* 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)
{
[Link]++;
st.s[[Link]] =item;
}
/*
The stempty Function
Input:none
Output:returns 1 or 0 for stack empty or not
Called By:main
Calls:none
*/
int stempty()
{
if([Link]==-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[[Link]];
[Link]—;
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;
[Link]=-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();
}

You might also like