You are on page 1of 104

Lab Record File

of

Data Structures
(CSF102)

BACHELOR OF TECHNOLOGY
In

Applicable branch Branch

Session 2020-21

Submitted to: - Submitted by: -

Name of Faculty-Mitali Srivastava Name: Memansha Choudhary


Assistant Professor Roll No.: 200102456
School of Computing Sap ID: 1000015482
Branch/Section: - BT-CSE

SCHOOL OF COMPUTING
DIT UNIVERSITY, DEHRADUN
(State Private University through State Legislature Act No. 10 of 2013 of Uttarakhand and approved by UGC)
Mussoorie Diversion Road, Dehradun, Uttarakhand - 248009, India.

Feb 2021-June 2021


INDEX

S.NO. TUSHAR GAHTORI OF THE DATE OF DATE OF REMARKS


EXPERIMENTS CONDUCTION SUBMISSION
1. Write a C program to implement
binary search algorithm using
recursion and iterative approach
both.

2. Write a C program to perform


various operations such as insertion,
deletion, searching and traversing
on Array data structure.

3. Write a C program to implement


stack and its various operations such
as push, pop, peek, searching,
traversing using an Array.

4. Write a C program to implement


stack and its various operations such
as push, pop, peek, searching,
traversing using an Array.

5. Write a C program to implement


Circular Queue and its various
operations such as en-queue, de-
queue, traversing using an Array.

6. Write a C program to implement


DQueue and its various operations
such as en-queue, de-queue,
traversing using an Array.

7. Write a C program to implement


linear linked list, circular linked list,
and doubly linked list and its
various operations such insertion,
deletion, searching and traversing.

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
INDEX
S.NO. TUSHAR GAHTORI OF THE DATE OF DATE OF REMARKS
EXPERIMENTS CONDUCTION SUBMISSION
8. Write a C program to implement
linear circular linked list and its
various operations such insertion,
deletion, searching and traversing.

9. Write a C program to implement


linear doubly linked list and its
various operations such insertion,
deletion, searching and traversing.

10.Write a C program to implement


Linear Queue and its various
operations such as en-queue, de-
queue, traversing using a Linked
list.
11.Write a C program to implement
Stack and its various operations
such as en-queue, de-queue,
traversing using a Linked list.
12.Write a C program to implement in-
order, pre-order and post-order
traversal of a binary tree.

13.Write a C program to implement


any three sorting algorithm such
as Bubble sort, Selection, Quick
sort, Merge sort, Insertion and
Heap sort.

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
Experiment-1
Objective 1: Write a C program to implement binary search algorithm
using recursion and iterative approach both.
.
Program:

Recursive Method:

#include<stdio.h>

#define max 1000

int binarySearch(int arr[], int l, int r, int x)

if (r >= l)

int mid = l + (r - l)/2;

if (arr[mid] == x) return mid;

if (arr[mid] > x) return binarySearch(arr, l, mid-1, x);

return binarySearch(arr, mid+1, r, x);

return -1;

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
int main(void)

int arr[max],n,i,key;

printf("MEMANSHA CHOUDHARY\n 1000015482");

printf("enter size of array\n");

scanf("%d",&n);

