0% found this document useful (0 votes)
27 views

Data Structure

The document is a lab manual containing questions and programs related to various data structures and algorithms topics. It includes programs to implement linear search on an array, merge two sorted arrays, implement a stack using an array, implement a queue using an array, implement a linked list, implement selection sort, insertion sort, and quicksort. The programs contain functions to perform operations like insertion, deletion, traversal, sorting on the different data structures.

Uploaded by

Prateek singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Data Structure

The document is a lab manual containing questions and programs related to various data structures and algorithms topics. It includes programs to implement linear search on an array, merge two sorted arrays, implement a stack using an array, implement a queue using an array, implement a linked list, implement selection sort, insertion sort, and quicksort. The programs contain functions to perform operations like insertion, deletion, traversal, sorting on the different data structures.

Uploaded by

Prateek singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Data Structure

LAB MANUAL

Mr. Navdeep Charan | Lecturer | Abhinav College


Q1. Write a program to search an element in the array data
structure using Linear Search.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,item,flag=0;
clrscr();
printf("Enter the data in the array");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
printf("Enter the element to be searched");
scanf("%d",&item);
for(i=0;i<10;i++)
{
if(item==a[i])
{
flag=1;
break;
}
}
if(flag==0)
printf("Element Not Found");
else
printf("Element Found at Position =%d",i);
getch();
}

PAGE 1
Q2. Write a program to merge two sorted arrays into one sorted
array.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],b[10],c[20],i,j,k,n,m,t;
clrscr();
printf("Enter size of Array A\n");
scanf("%d",&n);
printf("Enter the data in Array A\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter size of Array B\n");
scanf("%d",&m);
printf("Enter the data in Array B\n");
for(j=0;j<m;j++)
scanf("%d",&b[j]);
i=j=k=0;
while(i<n&&j<m)
{
if(a[i]<b[j])
c[k++]=a[i++];
else if(a[i]>=b[j])
c[k++]=b[j++];
}
if(i<n)
{
for(t=0;t<n;t++)
c[k++]=a[i++];
}
else
{
for(t=0;t<m;t++)
c[k++]=b[j++];
}
printf("\n");
for(k=0;k<(m+n);k++)
printf("\n %d ",c[k]);
getch();
}

PAGE 2
Q3. Write a program to implement a stack data structure using
an array.
#include<stdio.h>
#include<conio.h>
#include<process.h>
void push();
void pop();
void display();
int top;
int a[5];
void main()
{
int choice;
char ch;
top=-1;
clrscr();
do
{
printf("\n\t 1. PUSH");
printf("\n\t 2. POP");
printf("\n\t 3. DISPLAY");
printf("\n\t 4. EXIT");
printf("\nEnter your choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\nBAD CHOICE");
}
printf("\ndo you want to continue y/n");
ch=getche();
}while(ch=='y');
}
void push()

PAGE 3
{
int item;
if(top==4)
printf("STACK IS FULL");
else
{
printf("Enter the item to be inserted");
scanf("%d",&item);
top=top+1;
a[top]=item;
}
}
void pop()
{
int item;
if(top==-1)
printf("STACK IS EMPTY");
else
{
item=a[top];
top=top-1;
printf("%d is deleted",item);
}
}
void display()
{
int i;
for(i=top;i>=0;i--)
printf("\n%d",a[i]);
}

PAGE 4
Q4. Write a program to implement a queue data structure
using an array.
#include<stdio.h>
#include<conio.h>
#include<process.h>
void insert();
void delet();
void display();
int front,rear;
int q[5];
void main()
{
int choice;
char ch;
front=-1;
rear=-1;
clrscr();
do
{
printf("\n\t 1. INSERT");
printf("\n\t 2. DELETE");
printf("\n\t 3. DISPLAY");
printf("\n\t 4. EXIT");
printf("\nEnter your choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delet();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\nBAD CHOICE");
}
printf("\ndo you want to continue y/n");
ch=getche();
}while(ch=='y'||'Y');
}

