Professional Documents
Culture Documents
DSA Practical Exam Codes
DSA Practical Exam Codes
DSA Practical Exam Codes
struct node
{
int data; //Data of the node
struct node *next; //Address of the next node
}*node;
int main()
{
int n;
printf("\n\n Linked List : To create and display Singly Linked List :\
n");
printf("-------------------------------------------------------------\n");
struct node {
int data;
struct node * pre;
struct node * next;
}*head, *tail;
int main()
{
int n;
head = NULL;
tail = NULL;
printf("\n\n Doubly Linked List : Create and display a doubly linked list :\
n");
printf("-------------------------------------------------------------------\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;
while(tmp != NULL)
{
printf(" node %d : %d\n", n, tmp->data);
n++;
tmp = tmp->next; // current pointer moves to the next node
}
}
}
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]);
heapsort(arr, n);
return 0;
}
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 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);
}
}
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;
}
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 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");
return 0;
}