0% found this document useful (0 votes)
29 views13 pages

CN Lab

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views13 pages

CN Lab

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Bit stuffing

#include<stdio.h>
#include<conio.h>
#include<string.h>
#define max 100
#define flag "01111110"
void bitstuffingwithflag(char *input,char *stuffed)
{
int count=0,j=0,i;
strcpy(stuffed,flag);
j=strlen(flag);
for(i=0;i<strlen(input);i++)
{
stuffed[j++]=input[i];
if(input[i]=='1')
{
count++;
if(count==5)
{
stuffed[j++]='0';
count=0;
}
}
else
{
count=0;
}
}
strcpy(&stuffed[j],flag);
j+=strlen(flag);
stuffed[j]='\0';
}
void bitdestuffingwithflag(char *stuffed,char *destuffed)
{
int count=0,j=0,i;
i=strlen(flag);
for(i=8;i<strlen(stuffed)-strlen(flag);i++)
{
destuffed[j++]=stuffed[i];
if(stuffed[i]=='1')
{
count++;
if(count==5)
{
i++;
count=0;
}
}
else
{
count=0;
}
}
destuffed[j]='\0';
}
int main()
{
char input[max],stuffed[max],destuffed[max];
clrscr();
printf("Enter the input bit stream:");
scanf("%s",input);
bitstuffingwithflag(input,stuffed);
printf("After bit stuffing with flag:%s\n",stuffed);
bitdestuffingwithflag(stuffed,destuffed);
printf("After bit destuffing with flag:%s\n",destuffed);
getch();
return 0;
}

Character count
#include<stdio.h>
#include<conio.h>
#include<string.h>
void char_count_send(const char *input,char *framed)
{
int i=0,j=0;
int count=strlen(input);
framed[j++]=count+'0';
while(input[i]!='\0')
{
framed[j++]=input[i++];
}
framed[j]='\0';
}
void char_count_reciever(const char *framed,char *output)
{
int count=framed[0]-'0';
int i=1,j=0;
while(i<=count)
{
output[j++]=framed[i++];

}
output[j]='\0';
}
void main()
{
char framed[256];
char output[256];
char input[256];
int first,i;
clrscr();
printf("enter a string:");
gets(input);
char_count_send(input,framed);
printf("orginal string:%s\n",input);
first=framed[0]-'0';
printf("framed string:%d",first);
i=1;
while(framed[i])
{
printf("%c",framed[i++]);
}
char_count_reciever(framed,output);
printf("\nrecieved string %s",output);
getch();
}
Character stuffing
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define DLE 0x10
#define STX 0x02
#define ETX 0x03
void char_stuff(const char *input,char *stuffed)
{
int i=0,j=0;
stuffed[j++]=DLE;
stuffed[j++]=STX;
while(input[i]!='\0')
{
if(input[i]==DLE)
{
stuffed[j++]=DLE;
}
stuffed[j++]=input[i++];
}
stuffed[j++]=DLE;
stuffed[j++]=ETX;
stuffed[j]='\0';
}
void char_destuffing(const char *stuffed,char *destuffed)
{
int i=0,j=0;
if(stuffed[i]==DLE&&stuffed[i++]==STX)
{
i+=2;
}
else
{
destuffed[i]='\0';
return ;
}
while(stuffed[i]!='\0')
{
if(stuffed[i]==DLE)
{
if(stuffed[i++]==DLE)
{
destuffed[j++]=DLE;
i+=2;
}
else if(stuffed[i+1]==ETX)
{
destuffed[j]='\0';
return ;
}
else
{
destuffed[j++]=stuffed[i++];
}
}
}
destuffed[j]='\0';
}
int main()
{
int i;
const char input[100];
char stuffed[100];
char destuffed[100];
printf("enter the string:");
gets(input);
char_stuff(input,stuffed);
printf("stuffed data:\n");
for(i=0;stuffed[i]!='\0';i++)
{
if(stuffed[i]==DLE)
printf("DLE");
else if(stuffed[i]==STX)
printf("STX");
else if(stuffed[i]==ETX)
printf("ETX");
else if(stuffed[i]=='*')
printf("DLEDLE");
else
printf("%c",stuffed[i]);
}
printf("\n");
char_destuffing(stuffed,destuffed);
printf("destuffed data: %s\n",input);
return 0;

}
Dijkstra(shortest path)
#include <stdio.h>
#include <limits.h>
#define MAX 10
#define INFINITY 999
// Function to find the vertex with the minimum distance value
int findMinDistance(int dist[], int visited[], int n) {
int min = INFINITY, minIndex;
for (int v = 0; v < n; v++) {
if (!visited[v] && dist[v] <= min) {
min = dist[v];
minIndex = v;
}
}
return minIndex;
}
// Dijkstra's algorithm to find the shortest path
void dijkstra(int graph[MAX][MAX], int n, int src) {
int dist[MAX]; // dist[i] will hold the shortest distance from src to i
int visited[MAX]; // visited[i] will be true if vertex i is included in the shortest path tree
int i, count, u, v;
// Initialize all distances as INFINITY and visited[] as false
for (i = 0; i < n; i++) {
dist[i] = INFINITY;
visited[i] = 0;
}
// Distance of source vertex from itself is always 0
dist[src] = 0;
// Find shortest path for all vertices
for (count = 0; count < n - 1; count++) {
u = findMinDistance(dist, visited, n);
// Mark the picked vertex as visited
visited[u] = 1;
// Update dist value of the adjacent vertices of the picked vertex
for (v = 0; v < n; v++) {
// Update dist[v] only if it's not visited, there's an edge from u to v,
// and total weight of path from src to v through u is smaller than current value of dist[v]
if (!visited[v] && graph[u][v] && dist[u] != INFINITY && dist[u] + graph[u][v] < dist[v]) {
dist[v] = dist[u] + graph[u][v];
}
}
}
// Print the shortest distance from source
printf("\nVertex\tDistance from Source %d\n", src);
for (i = 0; i < n; i++) {
if (dist[i] == INFINITY)
printf("%d\tINF\n", i);
else
printf("%d\t%d\n", i, dist[i]);
}
}
int main() {
int graph[MAX][MAX], n, src;
int i, j;
printf("Enter the number of nodes: ");
scanf("%d", &n);
printf("Enter the adjacency matrix (Enter %d for infinity):\n", INFINITY);
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &graph[i][j]);
if (graph[i][j] == 0 && i != j) {
graph[i][j] = INFINITY; // No self-loops, mark as infinity if no edge exists
}
}
}
printf("Enter the source node: ");
scanf("%d", &src);
// Call Dijkstra's algorithm
dijkstra(graph, n, src);
return 0;
}

