Sei sulla pagina 1di 32

Program Code ://Program No.

8 Stack Implementation using Array


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define size 5
struct stack {
int s[size];
int top;
} st;
int stfull() {
if (st.top >= size - 1)
return 1; #include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define size 5
struct stack {
int s[size];
int top;
} st;
int stfull() {
if (st.top >= size - 1)
return 1;
else
return 0;
}
void push(int item) {
st.top++;
st.s[st.top] = item;
}
int stempty() {
if (st.top == -1)
return 1;
else
return 0;
}

int pop() {
int item;
item = st.s[st.top];
st.top--;
return (item);
}
void display() {
int i;
if (stempty())
printf("\nStack Is Empty!");
else {
for (i = st.top; i >= 0; i--)
printf("\n%d", st.s[i]);
}
}
void main() {
int item, choice;
char ans;
st.top = -1;
printf("\n\tImplementation Of Stack");
do {
printf("\nMain Menu");
printf("\n1.Push \n2.Pop \n3.Display \n4.exit");
printf("\nEnter Your Choice");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("\nEnter The item to be pushed");
scanf("%d", &item);
if (stfull())
printf("\nStack is Full!");
else
push(item);
break;
case 2:
if (stempty())
printf("\nEmpty stack!Underflow !!");
else {
item = pop();

printf("\nThe popped element is %d", item);


}
break;
case 3:
display();
break;
case 4:
exit(0);
}
}while(choice!=4);
}
Sample Input Output :Implementation Of Stack
Main Menu
1.Push
2.Pop
3.Display
4.exit
Enter Your Choice1
Enter The item to be pushed78
Main Menu
1.Push
2.Pop
3.Display
4.exit
Enter Your Choice1
Enter The item to be pushed90
Main Menu
1.Push
2.Pop
3.Display
4.exit
Enter Your Choice1
Enter The item to be pushed-45

Main Menu
1.Push
2.Pop
3.Display
4.exit
Enter Your Choice3
-45
90
78
Main Menu
1.Push
2.Pop
3.Display
4.exit
Enter Your Choice2
The popped element is -45
Main Menu
1.Push
2.Pop
3.Display
4.exit
Enter Your Choice4

Program Code ://Program No.9 Infix to Postfix Expression


#define SIZE 50
#include <ctype.h>
#include<stdio.h>
char s[SIZE];
int top=-1;
push(char elem)
{
s[++top]=elem;
}
char pop()
{
return(s[top--]);
}
int pr(char elem)
{
switch(elem)
{
case '#': return 0;
case '(': return 1;
case '+':
case '-': return 2;
case '*':
case '/': return 3;
}
}
main()
{
char infx[50],pofx[50],ch,elem;
int i=0,k=0;
printf("\nInfix to Postfix Expression");
printf("\n*--------------------------*");
printf("\n\nRead the Infix Expression ? ");
scanf("%s",infx);
push('#');
while( (ch=infx[i++]) != '\0')
{
if( ch == '(') push(ch);

else
if(isalnum(ch)) pofx[k++]=ch;
else
if( ch == ')')
{
while( s[top] != '(')
pofx[k++]=pop();
elem=pop();
}
else
{
while( pr(s[top]) >= pr(ch) )
pofx[k++]=pop();
push(ch);
}
}
while( s[top] != '#')
pofx[k++]=pop();
pofx[k]='\0';
printf("\nGiven Infix Expn: %s \nPostfix Expn: %s\n",infx,pofx);
}
Sample Input Output :Infix to Postfix Expression
*--------------------------*
Read the Infix Expression ? a+b/c
Given Infix Expn: a+b/c
Postfix Expn: abc/+

Program Code ://Program No.10 Queue Implementation using Array


#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#define MAX 10
int queue[MAX],front=-1,rear=-1;
void insert_element();
void delete_element();
void display_queue();
void main()
{
int option;
printf("\n Queue Implementation");
printf("\n*--------------*");
do
{
printf("\n\n 1.Insert");
printf("\n 2.Delete");
printf("\n 3.Display");
printf("\n 4.Exit");
printf("\n Enter your choice: ");
scanf("%d",&option);
switch(option)
{
case 1: insert_element();
break;
case 2: delete_element();
break;
case 3: display_queue();
break;
case 4: exit(0);
}
}while(option!=4);
}
void insert_element()
{
int num;
printf("\n Enter the number to be inserted: ");

scanf("%d",&num);
if(front==0 && rear==MAX-1)
printf("\n Queue OverFlow Occured");
else if(front==-1&&rear==-1)
{
front=rear=0;
queue[rear]=num;
}
else if(rear==MAX-1 && front!=0)
{
rear=0;
queue[rear]=num;
}
else
{
rear++;
queue[rear]=num;
}
}
void delete_element()
{
int element;
if(front==-1)
{
printf("\n Underflow");
}
element=queue[front];
if(front==rear)
front=rear=-1;
else
{
if(front==MAX-1)
front=0;
else
front++;
printf("\n The deleted element is: %d",element);
}
}
void display_queue()
{

int i;
if(front==-1)
printf("\n No elements to display");
else
{
printf("\n The queue elements are:\n ");
for(i=front;i<=rear;i++)
{
printf("\t %d",queue[i]);
}
}
}
Sample Input Output :Queue Implementation
*--------------------------*
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice: 1
Enter the number to be inserted: 56
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice: 1
Enter the number to be inserted: 34
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice: 1
Enter the number to be inserted:
-6

1.Insert
2.Delete
3.Display
4.Exit
Enter your choice: 3
The queue elements are:
56
34
-6
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice: 2
The deleted element is: 56
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice: 4

Program Code ://Program No.11 Singly Linked List


#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct sll
{
int data;
struct sll*link;
};
typedef struct sll node;
node*head=NULL,*temp,*p,*prev,size;
void insert();
void delte();
void display();
void main()
{
printf("\nSingly Linked List");
printf("\n*-----------------*");
while(1)
{
int ch;
printf("\nMENU");
printf("\n1.insert\n2.delete\n3.display\n4.exit");
printf("\n enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
delte();
break;
case 3:
display();
break;
case 4:
exit(0);
}
}
}

void insert()
{
temp=(node*)malloc(sizeof(node));
printf("\n enter the data");
scanf("%d",&temp->data);
if(head==NULL)
{
head=temp;
temp->link=NULL;
}
else
{
for(p=head,prev=head;p!=NULL;prev=p,p,p=p->link)
{
if(p->data>temp->data)
{
if(p==head)
{
temp->link=head;
head=temp;
break;
}
else
{
temp->link=prev->link;
prev->link=temp;
break;
}
}
}
if(p==NULL)
{
prev->link=temp;
temp->link=NULL;
}
printf("\n the given item is added");
}
}
void delte()
{

int x,f=0;
printf("\n enter the data to be deleted");
scanf("%d",&x);
if(head->data==x)
{
head=head->link;
f=1;
}
else
for(p=head;p!=NULL;prev=p,p=p->link)
if(p->data==x)
{
prev->link=p->link;
f=1;
break;
}
if(f==1)
printf("\n item is deleted");
else
printf("\n item is not found");
}
void display()
{
printf("\n\n the list is : ");
for(p=head;p!=NULL;p=p->link)
{
printf("%2d",p->data);
if(p->link!=NULL)
printf("->");
}
}

Sample Input Output :Singly Linked List


*------------------*
MENU
1.insert
2.delete
3.display
4.exit
enter your choice:1
enter the data78
MENU
1.insert
2.delete
3.display
4.exit
enter your choice:1
enter the data67
the given item is added
MENU
1.insert
2.delete
3.display
4.exit
enter your choice:1
enter the data-90
the given item is added
MENU
1.insert
2.delete
3.display
4.exit
enter your choice:3
the list is : -90->67->78
MENU

1.insert
2.delete
3.display
4.exit
enter your choice:2
enter the data to be deleted67
item is deleted
MENU
1.insert
2.delete
3.display
4.exit
enter your choice:3
the list is : -90->78
MENU
1.insert
2.delete
3.display
4.exit
enter your choice:4

