You are on page 1of 10

Stack using Array

#include<stdio.h> #include<conio.h> #include<stdlib.h> #define size 5 /* stack structure*/ 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("n Stack Is Empty!"); else { for(i=st.top;i>=0;i--) printf("n%d",st.s[i]); }

} //------------------------------------void main(void) { int item,choice; char ans; st.top=-1; clrscr(); printf("ntt Implementation Of Stack"); do { printf("n Main Menu"); printf("n1.Pushn2.Popn3.Displayn4.exit"); printf("n Enter Your Choice"); scanf("%d",&choice); switch(choice) { case 1: printf("n Enter The item to be pushed"); scanf("%d",&item); if(stfull()) printf("n Stack is Full!"); else push(item); break; case 2: if(stempty()) printf("n Empty stack!Underflow !!"); else { item=pop(); printf("n The popped element is %d",item); } break; case 3: display(); break; case 4: exit(0); } printf("n Do You want To Continue?"); ans=getche(); }while(ans =='Y'||ans =='y'); getch(); }

3.Queue Using Array


#include<stdio.h> #include<conio.h> #define MAX 10 void insert(int); int del(); int queue[MAX], rear=0, front=0; void display(); int main() { char ch , a='y'; int choice, token; printf("1.Insert"); printf("\n2.Delete"); printf("\n3.show or display"); do { printf("\nEnter your choice for the operation: "); scanf("%d",&choice); switch(choice) { case 1: insert(token); display(); break; case 2: token=del(); printf("\nThe token deleted is %d",token); display(); break; case 3: display(); break; default: printf("Wrong choice"); break; } printf("\nDo you want to continue(y/n):"); ch=getch(); } while(ch=='y'||ch=='Y'); getch(); } void display() { int i; printf("\nThe queue elements are:");

for(i=rear;i<front;i++) { printf("%d ",queue[i]); } } void insert(int token) { char a; if(rear==MAX) { printf("\nQueue full"); return; } do { printf("\nEnter the token to be inserted:"); scanf("%d",&token); queue[front]=token; front=front+1; printf("do you want to continue insertion Y/N"); a=getch(); } while(a=='y'); } int del() { int t; if(front==rear) { printf("\nQueue empty"); return 0; } rear=rear+1; t=queue[rear-1]; return t; }

Depth First Search on a Graph


#include<stdio.h> #include<conio.h> int a[20][20],reach[20],n; void dfs(int v) { int i; reach[v]=1; for(i=1;i<=n;i++) if(a[v][i] && !reach[i]) { printf("\n %d->%d",v,i); dfs(i); } } void main() { int i,j,count=0; clrscr(); printf("\n Enter number of vertices:"); scanf("%d",&n); for(i=1;i<=n;i++) { reach[i]=0; for(j=1;j<=n;j++) a[i][j]=0; } printf("\n Enter the adjacency matrix:\n"); for(i=1;i<=n;i++) for(j=1;j<=n;j++) scanf("%d",&a[i][j]); dfs(1); printf("\n"); for(i=1;i<=n;i++) { if(reach[i]) count++; } if(count==n) printf("\n Graph is connected"); else printf("\n Graph is not connected"); getch(); }

6. Breadth First Search on a Graph


#include<stdio.h> #include<conio.h> int a[20][20],q[20],visited[20],n,i,j,f=0,r=-1; void bfs(int v) { for(i=1;i<=n;i++) if(a[v][i] && !visited[i]) q[++r]=i; if(f<=r) { visited[q[f]]=1; bfs(q[f++]); } } void main() { int v; clrscr(); printf("\n Enter the number of vertices:"); scanf("%d",&n); for(i=1;i<=n;i++) { q[i]=0; visited[i]=0; } printf("\n Enter graph data in matrix form:\n"); for(i=1;i<=n;i++) for(j=1;j<=n;j++) scanf("%d",&a[i][j]); printf("\n Enter the starting vertex:"); scanf("%d",&v); bfs(v); printf("\n The node which are reachable are:\n"); for(i=1;i<=n;i++) if(visited[i]) printf("%d\t",i); else printf("\n Bfs is not possible"); getch(); }

Generating the prime factors of an Integer


This uses brute force to determine a prime, but that is all that is needed for this simple problem #include <strings.h> #include <stdlib.h> #include <unistd.h> int isprime( int x ) /* return true if x is a prime number */ { int i; for( i = 2; i < x && x % i; i++ ); return i < x ? 0 : 1; } int main( int argc, char **argv ) { int target; int i = 0; int j; int bidx = 0; int *buf = NULL; int nbuf = 0; nbuf = 100; buf = (int *) malloc( sizeof( int ) * nbuf ); argc--; i = 1; while( argc ) { target = atoi( argv[i] ); argc--; bidx = 0; if( isprime( target ) ) printf( "%s is prime: factors are 1 and %s\n", argv[i], argv[i] ); else { for( j = ((target -1) % 2) == 0 ? target - 2 : target - 1; j > 1; j -= 2 ) { if( !(target % j) && isprime( j ) ) { if( bidx >= nbuf-1 ) /* ensure room for 1 more after loop */ { nbuf += 1000; buf = (int *) realloc( buf, sizeof( int ) * nbuf ); } buf[bidx++] = j;

} } if( target % 2 == 0 ) buf[bidx] = 2; printf( "%s has prime factors of: 1, ", argv[i] ); for( ; bidx > 0; bidx-- ) printf( "%d, ", buf[bidx] ); printf( "%d\n", buf[bidx] ); } i++; } return 0; }

========== output ========= a.out 234 898 324 7182 71 234 has prime factors of: 1, 2, 3, 13 898 has prime factors of: 1, 2, 449 324 has prime factors of: 1, 2, 3 7182 has prime factors of: 1, 2, 3, 7, 19 71 is prime: factors are 1 and 71

. Building a Binary Tree #include<stdlib.h> #include<stdio.h> struct tree_el { int val; struct tree_el * right, * left; }; typedef struct tree_el node; void insert(node ** tree, node * item) { if(!(*tree)) { *tree = item; return; } if(item->val<(*tree)->val) insert(&(*tree)->left, item); else if(item->val>(*tree)->val) insert(&(*tree)->right, item); } void printout(node * tree) { if(tree->left) printout(tree->left); printf("%d\n",tree->val); if(tree->right) printout(tree->right); } void main() { node * curr, * root; int i; root = NULL; for(i=1;i<=10;i++) { curr = (node *)malloc(sizeof(node)); curr->left = curr->right = NULL; curr->val = rand(); insert(&root, curr); } printout(root); }

You might also like