Welcome To Uday Satya Blog

Showing posts with label DS. Show all posts
Showing posts with label DS. Show all posts

INDEX of These 26 PROGRAMS




LIST  OF  PROGRAMS

                                  2 - DIMENSIONAL ARRAYS


1.    WRITE A PROGRAM TO READ AND ENTER THE ELEMENTS INTO THE MATRIX .
2.    WRITE A PROGRAM TO READ THE ELEMENTS INTO THE MATRIX AND ADD THEM .
3.    WRITE A PROGRAM TO READ THE ELEMENTS INTO THE MATRIX AND SUBSTRACT THEM .
4.    WRITE A PROGRAM TO READ THE ELEMENTS INTO THE MATRIX AND MULTIPLY THEM .
5.    WRITE A PROGRAM TO TAKE THE DIMENSIONS OF THE MATRIX   AND  ADD THEM .
6.    WRITE A PROGRAM TO TAKE THE DIMENSIONS OF THE MATRIX AND SUBSTRACT THEM ,
7.    WRITE A PROGRAM TO TAKE THE DIMENSION OF THE MATRIX AND MULTIPLY THEM .
8.    WRITE A PROGRAM TO FIND THE TRACE OF THE GIVEN MATRIX.
9.    WRITE A PROGRAM TO FIND THE TRANSPOSE OF THE GIVEN MATRIX .
10.    WRITE A PROGRAM TO FIND THE UPPER TRIANGLE OF THE GIVEN MATRIX .
11.    WRITE A PROGRAM TO FIND THE LOWER TRIANGLE OF THE GIVEEN MATRIX .


                                         STACKS


12.    WRITE A PROGRAM TO PERFORM STACK OPERATIONS .
13.    WRITE A PROGRAM TO PERFORM STACK OPERATIONS USING LINKED LIST.


                                         QUEUES


14.    WRITE A PROGRAM TO PERFORM  QUEUE OPERATIONS .
15.    WRITE A PROGRAM TO PERFORM  QUEUE OPERATIONS USING LINKED LIST .


                                         LINKED LISTS


16.    WRITE A PROGRAM TO INSERT AN ELEMENT AT THE BEGINING
17.    WRITE A PROGRAM TO DELETE AN  ELEMENT AT THE  BEGINNING
18.    WRITE A PROGRAM TO INSERT AN ELEMENT IN BETWEEN TWO ELEMENTS .
19.    WRITE A PROGRAM TO DELETE AN ELEMENTS IN BETWEEN TWO ELEMENTS .
20.    WRITE A PROGRAM TO INSERT AN ELEMENT AT THE END .
21.    WRITE A PROGRAM TO DELETE AN ELEMENT AT THE END .


                                         SEARCHING


22.    WRITE A PROGRAM TO SEARCH AN ELEMENT USING BUBBLE SORT .
23.    WRITE A PROGRAM TO SEARCH AN ELEMENT USING QUICK SORT .
24.    WRITE A PROGRAM TO MERGE TWO  SORTED FILES .
25.    WRITE A PROGRAM TO SEARCH AN ELEMENT USING LINEAR SEARCH .
26.    WRITE A PROGRAM TO SEARCH AN ELEMENT USING BINARY SEARCH .

26 . BINARY SEARCH



/* ****************************************************
      26. TO SEARCH ELEMENT USING BINARY SEARCH .
 **************************************************** */