Program Code
//Program No.12 Doubly Linked List
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct Node
{
int data;
struct Node *next;
struct Node *prev;
}node;
void insert(node *pointer, int data)
{
while(pointer->next!=NULL)
{
pointer = pointer -> next;
}
pointer->next = (node *)malloc(sizeof(node));
(pointer->next)->prev = pointer;
pointer = pointer->next;
pointer->data = data;
pointer->next = NULL;
}
void delete(node *pointer, int data)
{
while(pointer->next!=NULL && (pointer->next)->data != data)
{
pointer = pointer -> next;
}
if(pointer->next==NULL)
{
printf("Element %d is not present in the list\n",data);
return;
}
node *temp;
temp = pointer -> next;
pointer->next = temp->next;
temp->prev = pointer;
free(temp);
}

void printmylist(node *pointer)


{
if(pointer==NULL)
{
return;
}
printf("->%d",pointer->data);
printmylist(pointer->next);
}
void main()
{
int data;
node *start,*temp;
start = (node *)malloc(sizeof(node));
temp = start;
temp -> next = NULL;
temp -> prev = NULL;
printf("\nDOUBLY LINKED LIST");
printf("\n------------------");
printf("\n1. Insert");
printf("\n2. Delete");
printf("\n3. Print");
printf("\n4. Exit");
while(1)
{
int ch;
printf("\nEnter ur Choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter the element to insert:");
scanf("%d",&data);
insert(start,data);
break;
case 2:
printf("\nEnter the element to delete :");
scanf("%d",&data);
delete(start,data);
break;
case 3:
printf("The list is ");

printmylist(start->next);
printf("\n");
break;
case 4:
exit(0);
break;
}
}
}
Sample Input Output :DOUBLY LINKED LIST
-----------------1. Insert
2. Delete
3. Print
4. Exit
Enter ur Choice1
Enter the element to insert:23
Enter ur Choice1
Enter the element to insert:90
Enter ur Choice1
Enter the element to insert:67
Enter ur Choice3
The list is ->23->90->67
Enter ur Choice2
Enter the element to delete :67
Enter ur Choice3
The list is ->23->90
Enter ur Choice4

Program Code ://Program No.13 Binary Tree Traversal using Recursion


#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
typedef struct tree
{
int info;
struct tree *left;
struct tree *right;
}tree;
tree *root;
int x;
void Buildtree(tree*,int);
void travlnorder(tree*);
void travpreorder(tree*);
void travpostorder(tree*);
void main()
{
printf("\nBINARY TREE TRAVERSAL USING RECURSION");
printf("\n*--------------------------------*");
printf("\nEnter the root values:");
scanf("%d",&x);
root=(tree*)malloc(sizeof(tree));
root->info=x;
root->left=NULL;
root->right=NULL;
printf("Enter the values^zto terminate:");
while(scanf("%d",&x)!=EOF)
Buildtree(root,x);
printf("\n");
printf("\ninorder");
travlnorder(root);
printf("\npreorder");
travpreorder(root);
printf("\nPostorder");
travpostorder(root);
}
void Buildtree(tree *node,int number)
{
tree *new_node;
if(number>node->info)

{
if(node->right==NULL)
{
new_node=(tree*)malloc(sizeof(tree));
new_node->info=number;
new_node->left=NULL;
new_node->right=NULL;
node->right=new_node;
}
else
Buildtree(node->right,number);
}
else
{
if(number<node->info)
if(node->left==NULL)
{
new_node=(tree*)malloc(sizeof(tree));
new_node->info=number;
new_node->left=NULL;
new_node->right=NULL;
node->left=new_node;
}
else
Buildtree(node->left,number);
else
printf("\nduplicate number=%d",number);
}
}
void travlnorder(tree*node)
{
if(node!=NULL)
{
travlnorder(node->left);
printf(" %d",node->info);
travlnorder(node->right);
}
}
void travpreorder(tree *node)
{
if(node!=NULL)
{

printf(" %d",node->info);
travpreorder(node->left);
travpreorder(node->right);
}
}
void travpostorder(tree*node)
{
if(node!=NULL)
{
travpostorder(node->left);
travpostorder(node->right);
printf(" %d",node->info);
}
}

Sample Input Output :BINARY TREE TRAVERSAL USING RECURSION


*----------------------------------------------------------------*
Enter the root values:66
Enter the values^zto terminate:55
88
44
60
77
99
22
11
33
^Z
inorder 11 22 33 44 55 60 66 77 88 99
preorder 66 55 44 22 11 33 60 88 77 99
Postorder 11 33 22 44 60 55 77 99 88 66

Program Code:
//Program No. 14 Graph Traversal (BFS & DFS)
#include<stdio.h>
int q[20],top=-1,front=-1,rear=-1,a[20][20],vis[20],stack[20];
int delete();
void add(int item);
void bfs(ints,int n);
void dfs(ints,int n);
void push(int item);
int pop();
main()
{
intn,i,s,ch,j;
char c,dummy;
printf(\n\t\tDFS&BFS);
printf(\t\t~~~~~~~~~);
printf("Enter the number vertices");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("Enter 1 if %d has a node with %d else 0",i,j);
scanf("%d",&a[i][j]);
}
}
printf("the adjacency matrix is\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("%d",a[i][j]);
}
printf("\n");
}
do
{
for(i=1;i<=n;i++)
vis[i]=0;

printf("\n menu");
printf("\n B.F.S");
printf("\n D.F.S");
printf("\n Enter your choice");
scanf("%d",&ch);
printf("Enter the source vertex:");
scanf("%d",&s);
switch(ch)
{
case 1:
bfs(s,n);
break;
case 2:
dfs(s,n);
break;
}
printf("do u want continue(y/n)?");
scanf("%c",&dummy);
scanf("%c",&c);
}
while((c=='y')||(c=='y'));
}
void bfs(ints,int n)
{
intp,i;
add(s);
vis[s]=1;
p=delete();
if(p!=0)
printf("%d",p);
while(p!=0)
{
for(i=1;i<=n;i++)
if((a[p][i]!=0)&&(vis[i]==0))
{
add(i);
vis[i]=1;
}

p=delete();
if(p!=0)
printf("%d",p);
}
for(i=1;i<=n;i++)
if(vis[i]==0)
bfs(i,n);
}
void add(int item)
{
if(rear==19)
printf("queue full");
else
{
if(rear==-1)
{
q[++rear]=item;
front++;
}
else
q[++rear]=item;
}
}
int delete()
{
int k;
if((front>rear)||(front==-1))
return(0);
else
{
k=q[front++];
return(k);
}
}
void dfs(ints,int n)
{
inti,k;

push(s);
vis[s]=1;
k=pop();
if(k!=0)
printf("%d",k);
while(k!=0)
{
for(i=1;i<=n;i++)
if((a[k][i]!=0)&&(vis[i]==0))
{
push(i);
vis[i]=1;
}
k=pop();
if(k!=0)
printf("%d",k);
}
for(i=1;i<=n;i++)
if(vis[i]==0)
dfs(i,n);
}
void push(int item)
{
if(top==19)
printf("stack ovwrflow");
else stack[++top]=item;
}
int pop()
{
int k;
if(top==-1)
return(0);
else
{
k=stack[top--];
return(k);
}}

Sample Input Output :DFS&BFS


~~~~~~~~
Enter the number vertices 2
Enter 1 if 1 has a node with 1 else 0 1
Enter 1 if 1 has a node with 2 else 0 0
Enter 1 if 2 has a node with 1 else 0 1
Enter 1 if 2 has a node with 2 else 0 0
The adjacency matrix is
10
10
menu
1. B.F.S
2.D.F.S
Enter your choice 1
Enter the source vertex: 2
21
do u want continue(y/n)?n

Program Code:
//Program No.15 Minimum Spanning Tree(Prims Algorithm)
#include<stdio.h>
#include<conio.h>
intn,cost[10][10];
void prim()
{
inti,j,k,l,x,nr[10],temp,min_cost=0,tree[10][3];
temp=cost[0][0];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(temp>cost[i][j])
{
temp=cost[i][j];
k=i;
l=j;
}
}
}
tree[0][0]=k;
tree[0][1]=l;
tree[0][2]=temp;
min_cost=temp;
for(i=0;i<n;i++)
{
if(cost[i][k]<cost[i][l])
nr[i]=k;
else
nr[i]=l;
}
nr[k]=100;
nr[l]=100;
temp=99;
for(i=1;i<n-1;i++)

