Showing posts with label Data structure. Show all posts
Showing posts with label Data structure. Show all posts

Monday, August 27, 2007

PREFIX TO INFIX in C

PREFIX TO INFIX in C

#include<stdio.h>
struct stack
{
char data[7][16];
int tos;
}s;


main()
{
char prefix[20];
clrscr();
printf("PLEASE ENTER THE PREFIX EXPRESSION:");
scanf("%s",prefix);
preinf(prefix);
getch();
}

preinf(char prefix[])
{
int length,i,len;
char str1[20];
length=strlen(prefix);
for(i=length;i>=0;i--)
{
switch(prefix[i])
{
case '+':
case '-':
case '*':
case '/':
case '^':
{
str1[0]='(';
str1[1]='\0';
strcat(str1,pop());
len=strlen(str1);
str1[len]=prefix[i];
str1[++len]='\0';
strcat(str1,pop());
len=strlen(str1);
str1[len]=')';
str1[++len]='\0';
push(str1);
break;
}
default:
{
str1[0]=prefix[i];
str1[1]='\0';
push(str1);
}
}
}
printf("\nTHE INFIX EXPRESSION IS:%s",pop());
}

push(char string[])
{
strcpy(s.data[s.tos],string);
s.tos++;
}

pop()
{
s.tos--;
return(s.data[s.tos]);
}

PROGRAMME TO CONVERT INFIX EXPRESSION TO POSTFIX

/* PROGRAMME TO CONVERT INFIX EXPRESSION TO POSTFIX
EXPRESSION */


#include<stdio.h>
# define size 20
struct stk
{
char ele[size];
int tos;
};
typedef struct stk stack;
stack s;
main()
{
int len;
char infix[20],ch;
clrscr();
printf("PLEASE ENTER INFIX EMPRESSION:");
scanf("%s",infix);
len=strlen(infix);
infix[len]=')';
infix[++len]='\0';
printf("\n THE POSTFIX EXPRESSION IS:");
infpre(infix);
getch();
}

infpre(char infix[20])
{
int length,i,a,b;
char ch;
s.tos=0;
s.ele[s.tos]='(';
length=strlen(infix);
for(i=0;i<length;i++)
{
switch(infix[i])
{
case '*':
case '+':
case '-':
case '/':
case '^':
case '(':
case '$':
{
a=isp(s.ele[s.tos]);
b=icp(infix[i]);
if(a>=b)
{
ch=pop();
printf("%c",ch);
s.tos++;
push(infix[i]);
}
else
{
s.tos++;
push(infix[i]);

}
break;
}
case ')':
{
ch=pop();
while(ch!='(')
{
printf("%c",ch);
ch=pop();
}
break;
}
default:
printf("%c",infix[i]);
}}}

push(char ch)
{
s.ele[s.tos]=ch;
}

pop()
{
char ch;
ch=s.ele[s.tos];
s.tos--;
return(ch);
}

int isp(char a)
{
switch(a)
{
case '^':return(3);
case '*':
case '/':return(2);
case '+':
case '-':return(1);
case '(':return(0);
case '$':return(5);
}}

int icp(char a)
{
switch(a)
{
case '^':return(4);
case '*':
case '/':return(2);
case '+':
case '-':return(1);
case ')':return(4);
case '$':return(5);
}}

PREFIX TO POSTFIX in C

/* PREFIX TO POSTFIX in C*/

#include<stdio.h>
#include<string.h>
#define size 10
char ch;
struct stk
{
char ele[size];
int top;
}s;
main()
{
int length;
char infix[20];
clrscr();
printf("PLEASE ENTER THE PREFIX EXPRESSION:");
scanf("%s",infix);
length=strlen(infix);
infix[length]=')';
infix[++length]='\0';
printf("\nTHE POSTFIX MESSAGE IS:");
infpost(infix);
getch();
}
infpost(char infix[])
{
int a,b,length,i,count=0;
s.top=0;
s.ele[s.top]='(';
s.top++;
length=strlen(infix);
for(i=0;i<length;i++)
{
switch(infix[i])
{
case '*':
case '^':
case '/':
case '+':
case '-':
case '(':
{
a=isp(s.ele[s.top]);
b=icp(infix[i]);
/*printf("\nisp=%d icp=%d",a,b);
getch();*/
if(a>=b)
{
ch=pop();
printf("%c",ch);
s.top++;
push(infix[i]);
if(i==10)
break;
if(s.top==2)
{
s.top--;
ch=pop();
printf("%c",ch);
s.ele[1]=s.ele[2];
s.top=1;
}
}
else
{
if(count!=0) s.top++;
push(infix[i]);
count++;
break;
}}
case ')':
{
ch=pop();
while(ch!='(')
{
printf("%c",ch);
ch=pop();
}
break;
}
default:
printf("%c",infix[i]);
} } }


int push(char ch)
{
if(s.top==size-1)
{
printf("stack full \n");
return(0);
}
s.ele[s.top]=ch;
return(1);
}
int pop()
{
if(s.ele[s.top]==-1)
{
printf("stack empty");
return(0);
}
ch=s.ele[s.top];
s.top--;
return(ch);
}
int isp(char a)
{
switch(a)
{
case '^': return(3);
case '*':
case '/': return(2);
case '+':
case '-':return(1);
case '(':return(0);
}
}

int icp(char a)
{
switch(a)
{
case '^': return(4);
case '*':
case '/': return(2);
case '+':
case '-':return(1);
case ')':return(4);
}
}

PROGRAMME TO CONVERT INFIX TO PREFIX

/* PROGRAMME TO CONVERT INFIX TO PREFIX */


#include<stdio.h>
# define size 20
struct stk
{
char ele[size];
int tos;
};
typedef struct stk stack;
stack s;
main()
{
int len;
char infix[20],ch;
clrscr();
printf("PLEASE ENTER INFIX EMPRESSION:");
scanf("%s",infix);
len=strlen(infix);
infix[len]=')';
infix[++len]='\0';
printf("\n THE PREFIX EXPRESSION IS:");
infpre(infix);
getch();
}

infpre(char infix[20])
{
int length,i,a,b,len;
char ch,prefix[20],temp[20];
prefix[0]='\0';
s.tos=0;
s.ele[s.tos]='(';
length=strlen(infix);
for(i=0;i<length;i++)
{
switch(infix[i])
{
case '*':
case '+':
case '-':
case '/':
case '^':
case '(':
{
a=isp(s.ele[s.tos]);
b=icp(infix[i]);
if(a>=b)
{
ch=pop();
temp[0]=ch;
temp[1]='\0';
strcat(temp,prefix);
strcpy(prefix,temp);
s.tos++;
push(infix[i]);
}
else
{
s.tos++;
push(infix[i]);
}
break;
}
case ')':
{
ch=pop();
while(ch!='(')
{
temp[0]=ch;
temp[1]='\0';
strcat(temp,prefix);
strcpy(prefix,temp);
ch=pop();
}
break;
}
default:
{
len=strlen(prefix);
prefix[len]=infix[i];
prefix[++len]='\0';
}
}}
printf("%s",temp);
}

push(char ch)
{
s.ele[s.tos]=ch;
}

pop()
{
char ch;
ch=s.ele[s.tos];
s.tos--;
return(ch);
}

int isp(char a)
{
switch(a)
{
case '^':return(3);
case '*':
case '/':return(2);
case '+':
case '-':return(1);
case '(':return(0);
}}

int icp(char a)
{
switch(a)
{
case '^':return(4);
case '*':
case '/':return(2);
case '+':
case '-':return(1);
case ')':return(4);
}}

FORMING A TREE USING PREFIX EXPRESSION

/* FORMING A TREE USING PREFIX EXPRESSION AND THEN PREFORM
ALL THE TRAVERSALS(RECURSIVE) */


#include<stdio.h>
#include<ctype.h>
struct tree
{
char data;
struct tree *left,*right;
};
typedef struct tree btree;
char prefix[20];
btree *stack[20];
int top;
main()
{
btree *create(void);
void inorder(btree *temp);
void preorder(btree *temp);
void postorder(btree *temp);
void push(btree *temp);
btree *pop(void);
btree *root;
clrscr();
printf("PLEASE ENTER THE PREFIX EXPRESSION:");
gets(prefix);
root=create();
printf("\nTHE INORDER TRAVERSAL IS:");
inorder(root);
printf("\nTHE PREORDER TRAVERSAL IS:");
preorder(root);
printf("\nTHE POSTORDER TRAVERSAL IS:");
postorder(root);
getch();
}

btree *create(void)
{
btree *head,*temp,*temp1;
int i=0,n;
top=-1;
head=(btree*)malloc(sizeof(btree));
temp=head;
temp->data=prefix[i];
i++;
temp->right=temp->left=NULL;
push(temp);
n=strlen(prefix);
while(top!=-1)
{
while(!isalnum(prefix[i]))
{
temp=pop();
temp1=(btree*)malloc(sizeof(btree));
temp1->right=temp1->left=NULL;
temp->left=temp1;
temp1->data=prefix[i];
i++;
push(temp);
push(temp1);
}
temp1=(btree*)malloc(sizeof(btree));
temp=pop();
temp->left=temp1;
temp1->data=prefix[i];
temp1->left=temp1->right=NULL;
push(temp);
do {
temp=pop();
i++;
temp1=(btree*)malloc(sizeof(btree));
temp->right=temp1;
temp1->data=prefix[i];
temp1->right=temp1->left=NULL;
if(!isalpha(prefix[i]))
{i++;
push(temp1);
break;}
}while(top!=-1);
}
return(head);
}

void push(btree *temp)
{
top++;
stack[top]=temp;
return;
}
btree *pop(void)
{
return(stack[top--]);
}

void inorder(btree *temp)
{
if(temp)
{
inorder(temp->left);
putch(temp->data);
inorder(temp->right);
}
return;
}

void preorder(btree *temp)
{
if(temp)
{
putch(temp->data);
preorder(temp->left);
preorder(temp->right);
}
return;
}

void postorder(btree *temp)
{
if(temp)
{
postorder(temp->left);
postorder(temp->right);
putch(temp->data);
}
return;
}

PROGRAM TO SWAP TWO NUMBERS WITHOUT USING A THIRD VARIABLE IN C PROGRAMMING

PROGRAM TO SWAP TWO NUMBERS WITHOUT USING A THIRD VARIABLE IN C PROGRAMMING

This eveyone computer science student must have encountered.

void main()
{
int a,b;
printf("PLEASE ENTER THE TWO NUMBERS:");
scanf("%d %d",&a,&b);
/*little bit of maths*/
a=a+b;
b=a-b;
a=a-b;
printf("THE SWAPPED NUMBERS ARE:");
printf("%d %d",a,b);
getch();
}

Stack implementation using Array - c program (stack operation)

Stack implementation using Array - c program (stack operation)
===================================================


#include <stdio.h>
#include <conio.h>
#define size 10

int top = -1;
int stack[size];
int flag = 0;

void push (int *,int);
int pop (int *);
int peep (int *,int);
int update (int *,int);
void display (int *);

void main ()
{
int info,popped_element,position,data,pos_update,update_element,update_data;
char ch;

clrscr ();

do
{
printf("\n\ni<-Insert(push), d<-Delete(pop),p<-peep,u<-update, q<-Quit\n");
fflush (stdin);
printf("\n\nEnter your choice: ");
scanf("%c",&ch);
switch (ch)
{
case 'i':
{
printf("\n\nEnter the element you want to push: ");
scanf("%d",&info);
push (stack,info);
display (stack);
break;
}

case 'd':
{
popped_element = pop(stack);

if (flag == 0)
printf("\n\nThe element popped is %d",popped_element);
if (flag == 1)
printf("\nStack Underflow");
display (stack);
break;
}

case 'p':
{
printf("\n\nEnter the position of the stack from which you want to peep the element: ");
scanf("%d",&position);
data = peep(stack,position);
if (flag == 1)
printf("\nInvalid choice\n");
else
printf("The element peeped is %d",data);
display (stack);
break;
}

case 'u':
{
printf("Enter the position of the stack from which you want to update the data: ");
scanf("%d",&pos_update);
update_element = update(stack,pos_update);
if (flag == 1)
printf("Data can't be updated due to some reason");
else
printf("The data updated is %d",update_element);
display (stack);
break;
}

case 'q':
exit(0);
}
}while (ch != 'q');

getch ();
}

void push (int stack[],int info)
{
if (top == size-1)
printf("\nStack Overflow");
else
{
top++;
stack[top] = info;
}
}

int pop (int stack[])
{
int popped_element;
if (top == -1)
flag = 1;
else
{
flag = 0;
popped_element = stack[top];
top--;
}
return (popped_element);
}

void display (int stack[])
{
int i;
printf("\n\nThe contents of the stack are:\n");
for (i = top ; i >= 0 ; i--)
printf("%d ",stack[i]);
}

int peep (int stack[],int i)
{
int data;
if (top - i + 1 < 0)
flag = 1;
else
{
flag = 0;
data = stack[top-i+1];
}
return (data);
}

int update(int stack[],int pos)
{
int old_data;
if (top - pos + 1 < 0)
flag = 1;
else
{
flag = 0;
old_data = stack[top-pos+1];
printf("Enter the data you want to insert: ");
scanf("%d",&stack[top-pos+1]);
}
return (old_data);
}


Stack implementation using link list - c program
==============================

#include <stdio.h>
#include <conio.h>
struct Stack
{
int info;
struct Stack *next;
};
typedef struct Stack node;
node *first = NULL;
node *push (node *);
node *pop (node *);
void display (node *);

void main ()
{
char ch;

clrscr ();

do
{
fflush (stdin);
printf("\n\ni<-insert(push), d<-delete(pop), q<-quit\n");
printf("\n\nEnter the choice: ");
scanf("%c",&ch);
switch (ch)
{
case 'i':
{
first = push (first);
printf("\n\nAfter the push operation the stack is as follows:\n");
display (first);
break;
}

case 'd':
{
first = pop (first);
printf("\n\nAfter the pop operation the stack is as follows:\n");
display (first);
break;
}

case 'q':
exit (0);
}
}while (ch != 'q');

getch ();
}

node *push(node *first)
{
node *data;
data = (node *)malloc(sizeof(node));
printf("\nEnter the data to be inserted: ");
scanf("%d",&data->info);
data->next = first;
first = data;
return (first);
}

node *pop (node *first)
{
node *temp;
if (first == NULL)
printf("\n\nSTACK IS EMPTY\n");
else
{
temp = (node *)malloc(sizeof(node));
temp = first;
first = first->next;
printf("\nThe element deleted is %d",temp->info);
free (temp);
}
return (first);
}

void display (node *first)
{
while (first != NULL)
{
printf("%d ",first->info);
first = first->next;
}
}

PROGRAMME TO REVERSE A DOUBLY LINKED LINK LIST

/* PROGRAMME TO REVERSE A DOUBLY LINKED LINK LIST */


#include<stdio.h>
struct llist
{
int val;
struct llist *next,*prev;
};
typedef struct llist list;

main()
{
list *create(void);
void reverse(list *temp);
list *root;
clrscr();
printf("PLEASE ENTER THE LIST,ENTER -99 TO END:");
root=create();
reverse(root);
getch();
}

list *create(void)
{
list *head,*temp,*temp1;
int data;
head=(list*)malloc(sizeof(list));
temp=head;
head->prev=NULL;
do
{
scanf("%d",&data);
if(data==-99)
return(head);
temp1=(list*)malloc(sizeof(list));
temp1->val=data;
temp->next=temp1;
temp1->prev=temp;
temp1->next=NULL;
temp=temp1;
}while(1);
}

void reverse(list *head)
{
list *t1,*t2,*t3;
t1=head->next;
t2=t1->next;
t3=head;
do
{
t1->next=t3;
t1->prev=NULL;
head->next=t2;
t3->prev=t1;
t3=t1;
t1=t2;
t2=t2->next;
}while(t1!=NULL);
printf("\nTHE REVERSED LIST IS:");
t1=t3;
while(t1!=head)
{
printf(" %d",t1->val);
t1=t1->next;
}
return;
}

Link list programs in C (single, doubly and circular link list)

Link list programs in C (single, doubly and circular link list)
--------------------------------------------------------------------------------------



Singly link list
=================


#include <stdio.h>
#include <malloc.h>
#include <conio.h>

struct list
{
int info;
struct list *next;
};
typedef struct list node;

void main ()
{
node *first;
int choice;
node *create (node *);
node *insert (node *);
node *del (node *);
void display (node *);

clrscr ();

while (1)
{
printf("\n\nYou have following choices:\n");
printf("1. Create List\n");
printf("2. Insert a node in the List\n");
printf("3. Delete a node from the List\n");
printf("4. Display the List\n");
printf("5. Termenate the Program\n");
printf("Enter your choice: ");
scanf ("%d",&choice);
switch (choice)
{
case 1:
first = create(first);
break;
case 2:
first = insert (first);
break;
case 3:
first = del (first);
break;
case 4:
display (first);
break;
case 5:
exit (0);
}
}
}

node *create (node *first)
{
node *new1 = NULL,*l;
int i;
first = NULL;
printf("\n\nTHE INPUT IS:\n");
printf("-------------\n\n");
while(1)
{
printf("\nEnter -1 to break.....\n");
printf("Enter the node you want to enter in ascending order: ");
scanf("%d",&i);

if (i == -1)
break;

else
{
new1 = ((node *) malloc (sizeof (node)));
new1 -> info = i;
if(first == NULL)
{
first = l = new1;
}
else
{
l -> next = new1;
l = new1;
}
l -> next = NULL;
}
}
return (first);
}

void display (node *first)
{
printf("\n\nTHE OUTPUT IS:\n");
printf("--------------\n\n");
while (first != NULL)
{
printf("%d -> ",first -> info);
first = first -> next;
}
printf("NULL");
}

node *insert (node *first)
{
node *l, *new1;
l = first;
new1 = (node *) malloc (sizeof (node *));
printf("Enter the node: ");
scanf("%d",&new1 -> info);
if (first == NULL)
{
first = new1;
first -> next = NULL;
}
else
{
if (new1 -> info < first -> info)
{
new1 -> next = first;
first = new1;
}
else
{
while (l -> next -> info < new1 -> info && l -> next != NULL)
l = l -> next;
new1 -> next = l -> next;
l -> next = new1;
l = new1;
}
}
display (first);
return (first);
}

node *del (node *first)
{
int n;
node *l = first, *new1, *temp;
printf("Enter the information: ");
scanf("%d",&n);
if (first == NULL)
printf("\nLIST IS EMPTY\n");
else
{
if (first -> info == n)
{
temp = first;
first = first -> next;
free (temp);
}
else
{
while (l -> next -> info != n && l -> next != NULL)
l = l -> next;
if (l -> next == NULL)
printf("\nINFORMATION IS NOT PRESENT\n");
else
{
temp = l -> next;
l -> next = l -> next -> next;
free(temp);
}
}
}
display (first);
return (first);
}

Doubly Link List
===============

#include <stdio.h>
#include <alloc.h>
#include <conio.h>

struct Double
{
int info;
struct Double *next;
struct Double *previous;
};
typedef struct Double node;

node *create(node *);
node *insert(node *);
node *del(node *);
void display (node *);

node *first = NULL;

void main ()
{
int choice;

clrscr ();

while (1)
{
printf("\n\nYou have following choices:\n");
printf("1. Create List\n");
printf("2. Insert a node in the List\n");
printf("3. Delete a node from the List\n");
printf("4. Display the List\n");
printf("5. Termenate the Program\n");
printf("Enter your choice: ");
scanf ("%d",&choice);
switch (choice)
{
case 1:
first = create(first);
break;
case 2:
first = insert (first);
break;
case 3:
first = del (first);
break;
case 4:
display (first);
break;
case 5:
exit (0);
}
}
}

node *create (node *first)
{
node *new1,*l = NULL;
int i;
//first -> next = first -> previous = NULL;
while (1)
{
printf("Enter the information(-1 to Exit): ");
scanf("%d",&i);
if (i == -1)
break;
new1 = (node *)malloc(sizeof(node));
new1 -> info = i;
if (first == NULL)
{
first = l = new1;
first -> next = first -> previous = NULL;
}
else
{
new1 -> previous = l;
l -> next = new1;
l = new1;
}
l -> next = NULL;
}
return (first);
}

void display (node *first)
{
node *l;
l = first;
printf("\n\n\t\tDOUBLY LINK LIST:\n");
printf("\t\t-----------------\n");
printf("NULL");
for (l = first ; l != NULL ; l = l -> next)
printf(" <- %d -> ",l -> info);
printf("NULL");
}

node *insert (node *first)
{
node *new1,*l=NULL;
new1 = (node *)malloc(sizeof(node));
printf("\n\nEnter the node you want to insert: ");
scanf("%d",&new1->info);
if (first == NULL)
{
printf("Empty list. New list will be created.");
first = new1;
first->next = NULL;
first->previous = NULL;
}
else
{
if (new1->info < first->info)
{
new1->next = first;
new1->previous = NULL;
first->previous = new1;
first = new1;
}
else
{
l = first;
while (l->next->info < new1->info && l->next != NULL)
l = l->next;
new1->next = l->next;
l->next = new1;
new1->previous = l;
l = new1;
}
}
display (first);
return (first);
}

node *del (node *first)
{
int del_info;
node *l=NULL,*temp;
printf("\n\nEnter the information to be deleted: ");
scanf("%d",&del_info);
if (first == NULL)
printf("The List is Empty.");
else
{
if (first->info == del_info)
{
temp = first;
first = first->next;
first->previous = NULL;
free(temp);
}
else
{
l = first;
while (l->info < del_info && l->next != NULL)
l = l->next;
l->previous->next = l->next;
l->next->previous = l->previous;
}
}
display (first);
return (first);
}

Circular link list
=================


#include <stdio.h>
#include <malloc.h>
#include <conio.h>

///////////////////////STRUCTURE DEFINITION////////////////////////////////
struct cir_link_list
{
int info;
struct cir_link_list *next;
};
typedef struct cir_link_list node;

///////////////////////VARIABLE DECLARATION////////////////////////////////
node *first=NULL;
int info;

///////////////////////FUNCTION DECLARATION////////////////////////////////
node *create (node *);
void display (node *);
node *insert (node *);
node *del (node *);
int count = 0;

///////////////////////VOID MAIN () DEFINITION/////////////////////////////
void main ()
{
int choice;

clrscr ();

while (1)
{
printf("\n\nYou have following choices:\n");
printf("1. Create List\n");
printf("2. Insert a node in the List\n");
printf("3. Delete a node from the List\n");
printf("4. Display the List\n");
printf("5. Termenate the Program\n");
printf("Enter your choice: ");
scanf ("%d",&choice);
switch (choice)
{
case 1:
first = create(first);
break;
case 2:
first = insert (first);
break;
case 3:
first = del (first);
break;
case 4:
display (first);
break;
case 5:
exit (0);
}
}
}

///////////////////////CREATION OF CIRCULAR LINK LIST//////////////////////
node *create (node *first)
{
int i;
node *new1,*l=NULL;

while (1)
{
printf("Enter -1 to break........\n");
printf("Enter the node: ");
scanf("%d",&i);
if (i == -1)
{
l->next=first;
break;
}
else
{
new1 = ((node *)malloc(sizeof(node)));
new1 -> info = i;
if (first == NULL)
{
first = l = new1;
}
else
{
l->next=new1;
l=new1;
}
}
count++;
}
return (first);
}

///////////////////////DISPLAY OF CIRCULAR LINK LIST///////////////////////
void display (node *first)
{
int i;
for (i=0 ; i< count ; i++)
{
printf("%d -> ",first->info);
first = first -> next;
}
}

node *insert (node *first)
{
int i = 0;
node *l, *new1;
l = first;
new1 = (node *) malloc (sizeof (node *));
printf("\nEnter the node: ");
scanf("%d",&new1 -> info);
if (first == NULL)
{
first = new1;
first -> next = first;
}
else
{
if (new1 -> info < first -> info)
{
new1 -> next = first;
first = new1;
}
else
{
while (l -> next -> info < new1 -> info && l -> next != first && i <= count)
{
l = l -> next;
i++;
}
if (l -> next == first i > count)
{
new1 -> next = l -> next;
l -> next = new1;
l = new1;
}
new1 -> next = l -> next;
l -> next = new1;
}
}
count++;
display (first);
return (first);
}

node *del (node *first)
{
int n,i=0;
node *l = first, *new1, *temp;
printf("\nEnter the information: ");
scanf("%d",&n);
if (first == NULL)
printf("\nLIST IS EMPTY\n");
else
{
if (first -> info == n)
{
temp = first;
first = first -> next;
free (temp);
}
else
{
while (l -> next -> info != n && l -> next != first && i <= count)
{
l = l -> next;
i++;
}
if (l -> next == first i > count)
{
printf("\nINFORMATION IS NOT PRESENT\n");
goto jmp;
}
else
{
temp = l -> next;
l -> next = l -> next -> next;
free(temp);
}
}
}
count --;
jmp:
display (first);
return (first);
}

insert and delete queue in C++ (queue operation)

insert and delete queue in C++ (queue operation)
=======================================


# include<iostream.h>
# include<conio.h>
# include<string.h>
# include<ctype.h>
# include<process.h>
# define size 10
class Queue
{
public: int rear, front;
int ch;
int q[size];

public: Queue()
{
rear = front = 0;
}
void Insert_queue();
void Delete_queue();
void Display_queue();

};

// Function to create queue
void Queue :: Insert_queue()
{
cout<<"\n Input the element :";
cin>>ch;
if(rear < size)
{
rear ++;
q[rear] = ch ;
if(front == 0)
front = 1;
}
else
cout<<"\nOverflow";
}

// Function to perform delete operation
void Queue :: Delete_queue() //char q[], char ch)
{
if (front == 0)
{
cout<<"\nUnderflow";
return ;
}
else
{
ch = q[front];
cout<<"\nElement deleted :"<<ch;
}
if(front == rear)
{
front = 0;
rear = 0;
}
else
front = front + 1;
}

// Output function
void Queue :: Display_queue() //char q[])
{
if (front == 0)
return;
for( int i = front ; i <= rear; i++)
cout<<" "<<q[i];
}

//Function main
void main()
{
Queue Q;
int k = 0;
char choice;

do
{
cout<<"\nInsert->i Delete->d Quit->q:";
cout<<"\nInput the choice : ";
do
{
cin>>choice;
choice = tolower(choice);
}
while(strchr("idq",choice)==NULL);
cout<<"Your choice is ->"<<choice;
switch(choice)
{
case 'i' :
Q.Insert_queue();
cout<<"\nQueue after inserting ";
Q.Display_queue();
break;

case 'd' :
Q.Delete_queue();
cout<<"\nQueue content after deleteion is as follows:\n";
Q.Display_queue();
break;
case 'q':
k = 1;
}
}
while(!k);
}

Circular queue in C - program

Circular queue in C (program)
========================


#include <stdio.h>
#include <conio.h>
#define size 10

int queue[size];
int front = 0;
int rear = 0;
int flag = 0;

int insert (int []);
int del (int []);
void display (int []);

void main ()
{
char ch;
int insert_element,del_element;

clrscr ();

do
{
fflush (stdin);
printf("\n\ni<-insert, d<-delete, q<-quit\n");
printf("\n\nEnter your choice: ");
scanf("%c",&ch);
switch (ch)
{
case 'i':
{
if (flag == 1)
printf("\nOVERFLOW\n");
else
{
insert_element = insert(queue);
printf("\n\nElement inserted is %d",insert_element);
printf("\n\nAfter the insertion the contents of the queue are:\n");
display (queue);
}
break;
}

case 'd':
{
del_element = del (queue);
if (flag == 1)
printf("\nUNDERFLOW\n");
else
{
printf("\n\nThe element deleted is %d",del_element);
printf("\n\nAfter the deletion the contents of the queue are:\n");
display (queue);
}
break;
}

case 'q':
exit (0);
}
}while (ch != 'q');

getch ();
}

int insert (int cq[])
{
int info;
printf("Enter the information you want to insert: ");
scanf("%d",&info);
if (rear == size)
rear = 1;
else
rear = rear + 1;
if (front == rear)
{
flag = 1;
return;
}
cq[rear] = info;
if (front == 0)
front = 1;
return (cq[rear]);
}

int del (int cq[])
{
int info;
if (front == 0)
{
flag = 1;
return;
}
info = cq[front];
if (front == rear)
{
front = rear = 0;
return (info);
}
if (front == size)
front = 1;
else
front = front + 1;
return (info);
}

void display (int cq[])
{
int i;
for (i = front ; i <= rear ; i++)
printf("%d ",cq[i]);
}

Saturday, August 25, 2007

C program inorder preorder postorder traversal

/* Data structure program to create a tree and traverse the tree in
1. preorder
2. inorder
3. postorder */


#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

struct treelist
{
int data;
struct treelist *left;
struct treelist *right;
};
typedef struct treelist tree;

tree *cretree(tree *t,int data);
void preorder(tree *t);
void inorder(tree *t);
void postorder(tree *t);

void main()
{
tree *t = NULL;
int n,a,i,ch;
clrscr();
printf("How many numbers do you wan't to enter : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter number %d :....: ",i+1);
scanf("%d",&a);
t = cretree(t,a);
}
while(1)
{
printf("\nEnter the order of traversal of tree \n");
printf("1. preorder \n");
printf("2. inorder \n");
printf("3. postorder \n");
printf("4. Terminate the program\n");
printf("Enter the choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1 :
preorder(t);
break;
case 2 :
inorder(t);
break;
case 3 :
postorder(t);
break;
case 4 :
printf("Good Bye");
getch();
exit(0);
default :
printf("You have entered wrong choice ");
break;
}
}
}

tree *cretree(tree *t,int x)
{
tree *newnode;
if(t == NULL )
{
newnode = (tree*)malloc(sizeof(tree));
newnode->data = x;
newnode->left = NULL;
newnode->right= NULL;
return(newnode);
}
else if(x < t->data)
{
if(t->left == NULL)
{
newnode = (tree*)malloc(sizeof(tree));
newnode->data = x;
t->left = newnode;
newnode->left = NULL;
newnode->right = NULL;
return(t);
}
else
cretree(t->left,x);
}
else
{
if(t->right == NULL)
{
newnode = (tree*)malloc(sizeof(tree));
newnode->data = x;
t->right = newnode;
newnode->left = NULL;
newnode->right = NULL;
return(t);
}
else
cretree(t->right,x);
}
return(t);
}
void preorder(tree *t)
{
if( t != NULL)
{
printf("Preorder : %d\n",t->data);
}
else
{
printf("Empty Tree");
return;
}
if(t->left != NULL)
preorder(t->left);
if(t->right != NULL)
preorder(t->right);
}


void inorder(tree *t)
{
if( t == NULL)
{
printf("Empty Tree");
return;
}
if(t->left != NULL)
inorder(t->left);

printf("Inorder : %d \n",t->data);

if(t->right != NULL)
inorder(t->right);
}

void postorder(tree *t)
{
if( t == NULL)
{
printf("Empty Tree");
return;
}
if(t->left != NULL)
postorder(t->left);


if(t->right != NULL)
postorder(t->right);

printf("Post Order : %d\n",t->data);
}

Simple Parser in C - operator precedence parser

Simple Parser in C - operator precedence parser

#include <iostream.h>
#include <conio.h>
#include <stdlib.h>

struct node
{
char symbol;
struct node *left;
struct node *right;
};
typedef struct node node;

struct stack
{
char op;
node *op_pointer;
};
typedef struct stack stack;

int TOS=-1;
stack s[10];

