2nd Sem Prog.

35
/*Insertion ,Deletion and Traversal in Binary Search Tree*/ # include<stdio.h> # include<conio.h> # include<process.h> # include<malloc.h> struct node { int info; struct node *lchild; struct node *rchild; }*root; void display(struct node *ptr,int level); void postorder(struct node *ptr); void preorder(struct node *ptr); void inorder(struct node *ptr); void case_a(struct node *par,struct node *loc); void case_b(struct node *par,struct node *loc); void case_c(struct node *par,struct node *loc); void del(int item); void insert(int item); void find(int item,struct node **par,struct node **loc); void main() { clrscr(); int choice,num; root=NULL; while(1) {  printf("\n");  printf("1.Insert\n");  printf("2.Delete\n");  printf("3.Inorder Traversal\n");  printf("4.Preorder Traversal\n");  printf("5.Postorder Traversal\n");  printf("6.Display\n");  printf("7.Quit\n");  printf("Enter your choice : "); scanf("%d",&choice); switch(choice) { case 1:  printf("Enter the number to be inserted : ");

Transcript of 2nd Sem Prog.

Page 1: 2nd Sem Prog.

8/8/2019 2nd Sem Prog.

http://slidepdf.com/reader/full/2nd-sem-prog 1/35

/*Insertion ,Deletion and Traversal in Binary Search Tree*/

# include<stdio.h>

# include<conio.h># include<process.h>

# include<malloc.h>

struct node

{

int info;struct node *lchild;

struct node *rchild;

}*root;

void display(struct node *ptr,int level);

void postorder(struct node *ptr);

void preorder(struct node *ptr);

void inorder(struct node *ptr);void case_a(struct node *par,struct node *loc);

void case_b(struct node *par,struct node *loc);void case_c(struct node *par,struct node *loc);

void del(int item);

void insert(int item);

void find(int item,struct node **par,struct node **loc);

void main()

{clrscr();

int choice,num;root=NULL;while(1)

{

 printf("\n"); printf("1.Insert\n");

 printf("2.Delete\n");

 printf("3.Inorder Traversal\n");

 printf("4.Preorder Traversal\n"); printf("5.Postorder Traversal\n");

 printf("6.Display\n");

 printf("7.Quit\n"); printf("Enter your choice : ");

scanf("%d",&choice);

switch(choice)

{

case 1:

 printf("Enter the number to be inserted : ");

Page 2: 2nd Sem Prog.

8/8/2019 2nd Sem Prog.

http://slidepdf.com/reader/full/2nd-sem-prog 2/35

scanf("%d",&num);

insert(num);

 break;case 2:

 printf("Enter the number to be deleted : ");

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

 break;

case 3:inorder(root);

 break;

case 4:

 preorder(root); break;

case 5:

 postorder(root);

 break;case 6:

display(root,1); break;

case 7:

exit(1);

default: printf("Wrong choice\n");

}/*End of switch */

}/*End of while */}/*End of main()*/