PAGE 5
void insert()
{
int item;
if(((front==1)&&(rear==5))||(front==rear+1))
printf("QUEUE IS FULL");
else
{
printf("Enter the element");
scanf("%d",&item);
if(front==-1)
{
front=1;
rear=1;
}
else if(rear==5)
rear=0;
else
rear=rear+1;
q[rear]=item;
}
}
void delet()
{
int item;
if(front==-1)
printf("QUEUE IS EMPTY");
else
{
item=q[front];
if(front==rear)
{
front=-1;
rear=-1;
}
else if(front==5)
front=0;
else
front=front+1;
printf("%d is deleted",item);
}
}
void display()
{
int i;
if(front==-1)
printf("QUEUE IS EMPTY");

PAGE 6
else
{
for(i=front;i<=rear;i++)
printf("\n%d",q[i]);
}
}

PAGE 7
Q5. Write a program to implement a Linked List.
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<process.h>
struct node
{
int info;
struct node *next;
};
struct node *start=NULL;
void ins();
void ins_at_beg();
void ins_at_mid();
void ins_at_end();
void del();
void del_at_beg();
void del_at_mid();
void del_at_end();
void display();
int count();
void main()
{
int ch=0,i=0,cnt;
clrscr();
while(1)
{
printf("***********menu************");
printf("\n1.insert");
printf("\n2.delete");
printf("\n3.display");
printf("\n4.count");
printf("\n5.exit");
printf ("\nenter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
ins();
break;
case 2:
del();
break;
case 3:
display();

PAGE 8
break;
case 4:
cnt=count();
printf("\n the no of nodes : %d\n",cnt);
break;
case 5:
exit(1);
}
}
}
void ins()
{
int j=0,ch1=0;
printf("\nenter your choice");
printf("\n1.insert at the beggning");
printf("\n2.insert at the middle");
printf("\n3.insert at the end");
scanf ("%d",&ch1);
switch(ch1)
{
case 1:
ins_at_beg();
break;
case 2:
ins_at_mid();
break;
case 3:
ins_at_end();
}
}
void ins_at_beg()
{
int info;
struct node *t=(struct node *)malloc(sizeof(struct node));
printf("\nenter information to be inserted in the
beggning");
scanf("%d",&info);
t->info=info;
t->next=start;
start=t;
}
void ins_at_mid()
{
int inform,x,i;
struct node *t=(struct node *)malloc(sizeof(struct node));
struct node *p=start;

PAGE 9
printf("\nenter the location after which new node to be
added");
scanf("%d",&x);
for(i=1;i<x;i++)
p=p->next;
printf("\nenter information of the new node");
scanf("%d",&inform);
t->info=inform;
t->next=p->next;
p->next=t;
}
void ins_at_end()
{
int inform1;
struct node *t=(struct node *)malloc(sizeof(struct node));
struct node *p=start;
printf("\nenter information to be added");
scanf("%d",&inform1);
t->info=inform1;
while(p->next!=NULL)
p=p->next;
p->next=t;
t->next=NULL;
}
void del()
{
int k=0,ch2=0;
printf("\nenter your choice");
printf("\n1.delete at the beggning");
printf("\n2.delete at the middle");
printf("\n3.delete at the end");
scanf ("%d",&ch2);
switch(ch2)
{
case 1:
del_at_beg();
break;
case 2:
del_at_mid();
break;
case 3:
del_at_end();
break;
}
}
void del_at_beg()

PAGE 10
{
struct node *t=start;
start=start->next;
free(t);
}
void del_at_mid()
{
int n;
struct node *cur=start;
struct node *pre=start;
printf("\nenter information to be deleted");
scanf("%d",&n);
while(cur->info!=n)
{
pre=cur;
cur=cur->next;
}
pre->next=cur->next;
free(cur);
}
void del_at_end()
{
struct node *cur=start;
struct node *pre=start;
while(cur->next!=NULL)
{
pre=cur;
cur=cur->next;
}
pre->next=NULL;
free(cur);
}
void display()
{
struct node *p=start;
printf("\n\n***************LINK
LIST*****************\n\n");
while(p!=NULL)
{
printf("%d\n",p->info);
p=p->next;
}
}
int count()
{
int c=0;

PAGE 11
struct node *q=start;
while(q!=NULL)
{
q=q->next;
c=c+1;
}
return c;
}

PAGE 12
Q6. Write a program to implement Selection Sort.
#include<stdio.h>
#include<conio.h>
void select(int [],int);
int min(int [],int,int);
void main()
{
int a[20],i,n;
clrscr();
printf("Enter the number of items in the array");
scanf("%d",&n);
printf("Enter the data in the array");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
select(a,n);
getch();
}
void select(int a[],int n)
{
int i,loc,temp;
loc=0;
temp=0;
for(i=0;i<n;i++)
{
loc=min(a,i,n);
temp=a[loc];
a[loc]=a[i];
a[i]=temp;
}
printf("\nData After Selection Sort");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
}
int min(int a[],int lb,int ub)
{
int m=lb;
while(lb<ub)
{
if(a[lb]<a[m])
m=lb;
lb++;
}
return m;
}

PAGE 13
Q7. Write a program to implement Insertion Sort.
#include<stdio.h>
#include<conio.h>
void insert(int [],int);
void main()
{
int a[20],i,n;
clrscr();
printf("Enter the number of items in the array");
scanf("%d",&n);
printf("Enter the data in the array");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
insert(a,n);
getch();
}
void insert(int a[],int n)
{
int i,j,temp;
for(i=1;i<n;i++)
{
temp=a[i];
for(j=i-1;j>=0;j--)
{
if(a[j]>temp)
a[j+1]=a[j];
else
break;
}
a[j+1]=temp;
}
printf("Data After Insertion Sort");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
}

PAGE 14
Q8. Write a program to implement Quick Sort.
#include<stdio.h>
#include<conio.h>
void quicksort(int[],int,int);
int partition(int [],int,int);
void main()
{
int a[20],i,n;
clrscr();
printf("Enter the size of array");
scanf("%d",&n);
printf("Enter the elements in the array");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quicksort(a,0,n-1);
for(i=0;i<n;i++)
printf("\n%d",a[i]);
getch();
}
void quicksort(int a[],int lb,int ub)
{
int mid;
if(lb<ub)
{
mid=partition(a,lb,ub);
quicksort(a,lb,mid-1);
quicksort(a,mid+1,ub);
}
}
int partition(int a[],int lb,int ub)
{
int i,p,q,t;
p=lb+1;
q=ub;
i=a[lb];
while(q>=p)
{
while(a[p]<i)
p++;
while(a[q]>i)
q--;
if(q>p)
{
t=a[p];
a[p]=a[q];

PAGE 15
a[q]=t;
}
}
t=a[lb];
a[lb]=a[q];
a[q]=t;
return q;
}

PAGE 16
Q9. Write a program to implement Merge Sort.
#include<stdio.h>
#include<conio.h>
void mergesort(int a[],int,int);
void merge(int [],int,int,int);
void main()
{
int a[20],i,n;
clrscr();
printf("Enter the number of elements");
scanf("%d",&n);
printf("Enter the elements");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
printf("Data After Merge Sort");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
getch();
}
void mergesort(int a[],int lb,int ub)
{
int mid;
if(lb<ub)
{
mid=(lb+ub)/2;
mergesort(a,lb,mid);
mergesort(a,mid+1,ub);
merge(a,lb,mid+1,ub);
}
}
void merge(int a[],int lb,int mid,int ub)
{
int k,p1,p2,p3,b[20];
p1=lb;
p3=lb;
p2=mid;
while((p1<mid)&&(p2<=ub))
{
if(a[p1]<=a[p2])
b[p3++]=a[p1++];
else
b[p3++]=a[p2++];
}
while(p1<mid)

PAGE 17
b[p3++]=a[p1++];
while(p2<=ub)
b[p3++]=a[p2++];
for(k=lb;k<p3;k++)
a[k]=b[k];
}

PAGE 18
Q10. Write a program to search an element in the sorted array
using Binary Search.
#include<stdio.h>
#include<conio.h>
void binary(int [],int,int);
void main()
{
int a[20],i,n,item;
clrscr();
printf("Enter the number of items in the array");
scanf("%d",&n);
printf("enter the data in array in ascending order");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the element to be searched");
scanf("%d",&item);
binary(a,n,item);
getch();
}
void binary(int a[],int n,int item)
{
int beg,end,mid,loc=-1;
beg=0;
end=n-1;
while(beg<=end)
{
mid=(beg+end)/2;
if(item==a[mid])
{
loc=mid;
break;
}
else if(item>a[mid])
beg=mid+1;
else
end=mid-1;
}
if(loc==-1)
printf("Element not Found");
else
printf("Element Found at position = %d",loc);
}

PAGE 19
PAGE 20

You might also like