0% found this document useful (0 votes)
27 views97 pages

DS File Sagar

The document provides information about a practical file for the course "Data Structures using C". It includes the name and designation of the faculty and student, name of the institute and course. It then lists 24 experiments to be performed involving different data structures and algorithms like linear search, binary search, sorting methods, stacks, queues, linked lists and binary trees. Each experiment is to be implemented in C programming language.

Uploaded by

Sagar Sharma
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)
27 views97 pages

DS File Sagar

The document provides information about a practical file for the course "Data Structures using C". It includes the name and designation of the faculty and student, name of the institute and course. It then lists 24 experiments to be performed involving different data structures and algorithms like linear search, binary search, sorting methods, stacks, queues, linked lists and binary trees. Each experiment is to be implemented in C programming language.

Uploaded by

Sagar Sharma
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/ 97

Data Structures using C

CSIT- 124
Practical File

Name of Faculty: Dr Rajbala Simon Student Name: Sagar Sharma


Designation: Assistant Professor Roll No: A1004821111
Institute: AIIT Course: BCA -B

AMITY INSTITUTE OF INFORMATION TECHNOLOGY


AMITY UNIVERSITY UTTAR PRADESH, NOIDA
Session: Even Semester
INDEX
Data Structure Using C
Course Code:CSIT124
S.no EXPERIMENTS PAGE T.sign
1. Write a program to search an element using Linear Search.
2. Write a program to search an element using Binary Search.
3. Write a program to sort the given array using Bubble Sort.
4. Write a program to sort the given array using Selection Sort.
5. Write a program to sort the given array using Insertion Sort.
6. Write a program to sort the given array using QuickSort.
7. Write a program to sort the given array using MergeSort.
8. Write a program to insert a new element in the given unsorted array at
kth position.
9. Write a program to delete an element from given sorted array.
10. Write a program to merge to given sorted arrays.
11. Write a program to implement Stack using array, also show overflow
and underflow in respective push and pop operations.
12. Write a program to implement Queue using array, which shows
insertion and deletion operations.
13. Write a program to implement Circular Queue using array, which
shows insertion and deletion operations,
14. Write a program to implement Linear Linked List, showing all the
operations, like creation, display, insertion, deletion and searching.
15. Write a program to implement Stack, using Linked List. Implement
Push, Pop and display operations.
16. Write a program to implement Queue, using Linked List. Implement
Insertion, deletion and display operations.
17. Write a program to count the number of times an item is present in a
linked list.
18. Write a program to increment the data part of every node present in a
linked list by 10. Display the data both before incrimination and after.
19. Write a program to implement Doubly Linked List, showing all the
operations, like creation, display, insertion, deletion and searching.
20. Write a program to create a Binary Search Tree and display its
contents using recursive preorder, postorder and inorder traversal.
21. Write a program to implement deletion of a node in binary search tree.
• no specific data or no child
• only left child
• only right child
• node has two children
22. Write a program to implement insertion of a node in binary search
tree.
23. Write a program to implement Binary tree and display the contents
using non-recursive preorder, postorder and inorder traversal
techniques.
24. Write a program to sort the given array using HeapSort.
1. Write a program to search an element implementing linear search.
Code:

#include <stdio.h>

void main()

int num;

int i, keynum, found = 0;

printf("Enter the number of elements ");

scanf("%d", &num);

int array[num];

printf("Enter the elements one by one \n");

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

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

printf("Enter the element to be searched ");

scanf("%d", &keynum);

/* Linear search begins */

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

if (keynum == array[i] )

found = 1;
break;

if (found == 1)

printf("Element is present in the array at position %d",i+1);

else

printf("Element is not present in the array\n");

Output:

2. Write a program to search an element implementing binary search