void find(int item,struct node **par,struct node **loc){

struct node *ptr,*ptrsave;

if(root==NULL) /*tree empty*/

{

*loc=NULL;

*par=NULL;return;

}

if(item==root->info) /*item is at root*/{

*loc=root;

*par=NULL;return;

}

/*Initialize ptr and ptrsave*/

if(item)

Page 3: 2nd Sem Prog.

8/8/2019 2nd Sem Prog.

http://slidepdf.com/reader/full/2nd-sem-prog 3/35

Page 4: 2nd Sem Prog.

8/8/2019 2nd Sem Prog.

http://slidepdf.com/reader/full/2nd-sem-prog 4/35

{

struct node *parent,*location;

if(root==NULL){

 printf("Tree empty");

return;}

find(item,&parent,&location);if(location==NULL)

{

 printf("Item not present in tree");

return;}

if(location->lchild==NULL && location->rchild==NULL)

case_a(parent,location);if(location->lchild!=NULL && location->rchild==NULL)

case_b(parent,location);if(location->lchild==NULL && location->rchild!=NULL)

case_b(parent,location);

if(location->lchild!=NULL && location->rchild!=NULL)

case_c(parent,location);free(location);

}/*End of del()*/

void case_a(struct node *par,struct node *loc )

{

if(par==NULL) /*item to be deleted is root node*/root=NULL;

else

if(loc==par->lchild) par->lchild=NULL;

else

 par->rchild=NULL;

}/*End of case_a()*/

void case_b(struct node *par,struct node *loc)

{struct node *child;

/*Initialize child*/if(loc->lchild!=NULL) /*item to be deleted has lchild */

child=loc->lchild;

else /*item to be deleted has rchild */

child=loc->rchild;

Page 5: 2nd Sem Prog.

8/8/2019 2nd Sem Prog.

http://slidepdf.com/reader/full/2nd-sem-prog 5/35

if(par==NULL ) /*Item to be deleted is root node*/

root=child;else

if( loc==par->lchild) /*item is lchild of its parent*/

 par->lchild=child;else /*item is rchild of its parent*/

 par->rchild=child;

}/*End of case_b()*/

void case_c(struct node *par,struct node *loc)

{

struct node *ptr,*ptrsave,*suc,*parsuc;

/*Find inorder successor and its parent*/

 ptrsave=loc;

 ptr=loc->rchild;while(ptr->lchild!=NULL)

{ ptrsave=ptr;

 ptr=ptr->lchild;

}

suc=ptr; parsuc=ptrsave;

if(suc->lchild==NULL && suc->rchild==NULL)case_a(parsuc,suc);

else

case_b(parsuc,suc);

if(par==NULL) /*if item to be deleted is root node */

root=suc;else

if(loc==par->lchild)

 par->lchild=suc;

else par->rchild=suc;

suc->lchild=loc->lchild;suc->rchild=loc->rchild;

}/*End of case_c()*/

void preorder(struct node *ptr)

{

if(root==NULL)

{

Page 6: 2nd Sem Prog.

8/8/2019 2nd Sem Prog.

http://slidepdf.com/reader/full/2nd-sem-prog 6/35

 printf("Tree is empty");

return;

}if(ptr!=NULL)

{

 printf("%d ",ptr->info); preorder(ptr->lchild);

 preorder(ptr->rchild);

}}/*End of preorder()*/

void inorder(struct node *ptr)

{if(root==NULL)

{

 printf("Tree is empty");

return;}

if(ptr!=NULL){

inorder(ptr->lchild);

 printf("%d ",ptr->info);

inorder(ptr->rchild);}

}/*End of inorder()*/

void postorder(struct node *ptr)

{

if(root==NULL){

 printf("Tree is empty");

return;}

if(ptr!=NULL)

{

 postorder(ptr->lchild); postorder(ptr->rchild);

 printf("%d ",ptr->info);

}}/*End of postorder()*/

void display(struct node *ptr,int level){

int i;

if ( ptr!=NULL )

{

Page 7: 2nd Sem Prog.

8/8/2019 2nd Sem Prog.

http://slidepdf.com/reader/full/2nd-sem-prog 7/35

display(ptr->rchild, level+1);

 printf("\n");

for (i = 0; i < level; i++) printf(" ");

 printf("%d", ptr->info);

display(ptr->lchild, level+1);}/*End of if*/

}/*End of display()*/

/

************************************OUTPUT****************************

*******

1.Insert

2.Delete

3.Inorder Traversal

4.Preorder Traversal5.Postorder Traversal

6.Display7.Quit

Enter your choice : 1

Enter the number to be inserted : 23

1.Insert

2.Delete

3.Inorder Traversal4.Preorder Traversal

5.Postorder Traversal

6.Display7.Quit

Enter your choice : 1

Enter the number to be inserted : 234

1.Insert

2.Delete

3.Inorder Traversal4.Preorder Traversal

5.Postorder Traversal

6.Display7.Quit

Enter your choice : 1

Enter the number to be inserted : 345

1.Insert

2.Delete

3.Inorder Traversal

Page 8: 2nd Sem Prog.

8/8/2019 2nd Sem Prog.

http://slidepdf.com/reader/full/2nd-sem-prog 8/35

4.Preorder Traversal

5.Postorder Traversal

6.Display7.Quit

Enter your choice : 1

Enter the number to be inserted : 2

1.Insert

2.Delete3.Inorder Traversal

4.Preorder Traversal

5.Postorder Traversal

6.Display7.Quit

Enter your choice : 1

Enter the number to be inserted : 43

1.Insert

2.Delete3.Inorder Traversal

4.Preorder Traversal

5.Postorder Traversal

6.Display7.Quit

Enter your choice : 1

Enter the number to be inserted : 467

1.Insert

2.Delete3.Inorder Traversal

4.Preorder Traversal

5.Postorder Traversal6.Display

7.Quit

Enter your choice : 6

23

234

3452

43

4671.Insert

2.Delete

3.Inorder Traversal

4.Preorder Traversal

Page 9: 2nd Sem Prog.

8/8/2019 2nd Sem Prog.

http://slidepdf.com/reader/full/2nd-sem-prog 9/35

5.Postorder Traversal

6.Display

7.QuitEnter your choice : 3

467 43 2 345 234 23

1.Insert2.Delete

3.Inorder Traversal

4.Preorder Traversal5.Postorder Traversal

6.Display

7.Quit

Enter your choice : 423 234 345 2 43 467

1.Insert

2.Delete

3.Inorder Traversal4.Preorder Traversal

5.Postorder Traversal6.Display

7.Quit

Enter your choice : 5

467 43 2 345 234 231.Insert

2.Delete

3.Inorder Traversal4.Preorder Traversal

5.Postorder Traversal

6.Display7.Quit

Enter your choice : 7

Page 10: 2nd Sem Prog.

8/8/2019 2nd Sem Prog.

http://slidepdf.com/reader/full/2nd-sem-prog 10/35

*//* Program of circular queue using array*/

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

#include<process.h>

# define MAX 5

int cqueue_arr[MAX];

int front = -1;int rear = -1;

void insert();

void del();void display();

main()

{clrscr();

int choice;while(1)

{

 printf("1.Insert\n");

 printf("2.Delete\n"); printf("3.Display\n");

 printf("4.Quit\n");

 printf("Enter your choice : ");scanf("%d",&choice);

switch(choice){

case 1 :

insert(); break;

case 2 :

del();

 break;case 3:

display();

 break;case 4:

exit(1);

default: printf("Wrong choice\n");

}/*End of switch*/

}/*End of while */

}/*End of main()*/

Page 11: 2nd Sem Prog.

8/8/2019 2nd Sem Prog.

http://slidepdf.com/reader/full/2nd-sem-prog 11/35

void insert()

{int added_item;

if((front == 0 && rear == MAX-1) || (front == rear+1))

{ printf("Queue Overflow \n");

return;

}if (front == -1) /*If queue is empty */

{

front = 0;

rear = 0;}

else

if(rear == MAX-1)/*rear is at last position of queue */

rear = 0;else

rear = rear+1; printf("Input the element for insertion in queue : ");

scanf("%d", &added_item);

cqueue_arr[rear] = added_item ;

}/*End of insert()*/

void del()

{if (front == -1)

{

 printf("Queue Underflow\n");return ;

}

 printf("Element deleted from queue is : %d\n",cqueue_arr[front]);if(front == rear) /* queue has only one element */

{

front = -1;

rear=-1;}

else

if(front == MAX-1)front = 0;

else

front = front+1;}/*End of del() */

void display()

{

Page 12: 2nd Sem Prog.

8/8/2019 2nd Sem Prog.

http://slidepdf.com/reader/full/2nd-sem-prog 12/35

int front_pos = front,rear_pos = rear;

if(front == -1)

{ printf("Queue is empty\n");

return;

} printf("Queue elements :\n");

if( front_pos <= rear_pos )

while(front_pos <= rear_pos){

 printf("%d ",cqueue_arr[front_pos]);

front_pos++;

}else

{

while(front_pos <= MAX-1)

{ printf("%d ",cqueue_arr[front_pos]);

front_pos++;}

front_pos = 0;

while(front_pos <= rear_pos)

{ printf("%d ",cqueue_arr[front_pos]);

front_pos++;

}}/*End of else */

 printf("\n");

}/*End of display() */

/

*********************************OUTPUT*************************************

1.Insert

2.Delete3.Display

4.Quit

Enter your choice : 1Input the element for insertion in queue : 345

1.Insert

2.Delete3.Display

4.Quit

Enter your choice : 1

Input the element for insertion in queue : 365

Page 13: 2nd Sem Prog.

8/8/2019 2nd Sem Prog.

http://slidepdf.com/reader/full/2nd-sem-prog 13/35

1.Insert2.Delete

3.Display

4.QuitEnter your choice : 1

Input the element for insertion in queue : 234

1.Insert2.Delete

3.Display

4.Quit

Enter your choice : 1Input the element for insertion in queue : 2

1.Insert

2.Delete

3.Display4.Quit

Enter your choice : 3Queue elements :

34 345 365 234 2

1.Insert

2.Delete3.Display

4.Quit

Enter your choice : 2Element deleted from queue is : 34

1.Insert

2.Delete3.Display

4.Quit

Enter your choice : 3Queue elements :

345 365 234 2

1.Insert

2.Delete3.Display

4.Quit

Enter your choice : 4

Page 14: 2nd Sem Prog.

8/8/2019 2nd Sem Prog.

http://slidepdf.com/reader/full/2nd-sem-prog 14/35

*/program of double linked list/*

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

#include <conio.h>

struct dllist {

int number;

struct dllist *next;

struct dllist *prev;};

struct dllist *head, *tail;

void append_node(struct dllist *lnode);

void insert_node(struct dllist *lnode, struct dllist *after);void remove_node(struct dllist *lnode);

void main(void) {

clrscr();

struct dllist *lnode;

int i = 0;

/* add some numbers to the double linked list */

for(i = 0; i <= 5; i++) {lnode = (struct dllist *)malloc(sizeof(struct dllist));

lnode->number = i;

append_node(lnode);}

/* print the dll list */

printf("\n->>");

for(lnode = head; lnode != NULL; lnode = lnode->next) {

printf("%d->", lnode->number);}

/* destroy the dll list */while(head != NULL)

remove_node(head);

getch();

Page 15: 2nd Sem Prog.

8/8/2019 2nd Sem Prog.

http://slidepdf.com/reader/full/2nd-sem-prog 15/35

}

void append_node(struct dllist *lnode) {if(head == NULL) {

head = lnode;

lnode->prev = NULL;} else {

tail->next = lnode;

lnode->prev = tail;}

tail = lnode;

lnode->next = NULL;}

void insert_node(struct dllist *lnode, struct dllist *after) {

lnode->next = after->next;lnode->prev = after;

if(after->next != NULL)

after->next->prev = lnode;

else

tail = lnode;

after->next = lnode;

}

void remove_node(struct dllist *lnode) {

if(lnode->prev == NULL)head = lnode->next;

else

lnode->prev->next = lnode->next;

if(lnode->next == NULL)

tail = lnode->prev;

elselnode->next->prev = lnode->prev;

}

/

*********************************OUTPUT*******************************

******

->>0->1->2->3->4->5->

Page 16: 2nd Sem Prog.

8/8/2019 2nd Sem Prog.

http://slidepdf.com/reader/full/2nd-sem-prog 16/35

*//*program for heap sort*/

#include<stdio.h>

#include<conio.h>

void restoreHup(int*,int);

void restoreHdown(int*,int,int);

void main()

{

clrscr();

int a[20],n,i,j,k; printf("Enter the number of elements to sort : ");

scanf("%d",&n);

 printf("Enter the elements :");

for(i=1;i<=n;i++){

scanf("%d",&a[i]);restoreHup(a,i);

}

 j=n;

for(i=1;i<=j;i++){

int temp;

temp=a[1];a[1]=a[n];

a[n]=temp;

n--;

restoreHdown(a,1,n);}

n=j;

 printf("\Here is it...");

for(i=1;i<=n;i++) printf("%4d",a[i]);

getch();

}