/* This is the operator precedence table stored as it is in the
form of matrix and operators are assigned values as follows:
NULL = 0, Equal (=) = 1, less than (<) = 2, greater than (>) = 3. */

// +,*,(,),<>,-,/,^
int m[8][8]={ {3,2,2,3,3,3,2,2}, //+
{3,3,2,3,3,3,2,2}, //*
{2,2,2,1,0,2,2,2}, //(
{3,3,0,3,3,3,3,3}, //)
{2,2,2,0,1,2,2,2}, //><
{3,2,2,3,3,3,2,2}, //-
{3,3,3,3,3,3,3,2}, ///
{3,3,3,3,3,3,3,2}}; //^

void main()
{
clrscr();

int comp(char );
void push(char);
void pop();
void display(node *);
node *temp;

char str[20];
int i=0;

cout << "Enter the string : ";
cin >> str;
push('<');
i++;
while(str[i]!='\0')
{
if((str[i]>='a'&&str[i]<='z'))
{
temp = new node;
temp->symbol=str[i];
temp->left=NULL;
temp->right=NULL;
s[TOS].op_pointer = temp;
}
else
{
int p_index,q_index;
p_index = comp(s[TOS].op);
q_index = comp(str[i]);
while(m[p_index][q_index]==3)
{
temp = new node;
temp->symbol=s[TOS].op;
temp->left=s[TOS-1].op_pointer;
temp->right=s[TOS].op_pointer;
pop();
s[TOS].op_pointer=temp;
p_index = comp(s[TOS].op);
q_index = comp(str[i]);
}

if(m[p_index][q_index]==2)
{
push(str[i]);
}
if(m[p_index][q_index]==1)
{
if(comp(s[TOS].op)==4)
break;
if(comp(s[TOS].op)==2)
{
temp=s[TOS].op_pointer;
pop();
s[TOS].op_pointer=temp;
}
}
if(m[p_index][q_index]==0)
{
cout << "Invalid string.";
getch();
exit(1);
}
}
i++;
}
display(s[TOS].op_pointer);
getch();
}
// function to get the index of the operator that comes in the TOS
// and also for the current operator that come in the string.
int comp(char x)
{
int y;
switch(x)
{
case '+':
y=0;
break;
case '*':
y=1;
break;
case '(':
y=2;
break;
case ')':
y=3;
break;
case '>':
case '<':
y=4;
break;
case '-':
y=5;
break;
case '/':
y=6;
break;
case '^':
y=7;
break;
}
return y;
}

void push(char x)
{
TOS++;
s[TOS].op=x;
}

void pop()
{
TOS--;
}

void display(node *s)
{
if(s==NULL)
return;
if(s->left!=NULL)
display(s->left);
if(s->right!=NULL)
display(s->right);
cout << s->symbol;
}


/*

INPUT:
Enter the string : <b*(c+d)*e/f>
OUTPUT:
bcd+*ef/*

*/

Sorting Method in C - program (bubble sort, insertion sort, linear sort, merge sort, quick sort, selection sort, redix sort, shell sort, heap sort)

Sorting Method in C - program
========================
(bubble sort, insertion sort, linear sort, merge sort, quick sort, selection sort, redix sort, shell sort, heap sort)


Bubble sort
===========


#include <stdio.h>
#include <conio.h>
void main ()
{
int a[20],n,i;
void bubble_sort(int [], int);
void display (int [], int);
clrscr ();
printf("Enter the number of elements you want to sort: ");
scanf("%d",&n);
printf("Enter the numbers\n");
for (i=0;i<n;i++)
scanf("%d",&a[i]);
bubble_sort (a,n);
display (a,n);
getch ();
}

void bubble_sort(int a[], int n)
{
int i,j,temp,exch,l;
l = n;
for (i = 0 ; i < n ; i++)
{
exch = 0;
for (j = 0 ;j < l - 1; j++)
{
if (a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
exch = exch + 1;
}
}
if (exch == 0)
return;
else
l = l-1;
}
}

void display (int a[], int n)
{
int i;
printf("The Output is:\n");
for (i = 0 ; i < n ; i++)
printf("%d ", a[i]);
}


Insertion sort
==============


#include <stdio.h>
#include <conio.h>
void main ()
{
int a[20],n,i;
void insertion_sort (int [], int);
void display (int [], int);
clrscr ();
printf("Enter the no. of elements you want to sort(less than 20): ");
scanf ("%d",&n);
printf("Enter %d elements:\n",n);
for (i = 0; i < n; i++)
scanf("%d",&a[i]);
insertion_sort(a,n);
display (a,n);
getch ();
}

void insertion_sort (int a[], int n)
{
int i, j, temp;
for (i = 1 ; i < n ; i++)
{
if (a[i] < a[i-1])
{
temp = a[i];
for (j = i-1 ; j >= 0 ; j--)
{
a[j+1] = a[j];
if(a[j-1] <= temp j == 0)
break;
}
a[j] = temp;
}
}
}

void display (int a[], int n)
{
int i;
printf("The Output is:\n");
for (i = 0 ; i < n ; i++)
printf("%d ",a[i]);
}


Linear Sort
===========


#include <stdio.h>
#include <conio.h>
void main ()
{
int a[20],n,i;
void linear_sort (int [], int);
void display (int [], int);
clrscr ();
printf("Enter the no. of elements you want to sort(less than 20): ");
scanf ("%d",&n);
printf("Enter %d elements:\n",n);
for (i = 0; i < n; i++)
scanf("%d",&a[i]);
linear_sort(a,n);
display (a,n);
getch ();
}

void linear_sort(int a[], int n)
{
int i, j, temp;
for (i=0; i<n; i++)
{
for (j=i+1; j<n; j++)
{
if(a[j] < a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}

void display (int a[], int n)
{
int i;
printf("The Output is:\n");
for (i=0; i<n; i++)
printf("%d ",a[i]);
}



Merge Sort
===========


#include <stdio.h>
#include <conio.h>
void main ()
{
int n1,a1[20],n2,a2[20],a[40],result[40],i,j,k;

void merge_list(int[],int,int[],int,int[]);
int merge_sort (int[],int,int,int[]);
void display (int[],int);

clrscr ();

printf("\nEnter the number of elements you want to sort in the first list: ");
scanf("%d",&n1);

printf("\nEnter %d elements in the first list in sorted order\n",n1);
for (i=0;i<n1;i++)
scanf("%d",&a1[i]);

printf("\nEnter the number of elements you want to sort in the second list: ");
scanf("%d",&n2);

printf("\nEnter %d elements in the second list in sorted order\n",n2);
for (j=0;j<n2;j++)
scanf("%d",&a2[j]);

merge_list(a1,n1,a2,n2,a);
k = merge_sort(a,n1,n2,result);
display(result,k);

getch ();
}

void merge_list (int a1[],int n1,int a2[],int n2,int a[])
{
int i,j;
for (i=0;i<n1;i++)
a[i] = a1[i];
for (j=0;j<n2;j++)
a[n1+j] = a2[j];
}

int merge_sort (int a[],int n1,int n2,int result[])
{
int first,second,third,i,j,k=0;

first = 0;
second = n1;
third = n1+n2-1;
i = first;
j = second;

while (i<second && j<=third)
{
if (a[i] <= a[j])
{
result[k] = a[i];
i++;
}
else
{
result[k] = a[j];
j++;
}
k++;
}

if (i < second)
{
while (i < second)
{
result[k] = a[i];
i++;
k++;
}
}

if (j <= third)
{
while (j <= third)
{
result[k] = a[j];
j++;
k++;
}
}
return (k);
}

void display (int result[],int k)
{
int i;
printf("The sorted list is as follows:\n");
for (i=0;i<k;i++)
printf(" %d",result[i]);
}


Quick Sort
============

#include <stdio.h>
#include <conio.h>
void main ()
{
int a[20],n,lb,ub,i,j;
void quick_sort (int[],int,int);
void display (int[],int);
clrscr ();
printf("Enter no. of elements you want to sort: ");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for (i=0;i<n;i++)
scanf("%d",&a[i]);
lb = 0;
ub = n-1;
quick_sort (a,lb,ub);
display (a,n);
getch ();
}

void quick_sort (int a[],int lb,int ub)
{
int i,j,key,flag=0,temp;
if (lb < ub)
{
i = lb;
j = ub+1;
key = a[i];
while (flag != 1)
{
i++;
while (a[i] < key)
i++;
j--;
while (a[j] > key)
j--;
if (i < j)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
else
{
flag = 1;
temp = a[lb];
a[lb] = a[j];
a[j] = temp;
}
}
quick_sort (a,lb,j-1);
quick_sort (a,j+1,ub);
}
}

void display (int a[],int n)
{
int i;
printf("The Output is:\n");
for (i=0;i<n;i++)
printf("%d ",a[i]);
}


Selection Sort
==============



#include <stdio.h>
#include <conio.h>
void main ()
{
int a[20],n,i;
void selection_sort (int [],int);
void display (int [],int);
clrscr ();
printf("Enter the no. of elements you want to sort: ");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for (i = 0 ; i < n ; i++)
scanf("%d",&a[i]);
selection_sort (a,n);
display (a,n);
getch ();
}

void selection_sort (int a[], int n)
{
int i,j,min,temp;
for (i = 0 ; i < n ; i++)
{
min = i;
for (j = i+1 ; j < n ; j++)
{
if (a[min] > a[j])
min = j;
}
temp = a[min];
a[min] = a[i];
a[i] = temp;
}
}

void display (int a[], int n)
{
int i;
printf("The Output is:\n");
for (i = 0 ; i < n ; i++)
printf("%d ",a[i]);
}


Redix sort
============


#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<math.h>

void main()
{

int arr[50],temparr[10][20],index[10];
int n,i,j,p,q,temp,m;
clrscr();
printf("Enter how many elements do you want to sort ? :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter element no %d ( don't enter -999 ): ",i+1);
scanf("%d",&arr[i]);
}

printf("The Sorted Elements BY REDIX SORT Are ");
for(i=0;i<10;i++)
{
for(j=0;j<20;j++)
{
temparr[i][j]=-999;
}
}


for( m=1 ; m<=5 ; m++ )
{
for(i=0;i<10;i++)
index[i]=0;
p=0;
for(j=0;j<n;j++)
{
temp = (double) pow( (double)10 , (double)m-1 );
p = (int)( arr[j] / temp ) % 10;
temparr[p][index[p]] = arr[j];
index[p] = index[p] + 1;
}
p=0;
for(i=0;i<10;i++)
{
for(j=0;j<20;j++)
{
if( temparr[i][j] != -999 )
{
arr[p] = temparr[i][j];
p++;
}
temparr[i][j] = -999;
}
}
}
for(i=0;i<n;i++)
printf("\n%d",arr[i]);
getch();
}


Shell Sort
==========


#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<math.h>

void shellsort(int arr[50],int n);
void main()
{

int arr[50];
int n,i,j,p,q,temp,m;
clrscr();
printf("Enter how many elements do you want to sort ? :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter element no %d : ",i+1);
scanf("%d",&arr[i]);
}

printf("The Sorted Elements BY SHELL SORT Are ");
shellsort(arr,n);
for(i=0;i<n;i++)
printf("\n%d",arr[i]);
getch();
}
void shellsort(int arr[50],int n)
{
int k,j,current,sorted,span,temp;
for( span = n/2 ; span>0 ; span = span/2 )
{
for(k=span;k<=n;k++)
{
current = arr[k];
j = k - span;
sorted = 0;
while((j >= 1) && (sorted == 0))
{
if(current < arr[j])
{
temp = arr[j+span];
arr[j+span] = arr[j];
arr[j] = temp;
j = j - span;
}
else
{
sorted = 1;
temp = arr[j+span];
arr[j+span] = current;
current = temp;
}
}
}
}
return;
}



Heap Sort
==========



# include<stdio.h>
# include<conio.h>

void create_heap(int k[],int n)
{
int q,i,key,j,temp,g;

for (q=2;q<n;q++)
{
i=q;
key=k[q];
j=(int) i/2;
while(i>1 && key>k[j])
{
temp=k[i];
k[i]=k[j];
k[j]=temp;
i=j;
j=(int)i/2;
if(j<1)
j=1;
}
k[i]=key;

}
return;
}

void heap_sort(int k[],int n)
{
int q,temp,i,key,j,g;

create_heap(k,n);
printf("\n HEAP CREATED :- ");
for(i=1;i<n;i++)
printf(" %d ",k[i]);
printf("\n");
printf("\n Elements from heap :- ");
for(q=n;q>=2;q--)
{

temp=k[1];
k[1]=k[q];
k[q]=temp;
i=1;
key=k[1];
j=2;

if((j+1)<q)
{
if(k[j+1]>k[j])
j++;
}

while(j<=(q-1) && k[j]>key)
{
k[i]=k[j];
i=j;
j=2*i;
if((j+1)<q)
{
if (k[j+1]>k[j])
j++;
else
if (j>n)
j=n;
}
k[i]=key;
}
printf("\n AT q = %d ELEMENT FROM HEAP IS ==> %d",q,k[q]);
}
return;
}

void main()
{
int k[11]={0,42,23,124,11,5,58,94,1236,99,87},i;

clrscr();
printf("\n BEFORE HEAP SORT :- ");
for(i=1;i<11;i++)
printf(" %d ",k[i]);
heap_sort(k,11);

printf("\n AFTER HEAP SORT :- ");
for(i=2;i<=11;i++)
printf(" %d ",k[i]);
getch();
}


Searching Method in C - Linear Search/Binary Search/Depth first search

Searching Method in C - Linear Search/Binary Search/Depth first search
==========================================================

Linear Search
=============


#include <stdio.h>
#include <conio.h>
void main ()
{
int array[20], n, sea_no, i, j;
int create (int []);
int linear_search (int [], int, int);
clrscr ();
n = create (array);
printf("\nEnter the number you want to search: ");
scanf ("%d",&sea_no);
clrscr ();
i = linear_search (array, n, sea_no);
if (i == 0)
printf ("\nSearch value does not exist");
else
printf("\nThe search value is at location %d\n",i);
printf("\nThe array is as follows:\n");
for (j = 0 ; j < n ; j++)
printf("%d ",array[j]);
getch ();
}

int create (int a[20])
{
int n, i;
printf("\nEnter how many numbers you want to enter(Enter less than 20): ");
scanf("%d",&n);
printf("\nEnter the numbers:\n");
for (i = 0 ; i < n ; i++)
scanf("%d",&a[i]);
return (n);
}

int linear_search (int a[], int n, int s)
{
int i;
for (i = 0 ; i < n ; i++)
{
if(s == a[i])
{
printf("\nSEARCH VALUE FOUND\n");
return (i+1);
}
}
return (0);
}


Binary Search
==============


#include <stdio.h>
#include <conio.h>

void main ()
{
int a[20], n, sea_no;
int create (int []);
void binary_search (int [], int, int);
clrscr ();
n = create(a);
printf("\nEnter the number you want to search: ");
scanf("%d",&sea_no);
clrscr ();
binary_search (a,n,sea_no);
getch ();
}

int create (int a[20])
{
int n, i;
do
{
printf("\nEnter how many numbers you want to enter(Enter less than 20): ");
scanf("%d",&n);
} while (n > 20);
if (n == 0)
{
printf("Invalid number, Press any key to exit\n");
getch ();
printf("Termenating the Program.....");
delay (1500);
exit (1);
}
printf("\nEnter the numbers in ascending order:\n");
for (i = 0 ; i < n ; i++)
scanf("%d",&a[i]);
return (n);
}

void binary_search (int a[], int n, int s)
{
int lb = 0,ub = n-1,mid, flag = 1, i;
while (lb <= ub)
{
mid = (lb + ub)/2;
if (s < a[mid])
ub = mid - 1;
if (s > a[mid])
lb = mid + 1;
if (s == a[mid])
{
flag = 0;
printf("\nSEARCH FOUND\n");
printf("\nThe number you want to search is at location %d\n",mid + 1);
printf("\nThe elements are as follows:\n");
for (i = 0 ; i < n ; i++)
printf("%d ",a[i]);
break;
}
}
if (flag == 1)
{
printf("\nSearch element does not found");
}
}


Depth first search
===============



#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>

#define size 20
#define T 1
#define F 0

struct Edge
{
int terminal;
struct Edge *next;
};
struct Vertex
{
int visit;
int vertexno;
char info;
int path_length;
struct Edge *edgeptr;
};
void table(int vertexno,int matrix[size][size],struct Vertex vert[size]);
struct Edge *insert_vertex(int vertexno,struct Edge * first);
void dfs(int index,int *dist,struct Vertex vert[size]);
void output(int number,int a[size][size]);
void input(int number,int a[size][size]);

struct Edge *insert_vertex(int vertexno, struct Edge *first)
{
struct Edge * newl,*current;
newl = (struct Edge *)malloc(sizeof(struct Edge));
newl->terminal = vertexno;
newl->next = NULL;
if(first)
return(newl);
for(current = first; current->next;current=current->next)
current->next = newl;
return(first);
}
void table(int vertexno,int matrix[size][size],struct Vertex vert[size])
{
int i,j;
for(i=0;i<vertexno;i++)
{
vert[i].visit = F;
vert[i].vertexno = i+1;
vert[i].info = 'A' + i;
vert[i].edgeptr = NULL;
}
for(i=0;i<vertexno;i++)
for(j=0;j<vertexno;j++)
if(matrix[i][j] > 0)
vert[i].edgeptr = insert_vertex(j,vert[i].edgeptr);
}
void dfs(int index,int *dist,struct Vertex vert[size])
{
struct Edge *link;
vert[index].visit = T;
vert[index].path_length = *dist;
*dist += 1;
for(link = vert[index].edgeptr;link;link = link->next)
if(vert[link->terminal].visit == F)
dfs(link->terminal,dist,vert);
}
void input(int number,int a[size][size])
{
int i,j;
printf("\n Input the adjacency matrix \n");
for(i=0;i<number;i++)
{
for(j=0;j<number;j++)
{
scanf("%d",&a[i][j]);
}
printf("\n");
}
}
void output(int number,int a[size][size])
{
int i,j;
printf("\n Input the adjacency matrix \n");
for(i=0;i<number;i++)
{
for(j=0;j<number;j++)
{
printf("%d",&a[i][j]);
}
printf("\n");
}
}

void main()
{
int i;
int number,index,dist;
int a[size][size];
struct Vertex vert[size];
struct Edge *list;
clrscr();
printf("\nInput the number of vertices in the graph : ");
scanf("%d",&number);
input(number,a);
output(number,a);
table(number,a,vert);
printf("\nInput the starting vertex %d : ",number-1);
scanf("%d",&index);
dist = 0;
dfs(index,&dist,vert);
printf("\nPath length of the vertex from %c ",vert[index].info);
printf("\nVertex Length Vertex Connectivity : \n ");
for(i=0;i<number;i++)
{
printf("\n %c %d",vert[i].info,vert[i].path_length);
for(list=vert[i].edgeptr;list;list=list->next)
{
printf(" ");
putchar(list->terminal+'A');
}
}
getch();
}