DATA Structure Lab Assignment
DATA Structure Lab Assignment
CERTIFICATE
Experiments in Laboratory
1
INDEX
Exp.No Name of the Experiment Date Page.No Signature Remarks
1. Stack operations
2. Queue
3. Linked List
4. Binary Search
Tree
5. Linear Search
6. Binary Search
7. Bubble_Short
8. Selection_Sho
rt
2
1. Stack operations:
#include <stdio.h>
#include<conio.h>
#include<stdlib.h>
#define maxsize 5
void push(){
int ele;
if (top == maxsize-1)
else{
scanf("%d",&ele);
top = top + 1;
stack[top]= ele;
void pop(){
if (top== -1)
printf("Stack is underflow\n");
else{
3
}
void peek(){
if (top == -1)
printf("stack is underflow\n");
else{
void display(){
int i;
if (top== -1)
printf("stack is empty\n");
else{
printf("%3d",stack[i]);
void main(){
int choice;
do
printf("\n1.push\n");
printf("2.pop\n");
printf("3.peek\n");
printf("4.display\n");
printf("5.exit\n");
4
printf("enter your choise:");
scanf("%d",&choice);
switch (choice)
case 5:exit(0);
} while (choice<=5);
OUTPUT:
2. Queue :-
Code:- #include <iostream>
5
using namespace std;
void Insert() {
int val;
if (rear == n - 1)
cout<<"Queue Overflow"<<endl;
else {
if (front == - 1)
front = 0;
cin>>val;
rear++;
queue[rear] = val;
void Delete() {
return ;
} else {
front++;;
void Display() {
if (front == - 1)
cout<<"Queue is empty"<<endl;
else {
cout<<queue[i]<<" ";
cout<<endl;
6
}
int main() {
int ch;
cout<<"4) Exit"<<endl;
do {
cin>>ch;
switch (ch) {
case 1: Insert();
break;
case 2: Delete();
break;
case 3: Display();
break;
case 4: cout<<"Exit"<<endl;
break;
} while(ch!=4);
return 0;
O/P:
7
3. Linked List:-
Code:- #include <stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node{
int data;
struct node*next;
*p,*tmp,*tmp1;
void insert_end(int);
void insert_beg(int);
void i_delete(int);
void delete_beg();
void delete_end();
void display();
void main(){
int val,n;
p=NULL;
do
printf("********menu*********");
8
printf("\n1.INSERT AT END");
printf("\n2.Insert at big");
printf("\n6.Display");
printf("\n7.exit");
scanf("%d",&n);
switch(n){
case 1:
scanf("%d",&val);
insert_end(val);
break;
case 2:
scanf("%d",&val);
insert_beg(val);
break;
case 3:
scanf("%d",&val);
i_delete(val);
break;
case 4:
delete_beg();
break;
case 5:
delete_end();
break;
9
case 6:
display();
break;
case 7:
exit(0);
break;
default:
break;
while('y'==getch());
tmp= p;
tmp1->data=ele;
tmp1->next=NULL;
if(p==NULL)
p=tmp1;
else
while(tmp->next!=NULL)
tmp=tmp->next;
tmp->next=tmp1;
tmp=p;
10
tmp1->data=ele;
tmp1->next=p;
p=tmp1;
tmp=p;
while(tmp!=NULL){
if(tmp->data==ele){
if(tmp==p){
p=tmp->next;
free(tmp);
return;
else{
pre->next=tmp->next;
free(tmp);
return;
else{
pre=tmp;
tmp=tmp->next;
void delete_beg(){
tmp=p;
if(p==NULL)
11
else{
printf("\nelement deleted-%d",p->data);
p=p->next;
void delete_end(){
tmp=p;
if(p==NULL)
else if(p->next==NULL){
p=NULL;
else{
while(tmp->next!=NULL){
pre=tmp;
tmp=tmp->next;
pre->next=NULL;
void display(){
tmp=p;
while(tmp!=NULL){
printf("\n%d",tmp->data);
tmp=tmp->next;
O/P:
12
4.BinarySearch Tree:-
Code: #include <stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node{
int data;
struct node*next;
*p,*tmp,*tmp1;
void insert_end(int);
void insert_beg(int);
void i_delete(int);
void delete_beg();
void delete_end();
void display();
void main(){
int val,n;
p=NULL;
do
printf("********menu*********");
printf("\n1.INSERT AT END");
printf("\n2.Insert at big");
13
printf("\n3.Delete a partucular ele");
printf("\n6.Display");
printf("\n7.exit");
scanf("%d",&n);
switch(n){
case 1:
scanf("%d",&val);
insert_end(val);
break;
case 2:
scanf("%d",&val);
insert_beg(val);
break;
case 3:
scanf("%d",&val);
i_delete(val);
break;
case 4:
delete_beg();
break;
case 5:
delete_end();
break;
case 6:
display();
14
break;
case 7:
exit(0);
break;
default:
break;
while('y'==getch());
tmp= p;
tmp1->data=ele;
tmp1->next=NULL;
if(p==NULL)
p=tmp1;
else
while(tmp->next!=NULL)
tmp=tmp->next;
tmp->next=tmp1;
tmp=p;
tmp1->data=ele;
tmp1->next=p;
15
p=tmp1;
tmp=p;
while(tmp!=NULL){
if(tmp->data==ele){
if(tmp==p){
p=tmp->next;
free(tmp);
return;
else{
pre->next=tmp->next;
free(tmp);
return;
else{
pre=tmp;
tmp=tmp->next;
void delete_beg(){
tmp=p;
if(p==NULL)
else{
16
printf("\nelement deleted-%d",p->data);
p=p->next;
void delete_end(){
tmp=p;
if(p==NULL)
else if(p->next==NULL){
p=NULL;
else{
while(tmp->next!=NULL){
pre=tmp;
tmp=tmp->next;
pre->next=NULL;
void display(){
tmp=p;
while(tmp!=NULL){
printf("\n%d",tmp->data);
tmp=tmp->next;
O/P:
17
5.Linear Search:
Code:
#include<iostream>
18
int main()
cin>>tot;
cin>>arr[i];
cin>>num;
if(arr[i]==num)
arrTemp[j] = i;
j++;
chk++;
if(chk>0)
tot = chk;
cout<<arrTemp[i]<<" ";
else
cout<<endl;
return 0;
19
O/P:
6. Binary Search:
Code:
#include <stdio.h>
int loc=0;
int mid=(beg+end)/2;
if(key>a[mid]){
beg=mid+1;
return bs(a,beg,end,key);
else if(key==a[mid]){
loc=mid+1;
return loc;
else if(key<a[mid]){
end=mid-1;
return bs(a,beg,end,key);
20
}
else
return -1;
int main()
int n,a[20],i,key,loc=-1;
printf("Enter range:\n");
scanf("%d",&n);
printf("Enter elements:\n");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
printf("Key:\n");
scanf("%d",&key);
loc=bs(a,0,n,key);
if(loc!=-1){
else{
return 0;
21
O/P:
7. Bubble_Short:
Code:- #include <stdio.h>
int main()
scanf("%d", &n);
scanf("%d", &array[c]);
22
for (c = 0 ; c < n - 1; c++)
if (array[d] > array[d+1]) /* For decreasing order use '<' instead of '>' */
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
printf("%d\n", array[c]);
return 0;
O/P:
23
8.Selection_Short:
Code:
#include<iostream>
int temp;
temp = a;
a = b;
b = temp;
int i, j, imin;
imin = i;
imin = j;
swap(array[i], array[imin]);
int main() {
int n;
cin >> n;
24
int arr[n];
display(arr, n);
selectionSort(arr, n);
display(arr, n);
}}
O/P:
25