Lab Exam Pactice
Lab Exam Pactice
#include<stdio.h>
#include<stdlib.h>
struct node{
int info;
};
int i, item;
if(head != NULL)
return (head);
temp = head;
for(i=1;i<=n;i++)
scanf("%d", &item);
ptr->info = item;
ptr->next = NULL;
if(head == NULL)
head = ptr;
}
else
temp->next = ptr;
temp = ptr;
return(head);
loc = head;
while(loc != NULL)
printf("%d->",loc->info);
loc = loc->next;
int i;
if(head == NULL)
return(head);
loc = head;
ptr = (struct node*)malloc(sizeof(struct node));
ptr->info = data;
ptr->next = NULL;
if(pos == 0)
ptr->next = head;
head = ptr;
return(head);
else
loc=loc->next;
ptr->next = loc->next;
loc->next = ptr;
return(head);
int i;
if(head == NULL)
return(head);
loc = head;
if(pos == 1)
head = loc->next;
free(loc);
return(head);
else
for(i=1;i<pos-1;i++)
loc = loc->next;
locn = loc->next;
loc->next = locn->next;
free(locn);
return(head);
if(head == NULL)
return(head);
loc = head;
locp = NULL;
while(loc!= NULL)
{
locn = loc->next;
loc->next = locp;
locp = loc;
loc = locn;
head = locp;
return(head);
int main()
int d, ch, p, n;
while(1)
printf("\n0.Exit\n1.Input\n2.Display\n3.Insert\n4.Delete");
scanf("%d", &ch);
switch(ch)
case 0:exit(0);
scanf("%d", &d);
break;
traverse(head);
break;
break;
scanf("%d", &p);
break;
break;
default:printf("Invalid input");
Stack(using array)
#include<stdio.h>
#include<stdlib.h>
#define MAX 6
int stack[MAX];
int isFull()
if(top == MAX-1)
return 1;
return 0;
int isEmpty()
{
if(top == -1)
return 1;
return 0;
void push(int d)
if (isFull())
printf("Overflow...");
else
top++;
stack[top] = d;
void pop()
int x;
if (isEmpty())
printf("Underflow...");
else
x = stack[top];
top--;
}
}
void traverse()
int i;
int main()
int ch, n;
while(1)
printf("\n0.Exit\n1.Push\n2.Pop\n3.Display");
scanf("%d", &ch);
switch(ch)
case 0:exit(0);
scanf("%d", &n);
push(n);
break;
case 2:pop();
break;
case 3:traverse();
break;
default:printf("Invalid choice");
}
}
#include<stdio.h>
#include<stdlib.h>
struct node
int info;
};
if(top == NULL)
ptr->info = item;
ptr->next = NULL;
top = ptr;
else
int Pop()
if(top == NULL)
printf("Underflow");
return -1;
else
top = top->next;
free(temp);
return temp_data;
void traverse()
loc = top;
while(loc!=NULL)
printf("%d->", loc->info);
loc = loc->next;
}
int main()
int ch, n;
while(1)
printf("\n0.Exit\n1.Push\n2.Pop\n3.Display");
scanf("%d", &ch);
switch(ch)
case 0:exit(0);
scanf("%d", &n);
Push(n);
break;
break;
case 3:traverse();
break;
default:printf("Invalid choice");
Queue(using array)
#include<stdio.h>
#include<stdlib.h>
# define MAX 5
int arr[MAX];
int isFull()
return 1;
return 0;
int isEmpty()
return 1;
return 0;
void Enqueue(int d)
if(isFull())
printf("Queue is full");
return;
if(rear == -1)
front = 0;
rear = 0;
rear = 0;
else
rear = rear + 1;
arr[rear] = d;
return;
void Dequeue()
int x;
if(isEmpty())
printf("Queue is empty");
return;
x = arr[front];
if(front == rear)
front = -1;
rear = -1;
front = 0;
else
{
front = front + 1;
return;
void traverse()
int i;
if (front == -1)
printf("Queue empty");
return;
for(i=front;i<=rear;i++)
else
for(i=0;i<=rear;i++)
for(i=front;i<=MAX-1;i++)
return;
int main()
{
int ch, n;
while(1)
printf("\n0.Exit\n1.Enqueue\n2.Dequeue\n3.Display");
scanf("%d", &ch);
switch(ch)
case 0:exit(0);
scanf("%d", &n);
Enqueue(n);
break;
case 2:Dequeue();
break;
case 3:traverse();
break;
default:printf("Invalid choice");
#include<stdlib.h>
struct node
int info;
else
rear = ptr;
int Dequeue()
if(front == NULL)
printf("Underflow");
return -1;
else
{
struct node * temp = front;
front = front->next;
free(temp);
return temp_data;
void traverse()
printf("\nQueue Empty");
else
loc = front;
while(loc)
printf("%d->", loc->info);
loc = loc->next;
int main()
int ch, n;
while(1)
{
printf("\n0.Exit\n1.Enqueue\n2.Dequeue\n3.Display");
scanf("%d", &ch);
switch(ch)
case 0:exit(0);
scanf("%d", &n);
Enqueue(n);
break;
break;
case 3:traverse();
break;
default:printf("Invalid choice");
#include<stdio.h>
#include<stdlib.h>
# define MAX 20
int n;
void input()
int i;
printf("Enter the size of array:\n");
scanf("%d", &n);
for(i=0;i<n;i++)
scanf("%d", &arr[i]);
return;
void display()
int i;
for(i=0;i<n;i++)
return;
int i;
n++;
for(i=n-1;i>=pos;i--)
arr[i]=arr[i-1];
arr[pos-1] = data;
int i;
for(i=pos-1;i<n;i++)
arr[i]=arr[i+1];
n--;
return;
void rev()
int i,j;
brr[j]=arr[i];
arr[i]=brr[i];
return;
void sort()
int i,j,temp;
for(i=0;i<n;i++)
if(arr[i]>arr[j])
temp = arr[i];
arr[i]= arr[j];
arr[j] = temp;
return;
int main()
while(1)
printf("\n0.Exit\n1.Input\n2.Insert\n3.Delete\n4.Reverse\n5.Sort\n6.Display");
scanf("%d", &ch);
switch(ch)
case 0:exit(0);
case 1:input();
break;
scanf("%d%d",&p, &d);
insert(p,d);
break;
scanf("%d", &p);
delete(p);
break;
case 4:rev();
break;
case 5:sort();
break;
case 6:display();
break;
default:printf("Invalid choice");
int i, j, swap;
swap = arr[j];
arr[j] = arr[j+1];
arr[j+1] = swap;
void merge(int arr1[20], int arr2[20], int n1, int n2) //merging arrays
int i, j, k;
int res[40];
i = 0;
j = 0;
k = 0;
// Merging starts
res[k] = arr1[i];
i++;
k++;
else
res[k] = arr2[j];
k++;
j++;
/* Some elements in array 'arr1' are still remaining where as array 'arr2' is exhausted */
while (i < n1)
res[k] = arr1[i];
i++;
k++;
/* Some elements in array 'arr2' are still remaining where as array 'arr1' is exhausted */
res[k] = arr2[j];
k++;
j++;
sort(res, (n1+n2));
printf("\n%d", res[i]);
int main()
scanf("%d", &n1);
{
scanf("%d", &arr1[i]);
scanf("%d", &n2);
scanf("%d", &arr2[i]);
return 0;
#include<stdio.h>
#include<stdlib.h>
struct BST {
int info;
};
loc = root;
par = NULL;
while(loc != NULL)
if(x == loc->info)
printf("Duplicate Entry");
return root;
par = loc;
if (x < loc->info)
else
loc = loc->right;
if(par == NULL)
root = ptr;
par->left = ptr;
else{
par->right = ptr;
return root;
if(root != NULL)
Inorder(root->left);
Inorder(root->right);
if(root != NULL)
Preorder(root->left);
Preorder(root->right);
if(root != NULL)
Postorder(root->left);
Postorder(root->right);
printf(" %d ", root->info);
loc = root;
while(loc != NULL)
if(loc->info == p)
return root;
if(loc->info < p)
else
return root;
int main()
{
struct BST* root = NULL;
int ch, d, q;
while(1)
printf("\n\n------MENU------\n\n");
printf("\n0.Exit\n1.Insert\n2.Inorder\n3.Preorder\n4.Postorder\n5.Search\n6.Delete\n");
scanf("%d", &ch);
switch(ch)
case 0: exit(0);
scanf("%d", &d);
break;
case 2: Inorder(root);
break;
case 3: Preorder(root);
break;
case 4: Postorder(root);
break;
scanf("%d", &q);
break;
default:printf("Invalid choice");
break;