Sei sulla pagina 1di 30

S.I.E.S College of Arts, Science and Commerce, Sion (W), Mumbai 400 002. S.Y.B.

Sc (Computer Science) CERTIFICATE

This is to certify that Miss. Nupur Kapur of S.Y.B.Sc (Computer Science) Div: C Roll No: 116 has successfully completed the necessary course of experiments in Paper under my supervision during the academic year 2011 2012.

Course Co-ordinator Charge (Dept. of Computer Science) Date: College Seal

Lecture-In-

Date:

INDEX
SR NO. DATE CONTENTS SIGN

1.

Stack

2.

Link List

3.

Queue

4.

Doubly Link list

5.

Dequeue

6.

Circular Queue

7.

Breadth First Search

8.

Depth First Search

9.

Warshall Algorithm

/* Program to implement link list as stack*/ #define MAXSIZE 10 struct st { int top; int stack[MAXSIZE]; }; struct st s; int empty(void); int full(void); void push(void); void pop(void); void display(void); void main() { char ans; int ch; do { clrscr(); printf("\n"); printf("1.PUSH\n"); printf("2.POP\n"); printf("3.DISPLAY\n"); printf("4.QUIT\n"); printf("Enter Your Choice : "); scanf("%d",&ch); switch(ch) { case 1: push(); break; case 2: pop(); break; case 3: display(); break; case 4: exit(1);

break; default: printf("INVALID CHOICE!!!!!!!!!!!!!!!!\n"); break; } printf("Want To Go To The Main Menu[y/n]"); ans = getch(); } while(ans == 'y' ans == 'Y'); printf("\nPress Any Key To Exit"); getch(); } int full(void) { if (s.top == MAXSIZE) return(1); else return(0); } int empty(void) { if (s.top == 0) return(1); else return(0); } void push(void) { char ch; int x; do { if(full() == 1) { printf("\nStack Full\n"); break; } else { s.top = s.top + 1; printf("\nEnter An Element To Be Pushed: "); scanf("%d",&x); s.stack[s.top] = x;

} printf("\nDo You Want To Push More Elements[y/n]"); ch = getch(); } while(ch == 'y' ch == 'Y'); } void pop(void) { char ch; do { if(empty() == 1) { printf("\nStack Empty\n"); break; } else { printf("\n%d has been popped !",s.stack[s.top]); s.top = s.top - 1; } printf("\nDo you Want To Pop Out More?[y/n]"); ch = getch(); } while(ch == 'Y' ch == 'y'); } void display(void) { int i; clrscr(); if(empty() == 1) printf("\nStack Empty!!!"); else { printf("Displaying Stack............\n"); for(i = s.top; i>0;i--) printf("%d",s.stack[i]); } }

OUTPUT 1. PUSH 2. POP 3. DISPLAY 4. QUIT Enter Your Choice : 1 Enter An Element To Be Pushed : 1 Do YOu Want To Push More Elements [y\n] y Enter An Element To Be Pushed : 2 Do YOu Want To Push More Elements [y\n] y Enter An Element To Be Pushed : 3 Do YOu Want To Push More Elements [y\n] y Enter An Element To Be Pushed : 4 Do YOu Want To Push More Elements [y\n] y Enter An Element To Be Pushed : 5 Do YOu Want To Push More Elements [y\n] n Want To Go Main Menu? [y\n] y Stack Program 1. PUSH 2. POP 3. DISPLAY 4. QUIT Enter Your Choice : 3 Displaying Stack...... 5 4 3 2 1 Want To Go Main Menu? [y\n] y Stack Program 1. PUSH 2. POP 3. DISPLAY 4. QUIT Enter Your Choice : 2 5 Has Been Popped! Do You Want To Pop More? [y\n] y

4 Has Been Popped! Do You Want To Pop More? [y\n] n Want To Go Main Menu? [y\n] y Stack Program 1. PUSH 2. POP 3. DISPLAY 4. QUIT Enter Your Choice : 3 Displaying Stack......... 3 2 1 Want To Go Main Menu? [y\n] y 1. PUSH 2. POP 3. DISPLAY 4. QUIT Enter Your Choice : 4

/* Program to create a list, print the list and count the number of elements in the list */ #include<stdio.h> #include<conio.h> struct link_list { int num; struct link_list *next; }; typedef struct link_list node; void main() { node *head; int res; void create(node*p); void print(node*p); int count(node*p); head=(node *) malloc(sizeof(node*)); clrscr(); create (head); create(head); print(head0; res= count(head); printf(The totoal nuber of elements is: %d,res); getch(); } void create(node * link) { printf(Enter the 1st element); scanf(%d,&list->num); if (list->num==-999) { list->next=NULL; } else { list->next=(node*)malloc(sizeof(node*)); create(list->next); }} void print(node *link) { if(list->next!=NULL) { printf(%d,list->num); print(list->next);

} } int count(node *link) { if(list->next == NULL) return 0; else return(1 count (link->next)); }

Output : Enter the input (1st element) enter -999 to the end of the list 5 Enter the input (1st element) enter -999 to the end of the list 6 Enter the input (1st element) enter -999 to the end of the list -999 56 total number of elements is 2

/* Program to implement link list as queue*/ #include<stdio.h> #include<conio.h> void insert(int); int delet(int); void display(void); int queue[3]; int rear=-1; int front=-1; void main() { int n=3; char op; clrscr(); do { printf("\nOptions"); printf("\nPress i for insertion"); printf("\nPress d for deletion"); printf("\nPress p for display"); printf("\nPress e for exit"); op=getche(); switch(op) { case 'i':insert(n);break; case 'd':delet(n);break; case 'p':display(); break; default:printf("\nWrong operator"); } } while(op!='e'); getch(); } void insert(int n) { int item; if((front==0&&rear==n)||(front==rear+1)) { printf("\nQueue over flow"); return; } if(front==-1)

{ front=0; rear=0; } else if(rear==n) rear=0; else rear=rear+1; printf("\nEnter the item to be inserted"); scanf("%d",&item); queue[rear]=item; } int delet(int n) { int item; if(front==-1) { printf("\nQueue is empty"); return; } printf("\nYou have deleted %d",queue[front]); queue[front]=0; if(front==rear) { front=-1; rear=-1; } else if(front==n) front=0; else front=front+1; return; } void display(void) { int i; printf("\nDisplaying Queue\n"); for(i=0;i<3;i++) printf("\n%d",queue[i]); }

Output : Options Press i for insertion Press d for deletion Press p for display Press e for exit Enter the item to be inserted Queue is empty Enter the item to be inserted 5 Enter the item to be inserted 6 Options Press i for insertion Press d for deletion Press p for display Press e for exit p Displaying Queue 5 6

/* Program to implement Doubly Link List*/ #include <stdio.h> #include <malloc.h> struct node { struct node *prev; int info; struct node *next; }*start; main() { int choice,n,m,po,i; start=NULL; while(1) { printf("1.Create List\n"); printf("2.Add at begining\n"); printf("3.Add after\n"); printf("4.Delete\n"); printf("5.Display\n"); printf("6.Count\n"); printf("7.Reverse\n"); printf("8.exit\n"); printf("Enter your choice : "); scanf("%d",&choice); switch(choice) { case 1: printf("How many nodes you want : "); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter the element : "); scanf("%d",&m); create_list(m); } break; case 2: printf("Enter the element : "); scanf("%d",&m); addatbeg(m); break;

case 3: printf("Enter the element : "); scanf("%d",&m); printf("Enter the position after which this element is inserted : "); scanf("%d",&po); addafter(m,po); break; case 4: printf("Enter the element for deletion : "); scanf("%d",&m); del(m); break; case 5: display(); break; case 6: count(); break; case 7: rev(); break; case 8: exit(); default: printf("Wrong choice\n"); create_list(int num) { struct node *q,*tmp; tmp= malloc(sizeof(struct node)); tmp->info=num; tmp->next=NULL; if(start==NULL) { tmp->prev=NULL; start->prev=tmp; start=tmp; } else { q=start; while(q->next!=NULL) q=q->next; q->next=tmp; tmp->prev=q; }

} addatbeg(int num) { struct node *temp; tmp=malloc(sizeof(struct node)); tmp->prev=NULL; tmp->info=num; tmp->next=start; start->prev=tmp; start=tmp; } addafter(int num,int c) { struct node *tmp,*q; int i; q=start; for(i=0;i<c-1;i++) { q=q->next; if(q==NULL) { printf("There are less than %d elements\n",c); return; } } tmp=malloc(sizeof(struct node) ); tmp->info=num; q->next->prev=tmp; tmp->next=q->next; tmp->prev=q; q->next=tmp; } del(int num) { struct node *tmp,*q; if(start->info==num) { tmp=start; start=start->next; start->prev = NULL; free(tmp); return; } q=start; while(q->next->next!=NULL) {

if(q->next->info==num) { tmp=q->next; q->next=tmp->next; tmp->next->prev=q; free(tmp); return; } q=q->next; } if(q->next->info==num) { tmp=q->next; free(tmp); q->next=NULL; return; } printf("Element %d not found\n",num); } display() { struct node *q; if(start==NULL) { printf("List is empty\n"); return; } q=start; printf("List is :\n"); while(q!=NULL) { printf("%d ", q->info); q=q->next; } printf("\n"); } count() { struct node *q=start; int cnt=0; while(q!=NULL) { q=q->next; cnt++; } printf("Number of elements are %d\n",cnt); }

rev() { struct node *p1,*p2; p1=start; p2=p1->next; p1->next=NULL; p1->prev=p2; while(p2!=NULL) { p2->prev=p2->next; p2->next=p1; p1=p2; p2=p2->prev; /*next of p2 changed to prev */ } start=p1; }

Output: 1.Create List 2.Add at begining 3.Add after 4.Delete 5.Display 6.Count 7.Reverse 8.exit Enter your choice : 1 How many nodes you want : 4 Enter the element : 10 Enter the element : 20 Enter the element : 30 Enter the element : 40 1.Create List 2.Add at begining 3.Add after 4.Delete 5.Display 6.Count 7.Reverse 8.exit 8

/* Program to implement Dequeue */ #include <stdio.h> #define MAX 20 struct queue{ int x[MAX]; int front; int rear; }q; void enqueueRight(int); void display(); int dequeueLeft(); void enqueueLeft(int); int dequeueRight(); int main() { q.front=-1; q.rear=-1; int ch,x,flag=1,c2; printf("1.Input Restricted Deque\n2.Output Restricted Deque\n\n"); scanf("%d",&c2); if(c2==1) { while(flag) { printf("1.Enqueue\n2.Dequeue Right\n3.Dequeue Left\n4.Display\n5.Exit\n"); scanf("%d",&ch); switch(ch) { case 1:printf("Enter element:"); scanf("%d",&x); enqueueRight(x); break; case 2:x=dequeueRight(); printf("Removed %d\n",x); break; case 3:x=dequeueLeft(); printf("Removed %d\n",x); break; case 4:display(); break; case 5:flag=0; break; default:printf("Wrong choice\n"); }

} }else if (c2==2) { while(flag) { printf("1.Enqueue Left\n2.Enqueue Right\n3.Dequeue\n4.Display\n5.Exit\n"); scanf("%d",&ch); switch(ch) { case 1:printf("Enter element:"); scanf("%d",&x); enqueueLeft(x); break; case 2:printf("Enter element:"); scanf("%d",&x); enqueueRight(x); break; case 3:x=dequeueLeft(); printf("Removed %d\n",x); break; case 4:display(); break; case 5:flag=0; break; default:printf("Wrong choice\n"); } } }else { printf("Wrong choice\n"); getch(); } } void enqueueRight(int x) { if(q.rear==MAX) printf("Queue full from Right\n"); else { q.x[++q.rear]=x; if(q.front==-1) q.front=0; } } void enqueueLeft(int x) {

if(q.rear==-1 && q.front==-1) enqueueRight(x); else if(q.front==0) printf("Queue full from Left\n"); else { q.x[--q.front]=x; } } int dequeueLeft() { int x; if(q.rear==-1 && q.front==-1) printf("Queue Empty\n"); else if(q.front==q.rear) { x=q.x[q.front]; q.front=q.rear=-1; return x; }else return q.x[q.front++]; } int dequeueRight() { int x; if(q.rear==-1 && q.front==-1) printf("Queue Empty\n"); else if(q.front==q.rear) { x=q.x[q.front]; q.front=q.rear=-1; return x; }else return q.x[q.rear--]; } void display() { printf("Queue is\n"); int i; for(i=q.front;i<=q.rear;i++) printf("%d\n",q.x[i]); }

Output : 1.Input Restricted Deque 2.Output Restricted Deque 1 1.Enqueue 2.Dequeue Right 3.Dequeue Left 4.Display 5.Exit 1 Enter element 56 Enter element 57 Enter element 58 Items inserted : 56 57 58

/* Program to implement Circular queue */ #include <stdio.h> #include <stdlib.h> #define MAX 10 void insert(int queue[], int *rear, int front, int value) { *rear= (*rear +1) % MAX; if(*rear == front) { printf("The queue is full can not insert a value\n"); exit(0); } queue[*rear] = value; } void delete(int queue[], int *front, int rear, int * value) { if(*front == rear) { printf("The queue is empty can not delete a value\n"); exit(0); } *front = (*front + 1) % MAX; *value = queue[*front]; } void main() { int queue[MAX]; int front,rear; int n,value; front=0; rear=0; insert(queue,&rear,front,1); insert(queue,&rear,front,2); insert(queue,&rear,front,3); insert(queue,&rear,front,4); delete(queue,&front,rear,&value); printf("The value deleted is %d\n",value); delete(queue,&front,rear,&value);

printf("The value deleted is %d\n",value); delete(queue,&front,rear,&value); printf("The value deleted is %d\n",value); }

Output: The value deleted is 1 The value deleted is 2 The value deleted is 3

/* Program to implement Breadth First Search */ #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(); }

/* Program to implement Depth First Search */ #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(); }

/* Program to implement Warshall algorithm*/ #include<stdio.h> #include<conio.h> #include<math.h> int max(int,int); void warshal(int p[10][10],int n) { int i,j,k; for(k=1;k<=n;k++) for(i=1;i<=n;i++) for(j=1;j<=n;j++) p[i][j]=max(p[i][j],p[i][k]&&p[k][j]); } int max(int a,int b) { ; if(a>b) return(a); else return(b); } void main() { int p[10][10]={0},n,e,u,v,i,j; clrscr(); printf("\n Enter the number of vertices:"); scanf("%d",&n); printf("\n Enter the number of edges:"); scanf("%d",&e); for(i=1;i<=e;i++) { printf("\n Enter the end vertices of edge %d:",i); scanf("%d%d",&u,&v); p[u][v]=1; } printf("\n Matrix of input data: \n"); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) printf("%d\t",p[i][j]); printf("\n"); } warshal(p,n); printf("\n Transitive closure: \n"); for(i=1;i<=n;i++) {

for(j=1;j<=n;j++) printf("%d\t",p[i][j]); printf("\n"); } getch(); }