DSA Practical Exam Codes

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

Q.

1) write a c program that uses function to create a singly linked list of


integers and display content of list
Ans:
#include <stdio.h>
#include <stdlib.h>

struct node
{
int data; //Data of the node
struct node *next; //Address of the next node
}*node;

void createNodeList(int n); // function to create the list


void displayList(); // function to display the list

int main()
{
int n;
printf("\n\n Linked List : To create and display Singly Linked List :\
n");
printf("-------------------------------------------------------------\n");

printf(" Input the number of nodes : ");


scanf("%d", &n);
createNodeList(n);
printf("\n Data entered in the list : \n");
displayList();
return 0;
}
void createNodeList(int n)
{
struct node *start, *tmp;
int num, i;
node = (struct node *)malloc(sizeof(struct node));

if(node == NULL) //check whether the fnnode is NULL and if so no memory


allocation
{
printf(" Memory can not be allocated.");
}
else
{
// reads data for the node through keyboard

printf(" Input data for node 1 : ");


scanf("%d", &num);
node->data = num;
node->next = NULL; // links the address field to NULL
tmp = node;
// Creating n nodes and adding to linked list
for(i=2; i<=n; i++)
{
start = (struct node *)malloc(sizeof(struct node));
if(start == NULL)
{
printf(" Memory can not be allocated.");
break;
}
else
{
printf(" Input data for node %d : ", i);
scanf(" %d", &num);

start->data = num; // links the num field of fnNode with num


start->next = NULL; // links the address field of fnNode with NULL

tmp->next = start; // links previous node i.e. tmp to the fnNode


tmp = tmp->next;
}
}
}
}
void displayList()
{
struct node *tmp;
if(node == NULL)
{
printf(" List is empty.");
}
else
{
tmp = node;
while(tmp != NULL)
{
printf(" Data = %d\n", tmp->data); // prints the data of current node
tmp = tmp->next; // advances the position of current node
}
}
}
Q.2) write a c program that uses function to create a doubly linked list of
integers and display content of list
ANS:
#include <stdio.h>
#include <stdlib.h>

struct node {
int data;
struct node * pre;
struct node * next;
}*head, *tail;

void DlListcreation(int n);


void displayDlList();

int main()
{
int n;
head = NULL;
tail = NULL;
printf("\n\n Doubly Linked List : Create and display a doubly linked list :\
n");
printf("-------------------------------------------------------------------\n");

printf(" Input the number of nodes : ");


scanf("%d", &n);

DlListcreation(n);
displayDlList();
return 0;
}

void DlListcreation(int n)
{
int i, num;
struct node *start;

if(n >= 1)
{
head = (struct node *)malloc(sizeof(struct node));

if(head != NULL)
{
printf(" Input data for node 1 : "); // assigning data in the first node
scanf("%d", &num);
head->data = num;
head->pre = NULL;
head->next = NULL;
tail = head;
// putting data for rest of the nodes
for(i=2; i<=n; i++)
{
start = (struct node *)malloc(sizeof(struct node));
if(start != NULL)
{
printf(" Input data for node %d : ", i);
scanf("%d", &num);
start->data = num;
start->pre = tail; // new node is linking with the previous node
start->next = NULL;

tail->next = start; // previous node is linking with the new node


tail = start; // assign new node as last node
}
else
{
printf(" Memory can not be allocated.");
break;
}
}
}
else
{
printf(" Memory can not be allocated.");
}
}
}
void displayDlList()
{
struct node * tmp;
int n = 1;
if(head == NULL)
{
printf(" No data found in the List yet.");
}
else
{
tmp = head;
printf("\n\n Data entered on the list are :\n");

while(tmp != NULL)
{
printf(" node %d : %d\n", n, tmp->data);
n++;
tmp = tmp->next; // current pointer moves to the next node
}
}
}

Q.3) from manual


Q.4) from manual
Q.5) from manual
Q.6) from book
Q.7) from book
Q.8) from book
Q.9) from book
Q.10) from manual
Q.11) from manual
Q.12) from manual

Q.13)
ANS :
/*
* Heap Sort Program in C
*/

#include<stdio.h>

// function prototyping
void heapify(int*,int, int);
void heapsort(int*, int);
void print_array(int*, int);

int main()
{
int arr[] = { 10, 30, 5, 63, 22, 12, 56, 33 };
int n = sizeof(arr) / sizeof(arr[0]);

printf("\nArray before sorting:\n");


print_array(arr, n);

heapsort(arr, n);

printf("\n\nArray after sorting:\n");


print_array(arr, n);

return 0;
}

/* sorts the given array of n size */


void heapsort(int* arr, int n)
{
// build the binary max heap
for (int i = n / 2 - 1; i >= 0; i--)
{
heapify(arr, n, i);
}

// sort the max heap


for (int i = n - 1; i >= 0; i--)
{
// swap the root node and the last leaf node
int temp = arr[i];
arr[i] = arr[0];
arr[0] = temp;

// again heapify the max heap from the root


heapify(arr, i, 0);
}
}

/* heapify the subtree with root i */


void heapify(int* arr, int n, int i)
{
// store largest as the root element
int largest = i;

int left = 2 * i + 1;
int right = 2 * i + 2;

// now check whether the right and left right is larger than the root or not
if (left < n && arr[left] > arr[largest])
{
largest = left;
}

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


{
largest = right;
}

// if the root is smaller than the children then swap it with the largest
children's value
if (largest != i)
{
int temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;

// again heapify that side of the heap where the root has gone
heapify(arr, n, largest);
}
}

/* printf the array */


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

Q.14)
ANS:

#include<stdio.h>

#define size 7

int arr[size];

void init()
{
int i;
for(i = 0; i < size; i++)
arr[i] = -1;
}

void insert(int value)


{
int key = value % size;

if(arr[key] == -1)
{
arr[key] = value;
printf("%d inserted at arr[%d]\n", value,key);
}
else
{
printf("Collision : arr[%d] has element %d already!\n",key,arr[key]);
printf("Unable to insert %d\n",value);
}
}

void del(int value)


{
int key = value % size;
if(arr[key] == value)
arr[key] = -1;
else
printf("%d not present in the hash table\n",value);
}

void search(int value)


{
int key = value % size;
if(arr[key] == value)
printf("Search Found\n");
else
printf("Search Not Found\n");
}

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

int main()
{
init();
insert(10); //key = 10 % 7 ==> 3
insert(4); //key = 4 % 7 ==> 4
insert(2); //key = 2 % 7 ==> 2
insert(3); //key = 3 % 7 ==> 3 (collision)

printf("Hash table\n");
print();
printf("\n");

printf("Deleting value 10..\n");


del(10);
printf("After the deletion hash table\n");
print();
printf("\n");

printf("Deleting value 5..\n");


del(5);
printf("After the deletion hash table\n");
print();
printf("\n");

printf("Searching value 4..\n");


search(4);
printf("Searching value 10..\n");
search(10);

return 0;
}

Q.15) from manual

You might also like