Professional Documents
Culture Documents
LAB MANUAL
INDEX
int Isempty(LIST L)
{
return L->size==0;
}
void MakeEmpty(LIST L)
{
if(Isempty(L))
printf("\n LIST is already Empty");
else
{
L->size=0;
printf("\n Now List becomes Empty");
}
}
int Isfull(LIST L)
{
return L->size==L->capacity;
}
L->array[i]=L->array[i+1];
L->size--;
}
}
LIST Deletelist(LIST L)
{
MakeEmpty(L);
free(L);
L=NULL;
return L;
}
void Display(LIST L)
{
int i;
for(i=0;i<L->size;i++)
printf("\n %d",L->array[i]);
}
“Larray.c” File:
#include"Larray.h"
#include<stdlib.h>
void main()
{
LIST L=NULL;
POSITION P;
int a,choice,ch,element;
clrscr();
printf("\n\n1.Create\n2.Insert\n3.Delete\n4.Display\n5.MakeEmpty\n6.Find\n7.IsEmpty\n8.IsFull\n9.Deletelist\
n10.Exit\n");
A:
printf("\n Enter Ur Option:\t");
scanf("%d",&choice);
switch(choice)
{
case 1:
if(L==NULL)
L=Createlist(5);
else
printf("\nList is already created");
break;
case 2:
if(L==NULL)
printf("\nList is not yet created");
else
{
printf("\nEnter the Element to insert:\t");
scanf("%d",&element);
if(L->size==0)
Insert(element,L,0);
else
{
printf("\n where u want to insert?\t1:Front\t2:Back\t3:middle\t::: ");
scanf("%d",&ch);
if(ch==1)
Insert(element,L,0);
else
if(ch==2)
Insert(element,L,L->size);
else
if(ch==3)
{
printf("\nWhere you want to insert:\t");
scanf("%d",&a);
P=Find(a,L);
if(P<L->size)
Insert(element,L,P);
else
printf("\nElement is not in the list");
}
else
printf("\n Ur choice is not available");
}
}
break;
case 3:
if(L==NULL)
printf("\nList is not yet created");
if(Isempty(L))
printf("\nList is empty");
else
{
printf("\nEnter the element to delete:\t");
scanf("%d",&a);
Delete(a,L);
}
break;
case 4:
if(L==NULL)
printf("\nList is not yet created");
else
if(Isempty(L))
printf("\nList is empty");
else
{
printf("\nElements present in the list are:");
Display(L);
}
break;
case 5:
if(L==NULL)
printf("\n List is not yet created ");
else
MakeEmpty(L);
break;
case 6:
if(L==NULL)
printf("\n Not yet created");
else
if(Isempty(L))
printf("\n List is empty");
else
{
printf("\n which element is to find:\t");
scanf("%d",&a);
P=Find(a,L);
printf("\n Element is at %d\t[0 to 4 means present]\t[5 means not present]",P);
}
break;
case 7:
if(L==NULL)
printf("\n Not yet created");
else
if(Isempty(L))
printf("\n List is empty");
else
printf("\n List is not empty");
break;
case 8:
if(L==NULL)
printf("\n Not yet created");
else
if(Isfull(L))
printf("\n List is FULL");
else
printf("\n List is not FULL");
break;
case 9:
if(L==NULL)
printf("\n Not yet created");
else
{
L=Deletelist(L);
printf("\n List is Deleted");
}
break;
case 10:
exit (0);
break;
default:
printf("\n\n *******WRONG ENTRY*******");
break;
}
goto A;
}
OUTPUT:
1.Create
2.Insert
3.Delete
4.Display
5.MakeEmpty
6.Find
7.IsEmpty
8.IsFull
9.Deletelist
10.Exit
Enter Ur Option: 1
List is created successfully
Enter Ur Option: 2
Enter the element to insert: 300
Enter Ur Option: 2
Enter the element to insert: 100
Where U want to insert? 1.Front 2.Back 3.Middle ::::: 1
Enter Ur Option: 2
Enter the element to insert: 200
Where U want to insert? 1.Front 2.Back 3.Middle ::::: 3
Enter Ur Option: 2
Enter the element to insert: 400
Where U want to insert? 1.Front 2.Back 3.Middle ::::: 2
Enter Ur Option: 2
Enter the element to insert: 500
Where U want to insert? 1.Front 2.Back 3.Middle ::::: 2
Enter Ur Option: 2
Enter the element to insert: 600
Where U want to insert? 1.Front 2.Back 3.Middle ::::: 1
List is Full
Enter Ur Option: 4
Elements present in the list are
100
200
300
400
500
Enter Ur Option: 7
List is not empty
Enter Ur Option: 6
Which element is to find: 500
Element at 4 [0 to 4 – present] [5 – not present]
Enter Ur Option: 3
Enter the element to delete: 300
Enter Ur Option: 4
Elements present in the list are:
100
200
400
500
Enter Ur Option: 8
List is not Full
Enter Ur Option: 5
Now List becomes Empty
Enter Ur Option: 9
List is Deleted
Enter Ur Option: 2
List is not yet created
Enter Ur Option: 12
*******WRONG ENTRY*******
Enter Ur Option: 10
// C Program to implementation of List ADT as linked-list
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "List.h"
// remove item(s)
// assumes no duplicates
void ListDelete(List L, Key k)
{
assert(L != NULL);
ListNode *prev, *curr;
prev = NULL; curr = L->first;
while (curr != NULL) {
if (eq(k,key(curr->value)))
break;
prev = curr;
curr = curr->next;
}
if (curr != NULL) {
if (prev == NULL)
L->first = curr->next;
else
prev->next = curr->next;
free(curr);
if (L->first == NULL)
L->last = NULL;
}
}
// # items in list
int ListLength(List L)
{
int n = 0;
ListNode *curr = L->first;
while (curr != NULL) {
n++;
curr = curr->next;
}
return n;
}
}
}
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:
Enter the size of STACK[MAX=100]:10
STACK OPERATIONS USING ARRAY
--------------------------------
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter the Choice:1
Enter a value to be pushed:12
98
24
12
Press Next Choice
Enter the Choice:2
EXIT POINT
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "List.h"
// display as [1,2,3,4...]
void showList(List L)
{
assert(L != NULL);
ListNode *curr = L->first;
printf("[");
while (curr != NULL) {
ItemShow(curr->value);
if (curr->next != NULL)
printf(",");
}
printf("]");
}
// remove item(s)
// assumes no duplicates
void ListDelete(List L, Key k)
{
assert(L != NULL);
ListNode *prev, *curr;
prev = NULL; curr = L->first;
while (curr != NULL) {
if (eq(k,key(curr->value)))
break;
prev = curr;
curr = curr->next;
}
if (curr != NULL) {
if (prev == NULL)
L->first = curr->next;
else
prev->next = curr->next;
free(curr);
if (L->first == NULL)
L->last = NULL;
}
}
// # items in list
int ListLength(List L)
{
int n = 0;
ListNode *curr = L->first;
while (curr != NULL) {
n++;
curr = curr->next;
}
return n;
}
#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;
printf("\n Enter the size of STACK[MAX=100]:");
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");
}
#include <stdio.h>
#include <stdlib.h>
// The queue, front stores the front node of LL and rear stores the
// last node of LL
struct Queue {
struct QNode *front, *rear;
};
// Add the new node at the end of queue and change rear
q->rear->next = temp;
q->rear = temp;
}
q->front = q->front->next;
free(temp);
}
#include<stdlib.h>
#include<stdio.h>
struct bin_tree {
int data;
struct bin_tree * right, * left;
};
typedef struct bin_tree node;
void main()
{
node *root;
node *tmp;
//int i;
root = NULL;
/* Inserting nodes into tree */
insert(&root, 9);
insert(&root, 4);
insert(&root, 15);
insert(&root, 6);
insert(&root, 12);
insert(&root, 17);
insert(&root, 2);
// Calculate height
int height(struct Node *N) {
if (N == NULL)
return 0;
return N->height;
}
// Create a node
struct Node *newNode(int key) {
struct Node *node = (struct Node *)
malloc(sizeof(struct Node));
node->key = key;
node->left = NULL;
node->right = NULL;
node->height = 1;
return (node);
}
// Right rotate
struct Node *rightRotate(struct Node *y) {
struct Node *x = y->left;
struct Node *T2 = x->right;
x->right = y;
y->left = T2;
return x;
}
// Left rotate
struct Node *leftRotate(struct Node *x) {
struct Node *y = x->right;
struct Node *T2 = y->left;
y->left = x;
x->right = T2;
return y;
}
// Insert node
struct Node *insertNode(struct Node *node, int key) {
// Find the correct position to insertNode the node and insertNode it
if (node == NULL)
return (newNode(key));
return node;
}
return current;
}
// Delete a nodes
struct Node *deleteNode(struct Node *root, int key) {
// Find the node and delete it
if (root == NULL)
return root;
if (temp == NULL) {
temp = root;
root = NULL;
} else
*root = *temp;
free(temp);
} else {
struct Node *temp = minValueNode(root->right);
root->key = temp->key;
if (root == NULL)
return root;
return root;
}
printPreOrder(root);
return 0;
}
#include<stdio.h>
#include<malloc.h>
void insert();
void del();
void display();
struct node
int priority;
int info;
}*start=NULL,*q,*temp,*new;
int main()
int ch;
clrscr();
do
scanf("%d",&ch);
switch(ch)
case 1:insert();
break;
case 2:del();
break;
case 3:display();
break;
case 4:
break;
while(ch<4);
void insert()
int item,itprio;
new=(N*)malloc(sizeof(N));
scanf("%d",&item);
scanf("%d",&itprio);
new->info=item;
new->priority=itprio;
new->next=NULL;
if(start==NULL )
//new->next=start;
start=new;
else if(start!=NULL&&itprio<=start->priority)
{ new->next=start;
start=new;
else
q=start;
{q=q->next;}
new->next=q->next;
q->next=new;
void del()
if(start==NULL)
printf("\nQUEUE UNDERFLOW\n");
else
new=start;
start=start->next;
//free(start);
void display()
temp=start;
if(start==NULL)
printf("QUEUE IS EMPTY\n");
else
printf("QUEUE IS:\n");
if(temp!=NULL)
for(temp=start;temp!=NULL;temp=temp->next)
//temp=temp->next;
Program 9: Given the eight queens puzzle problem of placing eight chess queens on an 8×8 chessboard so that
no two queens attack each other
/* C program to solve N Queen Problem using
backtracking */
#define N 4
#include <stdbool.h>
#include <stdio.h>
/* A utility function to print solution */
void printSolution(int board[N][N])
{
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++)
printf(" %d ", board[i][j]);
printf("\n");
}
}
/* A utility function to check if a queen can
be placed on board[row][col]. Note that this
function is called when "col" queens are
already placed in columns from 0 to col -1.
So we need to check only left side for
attacking queens */
bool isSafe(int board[N][N], int row, int col)
{
int i, j;
/* Check this row on left side */
for (i = 0; i < col; i++)
if (board[row][i])
return false;
/* Check upper diagonal on left side */
for (i = row, j = col; i >= 0 && j >= 0; i--, j--)
if (board[i][j])
return false;
/* Check lower diagonal on left side */
for (i = row, j = col; j >= 0 && i < N; i++, j--)
if (board[i][j])
return false;
return true;
}
/* A recursive utility function to solve N
Queen problem */
bool solveNQUtil(int board[N][N], int col)
{
/* base case: If all queens are placed
then return true */
if (col >= N)
return true;
/* Consider this column and try placing
this queen in all rows one by one */
for (int i = 0; i < N; i++) {
/* Check if the queen can be placed on
board[i][col] */
if (isSafe(board, i, col)) {
/* Place this queen in board[i][col] */
board[i][col] = 1;
/* recur to place rest of the queens */
if (solveNQUtil(board, col + 1))
return true;
/* If placing queen in board[i][col]
doesn't lead to a solution, then
remove queen from board[i][col] */
board[i][col] = 0; // BACKTRACK
}
}
/* If the queen cannot be placed in any row in
this column col then return false */
return false;
}
/* This function solves the N Queen problem using
Backtracking. It mainly uses solveNQUtil() to
solve the problem. It returns false if queens
cannot be placed, otherwise, return true and
prints placement of queens in the form of 1s.
Please note that there may be more than one
solutions, this function prints one of the
feasible solutions.*/
bool solveNQ()
{
int board[N][N] = { { 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 } };
if (solveNQUtil(board, 0) == false) {
printf("Solution does not exist");
return false;
}
printSolution(board);
return true;
}
// driver program to test above function
int main()
{
solveNQ();
return 0;
}
Output: The 1 values indicate placements of queens
0 0 1 0
1 0 0 0
0 0 0 1
0 1 0 0
Program 10: Given a queue of four elements, the program successfully executed concurrent linked Queues.
#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:
Program 11: C program to find out the Maximum flow and Minimum cut in a graph for any given number of
nodes.
#include <stdio.h>
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define MAX_NODES 1000
#define oo 1000000000
int n; // number of nodes
int e; // number of edges
int capacity[MAX_NODES][MAX_NODES]; // capacity matrix
int flow[MAX_NODES][MAX_NODES]; // flow matrix
int color[MAX_NODES]; // needed for breadth-first search
int pred[MAX_NODES]; // array to store augmenting path
int dequeue () {
int x = q[head];
head++;
color[x] = BLACK;
return x;
}
int bfs (int start, int target) {
int u,v;
for (u=0; u<n; u++) {
color[u] = WHITE;
}
head = tail = 0;
enqueue(start);
pred[start] = -1;
while (head!=tail) {
u = dequeue();
// Search all adjacent white nodes v. If the capacity
// from u to v in the residual network is positive,
// enqueue v.
for (v=0; v<n; v++) {
if (color[v]==WHITE && capacity[u][v]-flow[u][v]>0) {
enqueue(v);
pred[v] = u;
}
}
}
// If the color of the target node is black now,
// it means that we reached it.
return color[target]==BLACK;
}
int max_flow (int source, int sink) {
int i,j,u;
// Initialize empty flow.
int max_flow = 0;
for (i=0; i<n; i++) {
for (j=0; j<n; j++) {
flow[i][j] = 0;
}
}
// While there exists an augmenting path,
// increment the flow along this path.
while (bfs(source,sink)) {
// Determine the amount by which we can increment the flow.
int increment = oo;
for (u=n-1; pred[u]>=0; u=pred[u]) {
increment = min(increment,capacity[pred[u]][u]-flow[pred[u]][u]);
}
// Now increment the flow.
for (u=n-1; pred[u]>=0; u=pred[u]) {
flow[pred[u]][u] += increment;
flow[u][pred[u]] -= increment;
}
max_flow += increment;
}
// No augmenting path anymore. We are done.
return max_flow;
}
void read_input_file() {
int a,b,c,i,j;
FILE* input = fopen("data.txt","r");
// read number of nodes and edges
fscanf(input,"%d %d",&n,&e);
printf("\nNumber of Vertices : %d Edges : %d",n,e);
// initialize empty capacity matrix
for (i=0; i<n; i++) {
for (j=0; j<n; j++) {
capacity[i][j] = 0;
}
}
// read edge capacities
for (i=0; i<e; i++) {
fscanf(input,"%d %d %d",&a,&b,&c);
capacity[a][b] = c;
printf("\nA : %d B : %d Capacity : %d",a,b,c);
}
fclose(input);
}
int main () {
read_input_file();
printf("\nPlease enter Source(s) and Sink(t) :\n");
int s=0,t=n-1;
scanf("%d %d",&s,&t);
printf("\nMax Flow : %d\n",max_flow(s,t));
return 0;
}
Output: