0% found this document useful (0 votes)
10 views28 pages

Data Structures

The document contains a series of practical programming exercises focused on data structures and algorithms, including linear and binary search, memory management, linked lists, stacks, queues, and sorting algorithms. Each exercise includes the aim, solution code in C, and output examples. The experiments are organized chronologically with specific dates and page numbers.

Uploaded by

Abhay Damalu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views28 pages

Data Structures

The document contains a series of practical programming exercises focused on data structures and algorithms, including linear and binary search, memory management, linked lists, stacks, queues, and sorting algorithms. Each exercise includes the aim, solution code in C, and output examples. The experiments are organized chronologically with specific dates and page numbers.

Uploaded by

Abhay Damalu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

INDEX

S. Name of Experiment Date of Page Signature


No. Practical No.

1. Write a program for Linear search on 1-D Array 24-04- 3


2021

2. Write a program for Binary search on 1-D Array. 26-04- 5


2021

3. Write a program to insert and delete an element in 1-D 03-05- 7


array. 2021

4. Write a program to allocate and de-allocate memory 10-05- 9


dynamically. 2021

5. Write a program to create, traverse and search an 17-05- 11


element in Singly Linked List. 2021

6. Write a program for inserting and deleting element in 24-05- 13


Singly Linked List. 2021

7. Write a program to create Stack and do push and pop 28-05- 16


operation. 2021

8. Write a program to implement Queue using an array. 31-05- 19


2021

9. Write a program for Bubble Sort 04-06- 22


2021

10. Write a program for Selection Sort. 11-06- 24


2021

11. Write a program for Insertion Sort. 18-06- 26


2021
Pracrtical No: 1
Aim: Write a program for linear search on 1-D array.

Solution:

//Write a program for linear search on 1-D array....

#include<stdio.h>
#include<conio.h>
int main()
{
int array[50], search, c, n;
printf("Enter the size of array :");
scanf("%d",&n);
printf("Enter the element in the array :\n");
for(c=0;c<n;c++)
scanf("%d",&array[c]);
printf("Enter the number you want to search :\n");
scanf("%d",&search);
for(c=0;c<n;c++)
{
if (array[c]==search)
{
printf("\n%d is present at position %d\n",search,c+1);
break;
}
}
if (c==n)
printf("% is not present at position in the array\n",search);

3
Output:

Fig 1.1: The output of linear seraching an element in 1-D array

4
Practical No: 2

Aim: Write a program for binary search on 1-D array.

Solution:

// Write a program for Binary search on 1-D array....

#include<stdio.h>
#include<conio.h>

int main()
{
int array[100], c, first,last, mid, n, search;
printf("Enter the size of array:");
scanf("%d",&n);
printf("Enter the elements in array :");
for(c=0;c<n;c++)
scanf("%d",&array[c]);
printf("Enter the element you want to search :");
scanf("%d",&search);

first = 0;
last = n-1;
mid = (first+last)/2;
while(first<=last)
{
if(array[mid]<search)
first = mid + 1;
else if(array[mid] == search)
{
printf("%d is present at position %d",search, mid +1 );
break;
}
else last = mid -1;
mid = (first + last)/2;

if(first > last)


printf("%d is not present in the array",search);

5
Output:

Fig 1.2: The output of binary seraching an element in 1-D array

6
Practical No: 3

Aim: Write a program to insert and delete an element in 1-D array.

Solution:

//Write a program to insert and delete an element u 1-D array.....

#include<stdio.h>
#include<conio.h>

int main()
{
int LA[] = {1,3,5,7,8};
int item = 10, k=3,n=5;
int i = 0, j = n;
int choice;
printf("The elements present in an array element are :\n");
for(i=0; i<n; i++)
{
/ printf("LA[%d]=%d\n",i,LA[i]);
}
printf("1. Inserting an element in an array\n");
printf("2. Deleting an element in an array\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch (choice)
{
case 1:
n = n+1;
while(j>=k)
{
LA[j+1]=LA[j];
j = j-1;
}
LA[k] = item;
printf("The array element after insertion :\n");
for(i=0;i<n;i++)
{
printf("LA[%d]=%d\n",i,LA[i]);
}
break;
case 2:
j = k;
while(j<n)
{
LA[j-1]=LA[j];
j=j+1;
}
7
n=n-1;

printf("The array element after deletion :\n");


for(i=0;i<n;i++)
{
printf("LA[%d]=%d\n",i,LA[i]);
}
break;
default:
printf("invalid choice");
}
}
Output:

Fig 1.3: The output of insertion of an element

Fig 1.4: The output of deletion of an element


8
Practical No: 4

Aim: Write program to Allocate and De-allocate memory dynamically.

Solution:

.//Write a program for dynamic memory allocation and deallocation..// (malloc, calloc, free)

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

int main()
{
/ int i;
int *A, *B;
int N; //how many elements you want to unsert..
printf("\n Enter number of elements required :");
scanf("%d",&N);
//c...
// memory allocation..

A = (int*) malloc(N); // size of array not fixed.


//A= (int*) calloc(N,sizeof(int));
printf("\n Enter the array:\n");
for(i=0;i<N;i++)
{
scanf("%d",&A[i]);
}
printf("\n The array is:\n");
for(i=0;i<N;i++)
{

printf("%d\t",A[i]);

free(A); //memory will be deallocate...


//c closed

9
Output:

Fig 1.5: The output of allocating and deallocating memory dynamically

10
Practical No: 5

Aim: Write a program to create, traverse and search an element in singly linked list.

Solution:

// Write a program to create, traverse and search an element in singly linked list...

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void create(int);
void traverse();
struct node
{
int data;
struct node * next;
};
struct node*head;
int main()
{
int choice, item;
do
{

printf("\n1. Append list\n2.Traverse\n3.exit\n4.Enter your choice :");


scanf("%d",&choice);
switch (choice)
{
case 1:
printf("\n Enter the item :");
scanf("%d",&item);
create(item);/
break;
case 2:
traverse();
break;
case 3:
exit(0);
break;
default:
printf("\n Please enter a valid choice :");
}
}
while(choice!=3);
}
void create(int item)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node *));
11
if(ptr == NULL)

{
printf("\N OVERFLOW");
}
else
{/
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\n node inserted");
}
}
void traverse()
{
struct node *ptr;
ptr = head;
if(ptr == NULL)
{
printf("empty list..");
}
else
{
printf("printing values...\n");
while(ptr!=NULL)
{
printf("\n %d",ptr->data);
ptr = ptr->next;
}
}
}
Output:

Fig 1.6: The output of to create, traverse and search an element in singly linked list
12
Practical No: 6

Aim: Write a program for inserting and deleting an element in singly linked list.

Solution:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<process.h>

struct node
/{
int data;
struct data *next;
};
struct node *head;

void Insert();
void Delete();
void Display();
int main()
{
int choice;
while(1)
{
printf("\n***Menu***\n");
printf("\n1.Insert\n2.Delete\n3.Display\n4.exit");
printf("\nEnter your choice :");
scanf("%d",&choice);
switch(choice)
{
case 1:
Insert();
break;
case 2:
Delete();
break;
case 3:
Display();
break;
case 4:
exit(0);
/ default:
printf("Please enter a valid choice..");
}
}
13
}

void Insert()
{
struct node*ptr;
int item;
ptr = (struct node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\n OVERFLOW");
}
else
{
printf("\n Enter the numbers :");
scanf("%d",&item);
ptr->data = item;
ptr->next = head;
head = ptr;
/ printf("\n Node inserted is :%d\n",item);
}
}
void Delete()
{
struct node*ptr;
if(head == NULL)
{
printf("\n List is empty");
}
else
{
ptr = head;
head = ptr->next;
free(ptr);
printf("\n Node deleted is %d: ");
}
}
void Display()
{
struct node*ptr;
ptr = head;
if(ptr == NULL)
{
printf("Nothing to print");
}
else
{
printf("\n Printing values...\n");
while(ptr!=NULL)
{
14
/ printf("\n%d",ptr->data);
ptr =ptr->next;
}
}
}

Output:/

Fig 1.7: The output of insertion and deletion and display in linked list

15
Practical No: 7

Aim: Write a program to create Stack and do push and pop operation.

Solution:

// Write a program to create Stack and do push and pop operation....

#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
#define MAX 5
int top=-1,stack[MAX];
void push();
void pop();
void display();

int main()
{
/ int ch;
while(1)
{
printf("\n***stack menu***");
printf("\n\n1.push\n2.pop\n3.display\n4.Exit()");
printf("\n Enter your choice(1-4) :");
scanf("%d",&ch);
switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
break;
default:
printf("\n wrong choice");
}
}
}
void push()
{

16
int n,x;
if(top>=n-1)
{
printf("\n\tSTACK is over flow");

}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d\n",stack[top]);
top--;
}
}
void display()
{
int i;
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n\nPress Next Choice\n");
}
else
{
printf("\n The STACK is empty");
}
}

Output:

17
Fig 1.8: The output of insert and delete and display element in stack

18
Practical No: 8

Aim: Write a program to implement Queue using an array.

Solution:

// Write a program to implement Queue using array...

#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
#define MAX 50
void insert();
void Delete();
void display();
int queue_array[MAX];
int rear = -1;
int front = -1;

int main()
{
int ch;
while(1)
{
printf("\n1.Insert element in queue");
printf("\n2.Delete element from queue");
printf("\n3.Display all element of queue");
printf("\n4.Quite\n");
printf("\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
Delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice\n");
}
19
}
}

void insert()
{
int add_item;
if(rear == MAX - 1)
printf("Queue is overflow\n");
else
{
if(front == -1)
front = 0;
printf("Insert the element in queue:");
scanf("%d",&add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
}
void Delete()
{
if(front == -1)
{
printf("Queue is underflow\n");
return ;
}
else
{
printf("Element deleted from queue is : %d",queue_array[front]);
front = front + 1;

}
}
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");
}
}

Output:

20
Fig 1.9: The output of insert, delete and display an element in queue.

21
Practical No: 9

Aim: Write a program for bubble sort.

Solution:
// Write a program for bubble short...
#include<stdio.h>
#include<conio.h>

int main()
{
int array[100];
int n, i, j, swap;
printf("Enter number of elements :");
scanf("%d",&n);
printf("Enter %d integers\n",n);
for(i=0;i<n;i++)
scanf("%d",&array[i]);
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if (array[j]>array[j+1])
{
swap = array[j];
array[j]= array[j+1];
array[j+1]= swap;
}
}
}
printf("sorted list in shorted form :");
for(i=0;i<n;i++)
printf("%d\n",array[i]);
printf("\n");

}
Output:

22
Fig 2.1: The output of sorted list

23
Practical No:10

Aim: Write a program for selection sort.

Solution:

//Write a program for seletion short

#include<stdio.h>
#include<conio.h>

int main()
{
int size,i,j,temp,list[100];
printf("Enter the size of list:");
scanf("%d",&size);
printf("Enter %d inegter values :\n",size);
for(i=0;i<size;i++)
scanf("%d",&list[i]);
// selection short logic
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
{
if(list[i]>list[j])
{
temp=list[i];
list[i]=list[j];
list[j]=temp;
}
}
}
printf("List after shorting is :");
for(i=0;i<size;i++)
printf("%d\n",list[i]);
}

Output:

24
Fig 2.2: The output of selection sort

25
Practical No:11

Aim: Write a program for Insertion sort

Solution:

// Write a program on insertion short...

#include<stdio.h>
#include<conio.h>
int main()
{
int array[100],num,i,j,pos,temp;
printf("Enter the number of elements in array :");
scanf("%d",&num);
printf("Enter the numbers:");
for(i=0;i<num;i++)
{
scanf("%d",&array[i]);
}
for(i=0;i<num;i++)
{
temp= array[i];
j=i;
while(j>0 && temp<array[j-1])
{
array[j]=array[j-1];
j=j-1;
}
array[j]=temp;
}
printf("\n the array sorted is :\n");
for(i=0;i<num;i++)
{
printf("%d\n",array[i]);

Output:

26
Fig 2.3: The output of insertion sort .

27
Practical No: 12

Aim: Write a program on Merge sort.

Solution:

// Write a program for Merge short...

#include<stdio.h>
#include<conio.h>
#define max 10

int a[11]={10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0};
int b[10];
void merging(int beg, int mid, int end )
{

int beg_1, beg_2, i;

for(beg_1 = beg, beg_2 = mid + 1, i = beg; beg_1 <= mid && beg_2 <= end; i++)
{
if(a[beg_1] <= a[beg_2])
b[i] = a[beg_1++];
else
b[i] = a[beg_2++];
}

while(beg_1 <= mid)


b[i++] = a[beg_1++];

while(beg_2 <= end)


b[i++] = a[beg_2++];
for(i = beg; i <=end; i++)
a[i] = b[i];
}

void sort(int beg, int end)


{
int mid;

if(beg < end)


{
mid = (beg + end) / 2;
sort(beg, mid);
sort(mid+1, end);
merging(beg, mid, end);
}
else
{
return;
28
}
}

int main()
{
int i;

printf("List before sorting\n");

for(i = 0; i <= max; i++)


printf("%d ", a[i]);

sort(0, max);

printf("\nList after sorting\n");

for(i = 0; i <= max; i++)


printf("%d ", a[i]);

Output:

Fig 2.4: The output of merge sort

29

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy