You are on page 1of 44

VELLORE INSTITUTE OF TECHNOLOGY

DATA STRUCTURES AND ALGORITHMS .

NAME : G. Shiyam Sundaram

REGISTER NUMBER : 20BIT0100

COURSE CODE : ITE1004

LAB : L17+L18

SUBMITTED TO : PRABHUKUMAR M

1. Stack implementation using an array

C code:

#include <stdio.h>
#include<stdlib.h>
#define max 10 //this is the max size of the stack
int top=-1; //this is used to identify the top element
int stack[max];
void pop();
void push();
void disp();
int sum();
int main()
{
int opt;
//while loop to execute the code again and again
while(1){

printf("*****menu******\n 1.for pushing \n 2. for popping \n 3. for displaying the


stack \n 4. to find the sum \n 5. to know position of top \n 6.to exit\n");
scanf("%d",&opt);
switch(opt)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
disp();
break;
case 4:
sum();
break;
case 5:
printf("%d\n",top);
break;
case 6:
exit(0);// exit is under the header file stdlib.h
}
}
return 0;
}
void push()
{
int a;

if(top==max-1){

printf("stack overflow \n");


}
else
{

printf("enter the element to be pushed into the stack:");


scanf("%d",&a);
top=top+1;//incrementting the top to the next free space in stack
stack[top]=a;
printf("succesfully pushed \n");
}
}
void pop()
{
int a;
if(top==-1)
{
printf("stack underflow / stack is empty \n");
}
else
{
a=stack[top];
top=top-1;
printf("successfully poped \n");
}

}
void disp()//we need to display the items between 0 to top
{
int i;
if(top==-1)
{
printf("the stack is empty\n");
}
else
{
for(i=top;i>=0;--i)
{
printf("%d",stack[i]);
printf("\n");

}
}
printf("succesfully printed \n");
}
int sum()
{
int i;
int count;//count is used to store the sum of the elements
count=0;
for(i=top;i>=0;--i)
{
count=count+stack[i];
}
printf("the sum of the element in the stack is : %d\n",count);
}

COMMAND WINDOW:
2.balancing an equation:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define max 30
int top=-1;
char stack[max];
void push(char);
char pop();
bool match(char a,char b);
int ans(char []);
int main()
{
char ex[max];
int j;
printf("enter the exp...:");
scanf("%s",&ex);// scan the expression as a string//
j=ans(ex);//the function ans returns a value 1 if balanced//
if(j==1)
{
printf("balanced\n");
}
else
{
printf("unbalanced\n");
}
return 0;
}
int ans(char ex[])
{
int i;
char t;
int k;
for(i=0;i<strlen(ex);i++)//for loop to read character by character //
{
if(ex[i]=='('||ex[i]=='['||ex[i]=='{')
{
push(ex[i]);
}
if(ex[i]==')'||ex[i]==']'||ex[i]=='}')
{
if(top==-1)
{
return 0;
}
else
{
//compare ex[i] with the element at the top of the stack//
//if no match then it is unbalanced//
//if matched then continue till end//
t=pop();
if(!match(t, ex[i]))
{
return 0;
}
}
}
}
if(top==-1)//after reading all char check if the the stack is empty //
{
return 1;
}
else
{
return 0;
}
}
bool match(char a,char b)//it returns either 1 or 0 so it is a bool type function//
{
if(a=='[' && b==']'||a=='{' && b=='}'||a=='(' && b==')')
return 1;
return 0;
}
void push(char item)
{
if(top==(max-1))
{
printf("Stack Overflow\n");
return;
}
top=top+1;
stack[top]=item;
}
char pop()
{
if(top==-1)
{
printf("Stack Underflow\n");
exit(1);
}
return(stack[top--]);
}

COMMAND WINDOW:
3. Infix to prefix conversion:

#include<stdio.h>
#include<ctype.h>
#include<string.h>
#define max 100
char stack[max];
int top=-1;
void push(char x);
char pop();
int priority(char x);
int main()
{
char exp[max];
char ans[max];//reversed string of final expression //
char *e,x;
int i=0,j=0;
printf("Enter the expression:");//please enter with proper brackets//
gets(exp);
strrev(exp);//once reversed then the algorithm is same as the postfix
conversion//
while(exp[i]!='\0')
{
if(isalnum(exp[i]))
{
ans[j++]=exp[i];
}
else if(exp[i]==')')
{
push(exp[i]);
}
else if(exp[i]=='(')
{
while((x=pop())!=')')
{
ans[j++]=x;
}
}
else
{
while(priority(stack[top])>=priority(exp[i]))// we are checking the
priority //
ans[j++]=stack[top];
push(exp[i]);
}
i=i+1;
}
while(top!=-1)
{
ans[j++]=stack[top--];
}
strrev(ans);//the reversed string is again reversed to get final expression//
printf("%s",ans);
return 0;
}
void push(char x)
{
if(top==max-1)
{
printf("stack overflow\n");//stack is full//
}
else
{
stack[++top] = x;
}
}
char pop()
{
if(top == -1)// there is no element in the stack//
return -1;
else
return stack[top--];
}

int priority(char x)
{
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;//they have same priority//
if(x == '*' || x == '/')
return 2;
return 0;
}

COMMAND WINDOW:
4. Infix to postfix conversion

C code:

#include<stdio.h>
#include<ctype.h>//to use isalnum inbuild function//
char stack[100];
int top = -1;
void push(int x)
{
top=top+1;//top is incremented by 1//
stack[top] = x;
}
int pop()
{
if(top == -1){
return -1; //no elements in the stack//
}
else
{
return stack[top--];
}

}
int priority(char x)
{
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;
return 0;
}

int main()
{
char exp[100];
char *e, x;//* pointer is used to point the value at the address e//
printf("Enter the expression : ");
scanf("%s",exp);
printf("\n");
e = exp;//the address of exp is equated to e//
while(*e != '\0')
{
if(isalnum(*e))
printf("%c ",*e);
else if(*e == '(')
push(*e);
else if(*e == ')')
{
while((x = pop()) != '(')
printf("%c ", x);
}
else
{
while(priority(stack[top]) >= priority(*e))
printf("%c ",pop());
push(*e);
}
e++;
}

while(top != -1)//the remaining elements in the stack are printed//


{
printf("%c ",pop());
}return 0;
}

COMMAND WINDOW:
5. Prefix evaluation

C code:

#include<stdio.h>
#include<conio.h>
#include<string.h>
#define max 50
int stack[max];//stack contains integer//
int top=-1;
void push(int x);
int pop();

int main()
{
int a,b,c,i;
char prefix[max];
printf("\nEnter the expression;\n");
gets(prefix);//getting the input in the form of string //
for(i=strlen(prefix)-1;i>=0;i--)
{
if(prefix[i]=='+')
{
c=pop()+pop();
push(c);
}
else if(prefix[i]=='-')
{
a=pop();
b=pop();
c=a-b;
push(c);//push the value into the stack//
}
else if(prefix[i]=='*')
{ a=pop();
b=pop();
c=a*b;
push(c);
}
else if(prefix[i]=='/')
{
a=pop();
b=pop();
c=a/b;
push(c);
}
else
{
push(prefix[i]-'0');//storing the prefix[i] in the form of integer//
}
}
printf("%d",pop());
getch();
return 0;
}
void push(int ch)
{
if(top==max-1)
{
printf("stack overflow\n");//stack is full//
}
else
{
top++;
stack[top]=ch;
}
}
int pop()
{
int ch;
if(top==-1)
{
printf("stack underflow\n");//stack is empty//
}
else
{
ch=stack[top];
top=top-1;
return(ch);
}
}

COMMAND WINDOW:
6. Postfix evaluation

C code:

#include<stdio.h>
#include<ctype.h>// to use isalnum function//
#include<string.h>//to use strrev function //
#define max 100
int stack[max];
int top=-1;//global declaration of top//
void push(int x)
{
if(top>=max-1)
{
printf("stack overflow");//stack is full//
return;
}
else
{
top=top+1;
stack[top] = x;
}
}
int pop()
{
int item;
if (top < 0) {
printf("stack under flow");//stack is empty//
}
else {
item = stack[top];
top = top - 1;
return item;
}
}
void eval(char postfix[])
{
char x;
int i,a,b,val;
for(i=0;postfix[i]!='\0';i++)//for loop runs until an endline character is
scanned//
{
x=postfix[i];
if(isdigit(x))
{
push(x-'0');//pushing x in the int form into the stack//
}
else if(x=='+'||x=='-'||x=='*'||x=='/')
{
a=pop();
b=pop();
switch(x)
{
case'+':
val=b+a;
break;
case'-':
val=b-a;
break;
case'*':
val=b*a;
break;
case'/':
val=b/a;
break;
}
push(val);//push the value into the stack//
}

}printf("the evaluated answer is:%d",pop());//remove the top element from the


stack for final answer //

}
int main()
{
char postfix[max];
int i;
printf("enter the postfix expression:");
scanf("%s",&postfix);
eval(postfix);
printf("\n");
return 0;
}

COMMAND WINDOW:
7. Tower of Hanoi problem:

C code:

#include <stdio.h>
void towers(int, char, char, char);//defining a function//
int main()
{
int num;

printf("Enter the number of disks : ");//get the number of discs//


scanf("%d", &num);
printf("The sequence of moves involved in the Tower of Hanoi are :\n");
towers(num, 'A', 'C', 'B');
return 0;
}
void towers(int num, char from, char to, char middle)
{
if (num == 1)
{
printf("\n Move disk 1 from stack %c to stack %c", from, to);
return;
}
towers(num - 1, from, middle, to);
printf("\n Move disk %d from stack %c to stack %c", num, from, to);
towers(num - 1, middle, to, from);//inside the function the function is again
called so it is a recurssive function//
}
COMMAND WINDOW:
QUEUE AND ITS APPLICATIONS

QUEUE IMPLEMENTATION IN C:

C code:

#include<stdio.h>
#include<stdlib.h>
#define max 5
int top=-1;//USED FOR REMOVING
int rear=-1;//USED FOR INSERTING
void push(int x);
int pop();
void display();
int stack[max];//THIS IS THE QUEUE IN WHICH WE ARE INSERTING//
int main()
{
while(1)
{
int a;

printf("*************menu***********\n1.insertion\n2.deletion\n3.display\n4.exit
\n");
scanf("%d",&a);
switch(a)
{
case 1:
int x;
printf("enter the element to be entered into the queue: ");
scanf("%d",&x);
push(x);
break;
case 2:
int v;
v=pop();
printf("the popped element is :%d\n",v);
break;
case 3:
display();
break;
case 4:
exit(1);
}

}
return 0;
}
void push(int x)
{
if(top==(max-1))
{
printf("stack overflow\n");//QUEUE IS FULL//
}
else if(top==-1&&rear==-1)// INSERTING FIRST ELEMENT //
{
top=top+1;
rear=rear+1;
stack[top]=x;
}
else
{
top=top+1;
stack[top]=x;
}
}
int pop()
{
if(rear==top+1)
{
printf("stack underflow\n");//QUEUE
}
else
{
return stack[rear++];
}
}
void display()
{
int i;
for(i=rear;i<=top;i++)
{
printf("%d\n",stack[i]);
}
}

COMMAND WINDOW:
IMPLEMENTATION OF CIRCULAR QUEUE:

C code:

#include<stdio.h>
#include<stdlib.h>// for using exit function in switch statement //
#define max 5
int top=-1;
int rear=-1;
void push(int x);
void pop();
void display();
int stack[max];
int main()
{
while(1)
{
int a;

printf("*************menu***********\n1.insertion\n2.deletion\n3.display\n4.exit
\n");
scanf("%d",&a);
switch(a)
{
case 1:
int x;
printf("enter the element to be entered into the queue: ");
scanf("%d",&x);
push(x);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(1);
}

}
return 0;
}
void push(int x)
{
if(top==0&&rear==(max-1)||top==rear+1)
{
printf("queue overflow\n");//queue is full//
}
else if(top==-1&&rear==-1)// inserting first element//
{
top=top+1;
rear=rear+1;
stack[rear]=x;
}
else if(rear==(max-1)&&top!=0)
{
rear=0;
stack[rear]=x;
}
else
{
rear=rear+1;
stack[rear]=x;
}
}
void pop()
{
int c;
if(top==-1&&rear==-1)
{
printf("queue underflow\n");//queue is empty//
}
else if(top==rear)// there is only one element in the queue //
{
c=stack[top];
printf("the removed element from the front :%d",c);
top=-1;
rear=-1;
}
else if(top==max-1)
{
c=stack[top];
printf("the removed element from the front :%d",c);
top=0;
}
else
{
c=stack[top];
printf("the removed element from the front :%d",c);
top=top+1;
}
}
void display()
{
int i=top;
if(top==-1&&rear==-1)
{
printf("the queue is empty\n");
}
while(i!=rear)
{
printf("%d\n",stack[i]);
i=(i+1)%max;
}
printf("%d\n",stack[rear]);
}

COMMAND WINDOW:
DOUBLE ENDED QUEUE:

C code:

/* the implementation is similar as the circular queue*/


#include<stdio.h>
#define N 5
int dequeue[N];
int f=-1;
int r=-1;
void insfront(int x);// insertion in front
void insrear(int x);//insertion in rear
void delfront();//delete from front
void delrear();//delete from rear
void display();//displays the elements between top and rear
int main()
{
while(1)
{
int a;
printf("/****menu****/\n1.insert front\n2.insert rear\n3.delete
front\n4.delete rear\n5.display\n");
scanf("%d",&a);
switch(a)
{
case 1:
int b;
printf("enter the element to be inserted:");
scanf("%d",&b);
insfront(b);
break;
case 2:
int v;
printf("enter the element to be inserted:");
scanf("%d",&v);
insrear(v);
break;
case 3:
delfront();
break;
case 4:
delrear();
break;
case 5:
display();
break;
}

}
return 0;
}
void insfront(int x)
{
if(f==0&&r==N-1||f==r+1)
{
printf("queue overflow\n");//queue is full//
}
else if(f==-1&&r==-1)
{
f=0;
r=0;
dequeue[f]=x;// inserting first element to the queue//
}
else if(f==0)//when f is at zero then insert the element at n-1 position //
{
f=N-1;
dequeue[f]=x;
}
else // front is decremented to insert the element (general condition)//
{
f=f-1;
dequeue[f]=x;
}
printf("successfully pushed in front\n");
}
void insrear(int x)
{
if(f==0&&r==N-1||f==r+1)
{
printf("queue overflow\n");//queue is full//
}
else if(f==-1&&r==-1)
{
f=0;
r=0;
dequeue[f]=x;// inserting first element to the queue//
}
else if(r==N-1)//when the r reaches the end of the queue , then it is
updated to zero//
{
r=0;
dequeue[r]=x;
}
else
{
r=r+1;
dequeue[r]=x;
}
}
void delfront()
{
int c;
if(f==-1&&r==-1)
{
printf("queue underflow\n");//queue is empty//
}
else if(f==r)
{
c=dequeue[f];
printf("the removed element from the front :%d",c);//only one
element is there //
f=-1;
r=-1;
}
else if(f==N-1)
{
c=dequeue[f];
printf("the removed element from the front :%d",c);
f=0;
}
else
{
c=dequeue[f];
printf("the removed element from the front :%d",c);
f=f+1;
}
}
void delrear()
{
int c;
if(f==-1&&r==-1)
{
printf("queue underflow\n");
}
else if(f==r)
{
c=dequeue[r];
printf("the removed element from the rear :%d",c);
f=-1;
r=-1;
}
else if(r==0)
{
c=dequeue[r];
printf("the removed element from the rear:%d",c);
r=N-1;
}
else
{
c=dequeue[r];
printf("the removed element from the rear:%d",c);
r=r-1;
}
}
void display()
{
int i=f;
if(f==-1&&r==-1)
{
printf("the queue is empty\n");
}
while(i!=r)
{
printf("%d\n",dequeue[i]);
i=(i+1)%N;
}
printf("%d\n",dequeue[r]);
}

COMMAND WINDOW:
COMMAND WINDOW:

You might also like