{
for(j=0;j<n;j++)
{
if(nr[j]!=100 && cost[j] [nr[j]]<temp)
{
temp=cost[j][nr[j]];
x=j;
}
}
tree[i][0]=x;
tree[i][1]=nr[x];
tree[i][2]=cost[x][nr[x]];
min_cost=min_cost+cost[x][nr[x]];
nr[x]=100;
for(j=0;j<n;j++)
{
if(nr[j]!=100 && cost[j][nr[j]]>cost[j][x])
nr[j]=x;
}
temp=99;
}
printf("\n the min spanning tree is:-");
for(i=0;i<n-1;i++)
{
for(j=0;j<3;j++)
printf("%d",tree[i][j]);
printf("\n");
}
printf("\n min cost:-%d",min_cost);
}
main()
{
inti,j;
printf(\n\t\t\t Prims Algorithm\t);
printf(\t\t~~~~~~~~~~~~~~~~~~~\t\t);
printf("\n Enter the no. of vertices:");
scanf("%d",&n);

printf("\n Enter the costs of edges in matrix form:-");


for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&cost[i][j]);
printf("\n The matrix is:-");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%d\t",cost[i][j]);
printf("\n")
}
prim();
}
Sample Input Output:
Prims Algorithm
~~~~~~~~~~~~~~~~~~~
Enter the no. of vertices: 3
Enter the costs of edges in matrix form:99
2
3
2
99
5
3
5
99
The matrix is:
99
2
3
2
99
5
3
5
99
The min spanning tree is:012
203
Min cost:- 5

Program Code ://Program No.16 Shortest Path Algorithm(Dijsktra's Algorithm)


#define INFINITY 9999
#include <stdio.h>
#include <conio.h>
#define MAX 10
void dijkstra(int G[MAX][MAX],int n, int startnode);
void main()
{
int G[MAX][MAX],i,j,n,noofedges,u,v,w;
printf("\n\tDijsktra's Algorithm");
printf("\n\t*------------------*");
printf("\nEnter no. of vertices : ");
scanf("%d",&n);
printf("\nNo. of edges : ");
scanf("%d",&noofedges);
printf("\nEnter edges (u,v,weight) : ");
for(i=0;i<noofedges;i++)
{
scanf("%d%d%d",&u,&v,&w);
G[u][v]=G[v][u]=w;
}
printf("\nEnter the starting node : ");
scanf("%d",&u);
dijkstra(G,n,u);
}
void dijkstra(int G[MAX][MAX],int n, int startnode)
{
int cost[MAX][MAX],distance[MAX],pred[MAX];
int visited[MAX],count,mindistance,nextnode,i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(G[i][j]==0)
cost[i][j]=INFINITY;
else
cost[i][j]=G[i][j];
for(i=0;i<n;i++)

{
distance[i]=cost[startnode][i];
pred[i]=startnode;visited[i]=0;
}
distance[startnode]=0;visited[startnode]=1;
count=1;
while(count<n-1)
{
mindistance=INFINITY ;
for(i=0;i<n;i++)
if(distance[i] < mindistance && !visited[i])
{
mindistance=distance[i];
nextnode=i;
}
visited[nextnode]=1;
for(i=0;i<n;i++)
if(!visited[i])
if(mindistance+cost[nextnode][i]<distance[i])
{
distance[i]=mindistance+cost[nextnode][i];
pred[i]=nextnode;
}
count++;
}
for(i=0;i<n;i++)
if(i!=startnode)
{
printf("\n Distance of %d = %d ",i,distance[i]);
printf("\nPath = %d ",i);
j=i;
do
{
j=pred[j];
printf("<- %d ",j);
}while(j!=startnode);

}
}
Sample Input Output:Dijsktra's Algorithm
*------------------*
Enter no. of vertices : 4
No. of edges : 5
Enter edges (u,v,weight) : 0 1 2
121
232
305
023
Enter the starting node : 1
Distance of 0 = 2
Path = 0 <- 1
Distance of 2 = 1
Path = 2 <- 1
Distance of 3 = 3
Path = 3 <- 2 <- 1

Potrebbero piacerti anche