Lab Manual Solution of Data Structures
Lab Manual Solution of Data Structures
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
Practical 1
1.1 Write a program to insert/delete in linear array at specific position.
#include <stdio.h>
int main()
int arr[100];
scanf("%d",&arr[i]);
printf("\n");
scanf("%d",&item);
scanf("%d",&pos);
printf("\n");
return 0;
#include<stdio.h>
int main()
printf("\n");
scanf("%d",&key);
// traverse the array // if any element matches the key, store its position
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
if(arr[i] == key)
pos = i;
break;
if(pos != -1)
for(i = pos; i < size - 1; i++) //shift elements backwards by one position
arr[i] = arr[i+1];
printf("%d ",arr[i]);
else
return 0;
}
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
#include <stdio.h>
#include <conio.h>
int main ()
for ( i = 0; i < size; i ++) // use nested for loop to find the duplicate elements in array
for ( k = j; k < size - 1; k++) // delete the current position of the duplicate element
}
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
j--; // if the position of the elements is changes, don't increase the index j
printf (" \n Array elements after deletion of the duplicate elements: ");
return 0;
}
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
1.3 Write a program to read 10 integers in an array. Sort them out on the basis of number of
#include <stdio.h>
void main()
int arr1[100];
int n, i, j, tmp;
printf("----------------------------------------------\n");
scanf("%d", &n);
for(i=0;i<n;i++)
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
if(arr1[j] <arr1[i])
tmp = arr1[i];
arr1[i] = arr1[j];
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
arr1[j] = tmp;
printf("\n\n");
}
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
Practical 2
2.1 Demonstrate the concept of Call by value and Call by Reference.
#include<stdio.h>
num=num+100;
int main() {
int x=100;
return 0;
#include <stdio.h>
int main()
int a = 10;
int b = 20;
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
swap(a,b);
// The value of actual parameters do not change by changing the formal parameters in call by value, a =
10, b = 20
int temp;
temp = a;
a=b;
b=temp;
}
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
2.2 Write a program to prints array elements in reverse orders applying pointers
#include<stdio.h>
#define N 5
int main()
scanf("%d", &a[i]);
printf("%d\n", *ptr--);
return 0;
}
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
Practical 3
3.1 Write a program for stack using array for the following operations:
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
//clrscr();
top=-1;
scanf("%d",&n);
printf("\n\t--------------------------------");
do
scanf("%d",&choice);
switch(choice)
case 1:
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
break;
default:
}
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
while(choice!=4);
return 0;
void push()
if(top>=n-1)
else
scanf("%d",&x);
top++;
stack[top]=x;
void pop()
if(top<=-1)
else
{
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
top--;
void display()
if(top>=0)
printf("\n%d",stack[i]);
else
}
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
#include <bits/stdc++.h>
int rem;
rem=num1 % 10;
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
stck.push(rem);
int revrseNum(){
int revrs = 0;
int i = 1;
int temp;
int topp;
while (!stck.empty()){
topp=stck.top();
stck.pop();
temp=topp*i;
i *= 10;
return revrs;
int main(){
pushDigts(Num);
return 0;
#include<stdio.h>
#include<string.h>
#define MAX 20
#define true 1
#define false 0
int stack[MAX];
if(top == (MAX-1))
printf("Stack Overflow\n");
else
top=top+1;
stack[top] = item;
}/*End of push*/
if(top == -1)
printf("Stack Underflow\n");
else
return(stack[top--]);
}/*End of pop*/
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
char exp[MAX],temp;
int i,valid=true;
gets(exp);
for(i=0;i<strlen(exp);i++)
push( exp[i] );
valid=false;
else
temp=pop();
valid=false;
valid=false;
valid=false;
valid=false;
if( valid==true )
printf("Valid expression\n");
else
printf("Invalid expression\n");
} /*End of main*/
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
Practical 4
4.1 Write a program of conversion of an expression from infix to Postfix, Prefix.
#include<stdio.h>
#include<ctype.h>
char stack[100];
void push(char x)
stack[++top] = x;
char pop()
if(top == -1)
return -1;
else
return stack[top--];
int priority(char x)
if(x == '(')
return 0;
return 1;
return 2;
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
return 0;
int main()
char exp[100];
char *e, x;
scanf("%s",exp);
printf("\n");
e = exp;
while(*e != '\0')
if(isalnum(*e))
printf("%c ",*e);
push(*e);
else
printf("%c ",pop());
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
push(*e);
e++;
while(top != -1)
printf("%c ",pop());
}return 0;
Infix to Prefix
#include<stdio.h>
#include<string.h>
#include<limits.h>
#include<stdlib.h>
char stack[MAX];
int isFull() {
}
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
int isEmpty() {
// Push function here, inserts value in stack and increments stack top by 1
if (isFull())
return;
top++;
stack[top] = item;
int pop() {
if (isEmpty())
return INT_MIN;
return stack[top--];
int peek(){
if (isEmpty())
return INT_MIN;
return stack[top];
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
switch (ch)
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
return -1;
{
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
int i, j;
if (checkIfOperand(expression[i]))
expression[++j] = expression[i];
push(expression[i]);
// Here, if we scan character is an ‘)’, we need to pop and print from the stack
expression[++j] = pop(stack);
else
pop(stack);
else // if an opertor
expression[++j] = pop(stack);
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
push(expression[i]);
while (!isEmpty(stack))
expression[++j] = pop(stack);
expression[++j] = '\0';
char temp[size];
temp[j--]='\0';
while(exp[i]!='\0')
temp[j] = exp[i];
j--;
i++;
strcpy(exp,temp);
int i = 0;
while(exp[i]!='\0')
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
if(exp[i]=='(')
exp[i]=')';
else if(exp[i]==')')
exp[i]='(';
i++;
// reverse string
reverse(exp);
//change brackets
brackets(exp);
//get postfix
getPostfix(exp);
reverse(exp);
int main()
printf("%s\n",expression);
InfixtoPrefix(expression);
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
printf("%s\n",expression);
return 0;
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
// Stack type
struct Stack
int top;
unsigned capacity;
int* array;
};
// Stack Operations
stack->top = -1;
stack->capacity = capacity;
return stack;
return stack->top == -1 ;
return stack->array[stack->top];
if (!isEmpty(stack))
return stack->array[stack->top--] ;
return '$';
stack->array[++stack->top] = op;
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
int i;
if (isdigit(exp[i]))
else
switch (exp[i])
return pop(stack);
int main()
return 0;
}
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
Practical 5
5.1 Write a program for queue using array for the following operations:
Enqueue, Dequeue, IsEmpty, IsFull.
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int dequeue();
int isFull();
int isEmpty();
int getRear();
int getFront();
/* Driver function */
int main()
{
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
/* Queue menu */
printf("--------------------------------------\n");
printf("--------------------------------------\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Size\n");
printf("0. Exit\n");
printf("--------------------------------------\n");
scanf("%d", &ch);
case 1:
scanf("%d", &data);
if (enqueue(data))
else
printf("Queue is full.");
break;
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
case 2:
data = dequeue();
if (data == INT_MIN)
printf("Queue is empty.");
else
break;
case 3:
if (isEmpty())
printf("Queue is empty.");
else
break;
case 4:
if (isEmpty())
printf("Queue is empty.");
else
break;
case 5:
if (isEmpty())
printf("Queue is empty.");
else
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
break;
case 0:
exit(0);
default:
break;
printf("\n\n");
if (isFull())
return 0;
}
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
int dequeue()
return INT_MIN;
return data;
/** Checks if queue is full or not. It returns 1 if queue is full, * overwise returns 0. */
int isFull()
/** * Checks if queue is empty or not. It returns 1 if queue is empty, * otherwise returns 0. */
int isEmpty()
}
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
/** * Gets, front of the queue. If queue is empty return INT_MAX otherwise * returns front of queue. */
int getFront()
return (isEmpty())
? INT_MIN
: queue[front];
/** * Gets, rear of the queue. If queue is empty return INT_MAX otherwise * returns rear of queue. */
int getRear()
return (isEmpty())
? INT_MIN
: queue[rear];
}
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
5.2 Write a program for circular queue using array for the following operations:
Enqueue, Dequeue, IsEmpty, IsFull.
#include <stdio.h>
#define SIZE 5
int items[SIZE];
return 0;
return 0;
if (isFull())
else {
items[rear] = element;
}
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
// Removing an element
int deQueue() {
int element;
if (isEmpty()) {
return (-1);
} else {
element = items[front];
if (front == rear) {
front = -1;
rear = -1;
// Q has only one element, so we reset the // queue after dequeing it. ?
else {
return (element);
void display() {
int i;
if (isEmpty())
else {
int main() {
deQueue();
enQueue(1);
enQueue(2);
enQueue(3);
enQueue(4);
enQueue(5);
enQueue(6);
display();
deQueue();
display();
enQueue(7);
display();
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
enQueue(8);
return 0;
}
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
Practical 6
6.1 Write a program for single linked list for the following operations:
3. Insert the new node after the desired node into the linked list
#include <stdio.h>
#include <stdlib.h>
int number;
}Node;
Node* n = malloc(sizeof(Node));
n->number = A;
n->next = *head;
*head = n;
Node* temp;
Node* prev;
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
temp = *head;
prev = *head;
if (i == 0 && position == 1) {
*head = (*head)->next;
free(temp);
else {
prev->next = temp->next;
free(temp);
else {
prev = temp;
break;
temp = temp->next;
while(head){
head = head->next;
printf("\n\n");
int main()
list->next = NULL;
Push(&list, 1);
Push(&list, 2);
Push(&list, 3);
printList(list);
printList(list);
return 0;
}
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
6.2 Write a program for stack using linked list for the following operations:
#include <stdio.h>
#include <stdlib.h>
struct node
int info;
}*top,*top1,*temp;
int topelement();
void pop();
void empty();
void display();
void destroy();
void stack_count();
void create();
int count = 0;
void main()
printf("\n 1 - Push");
printf("\n 2 - Pop");
printf("\n 3 - Top");
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
printf("\n 4 - Empty");
printf("\n 5 - Exit");
printf("\n 6 - Dipslay");
create();
while (1)
scanf("%d", &ch);
switch (ch)
case 1:
scanf("%d", &no);
push(no);
break;
case 2:
pop();
break;
case 3:
if (top == NULL)
else
{
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
e = topelement();
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
stack_count();
break;
case 8:
destroy();
break;
default :
break;
}
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
void create()
top = NULL;
void stack_count()
if (top == NULL)
top->ptr = NULL;
top->info = data;
else
temp->ptr = top;
temp->info = data;
top = temp;
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
count++;
void display()
top1 = top;
if (top1 == NULL)
printf("Stack is empty");
return;
top1 = top1->ptr;
void pop()
top1 = top;
if (top1 == NULL)
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
return;
else
top1 = top1->ptr;
free(top);
top = top1;
count--;
int topelement()
return(top->info);
void empty()
if (top == NULL)
else
}
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
void destroy()
top1 = top;
top1 = top->ptr;
free(top);
top = top1;
top1 = top1->ptr;
free(top1);
top = NULL;
count = 0;
}
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
6.3 Write a program for queue using linked list for the following operations:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
};
ptr->data = value;
ptr->next = NULL;
} else {
rear->next = ptr;
rear = ptr;
printf("Node is Inserted\n\n");
}
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
int dequeue() {
if (front == NULL) {
printf("\nUnderflow\n");
return -1;
} else {
front = front->next;
free(temp);
return temp_data;
void display() {
printf("\nQueue is Empty\n");
} else {
temp = front;
while (temp) {
printf("%d--->", temp->data);
temp = temp->next;
}
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
printf("NULL\n\n");
int main() {
while (choice != 4) {
printf("1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
enqueue(value);
break;
case 2:
break;
case 3:
display();
break;
case 4:
exit(0);
break;
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
default:
printf("\nWrong Choice\n");
return 0;}
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
Practical 7
7.1 Write a program to implement doubly linked list for the following
operations:
1. Insert a new node after the desired node
2. Delete the desired node
3. Display the nodes of doubly linked list
4. Count the nodes of doubly linked list
#include<stdio.h>
#include<stdlib.h>
int data;
}node;
temp = head;
while(temp->data != num)
temp = temp->next;
}
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
if(temp == NULL)
return;
newnode = (node*)malloc(sizeof(node));
scanf("%d", &newnode->data);
newnode->next = temp->next;
newnode->prev = temp;
temp->next = newnode;
if(head == NULL)
head = newnode;
temp = head;
while(temp->data != num)
temp = temp->next;
if(temp == NULL)
return;
if(temp->next == NULL)
temp->prev->next = NULL;
return;
if(temp == head)
temp->next->prev = NULL;
head = temp->next;
return;
temp->prev->next = temp->next;
temp->next->prev = temp->prev;
void display()
node *temp;
temp = head;
while(temp != NULL)
temp = temp->next;
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
printf("\n");
node *temp;
temp = head;
while(temp->data != num)
temp = temp->next;
if(temp == NULL)
return;
void main()
head = NULL;
tail = NULL;
scanf("%d", &num);
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
insertAfter(num);
while(1)
printf("5. Exit\n");
scanf("%d", &choice);
switch(choice)
scanf("%d", &num);
insertAfter(num);
break;
scanf("%d", &num);
delete(num);
break;
case 3: display();
break;
scanf("%d", &num);
search(num);
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
break;
case 5: exit(0);
break;
break;
}
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
#include<stdio.h>
#include<stdlib.h>
struct Node
int data;
};
int main(void)
int choice,ele,item;
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
List head=NULL;
while(1)
printf("\n1.Insert\n2.Delete\n3.Display\n4.Search\n5.Count\n6.Exit\n");
scanf("%d",&choice);
switch(choice)
scanf("%d",&ele);
head=insert(head,ele);
break;
scanf("%d",&ele);
head=delete(head,ele);
break;
display(head);
break;
scanf("%d",&ele);
head=search(head,ele);
break;
item=count(head);
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
printf("%d",item);
break;
case 6: exit(0);
return 0;
List p,temp;
temp=(List)malloc(sizeof(struct Node));
temp->data=item;
temp->rlink=NULL;
if(head==NULL)
temp->llink=NULL;
head=temp;
else
p=head;
while(p->rlink!=NULL)
p=p->rlink;
p->rlink=temp;
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
temp->llink=p;
return head;
List p;
if(head==NULL)
printf("\nList is empty");
return head;
p=head;
p=p->rlink;
if(p==NULL)
return head;
if(head==p)
head=p->rlink;
if(p->llink!=NULL)
p->llink->rlink=p->rlink;
if(p->rlink!=NULL)
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
p->rlink->llink=p->llink;
free(p);
return head;
List p;
if(head==NULL)
printf("\nList is empty");
return;
p=head;
while(p!=NULL)
printf("%d\t",p->data);
p=p->rlink;
List p;
int pos=0;
if(head==NULL)
{
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
printf("\nList is empty");
return head;
p=head;
while(p!=NULL)
pos++;
if(p->data==item)
return head;
p=p->rlink;
return head;
List p;
int count=0;
if(head==NULL)
printf("\nList is empty");
return count;
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
p=head;
while(p!=NULL)
count++;
p=p->rlink;
return count;
}
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
Practical 8
8.1 Write a program to construct a binary search tree.
#include <stdio.h>
#include <stdlib.h>
struct node
int key;
};
temp->key = item;
return temp;
if (root != NULL)
inorder(root->left);
inorder(root->right);
return node;
int main()
{
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
50
/ \
30 70
/ \ / \
20 40 60 80 */
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
inorder(root);
return 0;
}
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
#include <stdio.h>
#include <stdlib.h>
struct node
int key;
};
temp->key = item;
return temp;
if (root != NULL)
inorder(root->left);
inorder(root->right);
}
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
return node;
int main()
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
inorder(root);
return 0;
}
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
Practical 9
9.1 Write a program to demonstrate DFS and BFS.
#include<stdio.h>
#include<stdlib.h>
struct Queue
unsigned capacity;
int* array;
};
struct Stack
int top;
unsigned capacity;
int* array;
};
// vertices in graph)
struct Graph
{
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
int V;
};
struct AdjList
int dest;
};
struct AdjListNode
int dest;
};
stack->top = -1;
stack->capacity = capacity;
return stack;
}
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
// top by 1
if (isFull(stack))
return;
stack->array[++stack->top] = item;
// decreases top by 1
if (isEmpty(stack))
return INT_MIN;
return stack->array[stack->top--];
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
queue->front = queue->size = 0;
queue->capacity = capacity;
return queue;
// capacity
}
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
if (isFull(queue))
return;
queue->array[queue->rear] = item;
queue->size = queue->size + 1;
if (isEmpty(queue))
return INT_MIN;
queue->size = queue->size - 1;
return item;
{
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
if (isEmpty(queue))
return INT_MIN;
return queue->array[queue->front];
if (isEmpty(queue))
return INT_MIN;
return queue->array[queue->rear];
newNode->dest = dest;
newNode->next = NULL;
return newNode;
graph->V = V;
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
// array will be V
int i;
graph->array[i].head = NULL;
return graph;
newNode->next = graph->array[src].head;
graph->array[src].head = newNode;
newNode = newAdjListNode(src);
newNode->next = graph->array[dest].head;
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
graph->array[dest].head = newNode;
// representation of graph
int v;
while (pCrawl)
pCrawl = pCrawl->next;
printf("\n");
newNode->dest = dest;
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
newNode->next = NULL;
return newNode;
graph->V = V;
// array will be V
int i;
graph->array[i].head = NULL;
return graph;
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
9.2 Write a program in c language for giving a directed graph, and check whether the graph contains a
cycle or not. It should printntrue if the given graph contains at least one cycle, else it should print
false.
#include <stdio.h>
#include <stdbool.h>
#define V 4
if(visited[v] == false)
visited[v] = true;
rs[v] = true;
return true;
else if(rs[i])
return true;
}
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
rs[v] = false;
return false;
int main()
int i,j;
bool visited[V];
bool rs[V];
{1, 0, 1, 1},
{0, 1, 0, 1},
{1, 1, 1, 0}
};
visited[i] = false;
rs[i] = false;
return 1;
return 0;
}
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
Practical 10
Write a program in c language to implement following sorting techniques:
1. Selection Sort
2. Quick Sort
3. Insertion Sort.
4. Merge Sort
5. Heap Sort
1. Selection Sort
#include <stdio.h>
*a = *b;
*b = temp;
int i, j, min_idx;
{
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
min_idx = i;
min_idx = j;
swap(&arr[min_idx], &arr[i]);
int i;
printf("n");
int main()
int n = sizeof(arr)/sizeof(arr[0]);
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
selectionSort(arr, n);
printArray(arr, n);
return 0;
2. Quick Sort
#include <stdio.h>
int t = *a;
*a = *b;
*b = t;
of pivot */
swap(&arr[i], &arr[j]);
return (i + 1);
{
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
at right place */
quickSort(arr, pi + 1, high);
int i;
printf("n");
int main()
int n = sizeof(arr)/sizeof(arr[0]);
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
quickSort(arr, 0, n-1);
printArray(arr, n);
return 0;
3. Insertion Sort
#include<stdio.h>
int i,j,temp;
for(i=1;i<n;i++)
j=i-1;
temp=a[i];
while((temp<a[j])&&(j>=0))
a[j+1]=a[j];
j=j-1;
if(temp>a[j])
a[j+1]=temp;
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
else
j=j+1;
a[j]=temp;
void main()
int a[30],n,i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
insertion_sort(a,n);
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
4. Merge Sort
#include<stdio.h>
#define MAX 50
int main()
int arr[MAX];
int n, i;
scanf("%d", &n);
scanf("%d", &arr[i]);
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
merge_sort(arr, 0, n - 1);
return 0;
int i = beg;
int j = mid + 1;
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
int k = beg;
int temp[MAX];
temp[k] = arr[i];
k++;
i++;
else
temp[k] = arr[j];
k++;
j++;
temp[k] = arr[i];
k++;
i++;
}
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
temp[k] = arr[j];
k++;
j++;
arr[i] = temp[i];
}
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
5. Heap Sort
#include<stdio.h>
largest = l;
largest = r;
if (largest != i)
swap(&arr[i], &arr[largest]);
heapify(arr, n, largest);
}
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
heapify(arr, n, i);
swap(&arr[0], &arr[i]);
heapify(arr, i, 0);
printf("%d ",arr[i]);
Madhuben and Bhanubhai Patel
Institute of Technology
New Vallabh Vidyanagar.
(Constituent College under CVM University)
printf("\n");
// Driver program
int main()
int n = sizeof(arr)/sizeof(arr[0]);
heapSort(arr, n);
printArray(arr, n);