#include<*stdio.h>
#include<*conio.h>
void main()
{
int a[10],n,i,mid,key,low,high,flag;
flag=1;
clrscr();
printf("\n enter the total number of elements : ");
scanf("%d",&n);
printf("\n enter sorted elements only : ");
for(i=0;i
{
scanf("%d",&a[i]);
}
printf("\n enter key element to be serched : ");
scanf("%d",&key);
low=0;
high=n-1;
while(low<=high&&flag==1)
{
mid=(low+high)/2;
if(key
high=mid-1;
else if(key>a[mid])
low=mid+1;
else
{
if(key==a[mid])
printf("\nelement found at position : %d ",mid+1);
flag=0;
}
}
if(flag)
{
printf("\n element not found ");
}
else
{
("\n search succesful");
}
getch();
}

output

 enter the total number of elements : 5

 enter sorted elements only : 1 2 3 4 5

 enter key element to be serched : 5

 element is foud at position : 5

 enter the total number of elements : 5

 enter sorted elements only : 1 2 3 4 5

 enter key element to be serched : 6

 element is not found

25 . LINEAR SEARCH



/* ********************************************* ******
      25. TO SEARCH ELEMENT USING LINEAR SEARCH
    ************************************************* */
#include<*stdio.h>
#include<*conio.h>
int lsearch(int a[],int n,int key);
void main()
{
int a[10],i,n,key,l;
clrscr();
printf("\n enter the total number of elements : ");
scanf("%d",&n);
printf("\n enter the element to be serched : ");
for(i=0;i
{
scanf("%d",&a[i]);
}
printf("\n enter the element to be searched : ");
scanf("%d",&key);
l=lsearch(a,n,key);
if(l==-1)
printf("\n element is not found");
else
printf("\n element is found at the position : %d ",l+1);
getch();
}

int lsearch(int a[],int n,int key)
{
int t=-1,i;
for(i=0;i
if(a[i]==key)
t=i;
return t;
}

outputs

 enter the total number of elements : 5

 enter the elements : 1 2 3 4 5

 enter the element to be searched : 3

 element is found at position : 3

 enter the total number of elements : 5

 enter the elements : 1 2 3 4 5

 enter the element to be searched : 6

 element is not found

24 . MERGE SORT



/* ***************************************************
     24. TO SEARCH ELEMENTS USING MERGE SORT .
  ************************************************** */
#include<*stdio.h>
#include<*conio.h>
void msort(int a[],int b[],int c[],int n1,int n2,int n3);
int a[50],b[50],c[50],n1,n2,n3,i;
void main()
{
printf("Enter size of a\n");
scanf("%d",&n1);
printf("Enter the elements of a");
for(i=0;i
    scanf("%d",&a[i]);
printf("Enter size of b\n");
scanf("%d",&n2);
printf("Enter the elements of b");
for(i=0;i
    scanf("%d",&b[i]);
n3=n1+n2;
msort(a,b,c,n1,n2,n3);
printf("Elements after sorting are:");
for(i=0;i
    printf("%2d\n",c[i]);
getch();
}

void msort(int a[],int b[],int c[],int n1,int n2,int n3)
{
int apoint,bpoint,cpoint,alimit,blimit,climit;
apoint=0;bpoint=0;
alimit=n1-1;blimit=n2-1;
if(n1+n2 != n3)
    printf("Array bounds Incompatable");
for(cpoint=0;apoint<=alimit&&bpoint<=blimit;cpoint++)
    {
    if(a[apoint]
        c[cpoint]=a[apoint++];
    else
        c[cpoint]=b[bpoint++];
    }
while(apoint<=alimit)
    c[cpoint++]=a[apoint++];
while(bpoint<=blimit)
    c[cpoint++]=b[bpoint++];
}

 output

 Enter Size of a : 4

enter the elements of a : 1  4  5  7

enter size of b : 4

Enter the elements of b : 2  3  6  8

elements after sorting :  1   2   3   4  5  6  7  8

23 . QUICK SORT


/* ***************************************************
     23. TO SEARCH ELEMENTS USING QUICK SORT .
  ************************************************** */
#include<*stdio.h>
#include<*conio.h>
#define MAXSIZE 50
void quicksort(int a[],int n);
void sort(int a[],int left,int right);
int a[MAXSIZE],n;
void main()
{
int i,n;
clrscr();
printf("\n how many elements are there : ");
scanf("%d",&n);
printf("\n\n enter the elements : ");
for(i=0;i
{
scanf("%d", &a[i]);
}
printf("\n\n elements before sorting : ");
for(i=0;i
{
printf("%d\t",a[i]);
}
quicksort(a,n);
printf("\n\n elements after sorting : ");
for(i=0;i
{
printf("%d\t",a[i]);
}
getch();
}

void quicksort(int a[],int n)
{
sort(a,0,n-1);
}

void sort(int a[],int left,int right)
{
int pivot,l,r;
l=left;
r=right;
pivot=a[l];
while(left
{
while((a[right]>=pivot)&&(left
right--;
if(left!=right)
{
a[left]=a[right];
left++;
}
while((a[left]<=pivot)&&(left
left++;
if(left!=right)
{
a[left]=a[right];
right--;
}
}
a[left]=pivot;
pivot=left;
left=l;
right=r;
if(left
sort(a,left,pivot-1);
if(right>pivot)
sort(a,pivot+1,right);

}

 output

 how many elements are there : 4


 enter the elements : 9  4  8  5


 elements before sorting :  9    4       8       5

 elements after sorting :  4     5      8        9

22 . BUBBLE SORTING



/* *************************************************
     22. TO SEARCH ELEMENTS USING BUBBLE SORTING .
  *************************************************** */
#include<*stdio.h>
#include<*conio.h>
#define MAXSIZE 50
void bubblesort(int arr[],int n);
int arr[MAXSIZE],n;
void main()
{
int i;
clrscr();
printf("\n how many elements are there : ");
scanf("%d",&n);
printf("\n\n enter the elements : ");
for(i=0;i
scanf("\t%d",&arr[i]);
printf("\n\n elements before sorting : ");
for(i=0;i
printf("\t%d",arr[i]);
bubblesort(arr,n);
printf("\n\n elements after sorting : ");
for(i=0;i
printf("\t%d",arr[i]);
getch();
}
void bubblesort(int arr[],int n)
{
int i,j,temp;
for(i=0;i
{
for(j=0;j
{
if(arr[i]
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
}

output

 how many elements are there : 3

 enter the elements : 3 2 1

 elements before sorting :      3       2       1

 elements after sorting :      1       2      3

21 . DELETE END LL



/* *************************************************
          21 . TO DELETE AN ELEMENT AT THE END
 ************************************************** */
#include<*stdio.h>
#include<*conio.h>
#include<*malloc.h>
void main()
{
struct link
{
int info;
struct link*ptr;
}*head,*cur,*temp;
char ch='y';
clrscr();
head=(struct link*)malloc(sizeof(struct link));
printf("\nenter an element :");
scanf("%d",&head->info);
head->ptr=NULL;
cur=head;
do
{
temp=(struct link*)malloc(sizeof(struct link));
printf("\nenter number to be inserted :");
scanf("%d",&temp->info);
printf("\ndo you want to insert more.....");
ch=getch();
while(cur->ptr!=NULL)
{
cur=cur->ptr;
}
temp->ptr=NULL;
cur->ptr=temp;
}while(ch=='y');
cur=head;
temp=cur->ptr;
do
{
cur=cur->ptr;
temp=temp->ptr;
}while(temp->ptr!=NULL);
cur->ptr=NULL;
free(temp);
cur=head;
printf("\nelements after deletion :\n");
while(cur!=NULL)
{
printf("%d\t",cur->info);
cur=cur->ptr;
}
getch();
}

output

enter an element : 1

enter number to be inserted : 2

do you want to insert more(yes,no) :

 enter number to be inserted : 3

 do you want to insert more(yes,no) :

 elements after deletion :
         1       2

20 . INSERT END LL


/* *************************************************
          20 . TO INSERT AN ELEMENT AT THE END
 ************************************************** */
#include<*stdio.h>
#include<*conio.h>
#include<*malloc.h>
void main()
{
struct link
{
int info;
struct link*ptr;
}*head,*cur,*temp;
char ch=’y’;
clrscr();
head=(struct link*)malloc(sizeof(struct link));
printf(“\n enter an element : “);
scanf(“%d”,&head->info);
head->ptr=NULL;
cur=head;
do
{
temp=(struct link*)malloc(sizeof(struct link));
printf(“\n\n enter number to be inserted : “);
scanf(“%d”,&temp->info);
printf(“\n\n do you want to insert more (yes,no) :”);
ch=getch();
while(cur->ptr!=NULL)
{
cur=cur->ptr;
}
temp->ptr=NULL;
cur->ptr=temp;
}while(ch==’y’);
printf(“\n\n the elements after deletion “);
printf(“\n”);
cur=head;
while(cur!=NULL)
{
printf(“\t%d”,cur->info);
cur=cur->ptr;
}
getch();
}

 output

 enter an element : 1
 enter number to be inserted : 2

 do you want to insert more (yes,no) :
 enter number to be inserted : 3

 do you want to insert more (yes,no) :

 the elements after deletion
        1       2       3

19 . DELETE BETWEEN LL


/* ****************************************************
  19 . TO DELETE ELEMENT INBETWN TWO ELEMENTS
  ************************************************** */
#include<*stdio.h>
#include<*conio.h>
#include<*malloc.h>
void main()
{
struct link
{
int info;
struct link*ptr;
};
int ele;
struct link*head,*cur,*temp;
char ch='y';
clrscr();
head=(struct link*)malloc(sizeof(struct link));
printf("\n enter an head element : ");
scanf("%d",&head->info);
cur=head;
cur->ptr=NULL;
do
{
temp=(struct link*)malloc(sizeof(struct link));
printf("\n\n enter number to be inserted : ");
scanf("%d",&temp->info);
printf("\n\n do you want to insert more (yes,no) : ");
ch=getch();
temp->ptr=NULL;
cur->ptr=temp;
cur=cur->ptr;
}while(ch=='y');
cur=head;
printf("\n\n elements before deletion : ");
while(cur!=NULL)
{
printf("%d\t",cur->info);
cur=cur->ptr;
}
cur=head;
temp=cur->ptr;
printf("\n\n enter the element to be deleted : ");
scanf("%d",&ele);
while(temp->info!=ele&&cur!=NULL)
{
cur=cur->ptr;
temp=temp->ptr;
}
if(temp->info==ele)
{
if(temp->ptr==NULL)
cur->ptr=NULL;
else
cur->ptr=temp->ptr;
}
cur=head;
while(cur!=NULL)
{
printf("%d\t",cur->info);
cur=cur->ptr;
getch();
}
}

output

 enter an head element : 1

 enter number to be inserted : 2

 do you want to insert more (yes,no) :

 enter number to be inserted : 3

 do you want to insert more (yes,no) :

 elements before deletion : 1    2       3

 enter the element to be deleted : 2

 elements after deletion : 1       3

18 . INSERT BETWEEN LL



/* ***************************************************
    18 . TO INSERT ELEMENT IN BETWN TWO ELEMENTS
   *************************************************** */
#include<*stdio.h>
#include<*conio.h>
#include<*malloc.h>
void main()
{
struct link
{
int info;
struct link*ptr;
};
int ele;
struct link*head,*cur,*temp,*temp1;
char ch='y';
clrscr();
head=(struct link*)malloc(sizeof(struct link));
temp1=(struct link*)malloc(sizeof(struct link));
printf("\n enter the head element : ");
scanf("%d",&head->info);
cur=head;
cur->ptr=NULL;
do
{
temp=(struct link*)malloc(sizeof(struct link));
printf("\n\n enter number to be inserted : ");
scanf("%d",&temp->info);
printf("\n\n do you want to insert more (yes,no) : ");
ch=getch();
temp->ptr=NULL;
cur->ptr=temp;
cur=cur->ptr;
}while(ch=='y');
cur=head;
printf("\n\n elements before insertion : ");
while(cur!=NULL)
{
printf("%d\t",cur->info);
cur=cur->ptr;
}
cur=head;
temp=cur->ptr;
printf("\n\n enter the element before which you want to insert : ");
scanf("%d",&ele);
while(cur->info!=ele&&cur!=NULL)
{
cur=cur->ptr;
}
if(cur->info==ele)
{
printf("\n\n enter new element to be inserted : ");
scanf("%d",&temp1->info);
cur=head;
temp=cur->ptr;
while(temp->info!=ele)
{
cur=cur->ptr;
temp=temp->ptr;
}
cur->ptr=temp1;
temp1->ptr=temp;
cur=head;
printf("\n\n elements after insertion :\n");
while(cur!=NULL)
{
printf("\t %d",cur->info);
cur=cur->ptr;
}
}
else
{
printf("\n\n element does not exist");
}
getch();
}

output

 enter the head element : 1

 enter number to be inserted : 2

 do you want to insert more (yes,no) :

 elements before insertion : 1  2

 enter the element before which you want to insert : 2

 enter new element to be inserted : 3

 elements after insertion :
        1       3       2

17 . DELETE BEGINING LL



/* ****************************************************
      17 . TO DELETE AN ELEMENT AT THE BEGINNING .
     ************************************************** */
#include<*stdio.h>
#include<*conio.h>
#include<*malloc.h>
void main()
{
struct link
{
int info;
struct link*next;
};
struct link*head,*cur,*temp;
char ch='y';
clrscr();
head=(struct link*)malloc(sizeof(struct link));
printf("\n enter head element : ");
scanf("%d",&head->info);
head->next=NULL;
cur=head;
do
{
temp=(struct link*)malloc(sizeof(struct link));
printf("\n\n enter number to be inserted : ");
scanf("%d",&temp->info);
printf("\n\n do you want to insert more (yes,no) : ");
ch=getch();
temp->next=cur;
cur=temp;
}while(ch=='y');
head=cur;
printf("\n\n the elements inserted  before deletion : ");
while(cur!=NULL)
{
printf("%d\t",cur->info);
cur=cur->next;
}
printf("\n\n elements after deletion : ");
cur=head;
temp=cur->next;
free(cur);
cur=temp;
while(cur!=NULL)
{
printf("%d\t",cur->info);
cur=cur->next;
}
getch();
}

output

 enter head element : 1

 enter number to be inserted : 2

 do you want to insert more (yes,no) :

 the elements inserted  before deletion : 2     1

 elements after deletion : 1

16 . INSERT BEGINNING LL



/* ****************************************************
       16 . TO INSERT AN ELEMENT AT THE BEGINING
   ************************************************** */
#include<*stdio.h>
#include<*conio.h>
#include<*malloc.h>
void main()
{
struct link
{
int info;
struct link*next;
};
struct link*head,*cur,*temp;
char ch='y';
clrscr();
head=(struct link*)malloc(sizeof(struct link));
printf("\n enter head element : ");
scanf("%d",&head->info);
head->next=NULL;
cur=head;
do
{
temp=(struct link*)malloc(sizeof(struct link));
printf("\n\n enter number to be inserted : ");
scanf("%d",&temp->info);
printf("\n do you want to insert more (yes,no) : ");
ch=getch();
temp->next=cur;
cur=temp;
}while(ch=='y');
head=cur;
printf("\n\n");
while(cur!=NULL)
{
printf("\t%d",cur->info);
cur=cur->next;
}
getch();
}

output

 enter head element : 1

 enter number to be inserted : 2

 do you want to insert more (yes,no) :

 enter number to be inserted : 3

 do you want to insert more (yes,no) :

        3       2       1

15 . QUEUE LINKED LIST



/* ****************************************************
    15 . TO PERFORM Q OPERATN USIN LINKED LISTS .
  ************************************************** */
#include<*stdio.h>
#include<*conio.h>
#include<*stdlib.h>
#include<*alloc.h>
struct link
{
int info;
struct link*next;
}*front,*rear,*new_rec,*temp;
void create();
void display();
void insert_rear();
void del_front();
void display()
{
temp=front;
while(temp!=NULL)
{
printf("%d\t",temp->info);
temp=temp->next;
}
}
void create()
{
if(front==NULL)
{
front=(struct link*)malloc(sizeof(struct link));
printf("\n\n enter first node : ");
scanf("%d",&front->info);
front->next=NULL;
rear=front;
}
else
printf("\n\n list already created");
}
void insert_rear()
{
if(front!=NULL)
{
printf("\n\n enter data into node : ");
new_rec=(struct link*)malloc(sizeof(struct link));
scanf("%d",&new_rec->info);
new_rec->next=NULL;
rear->next=new_rec;
rear=new_rec;
}
}
void del_front()
{
if(front==NULL)
{
printf("\n\n queue empty");
}
else
{
if(front==rear)
{
temp=front;
front=rear=NULL;
free(temp);
}
else
{
temp=front;
front=front->next;
free(temp);
}
printf("\n\n display of list after deletion : ");
display();
}}
void main()
{
int ch;
clrscr();
front=rear=NULL;
printf("\n");
printf("\n1.create");
printf("\n2.insert");
printf("\n3.delete");
printf("\n4.display");
printf("\n5.exit");
ch=1;
do
{
switch(ch)
{
case 1 : create();
            printf("\n\n displaying the elements in queue : ");
            display();
            break;
case 2 : insert_rear();
       printf("\n\n displaying the elements in queue after             
             insertion : ");
       display();
       break;
case 3 : printf("\n\n displaying the elements in queue after deletion : ");
       del_front();
       break;
case 4 : printf("\n\n displaying the elements in queue : ");
      display();
       break;
case 5 : printf("\n\n end of the program ");
      getch();
      exit(1);
}
printf("\n\n enter your choice(1-5) : ");
scanf("%d",&ch);
}
while(ch<=5);
getch();
}

OUTPUT

1.create
2.insert
3.delete
4.display
5.exit

 enter first node : 1

 displaying the elements in queue : 1

 enter your choice(1-5) : 2

 enter data into node : 2

 displaying the elements in queue after insertion : 1   2

 enter your choice(1-5) : 2

 enter data into node : 3

 displaying the elements in queue after insertion : 1   2       3

 enter your choice(1-5) : 1

 list already created

 displaying the elements in queue : 1   2       3

 enter your choice(1-5) : 4

 displaying the elements in queue : 1   2       3

 enter your choice(1-5) : 3

 displaying the elements in queue after deletion :

 display of list after deletion : 2     3

 enter your choice(1-5) : 3

 displaying the elements in queue after deletion :

 display of list after deletion : 3

 enter your choice(1-5) : 3

 displaying the elements in queue after deletion :

 display of list after deletion :

 enter your choice(1-5) : 5

 end of the program

14 . QUEUE ARRAY



/* *******************************************
          14 . TO PERFORM QUEUE OPERATIONS .
    ******************************************* */
#include<*stdio.h>
#include<*conio.h>
#include<*stdlib.h>
int a[20],f,r,ch,data,n;
void main()
{
int create();
void insert();
void del();
void display();
void option();
clrscr();
printf("\n enter the size of the queue : ");
scanf("%d",&n);
create();
printf("\n the queue created is : ");
display();
option();
printf("\n\n enter your choice : ");
scanf("%d",&ch);
while((ch>=1)&&(ch<=3))
{
switch(ch)
{
case 1 :  printf("\n enter the element to be inserted : ");
        scanf("%d",&data);
        insert();
        printf("\n the updated queue after insertion is : ");
        display();
        break;
case 2 :  del();
        printf("\n the updated queue after deletion is: ");
       display();
       break;
case 3 : printf("\n end of the program ");
       exit(1);
       break;
}
printf("\n\n enter your choice : ");
scanf("%d",&ch);
}
getch();
}
int create()
{
f=r=0;
while(r
{
printf("\n enter data into the queue : ");
scanf("%d",&data);
a[r]=data;
r++;
}
return(a[r]);
}
void display()
{
int cnt=f;
while(cnt
{
printf("%d\t",a[cnt]);
cnt++;
}
}
void insert()
{
if(r>20)
{
printf("\n queue over flow");
}
else
{
a[r]=data;
r++;
}
}
void del()
{
if(f==r)
{
printf("\n queue is empty");
}
else
{
f++;
}
}
void option()
{
printf("\n");
printf("\n1.insert");
printf("\n2.delete");
printf("\n3.exit");
}

output

 enter the size of the queue : 3

 enter data into the queue : 1

 enter data into the queue : 2

 enter data into the queue : 3

 the queue created is : 1       2       3

 1.insert
 2.delete
 3.exit

 enter your choice : 1

 enter the element to be inserted : 4

 the updated queue after insertion is : 1       2       3       4

 enter your choice : 2

 the updated queue after deletion is: 2     3       4

 enter your choice : 2

 the updated queue after deletion is: 3     4

 enter your choice : 2

 the updated queue after deletion is: 4

 enter your choice : 2

 the updated queue after deletion is:

 enter your choice : 3

 end of the program

13 . STACK LINKED LIST



/* ****************************************************
13.TO PERFORM STACK OPERATNS USIN LINKED LISTS.
   *************************************************** */
#include<*stdio.h>
#include<*conio.h>
#include<*alloc.h>
struct link
{
int info;
struct link*next;
}*start;
void display(struct link*);
struct link*push(struct link*);
struct link*pop(struct link*);
int option();
void display(struct link*rec)
{
while(rec!=NULL)
{
printf("%d\t",rec->info);
rec=rec->next;
}
}
struct link*push(struct link*rec)
{
struct link*new_rec;
printf("\n\n enter the new element : ");
new_rec=(struct link*)malloc(sizeof(struct link));
scanf("%d",&new_rec->info);
new_rec->next=rec;
rec=new_rec;
return(rec);
}
struct link*pop(struct link*rec)
{
struct link*temp;
if(rec==NULL)
{
printf("\n\n stack is empty");
}
else
{
temp=rec->next;
free(rec);
rec=temp;
printf("\n\n after pop operation : ");
display(rec);
}
if(rec==NULL)
{
printf("\n\n stack is empty");
}
return(rec);
}
int option()
{
int ch;
do
{
printf("\n\n 1.push \n 2.pop \n 3.quit");
printf("\n\n enter your choice : ");
scanf("%d",&ch);
if(ch<1||ch>3)
printf("\n incorrect choice");
}while(ch<1||ch>3);
return(ch);
}
void main()
{
struct link*start;
int ch;
clrscr();
start=NULL;
do
{
ch=option();
switch(ch)
{
case 1: start=push(start);
          printf("\n\n after push operation : ");
          display(start);
          break;
case 2: start=pop(start);
          break;
case 3: printf("\n\n end of the program");

}
}
while(ch!=3);
getch();
}

outputs

 1.push
 2.pop
 3.quit

 enter your choice : 1

 enter the new element : 1

 after push operation : 1

 1.push
 2.pop
 3.quit

 enter your choice : 1

 enter the new element : 2

 after push operation : 2       1

 1.push
 2.pop
 3.quit

 enter your choice : 1

 enter the new element : 3

 after push operation : 3       2       1

 1.push
 2.pop
 3.quit

 enter your choice : 2

 after pop operation : 2        1

 1.push
 2.pop
 3.quit

 enter your choice : 2

 after pop operation : 1

 1.push
 2.pop
 3.quit

 enter your choice : 2

 after pop operation :

 stack is empty

 1.push
 2.pop
 3.quit

 enter your choice : 3

 end of the program

12 . STACK ARRAY


/* **************************************
                 12.Basic Operations on Stack
    ************************************** */
#include<*stdio.h>
#include<*conio.h>
#include<*stdlib.h>
int a[20],top;
void main()
{
int ch,n,data;
void push(int);
void pop();
void display();
void option();
clrscr();
printf("\n enter the size of the stack : ");
scanf("%d",&n);
top=0;
 do
{
option();
printf("\n\n enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1 : if(top>=n)
            {
             printf("\n stack size is over flown");
             }
             else
             {
              printf("\n enter the data into the stack : ");
              scanf("%d",&data);
              push(data);
              }
              getch();
              break;
case 2 :  if(top==0)
             {
              printf("\n stack is empty");
              }
              else
              {
              pop();
              getch();
              }
              break;
 case 3 :  if(top==0)
            {
              printf("\n stack is empty");
              }
              else
              {
              display();
              }
              break;
case 4 :  printf("\n end of the program ");
              exit(1);
              break;
}
}while((ch>0)&&(ch<5));
getch();
}

void option()
{
printf("\n");
printf("\n 1 : push");
printf("\n 2 : pop");
printf("\n 3 : display");
printf("\n 4 : exit");
}

void push(int data)
{
a[top]=data;
top++;
printf("\n the pushed element is : %d",data);
}

void pop()
{
top--;
printf("\n the popped element is : %d ",a[top]);
}

 void display()
{
int i;
printf("\n the elements of the stack are :");
for(i=0;i
{
printf("\t%d",a[i]);
}
}

 Outputs

 enter the size of the stack : 3
 1 : push
 2 : pop
 3 : display
 4 : exit

enter your choice : 1

enter the data into the stack : 1

the pushed element is : 1

  1 : push
  2 : pop
  3 : display
  4 : exit

 enter your choice : 1

enter the data into the stack : 2

 the pushed element is : 2

1 : push
2 : pop
3 : display
4 : exit

  enter your choice : 1

 enter the data into the stack : 3

 the pushed element is : 3

 1 : push
 2 : pop
 3 : display
 4 : exit

 enter your choice : 3

 the elements of the stack are :        1       2       3

 1 : push
 2 : pop
 3 : display
 4 : exit

 enter your choice : 1
stack size is over flown

  1 : push
  2 : pop
  3 : display
  4 : exit























 enter your choice : 2

 the popped element is : 3

 1 : push

 2 : pop

 3 : display

 4 : exit

 enter your choice : 2

 the popped element is : 2



 1 : push

 2 : pop

 3 : display

 4 : exit



 enter your choice : 2



 the popped element is : 1



 1 : push

 2 : pop

 3 : display

 4 : exit

 enter your choice : 2

 stack is empty

 1 : push

 2 : pop

 3 : display

 4 : exit

 enter your choice : 4

 end of the program

06 - 11 PROGRAMS


/* ****************************************************
 6.TO TAKE DIMENSIONS OF MATRIX & SUBSTRACT    *************************************************** */
#include<*stdio.h>
#include<*conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],I,j,m,n,p,q;
clrscr();
printf(“\n enter the dimensions of matrix ‘A’ : “);
scanf(“%d %d”,&m,&n);
printf(“\n enter the dimensions of matrix ‘B’ : “);
scanf(“%d %d”,&p,&q);
if((m==p)&&(n==q))
{
printf(“\n matrix substraction is possible”);
printf(“\n”);
printf(“\n enter the elements into the matrix ‘A’ : “);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“\n enter the elements into the matrix ‘B’ : “);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”,&b[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]-b[i][j];
}
}
printf(“\n the sum of two matrices ‘A’ and ‘B’ is : “);
printf(“\n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(“\t %d”,c[i][j]);
}
printf(“\n”);
}
}
else
{
printf(“\n matrix substraction is not possible”);
}
getch();
}

 output

 enter the dimensions of matrix ‘A’ : 3 3

 enter the dimensions of matrix ‘B’ : 3 3

 matrix substraction is possible

 enter the elements into the matrix ‘A’ : 1 1 1 1 1 1 1 1 1

 enter the elements into the matrix ‘B’ : 1 1 1 1 1 1 1 1 1

 the sum of two matrices ‘A’ and ‘B’ is :
         0       0       0
         0       0       0
         0       0       0

enter the dimensions of matrix ‘A’ : 2 3

 enter the dimensions of matrix ‘B’ : 3 2

 matrix substraction is not possible
/* ***************************************************
   7.TO TAKE DIMENSIONS OF MATRIX & MULTIPLY    ************************************************** */
#include<*stdio.h>
#include<*conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j,m,n,p,q;
clrscr();
printf("\n enter the dimensions of matrix 'A' : ");
scanf("%d %d",&m,&n);
printf("\n enter the dimensions of matrix 'B' : ");
scanf("%d %d",&p,&q);
if((n==p)&&(m==q))
{
printf("\n matrix multiplication is possible");
printf("\n");
printf("\n enter the elements into the matrix 'A' : ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n enter the elements into the matrix 'B' : ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=0;
c[i][j]=c[i][j]+(a[i][j]*b[j][i]);
}
}
printf("\n the product of two matrices 'A' and 'B' is : ");
printf("\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\t %d",c[i][j]);
}
printf("\n");
}
}
else
{
printf("\n matrix multiplication is not possible");
}
getch();
}
output

 enter the dimensions of matrix 'A' : 3 3

 enter the dimensions of matrix 'B' : 3 3

 matrix multiplication is possible

 enter the elements into the matrix 'A' : 1 1 1 1 1 1 1 1 1

 enter the elements into the matrix 'B' : 1 1 1 1 1 1 1 1 1

 the product of two matrices 'A' and 'B' is :
         1       1       1
         1       1       1
         1       1       1

 enter the dimensions of matrix 'A' : 2 2

 enter the dimensions of matrix 'B' : 3 3

  matrix multiplication is not possible
/* ***************************************************
     8 .TO FIND THE TRACE OF THE GIVEN MATRIX .
   *************************************************** */
#include<*stdio.h>
#include<*conio.h>
void main()
{
int a[3][3],i,j,sum=0;
clrscr();
printf(“\n enter the elements into the matrix ‘A’ : “);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”,&a[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
{
sum=sum+a[i][j];
}
}
}
printf(“\n the trace of the matrix is : %d “,sum);
getch();
}

output

 enter the elements into the matrix ‘A’ : 1 1 1 1 1 1 1 1 1

 the trace of the matrix is : 3
/* *************************************************
      9 .TO FIND  TRANSPOSE OF  GIVEN MATRIX.
   ************************************************** */
#include<*stdio.h>
#include<*conio.h>
void main()
{
int a[3][3],i,j;
clrscr();
printf(“\n enter the elements into the matrix : “);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“\n the matrix is : “);
printf(“\n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(“\t %d”,a[j][i]);
}
printf(“\n”);
}
getch();
}

output

 enter the elements into the matrix : 1 2 3 1 2 3 1 2 3

 the matrix is :
         1       1       1
         2       2       2
         3       3       3

/* ***************************************************
   10 .TO FIND UPPER TRIANGULAR OF GIVEN MATRIX.
   ************************************************ */
#include<*stdio.h>
#include<*conio.h>
void main()
{
int a[3][3],i,j;
clrscr();
printf(“\n enter the elements into the matrix : “);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“\n the upper triangular matrix is  : “);
printf(“\n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i<=j)
{
printf(“\t %d”,a[i][j]);
}
}
}
getch();
}

output

 enter the elements into the matrix : 1 2 3 4 5 6 7 8 9

 the upper triangular matrix is  :

       1       2        3       5       6       9       


/* **************************************************
      11. TO FIND LOWER TRIANGLE OF GIVEN MATRIX .
    ************************************************* */
#include<*stdio.h>
#include<*conio.h>
void main()
{
int a[3][3],i,j;
clrscr();
printf(“\n enter the elements into the matrix ‘A’ : “);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“\n the lower triangular of matrix ‘A’ is : “);
printf(“\n\n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i>=j)
{
printf(“\t%d”,a[i][j]);
}
}
}
getch();
}

output

 enter the elements into the matrix ‘A’ : 1 2 3 4 5 6 7 8 9

 the lower triangular of matrix ‘A’ is :
    
        1       4       5       7       8       9
     

01 - 05 PROGRAMS


/* ****************************************************
 1 . TO READ & ENTER ELEMENTS INTO THE MATRIX   **************************************************** */
#include<*stdio.h>
#include<*conio.h>
void main()
{
int a[3][3],i,j;
clrscr();
printf("\n enter the elements into the matrix : ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n the matrix is : ");
printf("\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\t %d",a[i][j]);
}
printf("\n");
}
getch();
}

output

 enter the elements into the matrix : 1 1 1 1 1 1 1 1 1

 the matrix is :
         1       1       1
         1       1       1
         1       1       1

/* *****************************************
                2 . TO ADD TWO MATRICES
    ***************************************** */
#include<*stdio.h>
#include<*conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j;
clrscr();
printf("\n enter the elements into the matrix 'A' : ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n enter the elements into the matrix 'B' : ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("\n the sum of two matrices 'A' and 'B' is : ");
printf("\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\t %d",c[i][j]);
}
printf("\n");
}
getch();
}

output

 enter the elements into the matrix 'A' : 1 1 1 1 1 1 1 1 1

 enter the elements into the matrix 'B' : 1 1 1 1 1 1 1 1 1

 the sum of two matrices 'A' and 'B' is :
         2       2       2
         2       2       2
         2       2       2

/* *********************************************
                3 . TO SUBSTRACT TWO MATRICES.
    ******************************************** */
#include<*stdio.h>
#include<*conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j;
clrscr();
printf("\n enter the elements into the matrix 'A' : ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n enter the elements into the matrix 'B' : ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]-b[i][j];
}
}
printf("\n the substraction of two matrices 'A' and 'B' is : ");
printf("\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\t %d",c[i][j]);
}
printf("\n");
}
getch();
}

 output

 enter the elements into the matrix 'A' : 1 1 1 1 1 1 1 1 1
 enter the elements into the matrix 'B' : 1 1 1 1 1 1 1 1 1
 the substraction of two matrices 'A' and 'B' is :
         0       0       0
         0       0       0
         0       0       0

/* *********************************************
                4 . TO MULTIPLY TWO MATRICES.
    ******************************************** */
#include<*stdio.h>
#include<*conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j;
clrscr();
printf("\n enter the elements into the matrix 'A' : ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n enter the elements into the matrix 'B' : ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=0;
c[i][j]=c[i][j]+(a[i][j]*b[j][i]);
}
}
printf("\n the multiplication of two matrices 'A' and 'B' is : ");
printf("\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\t %d",c[i][j]);
}
printf("\n");
}
getch();
}

 output

 enter the elements into the matrix 'A' : 1 1 1 1 1 1 1 1 1

 enter the elements into the matrix 'B' : 1 1 1 1 1 1 1 1 1

 the multiplication of two matrices 'A' and 'B' is :
         1       1       1
         1       1       1
         1       1       1

