Data Structure1
Data Structure1
PROBLEM SHEET
Mahale Tanisha
SYBCA-B
SEM -3
2312117
#include<stdio.h>
#include<conio.h>
#define MAX 5
void push();
void pop();
void display();
int
stack[5]; int
top=-1;
void main()
{
int ch;
do
{
printf("\n 1. PUSH");
printf("\n 2. POP"); printf("\n 3.
Display");
printf("\n 4. Exit");
switch(ch)
{
case 1:
push();
break;
case 2:
pop(); break;
case 3:
display();
break;
case 4:
exit(0);
break;
deafult:
printf("invalid chocice");
break;
}
getch();
}while(ch!=4);
} void
push()
{
int value;
if(top==MAX-1)
{
printf("\n Overflow");
}
else
{
printf("\n enter a value you want to push:");
scanf("%d",&value);
top=top+1;
stack[top]=value;
}
} void
pop()
{
int value;
if(top==-1)
{
printf("\n Underflow");
}
else
{
value=stack[top];
top=top-1;
printf("\n %d value deleted from stack",value);
}
}
void display()
{
int i;
if(top==-1)
{
printf("\n Underflow");
}
else
{
printf("\n stack elements are......");
for(i=top; i>=0; i--)
{
printf("\n %d",stack[i]);
}
}
}
OUTPUT:
2. Write a program to perform the operations on the Simple QUEUE.
#include<stdio.h>
#define MAX 5
void insert();
void delete();
void display();
int
queue_array[MAX]; int
rear = - 1; int front
= - 1;
main()
{ int
choice;
while (1) {
printf("1.Insert element to queue \n");
printf("2.Delete element from queue \n");
printf("3.Display all elements of queue \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break; case
2: delete();
break;
case 3:
display();
break; case
4: exit(1);
default:
printf("Wrong choice \n");
} /* End of switch */
} /* End of while */
} /* End of main() */
void insert() { int
add_item; if (rear == MAX -
1) printf("Queue Overflow \
n"); else
{
if (front == - 1) /*If queue
is initially empty */ front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item); rear = rear + 1;
queue_array[rear] = add_item;
}
} /* End of insert() */
void
delete() {
if (front == - 1 || front > rear)
{
printf("Queue Underflow \n");
return ;
} else { printf("Element deleted
from queue is :
%d\n",queue_array[front]);
front = front + 1;
}
} /* End of delete() */
void display() { int i; if
(front == - 1) printf("Queue
is empty \n");
else
{
printf("Queue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("\n");
}
/* End of display() */
}
OUTPUT:
3. Write a program to perform the operations on the CIRCULAR QUEUE.
#include<stdio.h>
#define MAX 5 int
cqueue_arr[MAX]; int
front = -1; int rear
= -1; void
insert(int item)
{
if((front == 0 && rear == MAX-1) || (front == rear+1))
{
printf("\nQueue Overflow \n");
return; }
if(front == -1)
{ front = 0; rear
= 0; } else
{ if(rear == MAX-
1) rear = 0; else
rear = rear+1;
}
cqueue_arr[rear] = item ;
}
void deletion()
{
if(front == -1)
{
printf("\nQueue Underflow \n");
return ; }
printf("Element deleted from queue is : %d \n",cqueue_arr[front]); if(front
== rear)
{ front = -1;
rear=-1; } else
{ if(front == MAX-
1) front = 0; else
front = front+1;
} }
void display()
{
int front_pos = front,rear_pos = rear; if(front
== -1)
{
printf("Queue is empty \n");
return; } printf("\nQueue
elements :\n"); if( front_pos
<= rear_pos ) while(front_pos
<= rear_pos)
{
printf("%d ",cqueue_arr[front_pos]); front_pos++;
} else
{
while(front_pos <= MAX-1)
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++; } front_pos = 0;
while(front_pos <= rear_pos)
{
printf("%d ",cqueue_arr[front_pos]); front_pos++;
} } printf("\n"); } int main()
{ int choice,item; do
{ printf("1.Insert \n");
printf("2.Deleten \n");
printf("3.Display \n");
printf("4.Quit \n"); printf("\
nEnter your choice : ");
scanf("%d",&choice);
switch(choice) { case 1 :
printf("Input the element for insertion in queue : ");
scanf("%d", &item); insert(item); break; case 2 :
deletion(); break; case 3: display(); break; case 4:
break; default:
printf("Wrong choice\n");
}
}while(choice!=4);
return 0; }
OUTPUT:
4. Write a program to implement Sequential search.
#include <stdio.h>
void main() { int
num;
int array[10], i, search, found = 0;
printf("Enter the number of elements ");
scanf("%d", &num);
printf("Enter the elements one by one \n");
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
printf("Enter the element to be searched ");
scanf("%d", &search);
OUTPUT:
OUTPUT:
OUTPUT: