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

Dsa File by Aks

The document contains code submissions and outputs from Akshat Kumar Sharma (Roll No. 2821005) for various data structures programs as part of their practical file submission. The programs cover linear search, binary search, bubble sort, selection sort, insertion sort, quick sort, merge sort, stack implementation and operations, and queue implementation and operations. For each program, the code, inputs if any, and output are included.

Uploaded by

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

Dsa File by Aks

The document contains code submissions and outputs from Akshat Kumar Sharma (Roll No. 2821005) for various data structures programs as part of their practical file submission. The programs cover linear search, binary search, bubble sort, selection sort, insertion sort, quick sort, merge sort, stack implementation and operations, and queue implementation and operations. For each program, the code, inputs if any, and output are included.

Uploaded by

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

Roll No.

2821005

Panipat Institute of Engineering & Technology


Samalkha
Computer Science & Engineering Department

Practical File of Data Structures


Code - PC-CS205AL

Submitted to: Shivani Gaba Mam Submitted by:


Akshat Kumar Sharma
Roll: 2821005
Section : A

Affiliated to

1
Roll No. 2821005

PROGRAM:-1a

AIM: WRITE A PROGRAM FOR LINEAR SEARCH.

CODE:

#include <stdio.h>
int main()
{
printf("NAME: AKSHAT KUMAR SHARMA\n");
printf("ROLL: 2821005\n");
int a[10], i, item,n;
printf("\nEnter number of elements of an array:\n");
scanf("%d",&n);
printf("\nEnter elements: \n");
for (i=0; i<n; i++)
scanf("%d", &a[i]);
printf("\nEnter item to search: ");
scanf("%d", &item);
for (i=0; i<=9; i++)
if (item == a[i])
{
printf("\nItem found at location %d", i+1);
break;
}
if (i > 9)
printf("\nItem does not exist.");
return 0;
}

2
Roll No. 2821005

OUTPUT:-

3
Roll No. 2821005

PRACTICAL :- 1b

AIM: WRITE A PROGRAM FOR BINARY SEARCH.

CODE:

#include<stdio.h>

void main()
{
int mid,f,l,a[5],i,item;
printf("NAME: AKSHAT KUMAR SHARMA \n");
printf("ROLL NO.: 2821005\n");

printf("enter 5 elements in array\n");


for(i=0;i<=4;i++)
{
scanf("%d",&a[i]);
}
f=0;
l=4;
mid=(f+l)/2;
printf("enter item to be searched\n");
scanf("%d",&item);
while ((a[mid]!=item) && (f<=l))
{if(a[mid]<item)
{f=mid+1;

}
else
{l=mid-1;
}
mid=(f+l)/2;
}
if(f>l)
printf("Item not found");
else
printf("Item found at location %d",mid+1);
}

4
Roll No. 2821005

OUTPUT:

5
Roll No. 2821005

PRACTICAL :- 2a

AIM: WRITE A PROGRAM TO ILLUSTRATE BUBBLE SORTING

CODE:

#include<stdio.h>

void main()

printf("NAME: AKSHAT KUMAR SHARMA \n");

printf("ROLL NO.: 2821005\n");

int a[5],i,j,temp;

printf("enter 5 elements of array\n");

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

scanf("%d",&a[i]);

for(i=5;i>1;i--)

{for(j=0;j<i-1;j++)

{if(a[j]>a[j+1])

{ temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;}

}}

printf("after bubble sorting\n");

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

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

6
Roll No. 2821005

OUTPUT:

7
Roll No. 2821005

PRACTICAL:- 2b

AIM: WRITE A PROGRAM TO IMPLEMENT SELECTION SORTING

CODE:

#include<stdio.h>

void main()
{
printf("NAME: AKSHAT KUMAR SHARMA \n");
printf("ROLL NO.: 2821005\n");
int a[5],i,j,loc,min,temp;
printf("enter 5 elements of array\n");
for(i=0;i<5;i++)
scanf("%d\n",&a[i]);
for(i=0;i<4;i++)
{ min=a[i];
loc=i;
for(j=i+1;j<5;j++)
{
if(a[j]<min)
{
min=a[j];
loc=j;
}
}
temp=a[i];
a[i]=a[loc];
a[loc]=temp;
}
printf("after selection sorting\n") ;
for(i=0;i<5;i++)
printf("%d\n",a[i]);

8
Roll No. 2821005

OUTPUT:

9
Roll No. 2821005

PRACTICAL :- 2c

AIM: WRITE A PROGRAM TO IMPLEMENT INSERTION SORT

CODE:

// C program for insertion sort


#include <math.h>
#include <stdio.h>

/* Function to sort an array


using insertion sort*/
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i - 1;

/* Move elements of arr[0..i-1],


that are greater than key,
to one position ahead of
their current position */
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}

// A utility function to print


// an array of size n
void printArray(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
10
Roll No. 2821005

// Driver code
int main()
{
printf("NAME: AKSHAT KUMAR SHARMA \n");
printf("ROLL: 2821005\n");
int arr[10],n,i;
printf("enter the size of an array: ");
scanf("%d",&n);
printf("enter the elements of the array: ");
for(i=0; i<n; i++){
scanf("%d",&arr[i]);
}

insertionSort(arr, n);
printf("the sorted array is: \n");
printArray(arr, n);

return 0;
}

11
Roll No. 2821005

OUTPUT:

12
Roll No. 2821005

PRACTICAL :- 3
AIM: WRITE A PROGRAM TO IMPLEMENT STACK AND ITS OPERATIONS

CODE:

#include <stdio.h>

int MAXSIZE = 8;
int stack[8];
int top = -1;

int isempty() {

if(top == -1)
return 1;
else
return 0;
}

int isfull() {

if(top == MAXSIZE)
return 1;
else
return 0;
}

int peek() {
return stack[top];
}

int pop() {
int data;

if(!isempty()) {
data = stack[top];
top = top - 1;
return data;
} else {
printf("Could not retrieve data, Stack is empty.\n");

13
Roll No. 2821005

}
}

int push(int data) {

if(!isfull()) {
top = top + 1;
stack[top] = data;
} else {
printf("Could not insert data, Stack is full.\n");
}
}

int main() {
printf("NAME: AKSHAT KUMAR SHARMA \n");
printf("ROLL: 2821005\n");

// push items on to the stack


push(7);
push(8);
push(10);
push(32);
push(5);
push(19);

printf("Element at top of the stack: %d\n" ,peek());


printf("Elements: \n");

// print stack data


while(!isempty()) {
int data = pop();
printf("%d\n",data);
}

printf("Stack full: %s\n" , isfull()?"true":"false");


printf("Stack empty: %s\n" , isempty()?"true":"false");

return 0;
}

14
Roll No. 2821005

OUTPUT:

15
Roll No. 2821005

PRACTICAL :- 4

AIM: WRITE A PROGRAM TO IMPLEMENT QUICK SORT

CODE:

#include <stdio.h>

void quick_sort(int[],int,int);
int partition(int[],int,int);
int main()
{
printf("NAME: AKSHAT KUMAR SHARMA \n");
printf("ROLL: 2821005\n");

int a[50],n,i;
printf("How many elements:");
scanf("%d",&n);
printf("Enter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quick_sort(a,0,n-1);
printf("Array after sorting:");
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
void quick_sort(int a[],int l,int u)
{
int j;
if(l<u)
{
j=partition(a,l,u);
quick_sort(a,l,j-1);
quick_sort(a,j+1,u);
}
}
int partition(int a[],int l,int u)
{

16
Roll No. 2821005

int v,i,j,temp;
v=a[l];
i=l;
j=u+1;
do
{
do
i++;
while(a[i]<v&&i<=u);
do
j--;
while(v<a[j]);
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
while(i<j);
a[l]=a[j];
a[j]=v;
return(j);
}

17
Roll No. 2821005

OUTPUT:

18
Roll No. 2821005

PRACTICAL :- 5

AIM: WRITE A PROGRAM FOR MERGE SORT

CODE:

#include <stdio.h>

#define max 10

int a[11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0 };
int b[10];

void merging(int low, int mid, int high) {


int l1, l2, i;

for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) {
if(a[l1] <= a[l2])
b[i] = a[l1++];
else
b[i] = a[l2++];
}

while(l1 <= mid)


b[i++] = a[l1++];
while(l2 <= high)
b[i++] = a[l2++];

for(i = low; i <= high; i++)


a[i] = b[i];
}

void sort(int low, int high) {


int mid;

if(low < high) {


mid = (low + high) / 2;
sort(low, mid);
sort(mid+1, high);

19
Roll No. 2821005

merging(low, mid, high);


} else {
return;
}
}

int main() {
printf("NAME: AKSHAT KUMAR SHARMA \n");
printf("ROLL: 2821005\n");

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

20
Roll No. 2821005

OUTPUT:

21
Roll No. 2821005

PRACTICAL :- 6

AIM: WRITE A PROGRAM TO IMPLEMENT QUEUE AND ITS


OPERATIONS

CODE:

/*/
* C Program to Implement a Queue using an Array
*/
#include <stdio.h>

#define MAX 50

void insert();
void delete();
void display();
int queue_array[MAX];
int rear = - 1;
int front = - 1;
int main()
{
printf("NAME: AKSHAT KUMAR SHARMA \n");
printf("ROLL: 2821005\n");

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;

22
Roll No. 2821005

case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice \n");
} /* End of switch */
}
return 0; /* 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
{
23
Roll No. 2821005

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

24
Roll No. 2821005

OUTPUT:

25
Roll No. 2821005

PRACTICAL :-7

AIM: WRITE A PROGRAM TO IMPLEMENT CIRCULAR QUEUE AND


ITS OPERATIONS

CODE:
#include <stdio.h>

# define max 6
int queue[max]; // array declaration
int front=-1;
int rear=-1;
// function to insert an element in a circular queue
void enqueue(int element)
{
if(front==-1 && rear==-1) // condition to check queue is empty
{
front=0;
rear=0;
queue[rear]=element;
}
else if((rear+1)%max==front) // condition to check queue is full
{
printf(“Queue is overflow..”);
}
else
{
rear=(rear+1)%max; // rear is incremented
queue[rear]=element; // assigning a value to the queue at the rear position.
}
}

// function to delete the element from the queue


int dequeue()
{
if((front==-1) && (rear==-1)) // condition to check queue is empty
{
printf(“\nQueue is underflow..”);
}
else if(front==rear)

26
Roll No. 2821005

{
printf(“\nThe dequeued element is %d”, queue[front]);
front=-1;
rear=-1;
}
else
{
printf(“\nThe dequeued element is %d”, queue[front]);
front=(front+1)%max;
}
}
// function to display the elements of a queue
void display()
{
int i=front;
if(front==-1 && rear==-1)
{
printf(“\n Queue is empty..”);
}
else
{
printf(“\nElements in a Queue are :”);
while(i<=rear)
{
printf(“%d,”, queue[i]);
i=(i+1)%max;
}
}
}
int main()
{
printf(“NAME: AKSHAT KUMAR SHARMA \n”);
printf(“ROLL: 2821005\n”);

int choice=1,x; // variables declaration

while(choice<4 && choice!=0) // while loop


{
printf(“\n Press 1: Insert an element”);
printf(“\nPress 2: Delete an element”);
printf(“\nPress 3: Display the element”);
27
Roll No. 2821005

printf(“\nEnter your choice “);


scanf(“%d”, &choice);

switch(choice)
{

case 1:

printf(“Enter the element which is to be inserted “);


scanf(“%d”, &x);
enqueue(x);
break;
case 2:
dequeue();
break;
case 3:
display();

}}
return 0;
}

OUTPUT:

28
Roll No. 2821005

PRACTICAL-8

29
Roll No. 2821005

AIM: WRITE A PROGRAM TO IMPLEMENT SINGLY LINKED LIST

CODE:

// C program for the all operations in


// the Singly Linked List
#include <stdio.h>
#include <stdlib.h>
// Linked List Node
struct node {
int info;
struct node* link;
};
struct node* start = NULL;
// Function to create list with n nodes initially
void createList()
{
if (start == NULL) {
int n;
printf("\nEnter the number of nodes: ");
scanf("%d", &n);
if (n != 0) {
int data;
struct node* newnode;
struct node* temp;
newnode = malloc(sizeof(struct node));
start = newnode;
temp = start;
printf("\nEnter number to"
" be inserted : ");
scanf("%d", &data);
start->info = data;

for (int i = 2; i <= n; i++) {


newnode = malloc(sizeof(struct node));
temp->link = newnode;
printf("\nEnter number to"
" be inserted : ");
scanf("%d", &data);
newnode->info = data;
temp = temp->link;
}
}

30
Roll No. 2821005

printf("\nThe list is created\n");


}
else
printf("\nThe list is already created\n");
}
// Function to traverse the linked list
void traverse()
{
struct node* temp;

// List is empty
if (start == NULL)
printf("\nList is empty\n");

// Else print the LL


else {
temp = start;
while (temp != NULL) {
printf("Data = %d\n", temp->info);
temp = temp->link;
}
}
}
// Function to insert at the front
// of the linked list
void insertAtFront()
{
int data;
struct node* temp;
temp = malloc(sizeof(struct node));
printf("\nEnter number to"
" be inserted : ");
scanf("%d", &data);
temp->info = data;

// Pointer of temp will be


// assigned to start
temp->link = start;
start = temp;
}
// Function to delete from the front
// of the linked list
void deleteFirst()
{
struct node* temp;
if (start == NULL)

31
Roll No. 2821005

printf("\nList is empty\n");
else {
temp = start;
start = start->link;
free(temp);
}
}
// Driver Code
int main()
{
printf("NAME: AKSHAT KUMAR SHARMA\n");
printf("ROLL: 2821005");

int choice;
while (1) {

printf("\n\t1 To see list\n");


printf("\t2 For insertion\n");
printf("\t3 For deletion\n");
printf("\t4 To exit\n");
printf("\nEnter Choice :\n");
scanf("%d", &choice);

switch (choice) {
case 1:
traverse();
break;
case 2:
insertAtFront();
break;
case 3:
deleteFirst();
break;
case 4:
exit(1);
break;
default:
printf("Incorrect Choice\n");
}
}
return 0;
}

32
Roll No. 2821005

OUTPUT:

33
Roll No. 2821005

PRACTICAL-9

AIM: WRITE A PROGRAM TO IMPLEMENT DOUBLY LINKED LIST


CODE:

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

// Node Structure of the linked list


struct node {
int data;
struct node *prev, *next;
};
struct node* start = NULL;

// Function to traverse and print the linked list


void traverse(){
// List is empty
// just return
if (start == NULL) {
printf("\nList is empty\n");
return;
}
// Else print the Node's Data
struct node* temp;
temp = start;
while (temp != NULL) {
printf("Data = %d\n", temp->data);
temp = temp->next;
}
}

// function to insert node at the front


// of the linked list
void insertAtFront(){
int data;
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
printf("\nEnter number to be inserted: ");
scanf("%d", &data);
temp->data = data;
temp->prev = NULL;

// Pointer of temp will be assigned to start


temp->next = start;

34
Roll No. 2821005

start = temp;
}

// function to insert at the end of the linked list


void insertAtEnd(){
int data;
struct node *temp, *trav;
temp = (struct node*)malloc(sizeof(struct node));
temp->prev = NULL;
temp->next = NULL;
printf("\nEnter number to be inserted: ");
scanf("%d", &data);
temp->data = data;
temp->next = NULL;
trav = start;

// If start is NULL
if (start == NULL) {

start = temp;
}

// Changes Links
else {
while (trav->next != NULL)
trav = trav->next;
temp->prev = trav;
trav->next = temp;
}
}

// Function to insert at any given position in the linked list


void insertAtPosition(){
int data, pos, i = 1;
struct node *temp, *newnode;
newnode = malloc(sizeof(struct node));
newnode->next = NULL;
newnode->prev = NULL;

// Enter the position and data


printf("\nEnter position : ");
scanf("%d", &pos);
printf("\nEnter number to be inserted: ");
scanf("%d", &data);
newnode->data = data;
temp = start;

35
Roll No. 2821005

// If start==NULL,
if (start == NULL) {
start = newnode;
newnode->prev = NULL;
newnode->next = NULL;
}

// If position==1,
else if (pos == 1) {
newnode->next = start;
newnode->next->prev = newnode;
newnode->prev = NULL;
start = newnode;
}

// Change links
else {
while (i < pos - 1) {
temp = temp->next;
i++;
}
newnode->next = temp->next;
newnode->prev = temp;
temp->next = newnode;
temp->next->prev = newnode;
}
}

// function to delete from the front of the linked list


void deleteFirst(){
struct node* temp;
if (start == NULL)
printf("\nList is empty\n");
else {
temp = start;
start = start->next;
if (start != NULL)
start->prev = NULL;
free(temp);
}
}

// function to delete from the end


// of the linked list
void deleteEnd(){
struct node* temp;
if (start == NULL)
36
Roll No. 2821005

printf("\nList is empty\n");
temp = start;
while (temp->next != NULL)
temp = temp->next;
if (start->next == NULL)
start = NULL;
else {
temp->prev->next = NULL;
free(temp);
}
}

// function to delete from any given


// position from the linked list
void deletePosition(){
int pos, i = 1;
struct node *temp, *position;
temp = start;

// If DLL is empty
if (start == NULL)
printf("\nList is empty\n");

// Otherwise
else {
// Position to be deleted
printf("\nEnter position : ");
scanf("%d", &pos);

// If the position is the first node


if (pos == 1) {
position = start;
start = start->next;
if (start != NULL) {
start->prev = NULL;
}
free(position);
return;
}

// Traverse till position


while (i < pos - 1) {
temp = temp->next;
i++;
}
// Change Links
position = temp->next;
37
Roll No. 2821005

if (position->next != NULL)
position->next->prev = temp;
temp->next = position->next;

// Free memory
free(position);
}
}

int main(){
printf("NAME: AKSHAT KUMAR SHARMA \n");
printf("ROLL: 2821005\n");
int choice;
while (1) {

printf("\n\t1 To see list\n");


printf("\t2 For insertion at"
" starting\n");
printf("\t3 For insertion at"
" end\n");
printf("\t4 For insertion at "
"any position\n");
printf("\t5 For deletion of "
"first element\n");
printf("\t6 For deletion of "
"last element\n");
printf("\t7 For deletion of "
"element at any position\n");
printf("\t8 To exit\n");
printf("\nEnter Choice :\n");
scanf("%d", &choice);

switch (choice) {
case 1:
traverse();
break;
case 2:
insertAtFront();
break;
case 3:
insertAtEnd();
break;
case 4:
insertAtPosition();
break;
case 5:
deleteFirst();
38
Roll No. 2821005

break;
case 6:
deleteEnd();
break;
case 7:
deletePosition();
break;

case 8:
exit(1);
break;
default:
printf("Incorrect Choice. Try Again \n");
continue;
}
}
return 0;
}

OUTPUT:

39
Roll No. 2821005

PRACTICAL-10

40
Roll No. 2821005

AIM: WRITE A PROGRAM TO IMPLEMENT CIRCULAR LINKED LIST


CODE:

#include<stdio.h>
#include<stdlib.h>
struct Node;
typedef struct Node * PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;

struct Node
{
int e;
Position next;
};

void Insert(int x, List l, Position p)


{
Position TmpCell;
TmpCell = (struct Node*) malloc(sizeof(struct Node));
if(TmpCell == NULL)
printf("Memory out of space\n");
else
{
TmpCell->e = x;
TmpCell->next = p->next;
p->next = TmpCell;
}
}

int isLast(Position p, List l)


{
return (p->next == l);
}

Position FindPrevious(int x, List l)


{
Position p = l;
while(p->next != l && p->next->e != x)
p = p->next;
return p;
}

Position Find(int x, List l)

41
Roll No. 2821005

{
Position p = l->next;
while(p != l && p->e != x)
p = p->next;
return p;
}

void Delete(int x, List l)


{
Position p, TmpCell;
p = FindPrevious(x, l);
if(!isLast(p, l))
{
TmpCell = p->next;
p->next = TmpCell->next;
free(TmpCell);
}
else
printf("Element does not exist!!!\n");
}

void Display(List l)
{
printf("The list element are :: ");
Position p = l->next;
while(p != l)
{
printf("%d -> ", p->e);
p = p->next;
}
}

int main()
{
printf("NAME: AKSHAT KUMAR SHARMA \n");
printf("ROLL: 2821005\n");

int x, pos, ch, i;


List l, l1;
l = (struct Node *) malloc(sizeof(struct Node));
l->next = l;
List p = l;
printf("CIRCULAR LINKED LIST IMPLEMENTATION OF LIST ADT\n\n");
do
{
printf("\n\n1. INSERT\t 2. DELETE\t 3. FIND\t 4. PRINT\t 5. QUIT\n\nEnter the
choice :: ");
42
Roll No. 2821005

scanf("%d", &ch);
switch(ch)
{
case 1:
p = l;
printf("Enter the element to be inserted :: ");
scanf("%d",&x);
printf("Enter the position of the element :: ");
scanf("%d",&pos);
for(i = 1; i < pos; i++)
{
p = p->next;
}
Insert(x,l,p);
break;

case 2:
p = l;
printf("Enter the element to be deleted :: ");
scanf("%d",&x);
Delete(x,p);
break;

case 3:
p = l;
printf("Enter the element to be searched :: ");
scanf("%d",&x);
p = Find(x,p);
if(p == l)
printf("Element does not exist!!!\n");
else
printf("Element exist!!!\n");
break;

case 4:
Display(l);
break;
}
}while(ch<5);
return 0;
}

OUTPUT:-

43
Roll No. 2821005

44

You might also like