Code:
#include<stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d",&array[c]);
printf("Enter value to find\n");
scanf("%d",&search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while( first <= last )
{
if ( array[middle] < search )
first = middle + 1;
else if ( array[middle] == search )
{
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if ( first > last )
printf("Not found! %d is not present in the list.\n", search);
return 0;
}

Output:

3.Write a program to sort an array implementing bubble sort


Code:

#include <stdio.h>

int main()
{
int array[100], n, c, d, swap;
printf("Enter number of elements\n");
scanf("%d", &n);

printf("Enter %d integers\n", n);

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


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

for (c = 0 ; c < n - 1; c++)


{
for (d = 0 ; d < n - c - 1; d++)
{
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("Sorted list in ascending order:\n");

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


printf("%d\n", array[c]);

return 0;
}

Output:

4. Write a program in c to sort an array implementing selection sort


Code:

#include <stdio.h>
int main() {

int arr[10]={6,12,0,18,11,99,55,45,34,2};

int n=10;

int i, j, position, swap;

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

position = i;

for (j = i + 1; j < n; j++) {

if (arr[position] > arr[j])

position = j;

if (position != i) {

swap = arr[i];

arr[i] = arr[position];

arr[position] = swap;

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

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

return 0;

Output:
5. Write a program in c to sort an array implementing insertion sort
Code:

#include <stdio.h>

void insert(int a[], int n) /* function to sort an aay with insertion sort */

int i, j, temp;

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

temp = a[i];

j = i - 1;

while(j>=0 && temp <= a[j]) /* Move the elements greater than temp to one position ahead
from their current position*/

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

j = j-1;

a[j+1] = temp;

void printArr(int a[], int n) /* function to print the array */

int i;

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

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

}
int main()

int a[] = { 12, 31, 25, 8, 32, 17 };

int n = sizeof(a) / sizeof(a[0]);

printf("Before sorting array elements are - \n");

printArr(a, n);

insert(a, n);

printf("\nAfter sorting array elements are - \n");

printArr(a, n);

return 0;

Output:

6. Write a program to sort an array implementing quick sort

Code:

#include <stdio.h>

void quick_sort(int[],int,int);

int partition(int[],int,int);
int main()

int a[50],n,i;

printf("How many elements?");

scanf("%d",&n);

printf("\nEnter array elements:");

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

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

quick_sort(a,0,n-1);

printf("\nArray 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)

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

Output:
7. Write a program to sort the given array implementing merge sort
Code:

#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;

/* create temp arrays */

int L[n1], R[n2];

/* Copy data to temp arrays L[] and R[] */

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

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

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

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


/* Merge the temp arrays back into arr[l..r]*/

i = 0; // Initial index of first subarray

j = 0; // Initial index of second subarray

k = l; // Initial index of merged subarray

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

if (L[i] <= R[j]) {

arr[k] = L[i];

i++;

else {

arr[k] = R[j];

j++;

k++;

/* Copy the remaining elements of L[], if there

are any */

while (i < n1) {

arr[k] = L[i];

i++;

k++;

/* Copy the remaining elements of R[], if there

are any */

while (j < n2) {


arr[k] = R[j];

j++;

k++;

/* l is for left index and r is right index of the

sub-array of arr to be sorted */

void mergeSort(int arr[], int l, int r)

if (l < r) {

// Same as (l+r)/2, but avoids overflow for

// large l and h

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

// Sort first and second halves

mergeSort(arr, l, m);

mergeSort(arr, m + 1, r);

merge(arr, l, m, r);

/* UTILITY FUNCTIONS */

/* Function to print an array */

void printArray(int A[], int size)

{
int i;

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

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

printf("\n");

/* Driver code */

int main()

int arr[] = { 12, 11, 13, 5, 6, 7 };

int arr_size = sizeof(arr) / sizeof(arr[0]);

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;

Output:
8. Write a program to insert a new element in the given unsorted array at
kth position .

Code:

#include<stdio.h>

int main(){

int student[40],pos,i,size,value;

printf("enter no of elements in array of students:");

scanf("%d",&size);

printf("enter %d elements are:\n",size);

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

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

printf("enter the position where you want to insert the element:");

scanf("%d",&pos);

printf("enter the value into that poition:");

scanf("%d",&value);

for(i=size-1;i>=pos-1;i--)

student[i+1]=student[i];

student[pos-1]= value;

printf("final array after inserting the value is\n");

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

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

return 0;

Output
9. Write a program to delete an element from given sorted array
Code:

#include <stdio.h>

int main()
{
int array[100], position, c, n;

printf("Enter number of elements in array\n");


scanf("%d", &n);

printf("Enter %d elements\n", n);

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


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

printf("Enter the location where you wish to delete element\n");


scanf("%d", &position);

if (position >= n+1)


printf("Deletion not possible.\n");
else
{
for (c = position - 1; c < n - 1; c++)
array[c] = array[c+1];

printf("Resultant array:\n");

for (c = 0; c < n - 1; c++)


printf("%d\n", array[c]);
}

return 0;
}

Output:

10. Write a program to merge two given sorted arrays.


Code:

#include <stdio.h>

Int main()

{
int array1[50], array2[50], array3[100], m, n, i, j, k = 0;

printf("\n Enter size of array Array 1: ");

scanf("%d", &m);

printf("\n Enter sorted elements of array 1: \n");

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

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

printf("\n Enter size of array 2: ");

scanf("%d", &n);

printf("\n Enter sorted elements of array 2: \n");

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

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

i = 0;

j = 0;

while (i < m && j < n)

if (array1[i] < array2[j])

{
array3[k] = array1[i];

i++;

else

array3[k] = array2[j];

j++;

k++;

if (i >= m)

while (j < n)

array3[k] = array2[j];

j++;

k++;

if (j >= n)

while (i < m)

array3[k] = array1[i];
i++;

k++;

printf("\n After merging: \n");

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

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

return 0 ;

Output:

11. Write a program to implement stack using array . also show overflow
and underflow in respective to push and pop operation
Code:

#include<stdio.h>
int stack[20],choice,n,top,x,i;

void push(void);

void pop(void);

void display(void);

int main()

top=-1;

printf("\n Enter the size of STACK[MAX=20]:");

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\t 4.EXIT");

do

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

scanf("%d",&choice);

switch(choice)

case 1:

push();

break;

case 2:

pop();
break;

case 3:

display();

break;

case 4:

printf("\n\t EXIT POINT ");

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


}

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 display()

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

Output:

12. Write a program to implement a queue using array which shows


insertion and deletion operations
#include <stdio.h>
#define MAX 50

void insert();

void delete();

void display();

int queue_array[MAX];

int rear = - 1;

int front = - 1;

main()

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;

case 2:

delete();
break;

case 3:

display();

break;

case 4:

exit(1);

default:

printf("Wrong choice \n");

} /* End of switch */

} /* 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

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

Output:

13. Write a program to implement circular queue using array which showa
insertion and deletion 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)
{

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()

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

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:

14. Write a program to implement linear linked list showing all operations
like creation, insertion, deletion , and searching

Code:

#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;

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 insert at the end of

// the linked list

void insertAtEnd()

{
int data;

struct node *temp, *head;

temp = malloc(sizeof(struct node));

// Enter the number

printf("\nEnter number to"

" be inserted : ");

scanf("%d", &data);

// Changes links

temp->link = 0;

temp->info = data;

head = start;

while (head->link != NULL) {

head = head->link;

head->link = temp;

// Function to insert at any specified

// position in the linked list

void insertAtPosition()

struct node *temp, *newnode;

int pos, data, i = 1;

newnode = malloc(sizeof(struct node));


// Enter the position and data

printf("\nEnter position and data :");

scanf("%d %d", &pos, &data);

// Change Links

temp = start;

newnode->info = data;

newnode->link = 0;

while (i < pos - 1) {

temp = temp->link;

i++;

newnode->link = temp->link;

temp->link = 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->link;

free(temp);
}

// Function to delete from the end

// of the linked list

void deleteEnd()

struct node *temp, *prevnode;

if (start == NULL)

printf("\nList is Empty\n");

else {

temp = start;

while (temp->link != 0) {

prevnode = temp;

temp = temp->link;

free(temp);

prevnode->link = 0;

// Function to delete from any specified

// position from the linked list

void deletePosition()

struct node *temp, *position;

int i = 1, pos;
// If LL is empty

if (start == NULL)

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

// Otherwise

else {

printf("\nEnter index : ");

// Position to be deleted

scanf("%d", &pos);

position = malloc(sizeof(struct node));

temp = start;

// Traverse till position

while (i < pos - 1) {

temp = temp->link;

i++;

// Change Links

position = temp->link;

temp->link = position->link;

// Free memory

free(position);

}
}

void reverseLL()

struct node *t1, *t2, *temp;

t1 = t2 = NULL;

// If LL is empty

if (start == NULL)

printf("List is empty\n");

// Else

else {

// Traverse the LL

while (start != NULL) {

// reversing of points

t2 = start->link;

start->link = t1;

t1 = start;

start = t2;

start = t1;

// New head Node

temp = start;
printf("Reversed linked "

"list is : ");

// Print the LL

while (temp != NULL) {

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

temp = temp->link;

void search()

struct node *ptr;

int item,i=0,flag;

ptr = start;

if(ptr == NULL)

printf("\nEmpty List\n");

else

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

scanf("%d",&item);

while (ptr!=NULL)

if(ptr->info == item)

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

flag=0;

else

flag=1;

i++;

ptr = ptr -> link;

if(flag==1)

printf("Item not found\n");

void display()

struct node *ptr;

ptr = start;

if(ptr == NULL)

printf("Nothing to print");

else

{
printf("\nprinting values . . . . .\n");

while (ptr!=NULL)

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

ptr = ptr -> link;

// Driver Code

int main()

int choice;

while (1) {

printf("\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 reverse the "

"linked list\n");

printf("\t9 To search an element\n");

printf("\t10 To display the elemnts");

printf("\t11 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();

break;
case 6:

deleteEnd();

break;

case 7:

deletePosition();

break;

case 8:

reverseLL();

break;

case 9:

search();

break;

case 10:

display();

break;

case 11:

exit(1);

break;

default:

printf("Incorrect Choice\n");

return 0;

Output:
15. Write a program to implement stack using linked list. Implement push
,pop and display operations.

Code:

#include <stdio.h>

#include <stdlib.h>

void push();

void pop();

void display();

struct node

{
int val;

struct node *next;

};

struct node *head;

void main ()

int choice=0;

printf("\n*********Stack operations using linked list*********\n");

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

while(choice != 4)

printf("\n\nChose one from the below options...\n");

printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");

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

scanf("%d",&choice);

switch(choice)

case 1:

push();

break;

case 2:

pop();

break;
}

case 3:

display();

break;

case 4:

printf("Exiting....");

break;

default:

printf("Please Enter valid choice ");

};

void push ()

int val;

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

if(ptr == NULL)

printf("not able to push the element");

else
{

printf("Enter the value");

scanf("%d",&val);

if(head==NULL)

ptr->val = val;

ptr -> next = NULL;

head=ptr;

else

ptr->val = val;

ptr->next = head;

head=ptr;

printf("Item pushed");

void pop()

int item;

struct node *ptr;

if (head == NULL)

{
printf("Underflow");

else

item = head->val;

ptr = head;

head = head->next;

free(ptr);

printf("Item popped");

void display()

int i;

struct node *ptr;

ptr=head;

if(ptr == NULL)

printf("Stack is empty\n");

else

printf("Printing Stack elements \n");

while(ptr!=NULL)

printf("%d\n",ptr->val);
ptr = ptr->next;

Output:

16. Write a program to implement queue using linked list. Implement


insertion , deletion and display operations.

Code:

#include<stdio.h>

#include<stdlib.h>

struct node

int data;

struct node *next;


};

struct node *front;

struct node *rear;

void insert();

void delete();

void display();

void main ()

int choice;

while(choice != 4)

printf("\n*************************Main Menu*****************************\n");

printf("\n=================================================================\n");

printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");

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

break;

default:

printf("\nEnter valid choice??\n");

void insert()

struct node *ptr;

int item;

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

if(ptr == NULL)

printf("\nOVERFLOW\n");

return;

else

printf("\nEnter value?\n");

scanf("%d",&item);

ptr -> data = item;

if(front == NULL)

front = ptr;
rear = ptr;

front -> next = NULL;

rear -> next = NULL;

else

rear -> next = ptr;

rear = ptr;

rear->next = NULL;

void delete ()

struct node *ptr;

if(front == NULL)

printf("\nUNDERFLOW\n");

return;

else

ptr = front;

front = front -> next;

free(ptr);

}
void display()

struct node *ptr;

ptr = front;

if(front == NULL)

printf("\nEmpty queue\n");

else

{ printf("\nprinting values .....\n");

while(ptr != NULL)

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

ptr = ptr -> next;

Output:
17. Write a program to count the number of times an element is present in
a linked list
Code:

#include<stdio.h>

#include<stdlib.h>

struct node

int info;

struct node *link;

};

struct node *create_list(struct node *start);

void display(struct node *start);

struct node *addatbeg(struct node *start,int data);

struct node *addatend(struct node *start,int data);

int countOccurrences(struct node *start, int n);


int main()

struct node *start=NULL;

int n;

start=create_list(start);

display(start);

printf("\nEnter element to count occurrences : ");

scanf("%d",&n);

printf("\nThe element %d occurs %d times\n",n,countOccurrences(start,n) );

return 0;

}/*End of main()*/

int countOccurrences(struct node *ptr, int n)

int k=0;

while(ptr!=NULL)

if(ptr->info == n)

k++;

ptr=ptr->link;

return k;

}/*End of countOccurrences()*/
struct node *create_list(struct node *start)

int i,n,data;

printf("Enter the number of nodes u want : ");

scanf("%d",&n);

start=NULL;

if(n==0)

return start;

printf("\nEnter the element to be inserted : ");

scanf("%d",&data);

start=addatbeg(start,data);

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

printf("Enter the element to be inserted : ");

scanf("%d",&data);

start=addatend(start,data);

return start;

}/*End of create_list()*/

void display(struct node *start)

struct node *p;


if(start==NULL)

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

return;

p=start;

printf("\nList is :\n");

while(p!=NULL)

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

p=p->link;

printf("\n\n");

}/*End of display() */

struct node *addatbeg(struct node *start,int data)

struct node *tmp;

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

tmp->info=data;

tmp->link=start;

start=tmp;

return start;

}/*End of addatbeg()*/

struct node *addatend(struct node *start,int data)

{
struct node *p,*tmp;

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

tmp->info=data;

p=start;

while(p->link!=NULL)

p=p->link;

p->link=tmp;

tmp->link=NULL;

return start;

Output:

18. Write a program to increment the data part of every node present in a
linked list by 10. Display the data both before incrimination and after.
#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;

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;

void increment()

struct node *ptr;

ptr = start;
if(ptr == NULL)

printf("Nothing to print");

else

printf("\nprinting values with increment of 10. . . . .\n");

while (ptr!=NULL)

ptr-> info= ptr-> info + 10 ;

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

ptr = ptr -> link;

int main()

int choice;

while (1) {

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

printf("\t2 For insertion at"

" starting\n");

printf("\t3 To display incremented elemnts");

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


scanf("%d", &choice);

switch (choice) {

case 1:

traverse();

break;

case 2:

insertAtFront();

break;

case 3:

increment();

break;

default:

printf("Incorrect Choice\n");

return 0;

}Output:
19. Write a program to implement Doubly Linked List, showing all the
operations, like creation, display, insertion, deletion and searching.

#include<stdio.h>
#include<stdlib.h>
struct node
{
struct node *prev;
struct node *next;
int data;
};
struct node *head;
void insertion_beginning();
void insertion_last();
void insertion_specified();
void deletion_beginning();
void deletion_last();
void deletion_specified();
void display();
void search();
void main ()
{
int choice =0;
while(choice != 9)
{
printf("\n*********Main Menu*********\n");
printf("\nChoose one option from the following list ...\n");
printf("\n===============================================\
n");
printf("\n1.Insert in begining\n2.Insert at last\n3.Insert at any random location\
n4.Delete from Beginning\n
5.Delete from last\n6.Delete the node after the given data\n7.Search\n8.Show\
n9.Exit\n");
printf("\nEnter your choice?\n");
scanf("\n%d",&choice);
switch(choice)
{
case 1:
insertion_beginning();
break;
case 2:
insertion_last();
break;
case 3:
insertion_specified();
break;
case 4:
deletion_beginning();
break;
case 5:
deletion_last();
break;
case 6:
deletion_specified();
break;
case 7:
search();
break;
case 8:
display();
break;
case 9:
exit(0);
break;
default:
printf("Please enter valid choice..");
}
}
}
void insertion_beginning()
{
struct node *ptr;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter Item value");
scanf("%d",&item);

if(head==NULL)
{
ptr->next = NULL;
ptr->prev=NULL;
ptr->data=item;
head=ptr;
}
else
{
ptr->data=item;
ptr->prev=NULL;
ptr->next = head;
head->prev=ptr;
head=ptr;
}
printf("\nNode inserted\n");
}

}
void insertion_last()
{
struct node *ptr,*temp;
int item;
ptr = (struct node *) malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value");
scanf("%d",&item);
ptr->data=item;
if(head == NULL)
{
ptr->next = NULL;
ptr->prev = NULL;
head = ptr;
}
else
{
temp = head;
while(temp->next!=NULL)
{
temp = temp->next;
}
temp->next = ptr;
ptr ->prev=temp;
ptr->next = NULL;
}

}
printf("\nnode inserted\n");
}
void insertion_specified()
{
struct node *ptr,*temp;
int item,loc,i;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\n OVERFLOW");
}
else
{
temp=head;
printf("Enter the location");
scanf("%d",&loc);
for(i=0;i<loc;i++)
{
temp = temp->next;
if(temp == NULL)
{
printf("\n There are less than %d elements", loc);
return;
}
}
printf("Enter value");
scanf("%d",&item);
ptr->data = item;
ptr->next = temp->next;
ptr -> prev = temp;
temp->next = ptr;
temp->next->prev=ptr;
printf("\nnode inserted\n");
}
}
void deletion_beginning()
{
struct node *ptr;
if(head == NULL)
{
printf("\n UNDERFLOW");
}
else if(head->next == NULL)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
ptr = head;
head = head -> next;
head -> prev = NULL;
free(ptr);
printf("\nnode deleted\n");
}

}
void deletion_last()
{
struct node *ptr;
if(head == NULL)
{
printf("\n UNDERFLOW");
}
else if(head->next == NULL)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
ptr = head;
if(ptr->next != NULL)
{
ptr = ptr -> next;
}
ptr -> prev -> next = NULL;
free(ptr);
printf("\nnode deleted\n");
}
}
void deletion_specified()
{
struct node *ptr, *temp;
int val;
printf("\n Enter the data after which the node is to be deleted : ");
scanf("%d", &val);
ptr = head;
while(ptr -> data != val)
ptr = ptr -> next;
if(ptr -> next == NULL)
{
printf("\nCan't delete\n");
}
else if(ptr -> next -> next == NULL)
{
ptr ->next = NULL;
}
else
{
temp = ptr -> next;
ptr -> next = temp -> next;
temp -> next -> prev = ptr;
free(temp);
printf("\nnode deleted\n");
}
}
void display()
{
struct node *ptr;
printf("\n printing values...\n");
ptr = head;
while(ptr != NULL)
{
printf("%d\n",ptr->data);
ptr=ptr->next;
}
}
void search()
{
struct node *ptr;
int item,i=0,flag;
ptr = head;
if(ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
while (ptr!=NULL)
{
if(ptr->data == item)
{
printf("\nitem found at location %d ",i+1);
flag=0;
break;
}
else
{
flag=1;
}
i++;
ptr = ptr -> next;
}
if(flag==1)
{
printf("\nItem not found\n");
}
}

}
Output:

Menu:
Insert in starting: Insert at last:

Insert at random location: Printing:

Delete from start: Delete from end:

Printing: Delete node after given data:


Printing: Searching:

20. Write a program to create a Binary Search Tree and display its
contents using recursive preorder, postorder and inorder traversal

// C program for different tree traversals

#include <stdio.h>

#include <stdlib.h>

/* A binary tree node has data, pointer to left child

and a pointer to right child */

struct node {

int data;

struct node* left;

struct node* right;

};

/* Helper function that allocates a new node with the

given data and NULL left and right pointers. */

struct node* newNode(int data)

struct node* node

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

node->data = data;
node->left = NULL;

node->right = NULL;

return (node);

/* Given a binary tree, print its nodes according to the

"bottom-up" postorder traversal. */

void printPostorder(struct node* node)

if (node == NULL)

return;

// first recur on left subtree

printPostorder(node->left);

// then recur on right subtree

printPostorder(node->right);

// now deal with the node

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

/* Given a binary tree, print its nodes in inorder*/

void printInorder(struct node* node)

if (node == NULL)
return;

/* first recur on left child */

printInorder(node->left);

/* then print the data of node */

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

/* now recur on right child */

printInorder(node->right);

/* Given a binary tree, print its nodes in preorder*/

void printPreorder(struct node* node)

if (node == NULL)

return;

/* first print data of node */

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

/* then recur on left subtree */

printPreorder(node->left);

/* now recur on right subtree */

printPreorder(node->right);

}
/* Driver program to test above functions*/

int main()

struct node* root = newNode(1);

root->left = newNode(2);

root->right = newNode(3);

root->left->left = newNode(4);

root->left->right = newNode(5);

printf("\nPreorder traversal of binary tree is \n");

printPreorder(root);

printf("\nInorder traversal of binary tree is \n");

printInorder(root);

printf("\nPostorder traversal of binary tree is \n");

printPostorder(root);

getchar();

return 0;

Output:
21. Write a program to implement deletion of a node in binary search tree.
• no specific data or no child
• only left child
• only right child
• node has two children
// C program to demonstrate
// delete operation in binary
// search tree
#include <stdio.h>
#include <stdlib.h>

struct node {
int key;
struct node *left, *right;
};

// A utility function to create a new BST node


struct node* newNode(int item)
{
struct node* temp
= (struct node*)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}

// A utility function to do inorder traversal of BST


void inorder(struct node* root)
{
if (root != NULL) {
inorder(root->left);
printf("%d ", root->key);
inorder(root->right);
}
}

/* A utility function to
insert a new node with given key in
* BST */
struct node* insert(struct node* node, int key)
{
/* If the tree is empty, return a new node */
if (node == NULL)
return newNode(key);

/* Otherwise, recur down the tree */


if (key < node->key)
node->left = insert(node->left, key);
else
node->right = insert(node->right, key);

/* return the (unchanged) node pointer */


return node;
}

/* Given a non-empty binary search


tree, return the node
with minimum key value found in
that tree. Note that the
entire tree does not need to be searched. */
struct node* minValueNode(struct node* node)
{
struct node* current = node;

/* loop down to find the leftmost leaf */


while (current && current->left != NULL)
current = current->left;

return current;
}

/* Given a binary search tree


and a key, this function
deletes the key and
returns the new root */
struct node* deleteNode(struct node* root, int key)
{
// base case
if (root == NULL)
return root;
// If the key to be deleted
// is smaller than the root's
// key, then it lies in left subtree
if (key < root->key)
root->left = deleteNode(root->left, key);

// If the key to be deleted


// is greater than the root's
// key, then it lies in right subtree
else if (key > root->key)
root->right = deleteNode(root->right, key);

// if key is same as root's key,


// then This is the node
// to be deleted
else {
// node with only one child or no child
if (root->left == NULL) {
struct node* temp = root->right;
free(root);
return temp;
}
else if (root->right == NULL) {
struct node* temp = root->left;
free(root);
return temp;
}

// node with two children:


// Get the inorder successor
// (smallest in the right subtree)
struct node* temp = minValueNode(root->right);

// Copy the inorder


// successor's content to this node
root->key = temp->key;

// Delete the inorder successor


root->right = deleteNode(root->right, temp->key);
}
return root;
}

// Driver Code
int main()
{
/* Let us create following BST

50
/ \
30 70
/ \ / \
20 40 60 80 */
struct node* root = NULL;
root = insert(root, 50);
root = insert(root, 30);
root = insert(root, 20);
root = insert(root, 40);
root = insert(root, 70);
root = insert(root, 60);
root = insert(root, 80);

printf("Inorder traversal of the given tree \n");


inorder(root);

printf("\nDelete 20\n");
root = deleteNode(root, 20);
printf("Inorder traversal of the modified tree \n");
inorder(root);

printf("\nDelete 30\n");
root = deleteNode(root, 30);
printf("Inorder traversal of the modified tree \n");
inorder(root);

printf("\nDelete 50\n");
root = deleteNode(root, 50);
printf("Inorder traversal of the modified tree \n");
inorder(root);

return 0;
}
Output:

22. Write a program to implement insertion of a node in binary search tree.


// C program to demonstrate insert

// operation in binary

// search tree.

#include <stdio.h>

#include <stdlib.h>

struct node {

int key;

struct node *left, *right;

};

// A utility function to create a new BST node


struct node* newNode(int item)

struct node* temp

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

temp->key = item;

temp->left = temp->right = NULL;

return temp;

// A utility function to do inorder traversal of BST

void inorder(struct node* root)

if (root != NULL) {

inorder(root->left);

printf("%d \n", root->key);

inorder(root->right);

/* A utility function to insert

a new node with given key in

* BST */

struct node* insert(struct node* node, int key)

/* If the tree is empty, return a new node */

if (node == NULL)
return newNode(key);

/* Otherwise, recur down the tree */

if (key < node->key)

node->left = insert(node->left, key);

else if (key > node->key)

node->right = insert(node->right, key);

/* return the (unchanged) node pointer */

return node;

// Driver Code

int main()

{ struct node* root = NULL;

root = insert(root, 50);

insert(root, 30);

insert(root, 20);

insert(root, 40);

insert(root, 70);

insert(root, 60);

insert(root, 80);

// print inoder traversal of the BST

inorder(root);
return 0;

Output:

23. Write a program to implement Binary tree and display the contents
using non-recursive preorder, postorder and inorder traversal techniques.
#include <stdio.h>

#include <stdlib.h>

// Define the types of Traversals here

enum Traversal {PREORDER, INORDER, POSTORDER};

typedef enum Traversal Traversal;

typedef struct Node Node;

// Define the Tree Node here

struct Node {

int value;

// Pointers to the left and right children

Node* left, *right;


};

Node* init_tree(int data) {

// Creates the tree and returns the

// root node

Node* root = (Node*) malloc (sizeof(Node));

root->left = root->right = NULL;

root->value = data;

return root;

Node* create_node(int data) {

// Creates a new node

Node* node = (Node*) malloc (sizeof(Node));

node->value = data;

node->left = node->right = NULL;

return node;

void free_tree(Node* root) {

// Deallocates memory corresponding

// to every node in the tree.

Node* temp = root;

if (!temp)

return;

free_tree(temp->left);
free_tree(temp->right);

if (!temp->left && !temp->right) {

free(temp);

return;

void print_tree(Traversal traversal, Node* root) {

// Prints the tree according to

// various types of traversals

if (!root)

return;

switch(traversal) {

case (PREORDER):

// Do a Preorder Traversal

printf("%d -> ", root->value);

print_tree(traversal, root->left);

print_tree(traversal, root->right);

break;

case (INORDER):

// Do an Inorder Traversal

print_tree(traversal, root->left);

printf("%d -> ", root->value);

print_tree(traversal, root->right);
break;

case (POSTORDER):

// Do a postorder Traversal

print_tree(traversal, root->left);

print_tree(traversal, root->right);

printf("%d -> ", root->value);

break;

int main() {

// Program to demonstrate finding the height of a Binary Tree

// Create the root node having a value of 10

Node* root = init_tree(10);

// Insert nodes onto the tree

root->left = create_node(20);

root->right = create_node(30);

root->left->left = create_node(40);

root->left->right = create_node(50);

root->right->left = create_node(60);

root->right->right = create_node(70);
printf("----Preorder Traversal:----\n");

print_tree(PREORDER, root);

printf("\n\n");

printf("----Inorder Traversal:----\n");

print_tree(INORDER, root);

printf("\n\n");

printf("----Postorder Traversal:----\n");

print_tree(POSTORDER, root);

printf("\n\n");

// Free the tree!

free_tree(root);

return 0;

Output:
24. Write a program to sort elements in an array using heap sort

#include <stdio.h>
/* function to heapify a subtree. Here 'i' is the
index of root node in array a[], and 'n' is the size of heap. */

void heapify(int a[], int n, int i)


{
int largest = i; // Initialize largest as root
int left = 2 * i + 1; // left child
int right = 2 * i + 2; // right child

// If left child is larger than root

if (left < n && a[left] > a[largest])


largest = left;
// If right child is larger than root

if (right < n && a[right] > a[largest])


largest = right;

// If root is not largest

if (largest != i) {
// swap a[i] with a[largest]
int temp = a[i];
a[i] = a[largest];
a[largest] = temp;

heapify(a, n, largest);
}
}

/*Function to implement the heap sort*/

void heapSort(int a[], int n)


{
for (int i = n / 2 - 1; i >= 0; i--)
heapify(a, n, i);
// One by one extract an element from heap
for (int i = n - 1; i >= 0; i--) {
/* Move current root element to end*/
// swap a[0] with a[i]
int temp = a[0];
a[0] = a[i];
a[i] = temp;

heapify(a, i, 0);
}
}

/* function to print the array elements */

void printArr(int arr[], int n)


{
for (int i = 0; i < n; ++i)
{
printf("%d", arr[i]);
printf(" ");
}

int main()
{
int a[] = {48, 10, 23, 43, 28, 26, 1};
int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArr(a, n);
heapSort(a, n);
printf("\nAfter sorting array elements are - \n");
printArr(a, n);
return 0;
}
Output:

You might also like