Page 17: 2nd Sem Prog.

8/8/2019 2nd Sem Prog.

http://slidepdf.com/reader/full/2nd-sem-prog 17/35

void restoreHup(int *a,int i){

int v=a[i];

while((i>1)&&(a[i/2]<v))

{

a[i]=a[i/2];i=i/2;

}

a[i]=v;

}

void restoreHdown(int *a,int i,int n)

{

int v=a[i];int j=i*2;

while(j<=n){

if((j<n)&&(a[j]<a[j+1]))

 j++;

if(a[j]<a[j/2]) break;

a[j/2]=a[j];

 j=j*2;}

a[j/2]=v;

}

/

**********************************OUTPUT************************************

Enter the number of elements to sort : 6

Enter the elements :4565

23

4672

456

Here is it... 2 23 45 65 456 467

Page 18: 2nd Sem Prog.

8/8/2019 2nd Sem Prog.

http://slidepdf.com/reader/full/2nd-sem-prog 18/35

*/ insertion sort/*

#include<conio.h>

#include<stdio.h>void insertsort(int *,int);

void main()

{

int *a,i,n;clrscr();

 printf("\nEnter the size of the array..:");

scanf("%d",&n);

for(i=0;i<n;i++){

 printf("\nEnter a[%d]..:",i);scanf("%d",&a[i]);

}

insertsort(a,n); printf("\nThe sorted order is..:");

for(i=0;i<n;i++) printf("%d ",a[i]);

getch();

}void insertsort(int *a,int n)

{

int i,temp,j;for(i=1;i<n;i++) {

temp=a[i];

for(j=i-1;j>=0;j--)

{if(a[j]>temp)

a[j+1]=a[j];

else break;}

a[j+1]=temp;

}}

Page 19: 2nd Sem Prog.

8/8/2019 2nd Sem Prog.

http://slidepdf.com/reader/full/2nd-sem-prog 19/35

/

************************************OUTPUT****************************

******

Enter the size of the array..:6

Enter a[0]..:34

Enter a[1]..:234

Enter a[2]..:2

Enter a[3]..:455

Enter a[4]..:4

Enter a[5]..:56

The sorted order is..:2 4 34 56 234 455

Page 20: 2nd Sem Prog.

8/8/2019 2nd Sem Prog.

http://slidepdf.com/reader/full/2nd-sem-prog 20/35

*//*Shell Sort */