Distance
#include <stdio.h>
#include<conio.h>
#define MAX_NODES 10
#define INF 999
typedef struct {
int distance[MAX_NODES]; // Distance to each node
int via[MAX_NODES]; // Next hop for each node
} RoutingTable;
void printRoutingTable(RoutingTable rt, int node, int numNodes) {
int i;
printf("Routing Table for Node %d:\n", node);
printf("Destination\tCost\tNext Hop\n");
for ( i = 0; i < numNodes; i++) {
printf("%d\t\t%d\t%d\n", i, rt.distance[i], rt.via[i]);
}
getch();
printf("\n");
}
void initializeTables(int costMatrix[MAX_NODES][MAX_NODES], RoutingTable rt[MAX_NODES],
int numNodes) {
int i,j;
for ( i = 0; i < numNodes; i++) {
for ( j = 0; j < numNodes; j++) {
rt[i].distance[j] = costMatrix[i][j];
rt[i].via[j] = (costMatrix[i][j] == INF) ? -1 : j;
}
}
getch();
}
void distanceVectorRouting(int costMatrix[MAX_NODES][MAX_NODES], RoutingTable
rt[MAX_NODES], int numNodes) {
int updated,k,i,j,l;
for ( k = 0; k < numNodes - 1; k++) {
updated = 0;
for ( i = 0; i < numNodes; i++) {
for ( j = 0; j < numNodes; j++) {
for ( l = 0; l < numNodes; l++) {
if (rt[i].distance[j] > rt[i].distance[l] + rt[l].distance[j]) {
rt[i].distance[j] = rt[i].distance[l] + rt[l].distance[j];
rt[i].via[j] = l;
updated = 1;
}
}
}
}
if (!updated)
break; // Exit if no updates were made
}
getch();
}
int main() {
int costMatrix[MAX_NODES][MAX_NODES];
RoutingTable rt[MAX_NODES];
int numNodes, i, j;
clrscr();
printf("Enter the number of nodes: ");
scanf("%d", &numNodes);
// Input the cost matrix
printf("Enter the cost matrix (use %d for infinity):\n", INF);
for (i = 0; i < numNodes; i++) {
for (j = 0; j < numNodes; j++) {
scanf("%d", &costMatrix[i][j]);
if (costMatrix[i][j] == 0 && i != j)
costMatrix[i][j] = INF; // No direct connection between i and j
}
}
// Initialize the routing tables
initializeTables(costMatrix, rt, numNodes);
// Print initial routing tables
printf("\nInitial Routing Tables:\n");
for (i = 0; i < numNodes; i++) {
printRoutingTable(rt[i], i, numNodes);
}

// Perform Distance Vector Routing algorithm


distanceVectorRouting(costMatrix, rt, numNodes);
// Print final routing tables after convergence
printf("\nFinal Routing Tables After Convergence:\n");
for (i = 0; i < numNodes; i++) {
printRoutingTable(rt[i], i, numNodes);
}
getch();
return 0;
}

You might also like