for(i=0;i<n;i++)
{

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

printf("enter value to found\n");

scanf("%d",&key);

int result = binarySearch(arr, 0, n-1, key);

(result == -1)? printf("Element is not present in array")

: printf("Element is present at index %d", result);

return 0;

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
Output:

Iterative Method:

#include<stdio.h>

int iterativeBinarySearch(int array[], int start_index, int end_index, int


element){

while (start_index <= end_index){

int middle = start_index + (end_index- start_index )/2;

if (array[middle] == element)

return middle;

if (array[middle] < element)

start_index = middle + 1;

else
Memansha 1000015482 BT-CSE F
Choudhary School of
Computing
end_index = middle - 1;

return -1;

int main()

int array[100];

int n,i;

printf("Memansha Choudhary\

n1000015482"); printf("\nEnter size of

array\n"); scanf("%d",&n);

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

printf("enter element:\t");

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

int element;

printf("\nEnter element to be found:\t");

scanf("%d",&element);

int found_index = iterativeBinarySearch(array, 0, n-1, element);

if(found_index == -1 ) {

printf("Element not found in the array ");

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
else {

printf("Element found at index : %d",found_index);

return 0;

Output:

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
Experiment-2

Objective-2 : Write a C program to perform various operations such as


insertion, deletion, searching and traversing on Array data structure.

Program:

#include <stdio.h>

#include <math.h>

#include <stdlib.h>

#include <string.h>

#define max 10000

int array[max], size;

void insert()

int i;

printf("\nEnter size of Array\n");

scanf("%d", &size);

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

printf("\nEnter element at index[%d]\n", i);

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
scanf("%d",&array[i]);

void traverse()

int i;

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

printf("\nElement at index[%d] : %d\n",i,array[i]);

void search()

int s, i,flag;

printf("\nEnter element to search\n");

scanf("%d", &s);

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

if (array[i] == s)

printf("\nElement found at index[%d]\n",i);

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
}

void delete ()

int pos, i;

printf("\nEnter position of element to delete\n");

scanf("%d", &pos);

if (pos >= size + 1)

printf("\nDeletion not possible.\n");

else

for (i = pos - 1; i < size - 1; i++)

array[i] = array[i + 1];

printf("\nAfter deletion\n");

for (i = 0; i < size - 1; i++)

printf("\nElement at index[%d] : %d\n",i,array[i]);

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
}

int main()

int ch;

printf("\n Memansha Choudhary\n

1000015482\n"); while(1)

printf("\n1.Insert\t2.Display\t3.Search\t4.Delete\n");

scanf("%d",&ch);

switch(ch)

case 1:

insert();

break;

case 2:

traverse();

break;

case 3:

search();

break;

case 4:

delete();

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
break;

default: printf("\nWrong choice\n");

break;

return 0;

Output:

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
Output:

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
Experiment-3

14. Objective 3 : Write a C program to implement stack and its various


operations such as push, pop, peek, searching, traversing using an
Array.

Program:

#include<stdio.h>

int stack[100],choice,n,top,x,i;

void push();

void pop();

void traversing();

void peek();

int main()

top=-1;

printf("\nMemansha Choudhary \n1000015482\

n"); printf("\n Enter the size of

STACK[MAX=100]:"); scanf("%d",&n);

printf("\n\t STACK OPERATIONS USING ARRAY");

printf("\n\t ");

printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t4.PEEK\n\t5.EXIT");

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
do

printf("\n Enter the Choice:");

scanf("%d",&choice);

switch(choice)

case 1:

push();

break;

case 2:

pop();

break;

case 3:

traversing();

break;

case 5:

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
printf("\n\t EXIT POINT ");

break;

case 4:

peek();

break;

default:

printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");

while(choice!=4);

return 0;

void push()

if(top>=n-1)

printf("\n\tSTACK is over flow");

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
}

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",stack[top]);

top--;

void traversing()

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
{

if(top>=0)

printf("\n The elements in STACK \n");

for(i=top; i>=0; i--)

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

printf("\n Press Next Choice");

else

printf("\n The STACK is empty");

void peek()

if (top==-1)

printf("\nStack is empty\n");

return;

printf("The Top Most Element In Your Stack is %d", stack[top]);

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
}

Output:

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
Experiment-4

15. Objective-4 : Write a C program to implement Linear Queue and its


various operations such as en-queue, de-queue, traversing using an Array.

Program :

#include <stdio.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 choice;

printf("Memansha Choudhary\n

1000015482 \n"); printf("1.Insert

element to queue \n"); printf("2.Delete

element from queue \n");


Memansha 1000015482 BT-CSE F
Choudhary School of
Computing
printf("3.Display all elements of queue \n");

printf("4.Quit \n");

while (1)

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

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
}

void insert()

int add_item;

if (rear == MAX - 1)

printf("Queue Overflow \n");

else

if (front == - 1)

front = 0;

printf("Inset the element in queue : ");

scanf("%d", &add_item);

rear = rear + 1;

queue_array[rear] = add_item;

void delete()

if (front == - 1 || front > rear)

printf("Queue Underflow \n");

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
return ;

else

printf("Element deleted from queue is : %d\n", 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");

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
Output :

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
Experiment-5

Objective-5: Write a C program to implement Circular Queue and its various


operations such as en-queue, de-queue, traversing using an Array.

Program :

#include <stdio.h>

# define max 6

int queue[max];

int front=-1;

int rear=-1;

void enqueue(int element)

if(front==-1 && rear==-1)

front=0;

rear=0;

queue[rear]=element;

else if((rear+1)%max==front)

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
{

printf("Queue is overflow..\n");

else

rear=(rear+1)%max;

queue[rear]=element;

int dequeue()

if((front==-1) && (rear==-1))

printf("\nQueue is underflow..");

else if(front==rear)

printf("\nThe dequeued element is %d", queue[front]);

front=-1;

rear=-1;

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
else

printf("\nThe dequeued element is %d", queue[front]);

front=(front+1)%max;

void display()

int i=front;

if(front==-1 && rear==-1)

printf("\n Queue is empty..\n");

else

printf("\nElements in a Queue are :\t");

while(i<=rear)

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

i=(i+1)%max;

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
}

int main()

int choice=1,x;

printf("\nMemansha Choudhary\

n1000015482\n"); printf("\n Press 1: Insert

an element"); printf("\nPress 2: Delete an

element"); printf("\nPress 3: Display the

element"); while(choice<4 && choice!=0)

printf("\nEnter your choice\n");

scanf("%d", &choice);

switch(choice)

case 1:

printf("Enter the element which is to be inserted\n");

scanf("%d", &x);

enqueue(x);

break;

case 2:

dequeue();

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
break;

case 3:

display();

return 0;

Output:

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
Output:

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
Experiment-6

Objective 6 : Write a C program to implement DQueue and its various


operations such as en-queue, de-queue, traversing using an Array.

Program:

/* DOUBLE ENDED QUEUE */

#include<stdio.h>
#define MAX 10

int deque[MAX];
int left=-1, right=-1;

void insert_right(void);
void insert_left(void);
void delete_right(void);
void delete_left(void);
void display(void);

int main()
{
int choice;
printf("\nMemansha Choudhary\
n1000015482\n"); printf("\n1.Insert at
right "); printf("\n2.Insert at left ");
printf("\n3.Delete from right "); printf("\
n4.Delete from left "); printf("\n5.Display
");
printf("\n6.Exit");
do
{

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
printf("\n\nEnter your choice ");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert_right();
break;
case 2:
insert_left();
break;
case 3:
delete_right();
break;
case 4:
delete_left();
break;
case 5:
display();
break;
}
}while(choice!=6);
return 0;
}

void insert_right()
{
int val;
printf("\nEnter the value to be added ");
scanf("%d",&val);
if( (left==0 && right==MAX-1) || (left==right+1) )
{
printf("\nOVERFLOW");
}
if(left==-1)
{
left=0;
right=0;
}

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
else
{
if(right==MAX-1)
right=0;
else
right=right+1;
}
deque[right]=val;
}

void insert_left()
{
int val;
printf("\nEnter the value to be added ");
scanf("%d",&val);
if( (left==0 && right==MAX-1) || (left==right+1) )
{
printf("\nOVERFLOW");
}
if(left==-1)
{
left=0;
right=0;
}
else
{
if(left==0)
left=MAX-1;
else
left=left-1;
}
deque[left]=val;
}

void delete_right()
{
if(left==-1)
{

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
printf("\nUNDERFLOW");
return;
}
printf("\nThe deleted element is %d\n", deque[right]);
if(left==right)
{
left=-1;
right=-1;
}
else
{
if(right==0)
right=MAX-1;
else
right=right-1;
}
}

void delete_left()
{
if(left==-1)
{
printf("\nUNDERFLOW");
return;
}
printf("\nThe deleted element is %d\n", deque[left]);
if(left==right)
{
left=-1;
right=-1;
}
else
{
if(left==MAX-1)
left=0;
else
left=left+1;
}
}

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
void display()
{
int front=left, rear=right;
if(front==-1)
{
printf("\nQueue is Empty\n");
return;
}
printf("\nThe elements in the queue are: ");
if(front<=rear)
{
while(front<=rear)
{
printf("%d\t",deque[front]);
front++;
}
}
else
{
while(front<=MAX-1)
{
printf("%d\t",deque[front]);
front++;
}
front=0;
while(front<=rear)
{
printf("%d\t",deque[front]);
front++;
}
}
printf("\n");
}

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
Output :

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
Memansha 1000015482 BT-CSE F
Choudhary School of
Computing
Experiment-7

Objective-7: Write a C program to implement linear linked list, circular linked


list, and doubly linked list and its various operations such insertion, deletion,
searching and traversing.

Program:

#include <stdio.h>

#include <math.h>

#include <stdlib.h>

#include <string.h>

#define MAX 10000

struct node

int data;

struct node *next;

} * temp, *newnode, *head;

void create()

int choice;

head = 0;

while (choice)

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
{

newnode = malloc(sizeof(struct node));

printf("enter data in LL\n");

scanf("%d", &newnode->data);

newnode->next = 0;

if (head == 0)

temp = head = newnode;

else

temp->next = newnode;

temp = newnode;

printf("want to insert more (0,1)");

scanf("%d", &choice);

void insertbeg()

newnode = malloc(sizeof(struct node));

printf("enter data\n");

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
scanf("%d", &newnode->data);

newnode->next = head;

head = newnode;

void insertend()

newnode = malloc(sizeof(struct node));

printf("enter data\n");

scanf("%d", &newnode->data);

newnode->next = 0;

temp = head;

while (temp->next != 0)

temp = temp->next;

temp->next = newnode;

void insertpos()

int pos, i = 1;

printf("Enter position to insert\n");

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
scanf("%d", &pos);

newnode = malloc(sizeof(struct node));

temp = head;

while (i < pos)

temp = temp->next;

i++;

printf("enter data\n");

scanf("%d", &newnode->data);

newnode->next = temp->next;

temp->next = newnode;

void deletebeg()

temp = head;

head = head->next;

free(temp);

printf("\nElement deleted from begeining\n");

void deleteend()

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
{

struct node *prevnode;

temp = head;

while (temp->next != 0)

prevnode = temp;

temp = temp->next;

if (temp == head)

head = 0;

else

prevnode->next = 0;

free(temp);

printf("\nelement deleted from the end\n");

void deletepos()

struct node *nextnode;

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
int pos, i = 1;

temp = head;

printf("enter pos\n");

scanf("%d", &pos);

while (i < pos - 1)

temp = temp->next;

i++;

nextnode = temp->next;

temp->next = nextnode->next;

free(nextnode);

void print()

temp = head;

while (temp != 0)

printf("%d->", temp->data);

temp = temp->next;

int main()

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
{

int i;

printf("\n1.Create\n2.Print\n3.Insert at the beginning\n4.Insert at


end\n5.Insert at postion\n");

printf("6.Delete from begining\n7.Delete from End\n8.Delete from pos\n");

while (1)

printf("\nEnter Choice:\t");

scanf("%d", &i);

switch (i)

case 1:

create();

break;

case 2:

print();

break;

case 3:

insertbeg();

break;

case 4:

insertend();

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
break;

case 5:

insertpos();

break;

case 6:

deletebeg();

break;

case 7:

deleteend();

break;

case 8:

deletepos();

break;

default:

printf("wrong choice");

break;

return 0;

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
OUTPUT :

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
Experiment-8

16. Objective-8 : Write a C program to implement linear circular linked list


and its various operations such insertion, deletion, searching and traversing.

Program :

#include <stdio.h>

#include<stdlib.h>

struct node

int data;

struct node *next;

};

struct node *head;

void beginsert();

void lastinsert();

void randominsert();

void begin_delete();

void last_delete();

void random_delete();

void display();

void search();

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
void main()

int choice = 0;

printf("Memansha Choudhary\n1000015482\n");

printf("\n1.Insert in begining\n2.Insert at last\n3.Delete from Beginning\


n4.Delete from last\n5.Search for an element\n6.Show\n7.Exit\n");

while (choice != 7)

printf("\nEnter your choice?\t");

scanf("%d",&choice);

switch(choice)

case 1:

beginsert();

break;

case 2:

lastinsert();

break;

case 3:

begin_delete();

break;

case 4:

last_delete();

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
break;

case 5:

search();

break;

case 6:

display();

break;

case 7:

exit(0);

break;

default:

printf("Please enter valid choice..");

void beginsert()

struct node *ptr, *temp;

int item;

ptr = (struct node *)malloc(sizeof(struct node));

if (ptr == NULL)

printf("\nOVERFLOW");

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
}

else

printf("\nEnter the node data?\t");

scanf("%d", &item);

ptr->data = item;

if (head == NULL)

head = ptr;

ptr->next = head;

else

temp = head;

while (temp->next != head)

temp = temp->next;

ptr->next = head;

temp->next = ptr;

head = ptr;

printf("\nnode inserted");

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
void lastinsert()

struct node *ptr, *temp;

int item;

ptr = (struct node *)malloc(sizeof(struct node));

if (ptr == NULL)

printf("\nOVERFLOW\n");

else

printf("\nEnter Data?\t");

scanf("%d", &item);

ptr->data = item;

if (head == NULL)

head = ptr;

ptr->next = head;

else

temp = head;

while (temp->next != head)

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
{

temp = temp->next;

temp->next = ptr;

ptr->next = head;

printf("\nnode inserted");

void begin_delete()

struct node *ptr;

if (head == NULL)

printf("\nUNDERFLOW");

else if (head->next == head)

head = NULL;

free(head); printf("\

nnode deleted");

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
else

ptr = head;

while (ptr->next != head)

ptr = ptr->next;

ptr->next = head->next;

free(head);

head = ptr->next;

printf("\nnode deleted");

void last_delete()

struct node *ptr, *preptr;

if (head == NULL)

printf("\nUNDERFLOW");

else if (head->next == head)

head = NULL;

free(head); printf("\

nnode deleted");

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
}

else

ptr = head;

while (ptr->next != head)

preptr = ptr;

ptr = ptr->next;

preptr->next = ptr->next;

free(ptr);

printf("\nnode deleted");

void search()

struct node *ptr;

int item, i = 0, flag = 1;

ptr = head;

if (ptr == NULL)

printf("\nEmpty List\n");

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
}

else

printf("\nEnter item which you want to search?\t");

scanf("%d", &item);

if (head->data == item)

printf("\nitem found at location %d", i + 1);

flag = 0;

else

while (ptr->next != head)

if (ptr->data == item)

printf("item found at location %d ", i + 1);

flag = 0;

break;

else

flag = 1;

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
} i+

+;

ptr = ptr->next;

if (flag != 0)

printf("Item not found\n");

void display()

struct node *ptr;

ptr = head;

if (head == NULL)

printf("\nnothing to print");

else

printf("\n printing values ... \t");

while (ptr->next != head)

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
{

printf("%d->", ptr->data);

ptr = ptr->next;

printf("%d\n", ptr->data);

OUTPUT :

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
Memansha 1000015482 BT-CSE F
Choudhary School of
Computing
Experiment-9

Objective-9 : Write a C program to implement linear doubly linked list and its
various operations such insertion, deletion, searching and traversing.

Program :

#include <stdio.h>

#include <stdlib.h>

struct node

struct node *prev;

int n;

struct node *next;

}*h,*temp,*temp1,*temp2,*temp4;

void insert1();

void insert2();

void insert3();

void traversebeg();

void traverseend(int);

void search();

void delete();

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
int count = 0;

void main()

{ int ch; h = NULL;

temp = temp1 = NULL;

printf("Memansha Choudhary\

n1000015482\n"); printf("\n 1 - Insert

at beginning"); printf("\n 2 - Insert at

end");

printf("\n 3 - Insert at position i");

printf("\n 4 - Delete at i");

printf("\n 5 - Display from beginning");

printf("\n 6 - Display from end");

printf("\n 7 - Search for element");

printf("\n 8 - Exit");

while (1)

printf("\n Enter choice : ");

scanf("%d", &ch);

switch (ch)

case 1:
Memansha 1000015482 BT-CSE F
Choudhary School of
Computing
insert1();

break;

case 2:

insert2();

break;

case 3:

insert3();

break;

case 4:

delete();

break;

case 5:

traversebeg();

break;

case 6:

temp2 = h;

if (temp2 == NULL)

printf("\n Error : List empty to display ");

else

printf("\n Reverse order of linked list is : ");

traverseend(temp2->n);

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
break;

case 7:

search();

break;

case 8:

exit(0);

default:

printf("\n Wrong choice menu");

void create()

int data;

temp =(struct node *)malloc(1*sizeof(struct node));

temp->prev = NULL;

temp->next = NULL;

printf("\n Enter value to node : ");

scanf("%d", &data);

temp->n = data;

count++;

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
void insert1()

if (h == NULL)

create();

h = temp;

temp1 = h;

else

create();

temp->next = h;

h->prev = temp;

h = temp;

void insert2()

if (h == NULL)

create();

h = temp;

temp1 = h;

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
}

else

create();

temp1->next = temp;

temp->prev = temp1;

temp1 = temp;

void insert3()

int pos, i = 2;

printf("\n Enter position to be inserted : ");

scanf("%d", &pos);

temp2 = h;

if ((pos < 1) || (pos >= count + 1))

printf("\n Position out of range to insert");

return;

if ((h == NULL) && (pos != 1))

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
printf("\n Empty list cannot insert other than 1st position");

return;

if ((h == NULL) && (pos == 1))

create();

h = temp;

temp1 = h;

return;

else

while (i < pos)

temp2 = temp2->next;

i++;

create();

temp->prev = temp2;

temp->next = temp2->next;

temp2->next->prev = temp;

temp2->next = temp;

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
}

void delete()

int i = 1, pos;

printf("\n Enter position to be deleted : ");

scanf("%d", &pos);

temp2 = h;

if ((pos < 1) || (pos >= count + 1))

printf("\n Error : Position out of range to delete");

return;

if (h == NULL)

printf("\n Error : Empty list no elements to delete");

return;

else

while (i < pos)

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
temp2 = temp2->next;

i++;

if (i == 1)

if (temp2->next == NULL)

printf("Node deleted from list");

free(temp2);

temp2 = h = NULL;

return;

if (temp2->next == NULL)

temp2->prev->next = NULL;

free(temp2);

printf("Node deleted from list");

return;

temp2->next->prev = temp2->prev;

if (i != 1)

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
temp2->prev->next = temp2->next; 6

if (i == 1)

h = temp2->next;

printf("\n Node deleted");

free(temp2);

count--;

void traversebeg()

temp2 = h;

if (temp2 == NULL)

printf("List empty to display \n");

return;

printf("\n Linked list elements from begining : ");

while (temp2->next != NULL)

printf(" %d ", temp2->n);

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
temp2 = temp2->next;

printf(" %d ", temp2->n);

void traverseend(int i)

if (temp2 != NULL)

i = temp2->n;

temp2 = temp2->next;

traverseend(i);

printf(" %d ", i);

void search()

int data, count = 0;

temp2 = h;

if (temp2 == NULL)

printf("\n Error : List empty to search for data");

return;

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
}

printf("\n Enter value to search : ");

scanf("%d", &data);

while (temp2 != NULL)

if (temp2->n == data)

printf("\n Data found in %d position",count + 1);

return;

else

temp2 = temp2->next;

count++;

printf("\n Error : %d not found in list", data);

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
OUTPUT:

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
Memansha 1000015482 BT-CSE F
Choudhary School of
Computing
Experiment-10

Objective-10: Write a C program to implement Linear Queue and its various


operations such as en-queue, de-queue, traversing using a Linked list.

Program:

#include <stdio.h>

#include <stdlib.h>

struct node

int info;

struct node *ptr;

}*front,*rear,*temp,*front1;

int frontelement();

void enq(int data);

void deq();

void empty();

void display();

void create();

void queuesize();

int count = 0;

void main()

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
{

printf("Memansha Choudhary\n1000015482\n");

int no, ch, e;

printf("\n 1 - Enque");

printf("\n 2 - Deque");

printf("\n 3 - Front element");

printf("\n 4 - Empty");

printf("\n 5 - Exit"); printf("\

n 6 - Display"); printf("\n 7 -

Queue size"); create();

while (1)

printf("\n Enter choice : ");

scanf("%d", &ch);

switch (ch)

case 1:

printf("Enter data : ");

scanf("%d", &no);

enq(no);

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
break;

case 2:

deq();

break;

case 3:

e = frontelement();

if (e != 0)

printf("Front element : %d", e);

else

printf("\n No front element in Queue as queue is empty");

break;

case 4:

empty();

break;

case 5:

exit(0);

case 6:

display();

break;

case 7:

queuesize();

break;

default:

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
printf("Wrong choice, Please enter correct choice ");

break;

void create()

front = rear = NULL;

void queuesize()

printf("\n Queue size : %d", count);

void enq(int data)

if (rear == NULL)

rear = (struct node *)malloc(1*sizeof(struct node));

rear->ptr = NULL;

rear->info = data;

front = rear;

else {

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
temp=(struct node *)malloc(1*sizeof(struct node));

rear->ptr = temp;

temp->info = data;

temp->ptr = NULL;

rear = temp;

count++;

void display()

front1 = front;

if ((front1 == NULL) && (rear == NULL))

printf("Queue is empty");

return;

while (front1 != rear)

printf("%d ", front1->info);

front1 = front1->ptr;

if (front1 == rear)

printf("%d", front1->info);

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
}

void deq()

front1 = front;

if (front1 == NULL)

printf("\n Error: Trying to display elements from empty queue");

return;

else

if (front1->ptr != NULL)

front1 = front1->ptr;

printf("\n Dequed value : %d", front->info);

free(front);

front = front1;

else

printf("\n Dequed value : %d", front->info);

free(front);

front = NULL;

rear = NULL;

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
}

count--;

int frontelement()

if ((front != NULL) && (rear != NULL))

return(front->info);

else

return 0;

void empty()

if ((front == NULL) && (rear == NULL))

printf("\n Queue empty");

else

printf("Queue not empty");

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
OUTPUT:

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
Experiment-11

Objective-11: Write a C program to implement Stack and its various


operations such as en-queue, de-queue, traversing using a Linked list.

Program :

#include<stdio.h>

#include<stdlib.h>

struct node

{ int data;

struct node* next;

}*head,*top,*New,*temp;

int item;

void push(int item);

void pop();

void traverse();

int main()

int choice;

printf("Memansha Choudhary\

n1000015482\n"); printf("\n1.Push\

n2.Pop\n3.Traverse\n"); printf("\nEnter

the choice:\n"); scanf("%d",&choice);

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
switch(choice)

case 1:

printf("\nEnter the element to be stored in stack:\n");

scanf("%d",&item);

printf("\n%d is pushed.\n",item);

push(item);

break;

case 2:

pop();

break;

case 3:

traverse();

break;

default:

printf("\nInvalid Input\n");

break;

return main();

void push(int item)

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
New=(struct node*)malloc(sizeof(struct node));

if(New==NULL)

printf("\nMemory location falied.\n");

else

New->data=item;

New->next=NULL;

if(top==NULL)

top=New;

else

New->next=top;

top=New;

void pop()

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
if(top==NULL)

printf("\nStack underflow.\n");

else

temp=top;

top=top->next;

printf("\n%d is popped.\n",temp->data);

free(temp);

void traverse()

if(top==NULL)

printf("\nStack underflow.\n");

else

printf("\nDisplaying the elements of Stack:\n");

temp=top;

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
while(temp!=NULL)

printf(" %d->",temp->data);

temp=temp->next;

printf(" NULL ");

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
OUTPUT:

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
Experiment-12

Objective-12: Write a C program to implement in-order, pre-order and post-


order traversal of a binary tree.

Program:

#include <stdio.h>

#include <stdlib.h>

struct node

int data;

struct node *left, *right;

};

struct node *root;

void ins(struct node *n, int val, int opt)

struct node *t;

t = (struct node *)malloc(sizeof(struct node));

t->data = val;

t->right = t->left = NULL;

if (opt == 1)

n->left = t;

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
else

n->right = t;

printf("\n %d is inserted", val);

if (opt == 1)

printf("\tat the left");

else

printf("\tat the right");

void inser(struct node *t, int x)

if (t->data > x)

if (t->left == NULL)

ins(t, x, 1);

else

inser(t->left, x);

else if (t->data < x)

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
{

if (t->right == NULL)

ins(t, x, 2);

else

inser(t->right, x);

else

printf("\n Element is already present in the list");

void inorder(struct node *p)

if (p != NULL)

inorder(p->left); printf("\

n %5d", p->data);

inorder(p->right);

void preorder(struct node *p)

if (p != NULL)

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
{

printf("\n %5d", p->data);

preorder(p->left);

preorder(p->right);

void postorder(struct node *p)

if (p != NULL)

preorder(p->left);

preorder(p->right);

printf("\n %5d", p->data);

int main()

int op, n;

root = (struct node *)malloc(sizeof(struct node));

printf("Enter root of tree\n");

scanf("%d",&root->data);

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
root->right = root->left = NULL;

printf("Memansha Choudhary\

n1000015482\n"); printf("\n

1.Insertion");

printf("\n 2.Preorder");

printf("\n 3.Inorder");

printf("\n 4.Postorder");

printf("\n 5.Quit");

do

printf("\n Enter your choice\n");

scanf("%d", &op);

switch (op)

case 1:

printf("\n Enter the element to insert\t");

scanf("%d", &n);

inser(root, n);

break;

case 2:

printf("\n The preorder elements are\t");

preorder(root);

break;

case 3:
Memansha 1000015482 BT-CSE F
Choudhary School of
Computing
printf("\n The inorder elements are\t");

inorder(root);

break;

case 4:

printf("\n The postorder elements are\t");

postorder(root);

break;

default:

exit(0);

} while (op < 5);

return 0;

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
OUTPUT:

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
Memansha 1000015482 BT-CSE F
Choudhary School of
Computing
Experiment-13

Objective-13: Write a C program to implement any three sorting algorithm


such as Bubble sort, Selection, Quick sort, Merge sort, Insertion and Heap sort.

Program:

BUBBLE SORT

#include<stdio.h>

int main()

printf("Memansha Choudhary\

n1000015482\n"); int count, temp, i, j,

number[30]; printf("enter no. of

elements:\n "); scanf("%d",&count);

printf("Enter %d numbers: ",count);

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

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

for(i=count-2;i>=0;i--)

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

if(number[j]>number[j+1])

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
temp=number[j];

number[j]=number[j+1];

number[j+1]=temp;

printf("Sorted elements: ");

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

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

return 0;

OUTPUT

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
Merge Sort:

#include <stdio.h>

#include <stdlib.h>

void merge(int arr[], int l, int m, int r)

int i, j, k;

int n1 = m - l + 1;

int n2 = r - m;

int L[n1], R[n2];

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

L[i] = arr[l + i];

for (j = 0; j < n2; j++)

R[j] = arr[m + 1 + j];

i = 0;

j = 0;

k = l;

while (i < n1 && j < n2) {

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
if (L[i] <= R[j]) {

arr[k] = L[i];

i++;

else {

arr[k] = R[j];

j++;

} k+

+;

while (i < n1) {

arr[k] = L[i];

i++;

k++;

while (j < n2) {

arr[k] = R[j];

j++;

k++;

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
void mergeSort(int arr[], int l, int r)

if (l < r) {

int m = l + (r - l) / 2;

mergeSort(arr, l, m);

mergeSort(arr, m + 1, r);

merge(arr, l, m, r);

void printArray(int A[], int size)

int i;

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

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

printf("\n");

int main()

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
printf("Memansha Choudhary\

n1000015482\n"); int arr[100],n,i;

printf("Enter size of array:\t");

scanf("%d",&n);

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

printf("enter element\t");

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

int arr_size = n;

printf("Given array is \n");

printArray(arr, arr_size);

mergeSort(arr, 0, arr_size - 1);

printf("\nSorted array is \n");

printArray(arr, arr_size);

return 0;

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
OUTPUT:

Quick Sort:
#include<stdio.h>

void quicksort(int number[25],int first,int last){

int i, j, pivot, temp;

if(first<last){

pivot=first;

i=first;

j=last;

while(i<j)

{ while(number[i]<=number[pivot]&&i<las

t)

i++;
Memansha 1000015482 BT-CSE F
Choudhary School of
Computing
while(number[j]>number[pivot])

j--;

if(i<j)

{ temp=number[i];

number[i]=number[j];

number[j]=temp;

temp=number[pivot];

number[pivot]=number[j];

number[j]=temp;

quicksort(number,first,j-1);

quicksort(number,j+1,last);

int main(){

int i, count, number[25];

printf("Memansha Choudhary\

n1000015482\n"); printf("Enter no. of

elements\t"); scanf("%d",&count);

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing
printf("Enter %d elements:\n ", count);

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

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

quicksort(number,0,count-1);

printf("Order of Sorted elements: ");

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

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

return 0;

OUTPUT:

Memansha 1000015482 BT-CSE F


Choudhary School of
Computing

You might also like