/* ***************************************************
   5 . TO TAKE DIMENSIONS OF MATRIX & ADD THEM
    ************************************************ */
#include<*stdio.h>
#include<*conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j,m,n,p,q;
clrscr();
printf("\n enter the dimensions of matrix 'A' : ");
scanf("%d %d",&m,&n);
printf("\n enter the dimensions of matrix 'B' : ");
scanf("%d %d",&p,&q);
if((m==p)&&(n==q))
{
printf("\n matrix addition is possible");
printf("\n");
printf("\n enter the elements into the matrix 'A' : ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n enter the elements into the matrix 'B' : ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("\n the sum of two matrices 'A' and 'B' is : ");
printf("\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\t %d",c[i][j]);
}
printf("\n");
}
}
else
{
printf("\n matrix addition is not possible");
}
getch();
}

outputs

enter the dimensions of matrix 'A' : 3 3

 enter the dimensions of matrix 'B' : 3 3

 matrix addition is possible

 enter the elements into the matrix 'A' : 1 1 1 1 1 1 1 1 1

 enter the elements into the matrix 'B' : 1 1 1 1 1 1 1 1 1

 the sum of two matrices 'A' and 'B' is :
                             2       2       2
                             2       2       2
                             2       2       2

 enter the dimensions of matrix 'A' : 2 3

 enter the dimensions of matrix 'B' : 3 2

 matrix addition is not possible