#include<stdio.h>

#include<conio.h>

void shellsort(int a[],int n)

{

int j,i,k,m,mid;

for(m = n/2;m>0;m/=2){

for(j = m;j< n;j++){

for(i=j-m;i>=0;i-=m)

{

if(a[i+m]>=a[i]) break;

else

{mid = a[i];

a[i] = a[i+m];

a[i+m] = mid;}

}

}}

}

main(){

int a[10],i,n;

clrscr();

 printf("Enter The number Of Elements\t: ");

scanf("%d",&n);for(i=0;i< n;i++)

{

 printf("\nElement %d\t: ",i+1);

scanf("%d",&a[i]);

Page 21: 2nd Sem Prog.

8/8/2019 2nd Sem Prog.

http://slidepdf.com/reader/full/2nd-sem-prog 21/35

}

 printf("\nArray Befor Sorting : ");for(i=0;i< n;i++)

 printf("%5d",a[i]);

shellsort(a,n);

 printf("\nArray After Sorting : ");

for(i=0;i< n;i++) printf("%5d",a[i]);

getch();

return 0;

}

/*************************************

OUTPUT*******************************************

Enter The number Of Elements : 5

Element 1 : 21

Element 2 : 36

Element 3 : 54

Element 4 : 2

Element 5 : 0

Array Befor Sorting : 21 36 54 2 0

Array After Sorting : 0 2 21 36 54

Page 22: 2nd Sem Prog.

8/8/2019 2nd Sem Prog.

http://slidepdf.com/reader/full/2nd-sem-prog 22/35

*//* Singly Link list */

# include<stdio.h>

# include<conio.h>

# include<alloc.h># include<stdlib.h>

struct node

{ int data;

struct node *link;};

void append(struct node **,int);void in_begin(struct node **,int);

void del(struct node **,int);

void in_middle(struct node **,int,int);

int count(struct node *);void display(struct node *);

void main()

