You are on page 1of 34

APPENDIX

1. LINKED LIST CREATION AND IMPLEMENTING BASIC


OPERATIONS ON IT

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <stdlib.h>

using namespace std;


string x;

struct linked_list
{
int roll;
struct linked_list *next;
};
typedef struct linked_list node;
node* tmphead;
node* curr;

void print(node *list)


{
if(list->next != NULL)
{
printf("%d-->",list->roll);

if(list->next->next == NULL)
printf("%d",list->next->roll);

print(list->next);
}

}
/*
void append(node *current, int num)
{
current->roll = num;
current->next = (node *)malloc(sizeof(node));
current = current->next;
}*/

void append(node *list,int num)


{
if(list->next != NULL)
{
append(list->next,num);
}
else
{
node* s;
s=(node *)malloc(sizeof(node));
list->next = s;
s->next = NULL;
s->roll = num;

void add_at_first(int num,node* list)


{

node *q;
q =(node *)malloc(sizeof(node));
q->roll = num;

q->next = tmphead;
list = q;
tmphead = q;
}

void addafter(node* list, int c, int num)


{

node *q,*t;
int cnt = 0;;

while(1){
cnt++;
if(cnt == c)
{
q = list->next;
t =(node *)malloc(sizeof(node));
t->roll = num;
list->next = t;
t->next = q;
break;
}

list = list->next;
}

void del(node* list, int num )


{

node *prev,*next,*tmp;
if( list->roll == num )
{

tmp = list->next;
list = tmp;
exit(0);
}
prev = list;
while( list != NULL )
{
list = list->next;
if( list->roll == num )
{
prev->next = list->next;
list->roll = -999;
return;

}
prev = list;

cout<<"\nElement "<<num<<" not Found.";


}

void write_file(char fn[])


{
ofstream outFile;
//outFile.open("final.txt");
outFile.open(fn);
while(1)
{
if(tmphead->next != NULL)
{
outFile << tmphead->roll << " ";
tmphead = tmphead->next;
}
else
{
outFile << tmphead->roll;
break;

}
}

int count(node* list)


{

node *q;
int c=0;
for( q = list ; q != NULL ; q = q->next )
c++;
return c;

int main() {
ifstream inFile;

inFile.open("test.txt");

if (!inFile) {

cout << "Unable to open file";


system("pause");
exit(1); // terminate with error
}
//count=0;
int y;
node* head;
node* start;
head=(node *)malloc(sizeof(node));
tmphead= head;
start= head;
curr = head;

inFile >> y;
bool d;
do
{
head->roll = y;
head->next = (node *)malloc(sizeof(node));

d=inFile >> y;
if(d)
head = head->next;
else
head->next = NULL;

}while (d);

head = NULL;
printf("\n******LINKED LIST****************\n");
print(tmphead);
printf("\n\n");
inFile.close();
int choice;
while(1){
printf("\n\n************ CHOICE ***************************\n");
printf("\n1.ADD A NODE AT THE BEGINING\n2.APPEND A
NODE\n3.ADD A NODE AFTER ith NODE\n4.DELETE A
NODE\n5.WRITE THE DATA IN THE LIST TO A FILE\n6.DISPLAY
THE LIST\nENTER THE CHOICE no. : ");
scanf("%d",&choice);
int num;
switch(choice)
{
case 1: printf("\nenter the number: ") ;
scanf("%d",&num);
add_at_first(num,tmphead) ;
break;

case 2: printf("\nenter the number to appended to the list: ") ;


scanf("%d",&num);
append(tmphead,num) ;
break;

case 3:printf("\nenter the number to added to the list: ") ;


scanf("%d",&num);
int x;
printf("\nenter the node number after which to be added to the
list: ") ;
scanf("%d",&x);
addafter(tmphead,x,num);
break;

case 4:printf("\nenter the number to be deleted frm the list: ") ;


scanf("%d",&num);
del(tmphead,num);
break;

case 5:printf("\nenter the file name to which the list is to b written : ");
char filename[20];
scanf("%s",&filename);
write_file(filename);
break;

case 6:print(tmphead);
break;
default :
printf("\n**** ERROR : eneter a valid choice\n");
}

}
printf("\n\n");
print(tmphead);
system("pause");
return 0;
}

2. SEARCHING IN A TAPE

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <stdlib.h>

using namespace std;

struct linked_list
{

int roll;
struct linked_list *next;
struct linked_list *prev;
};
typedef struct linked_list node;

node* midnode; // a pointer to the middle node


node* lastnode; // a pointer to last node
int found; // its a flag it is set to 1 when search element is found
int comp; // it is used to store the no of comparision made while
searching

// this procedure sets the middle node pointer


node gotomid(node* list,int x)
{
static int c = 1;
if(c != (x/2))
{
c++;
gotomid(list->next,x);
}
else
midnode = list;

// this procedure sets the last node pointer


node gotolast(node* list,int x)
{

static int c = 1;
if(c != x)
{
c++;
gotolast(list->next,x);
}
else
lastnode = list;

// this procedure displays the linked list


void print(node *list)
{
if(list->next != NULL)
{
printf("%d-->",list->roll);

if(list->next->next == NULL)
printf("%d",list->next->roll);

print(list->next);
}

// this procedure search the item while moving towards right


void searchR(node *list,int x)
{
if(list->next != NULL)
{
if(found == 0) comp++;
if(list->roll == x){
found=1;
if(list->prev == NULL)
printf("NULL %d %d",list->roll,list->next->roll);
else
printf("%d %d %d",list->prev->roll,list->roll,list->next-
>roll);

if(list->next->next == NULL)
if(list->next->roll == x)
{
found = 1;
printf("%d %d NULL",list->roll,list->next->roll);

searchR(list->next,x);
}

// this procedure searches for an elemnt while moving towards left


void searchL(node *list,int x)
{
if(list->prev != NULL)
{
if(found == 0) comp++;
if(list->roll == x){
found=1;
if(list->next == NULL)
printf("%d %d NULL",list->prev->roll,list->roll);
else
printf("%d %d %d",list->prev->roll,list->roll,list->next-
>roll);

if(list->prev->prev == NULL)
if(list->prev->roll == x)
{
found = 1;
printf("NULL %d %d ",list->prev->roll,list->roll);

searchL(list->prev,x);
}

int main() {

found = 0;
comp = 0;
ifstream inFile;
ofstream outFile;
outFile.open("test.txt");
int j;
// below for loop generates random number and write to a file
for(int k=0;k<10000;k++)
{
j = rand() + 1;
outFile << j;
outFile << "\n";
}
outFile.close();

inFile.open("test.txt");
if (!inFile) {
cout << "Unable to open file";
exit(1); // terminate with error
}

int y;
node* head;
node* tmphead;
node* tmp;
head=(node *)malloc(sizeof(node));
tmphead= head;

inFile >> y;

bool d;

head->roll = y;

head->next = (node *)malloc(sizeof(node));


head->prev = NULL;

d = inFile >> y;
if(d)
{
tmp = head;
head = head->next;
}
else
{
head->next = NULL;
}
int c=1;
while (d)
{
head->roll = y;
c++;
head->prev = tmp;
tmp = head;
head->next = (node *)malloc(sizeof(node));
d = inFile >> y;
if(d)
head = head->next;
else
head->next = NULL;

gotomid(tmphead,c);
gotolast(tmphead,c);

inFile.close();
print(tmphead);
int x;
again :
printf("\n********** SEARCH OPTIONS **********");
printf("\n1.Search when the pointer is at beginning.");
printf("\n2.Search when the pointer is at middle( first mid to last then
from last to startin point)");
printf("\n3.Search when the pointer is at middle( first mid to startin
point then from startin point to last)");
printf("\n4.Search when the pointer is at last");

printf("\nEnter your choice : ");


int ch;
scanf("%d",&ch);
printf("\n\nenter the roll to be searched : ");
scanf("%d",&x);

switch(ch)
{
case 1:printf("\n\n");
searchR(tmphead,x);
printf("\n\n no of comparision : %d",comp);
break;
case 2:
printf("\n\n");
searchR(midnode,x);
if(found == 0)
searchL(lastnode,x);
printf("\n\n no of comparision : %d",comp);
break;
case 3:
printf("\n\n");
searchL(midnode,x);
if(found == 0)
searchR(tmphead,x);
printf("\n\n no of comparision : %d",comp);
break;
case 4:
searchL(lastnode,x);
printf("\n\n no of comparision : %d",comp);
break;
default :
break;

}
printf("\n\nPress e to exit or any key to continue...");
char choice;
scanf(" %c",&choice);
if(choice != 'e' || choice != 'E')
goto again;
printf("\n");
system("pause");
return 0;
}

3. SORTING THE DATA IN A FILE

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <stdlib.h>

using namespace std;


string x;

struct linked_list
{
char fname[20];
char lname[20];
int roll;
struct linked_list *next;
};
typedef struct linked_list node;
int cnt1;
int cnt2;

void print(node *list)


{
if(list->next != NULL)
{
printf("%s %d-->",list->fname,list->roll);

if(list->next->next == NULL)
printf("%s %d",list->next->fname,list->next->roll);

print(list->next);
}

// sorting procedire
void sort(node *first)
{
int i,j,m,n=0;
char hold[20];
int tmp;
node *q, *p, *t;
for(q = first ; q ; q=q->next)
++n;

for(i=1 , t = first ; i<=n-1 ; t = t->next , ++i)


for( j=0 , p = first ; j<n-i ; p = p->next , ++j)
if(strcmp(p->fname,(p->next)->fname) > 0)
{
cnt1++;
cnt2++;
int k;

strcpy(hold,p->fname);
tmp = p->roll;

strcpy(p->fname,(p->next)->fname);
p->roll = (p->next)->roll;

strcpy((p->next)->fname,hold);
(p->next)->roll = tmp;

}
else
cnt1++;

// sorting procedure ends

int main() {

ifstream inFile;
ofstream outFile;
inFile.open("test.txt");
outFile.open("final.txt");
if (!inFile) {
cout << "Unable to open file";
exit(1); // terminate with error
}
//count=0;
int y;
node* head;
node* tmphead;
head=(node *)malloc(sizeof(node));
tmphead= head;
inFile >> x;
bool d;
do
{
int i;
for(i =0;x[i]!='\0';i++)
{
head->fname[i] = x[i];
}
head->fname[i]='\0';

inFile >> y;
head->roll = y;

head->next = (node *)malloc(sizeof(node));


d=inFile >> x;
if(d)
head = head->next;
else
head->next = NULL;

}while (d);

head = NULL;
print(tmphead);
cnt1=0;
cnt2=0;
sort(tmphead);

printf("\n\n");
print(tmphead);
printf("\n\n");
inFile.close();
printf("\n\n\n");

while(1)
{
if(tmphead->next != NULL)
{
char ch1[20];
strcpy(ch1,tmphead->fname);
outFile << ch1 << " ";

int num;
num = tmphead->roll;
outFile << num << endl;

tmphead = tmphead->next;

}
else
{
outFile << tmphead->fname;
break;

}
}

printf("\n\n\nno of comparision and swapping is %d & %d


\n\n",cnt1,cnt2);
system("pause");
return 0;
}
4. GRAPH REPRESENTATION USING LINKED LIST

#include<stdio.h>

struct edge;

struct node
{

struct node *next;


char name;

struct edge *adj;


}*start = NULL;

struct edge
{
char dest;

struct edge *link;


};

struct node *find( char item );


char node, origin, destin;

void insert_node( char node_name )


{

struct node * tmp, *ptr;

tmp = malloc( sizeof( struct node ) );


tmp->name = node_name;
tmp->next = NULL;
tmp->adj = NULL;

if ( start == NULL )
{
start = tmp;
return ;
}

ptr = start;

while ( ptr->next != NULL )


ptr = ptr->next;

ptr->next = tmp;
} /*End of insert_node()*/

insert_edge( char u, char v )


{

struct node * locu, *locv;

struct edge *ptr, *tmp;


locu = find( u );
locv = find( v );

if ( locu == NULL )
{
printf( "Source node not present ,first insert node %c\n", u
);
return ;
}

if ( locv == NULL )
{
printf( "Destination node not present ,first insert node %c\n",
v );
return ;
}

tmp = malloc( sizeof( struct edge ) );


tmp->dest = v;
tmp->link = NULL;
if ( locu->adj == NULL ) /* item added at the begining */
{
locu->adj = tmp;
return ;
}

ptr = locu->adj;

while ( ptr->link != NULL )


ptr = ptr->link;

ptr->link = tmp;

} /*End of insert_edge()*/

struct node *find( char item )


{

struct node *ptr, *loc;


ptr = start;

while ( ptr != NULL )


{
if ( item == ptr->name )
{
loc = ptr;
return loc;
}
else
ptr = ptr->next;
}

loc = NULL;
return loc;
} /*End of find()*/
display()
{

struct node * ptr;

struct edge *q;

ptr = start;

while ( ptr != NULL )


{
printf( "%c ->", ptr->name );
q = ptr->adj;

while ( q != NULL )
{
printf( " %c", q->dest );
q = q->link;
}

printf( "\n" );
ptr = ptr->next;
}
} /*End of display()*/

create_graph()
{
printf("\n ENTER THE NODES \n");

while(1)
{
printf( "Enter a node : " );
fflush( stdin );
scanf( "%c", &node );
if(node=='0')
break;
insert_node( node );
}
while(1)
{
printf( "Enter an edge to be inserted : " );
fflush( stdin );
scanf( "%c %c", &origin, &destin );
if(origin == '0'&& destin == '0')
break;
insert_edge( origin, destin );

main()
{
int choice;
char node, origin, destin;
create_graph();
printf("**************************\n");
while ( 1 )
{
printf( "1.Insert a node\n" );
printf( "2.Insert an edge\n" );
printf( "3.Display\n" );
printf( "4.Exit\n" );
printf( "Enter your choice : " );
scanf( "%d", &choice );

switch ( choice )
{

case 1:
printf( "Enter a node to be inserted : " );
fflush( stdin );
scanf( "%c", &node );
insert_node( node );
break;
case 2:
printf( "Enter an edge to be inserted : " );
fflush( stdin );
scanf( "%c %c", &origin, &destin );
insert_edge( origin, destin );
break;

case 3:
display();
break;

case 4:
exit(0);

default:
printf( "Wrong choice\n" );
break;
} /*End of switch*/
} /*End of while*/
} /*End of main()*/
5. IMPLEMENTATION OF BFS AND DFS

#include<iostream>
using namespace std;

void create();
void dfs();
void bfs();

struct node
{
int data,status;
struct node *next;
struct link *adj;
};

struct link
{
struct node *next;
struct link *adj;
};

struct node *start,*p,*q;


struct link *l,*k;

int main()
{
int choice;
create();
while(1)
{
cout<<"\n\n-----------------------";
cout<<"\n1: DFS\n2: BSF\n3: Exit\nEnter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
dfs();
break;
case 2:
bfs();
break;
case 3:
exit(0);
break;
default:
cout<<"\nIncorrect choice!\nRe-enter your choice.";
}
}
return 0;
}

void create()
{
int dat,flag=0;
start=NULL;
cout<<"\nEnter the nodes in the graph(0 to end): ";
while(1)
{
cin>>dat;
if(dat==0)
break;
p=new node;
p->data=dat;
p->status=0;
p->next=NULL;
p->adj=NULL;
if(flag==0)
{
start=p;
q=p;
flag++;
}
else
{
q->next=p;
q=p;
}
}
p=start;
while(p!=NULL)
{
cout<<"Enter the links to "<<p->data<<" (0 to end) : ";
flag=0;
while(1)
{
cin>>dat;
if(dat==0)
break;
k=new link;
k->adj=NULL;
if(flag==0)
{
p->adj=k;
l=k;
flag++;
}
else
{
l->adj=k;
l=k;
}
q=start;
while(q!=NULL)
{
if(q->data==dat)
k->next=q;
q=q->next;
}
}
p=p->next;
}
cout<<"\n\n-------------------------";
return;
}
void bfs()
{
int qu[20],i=1,j=0;
p=start;
while(p!=NULL)
{
p->status=0;
p=p->next;
}
p=start;
qu[0]=p->data;
p->status=1;
while(1)
{
if(qu[j]==0)
break;
p=start;
while(p!=NULL)
{
if(p->data==qu[j])
break;
p=p->next;
}
k=p->adj;
while(k!=NULL)
{
q=k->next;
if(q->status==0)
{
qu[i]=q->data;
q->status=1;
qu[i+1]=0;
i++;
}
k=k->adj;
}
j++;
}
j=0;
cout<<"\n\nBreadth First Search Results\n";
cout<<"\n---------------------------\n";
while(qu[j]!=0)
{
cout<<qu[j]<<" ";
j++;
}
return;
}

void dfs()
{
int stack[25],top=1;
cout<<"\n\nDepth First Search Results\n";
cout<<"\n---------------------------\n";
p=start;
while(p!=NULL)
{
p->status=0;
p=p->next;
}
p=start;
stack[0]=0;
stack[top]=p->data;
p->status=1;
while(1)
{
if(stack[top]==0)
break;
p=start;
while(p!=NULL)
{
if(p->data==stack[top])
break;
p=p->next;
}
cout<<stack[top]<<" ";
top--;
k=p->adj;
while(k!=NULL)
{
q=k->next;
if(q->status==0)
{
top++;
stack[top]=q->data;
q->status=1;
}
k=k->adj;
}
}
return;
}
6. IMPLEMENTING THE CELEBRITY PROBLEM

#include<stdio.h>
#include<stdlib.h>
#define MAX 20

int adj[MAX][MAX];
int n;

void create_graph()
{
int i,j,max_edges,origin,destin;

printf("Enter number of person : ");


scanf("%d",&n);
max_edges=n*(n-1);

for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
adj[i][j]=0;;
}

for(i=1;i<=max_edges;i++)
{
printf("Enter who knows whoom(0 0 to quit) : ",i);
scanf("%d %d",&origin,&destin);
if((origin==0) && (destin==0))
break;
if( origin > n || destin > n || origin<=0 || destin<=0)
{
printf("Invalid edge!\n");
i--;
}
else
adj[origin][destin]=1;
}/*End of for*/
}/*End of create_graph()*/

void show_adj_matrix()
{
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%3d",adj[i][j]);
printf("\n");
}

}/*End of display()*/

void celebrity()
{
int i,j,c,cmp;
cmp=0;
for(i=1;i<=n;i++)
{
c=0;
for(j=1;j<=n;j++)
{
cmp++;
if(adj[i][j]==0)
c++;
}
if(c==n)
{
printf("%d person is a celebrity\nno of person asked is
%d\n",i,cmp);
break;
}
}
}
int main()
{
int count;
create_graph();
printf("The adjacency matrix is :\n");
show_adj_matrix();
celebrity();
system("pause");
return 0;
}

You might also like