{clrscr();struct node *p; /* p can be said as the head or a start ptr */

p=NULL;

/* Printing the menu */int num,loc;

char choice;

do{ clrscr();;

 printf("\n\n1.Create \\ Appending The List");

 printf("\n2.Insert Node At Begining");

 printf("\n3.Insert Node In Middle"); printf("\n4.Deleting a Node");

 printf("\n5.Counting The No Of Nodes");

 printf("\n6.Displaying the list"); printf("\n7.Exit");

oper:

gotoxy(1,15);printf(" ");gotoxy(1,11);printf("\n\nEnter ur Choice : ");

choice=getch();

switch(choice)

{ case '1':

Page 23: 2nd Sem Prog.

8/8/2019 2nd Sem Prog.

http://slidepdf.com/reader/full/2nd-sem-prog 23/35

char ans;

do

{ printf("Enter any number : ");scanf("%d",&num);

append(&p,num);

printf("Enter more (y/n) :");fflush(stdin);

ans=getchar();

}while(ans !='n');break;

case '2':

printf("Enter The Data : ");

scanf("%d",&num);in_begin(&p,num);

break;

case '3':printf("\nEnter The Position :");

scanf("%d",&loc);printf("\nEnter The Data : ");

scanf("%d",&num);

in_middle(&p,loc,num);

break;

case '4':

printf("\nEnter The Data u Want To Delete : ");scanf("%d",&num);

del(&p,num);

break;

case '5':

printf("\nThe No Of Nodes Are %d",count(p));getch();

break;

case '6':display(p);

getch();

break;

case '7':

printf("\n\nQuiting.......");getch();

exit(0);

break;

Page 24: 2nd Sem Prog.

8/8/2019 2nd Sem Prog.

http://slidepdf.com/reader/full/2nd-sem-prog 24/35

Page 25: 2nd Sem Prog.

8/8/2019 2nd Sem Prog.

http://slidepdf.com/reader/full/2nd-sem-prog 25/35

{ printf("Empty Link List.\n");

getch();

goto last;}

while(q!=NULL)

{ c++; q=q->link;

}

last:return c;

}

void in_begin(struct node **q,int num)

{ struct node *temp;

if(*q==NULL)

{ printf("Link List Is Empty.Can't Insert.");getch();

goto last;}

else

{ temp=(struct node *)malloc(sizeof(struct node));

temp->data=num;temp->link=*q;

*q=temp; /* pointing to the first node */

}last:

getch();

}

void in_middle(struct node **q,int loc,int num)

{ struct node *temp,*n;int c=1,flag=0;

temp=*q;

if(*q==NULL)

{ printf("\n\nLink List Is Empty.Can't Insert.");getch();

goto last;

}else

while(temp!=NULL)

{ if(c==loc){ n = (struct node *)malloc(sizeof(struct node));

n->data=num;

n->link=temp->link;

temp->link=n;

Page 26: 2nd Sem Prog.

8/8/2019 2nd Sem Prog.

http://slidepdf.com/reader/full/2nd-sem-prog 26/35

flag=1;

}

c++;temp=temp->link;

}

if(flag==0){ printf("\n\nNode Specified Doesn't Exist.Cant Enter The Data");

getch();

}else

{ printf("Data Inserted");

getch();

}last:

getch();

}

void del(struct node**q,int num)

{ if(*q==NULL){ printf("\n\nEmpty Linked List.Cant Delete The Data.");

getch();

goto last;

}else

{

struct node *old,*temp;int flag=0;

temp=*q;

while(temp!=NULL){ if(temp->data==num)

{ if(temp==*q) /* First Node case */

*q=temp->link; /* shifted the header node */else

old->link=temp->link;

free(temp);flag=1;

}

else{ old=temp;

temp=temp->link;

}}

if(flag==0)

 printf("\nData Not Found...");

else

Page 27: 2nd Sem Prog.

8/8/2019 2nd Sem Prog.

http://slidepdf.com/reader/full/2nd-sem-prog 27/35

Page 28: 2nd Sem Prog.

8/8/2019 2nd Sem Prog.

http://slidepdf.com/reader/full/2nd-sem-prog 28/35

Data Deleted...Tap a key to continue

1.Create \ Appending The List

2.Insert Node At Begining

3.Insert Node In Middle4.Deleting a Node

5.Counting The No Of Nodes

6.Displaying the list7.Exit

Enter ur Choice : 5

The No Of Nodes Are 5

1.Create \ Appending The List

2.Insert Node At Begining3.Insert Node In Middle

4.Deleting a Node5.Counting The No Of Nodes

6.Displaying the list

7.Exit

Enter ur Choice : 6

45

7078

89

90

*/

Page 29: 2nd Sem Prog.

8/8/2019 2nd Sem Prog.

http://slidepdf.com/reader/full/2nd-sem-prog 29/35

*/ transpose of sparse matrices/*

#include<stdio.h>

#include<conio.h>void main()

{

clrscr();

int a[10][10],b[10][10];int m,n,p,q,t,col;

int i,j;

printf("enter the no of row and columns :\n");

scanf("%d %d",&m,&n);

// assigning the value of matrix

for(i=1;i<=m;i++){

for(j=1;j<=n;j++)

{printf("a[%d][%d]= ",i,j);

scanf("%d",&a[i][j]);

}}

 printf("\n\n");

//displaying the matrix

printf("\n\nThe matrix is :\n\n");

for(i=1;i<=m;i++)

{

for(j=1;j<=n;j++){

printf("%d",a[i][j]);

}

printf("\n");

Page 30: 2nd Sem Prog.

8/8/2019 2nd Sem Prog.

http://slidepdf.com/reader/full/2nd-sem-prog 30/35

}

t=0;

 printf("\n\nthe non zero value matrix are :\n\n");

for(i=1;i<=m;i++)

{for(j=1;j<=n;j++)

{

// accepting only non zero valueif(a[i][j]!=0)

{

t=t+1;

b[t][1]=i;b[t][2]=j;

b[t][3]=a[i][j];} }

}

// displaying the matrix of non-zero value

 printf("\n");

 printf("a[0 %d %d %d\n",m,n,t);

for(i=1;i<=t;i++)

{printf("a[%d %d %d %d \n",i,b[i][1],b[i][2],b[i][3]);

}

 b[0][1]=n; b[0][2]=m; b[0][3]=t;

q=1;

// transpose of the matrix

 printf("\n\nthe transpose of the matrix :\n ");

if(t>0)

{

for(i=1;i<=n;i++){

for(j=1;j<=t;j++)

{

if(b[j][2]==i)

Page 31: 2nd Sem Prog.

8/8/2019 2nd Sem Prog.

http://slidepdf.com/reader/full/2nd-sem-prog 31/35

{

a[q][1]=b[j][2]; a[q][2]=b[j][1];

a[q][3]=b[j][3]; q=q+1;} }

} }

 printf("\n\n");

 printf("a[0 %d %d %d\n",b[0][1],b[0][2],b[0][3]);

for(i=1;i<=t;i++)

{

printf("a[%d %d %d %d\n",i,a[i][1],a[i][2],a[i][3]);

}getch();

}

/*********************************OUTPUT*******************************

******

enter the no of row and columns :

3

3a[1][1]= 12

a[1][2]= 23

a[1][3]= 1a[2][1]= 2

a[2][2]= 34

a[2][3]= 22a[3][1]= 36

a[3][2]= 67

a[3][3]= 15

The matrix is :

1223123422

366715

the non zero value matrix are :

a[0 3 3 9

a[1 1 1 12

a[2 1 2 23

Page 32: 2nd Sem Prog.

8/8/2019 2nd Sem Prog.

http://slidepdf.com/reader/full/2nd-sem-prog 32/35

a[3 1 3 1

a[4 2 1 2

a[5 2 2 34a[6 2 3 22

a[7 3 1 36

a[8 3 2 67a[9 3 3 15

the transpose of the matrix :

a[0 3 3 9

a[1 1 1 12

a[2 1 2 2a[3 1 3 36

a[4 2 1 23a[5 2 2 34

a[6 2 3 67

a[7 3 1 1

a[8 3 2 22a[9 3 3 15

*/

Page 33: 2nd Sem Prog.

8/8/2019 2nd Sem Prog.

http://slidepdf.com/reader/full/2nd-sem-prog 33/35

Page 34: 2nd Sem Prog.

8/8/2019 2nd Sem Prog.

http://slidepdf.com/reader/full/2nd-sem-prog 34/35

{

if(d.f==100)

printf("overflow\n");else

d.a[d.f++]=number;

}display();

break;

case 2:int number=-50;if(d.f==0)

printf("underflow\n");

else

number=d.a[--d.f];if(number!=-50)

printf("the number is deleted\n");

display();

break;case 3:exit(0);

default:printf("wrong choice\n");}

}while(choice!=3);

getch();

}

/

**********************************OUTPUT*************************************

1-push2-pop

3-exit

enter the choice1

enter the number 

65

the stack is65

54

231-push

2-pop

3-exitenter the choice

1

enter the number 

89

Page 35: 2nd Sem Prog.

8/8/2019 2nd Sem Prog.

http://slidepdf.com/reader/full/2nd-sem-prog 35/35

the stack is

89

6554

23

1-push2-pop

3-exit

enter the choice1

enter the number 

90

the stack is90

89

65

5423

1-push2-pop

3-exit

enter the choice

2the number is deleted

the stack is

8965

54

231-push

2-pop

3-exitenter the choice

3

*/