0% found this document useful (0 votes)
78 views

Programming For Bca

The document contains 9 programming questions and their solutions involving C code to perform operations on arrays and linked lists. The programs cover topics like replacing elements in arrays, sorting arrays, matrix addition/subtraction/multiplication, checking if a matrix is sparse/dense, linear/binary search, and inserting elements into singly linked lists. Menu-driven programs are provided to allow the user to choose between different sorting algorithms, matrix operations, and linked list insertions.

Uploaded by

Urvashi Sapra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views

Programming For Bca

The document contains 9 programming questions and their solutions involving C code to perform operations on arrays and linked lists. The programs cover topics like replacing elements in arrays, sorting arrays, matrix addition/subtraction/multiplication, checking if a matrix is sparse/dense, linear/binary search, and inserting elements into singly linked lists. Menu-driven programs are provided to allow the user to choose between different sorting algorithms, matrix operations, and linked list insertions.

Uploaded by

Urvashi Sapra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 83

PROGRAM 1

Ques 1. Write a program to replace an element from a desired


position in a linear array.
Coding:
#include<stdio.h>
#include<conio.h>

void replace(int a[],int n){ OUTPUT:


int pos,x=0,i;
printf("enter the position");
scanf("%d",&pos);
printf("enter the element");
scanf("%d",&x);
a[pos-1]=x;
for(i=0;i<n;i++)
printf("%d",a[i]);}
void main(){
int a[]={1,2,3,4,5,6};
clrscr();
replace(a,6);
getch();}

1
PROGRAM 2
Ques 2. Write a program to replace a desired element in a
linear array.
Coding:
#include<stdio.h>
#include<conio.h>
void replace_element(int a[],int n){
int i,pos,element;

printf("enter the desired element"); OUTPUT:


scanf("%d",&n);
for(i=0;i<n;i++){
if(a[i]==n){
pos=i+1;}}
if(pos==NULL){
printf("element not found");}
else{
printf("enter a new element");
scanf("%d",&element);
a[pos-1]=element;}
for(i=0;i<n;i++){
printf("%d",a[i]);}}
void main(){
int a[]={1,2,3,4,5};clrscr();replace_element(a,5);
getch();}

2
PROGRAM 3
Ques 3. Write a menu based program for insertion and
deletion in a linear array.
Coding:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void insert(int a[],int n){
int pos,val,i;
printf("enter the location where you want to insert");
scanf("%d",&pos);
printf("enter the value");
scanf("%d",&val);
for(i=n-1;i>=pos-1;i--){
a[i+1]=a[i];}
a[pos-1]=val;
for(i=0;i<=n;i++)
printf("%d",a[i]);
n++;}
void dlt(int a[],int n){
int pos,i;
printf("enter the value u want to delete");
scanf("%d",&pos);

3
for(i=pos+1;i<n;i++)
a[i-1]=a[i];
for(i=0;i<=n;i++)
printf("%d",&a[i]);
n--;}
void main(){
int ch,k,o=5,q[5];
clrscr();
printf("enter the elements");
for(k=0;k<o;k++){
scanf("%d",&q[k]);}
printf("\n menu: \n 1.insertion\n 2.deletion\n 3.exit\n");
printf("\n enter the choice");

scanf("%d",&ch); OUTPUT:
switch(ch){
case 1:
insert(q,5);
break;
case 2:
dlt(q,5);
break;
case 3:
exit(0);}
getch();

4
PROGRAM 4
Ques 4. Write a menu based program to perform bubble sort,
insertion sort and selection sort.
Coding:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void i_sort(int a[],int n){
int i,temp,j;
for(i=0;i<n;i++){
temp=a[i];
j=i-1;
while(temp<a[i]&&a[j>=0]){
a[j+1]=a[j];
j=j-1;}
a[j+1]=temp;}
for(i=0;i<n;i++){
printf("%d",a[i]);}}
void b_sort(int a[],int n){
int i,j,temp;
for(i=0;i<n-1;i++){
for(j=0;j<n-i;j++){
temp=a[j];

5
a[j]=a[j+1];
a[j+1]=temp;}}
for(i=0;i<n;i++){
printf("%d",a[i]);}}
void ssort(int a[],int n){
int i,min,j,temp;
for(i=0;i<n-1;i++){
min=i;
for(j=1+i;j<n;j++){
if(a[j]<a[min])
min=j;}
if(min!=i){
temp=a[i];
a[i]=a[min];
a[min]=temp;}}
for(i=0;i<n;i++){
printf("%d",a[i]);}}
void main(){
clrscr();
int ch,k,o=5,q[5];
printf("enter the elements");
for(k=0;k<o;k++){
scanf("%d",&q[k]);}
printf("\n menu: \n 1.insertion \n2.bubble \n 3.selection \n 4.exit\n");

6
printf("\n enter the choice");
scanf("%d",&ch);
switch(ch){
case 1:
i_sort(q,5);
break;
case 2:
b_sort(q,5);
break;
case 3:
ssort(q,5);
break;
case 4:
exit(0);

break;} OUTPUT:
getch();}

7
PROGRAM 5
Ques 5: Write a menu based program for addition, subtraction,
multiplication of two matrices.
Coding:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void add(){
int a[2][2],b[2][2],c[2][2],i,j;
printf("enter 3x3 matrix a\n");
for(i=0;i<2;i++){
for(j=0;j<2;j++){
scanf("%d",&a[i][j]);}}
printf("enter the value for matrix b\n");
for(i=0;i<2;i++){
for(j=0;j<2;j++){
scanf("%d",&b[i][j]);}}
for(i=0;i<2;i++){
for(j=0;j<2;j++){
c[i][j]=a[i][j]+b[i][j];
}}printf("result \n");
for(i=0;i<2;i++){
for(j=0;j<2;j++){
printf("%d\t",c[i][j]);

8
}printf("\n");}}
void sub(){
int a[2][2],b[2][2],c[2][2],i,j;
printf("enter 3x3 matrix a\n");
for(i=0;i<2;i++){
for(j=0;j<2;j++){
scanf("%d",&a[i][j]);}}
printf("enter the value for matrix b\n");
for(i=0;i<2;i++){
for(j=0;j<2;j++){
scanf("%d",&b[i][j]);}}
for(i=0;i<2;i++){
for(j=0;j<2;j++){
c[i][j]=a[i][j]-b[i][j];
}}printf("result \n");
for(i=0;i<2;i++){
for(j=0;j<2;j++){
printf("%d\t",c[i][j]);
}printf("\n");}}
void multi(){
int m,n,p,q,c,d,k,sum=0;
int first[10][10],second[10][10],multiply[10][10];
printf("enter the number of rows and columns of first matrix\n");
scanf("%d%d",&m,&n);

9
printf("enter elements of first matrix");
for(c=0;c<m;c++)
for(d=0;d<n;d++)
scanf("%d",&first[c][d]);
printf("enter the number of rows and columns of second matrix\n");
scanf("%d%d",&p,&q);
if(n!=p)
printf("the matrices cant be multiplied with each other\n");
else{
printf("enter the elements of second matrix");
for(c=0;c<p;c++)
for(d=0;d<q;d++)
scanf("%d",&second[c][d]);
for(c=0;c<m;c++){
for(d=0;d<q;d++){
for(k=0;k<p;k++){
sum=sum+first[c][k]*second[k][d];}
multiply[c][d]=sum;
sum=0;}}
printf("the product of matrices:\n");
for(c=0;c<m;c++){
for(d=0;d<q;d++)
printf("%d\t",multiply[c][d]);
printf("\n");}}}

10
void main(){
clrscr();
int c;
printf("1.addition\n 2.subtraction\n 3.multiply\n 4.exit");
scanf("%d",&c);
switch(c){
case 1: add();
break;
case 2: sub();
break;
case 3: multi();
break;
case 4: exit(0);
break;
}getch();} OUTPUT:

11
PROGRAM 6
Ques 6: Write a program to check whether the entered matrix is a
sparse matrix or a dense matrix and print in the triplet fashion.
Coding:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int matrix[10][10];
int i,j,m,n;
int sparse_counter=0;
printf("enter the order of the matrix\n");
scanf("%d%d",&m,&n);
printf("enter the elements of the matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&matrix[i][j]);
if(matrix[i][j]==0)
{
sparse_counter++;
}

12
}
}
if(sparse_counter>((m*n)/2))
{
printf("the given matrix is SPARSE MATRIX\n");
}
else
printf("the given matrix is a DENSE MATRIX\n");
printf("there are %d number of zeros.",sparse_counter);
printf("\n%d\t%d\t%d\n",i,j,matrix[i][j]);
getch();
}.
OUTPUT:

13
PROGRAM 7
Ques 7: Write a menu-based program to perform linear search and
binary search.
Coding:
#include<stdio.h>
#include<conio.h>
void lsearch(int a[],int n){
int x, k=0, i;
printf("Enter value to search:");
scanf("%d", &x);
for(i=0; i<n; i++) {
if(a[i]==x) {
printf("Number found at %d pointer", i++);
k=;
}}
if(k==0)
printf("Number not found");}
void bsearch(int a[],int n) {
int beg=0, end=n-1, item, loc;
int mid=(beg+end)/2;
printf("Enter value you want to search:");
scanf("%d", &item);
while((beg<end)&&(a[mid]!=item)){
if(item<a[mid])

14
{
end=mid-1;
else{
beg=mid+1; }
mid=(beg+end)/2;
if(beg>end) {
loc=-1;
printf("Number not found"); }
else {
loc=mid+1;
printf("Number found at %d",loc);} }
getch();}
void main()
{
int a[5], i, j;
clrscr();
printf("Enter the array:\n");
for(i=0; i<5; i++) {
scanf("%d", &a[i]);
}
printf("If you want to search a value from linear search, then type '1' and if you
want to search from binary search, then type '2' else type '0'\n");
scanf("%d",&j);
if(j==1
{
15
lsearch(a,5);
}
else if(j==2)
{
bsearch(a,5);
}
getch();
}

OUTPUT:

16
PROGRAM 8
Ques 8. Write a program to create a singly link list with n number of
nodes.
Coding:
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
int data;
struct node*next;
}
*temp,*q,*head;
void main()
{
clrscr();
int n,m,i;
printf("how many nodes you want");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the elements");
scanf("%d",&m);
temp=(node*)malloc(sizeof(struct node));
temp->data=m;
temp->next=NULL;

17
if(head==NULL){
head=temp;}
else{
q=head;
while(q->next!=NULL)
q=q->next;
q->next=temp;}}
display(head);
getch();
}
void display(struct node*n){
while(n!=NULL){
printf("%d",n->data);
n=n->next;
}}

OUTPUT:

18
PROGRAM 9
Ques 9. write a menu based program for inserting an element in a
singly link list.
1.inserting an element in the beginning of the list
2.inserting an element int the end of the list
3.inserting at a particular position in the list
Coding:
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
}
*head=NULL,*q,*temp;
void main(){
int ch;
void insert_beg();
void insert_end();
int insert_pos();
void display();
while(1)

19
{
printf("\n1.Insert at beginning\n2.Insert at end\n3.Insert at specified
position\n4.display the list\n 5.exit");
printf("\n\nEnter your choice(1-5):");
scanf("%d",&ch);
switch(ch){
case 1: insert_beg();
break;
case 2: insert_end();
break;
case 3: insert_pos();
break;
case 4: display();
break;
case 5: exit(0);
break;
default: printf("Wrong Choice!!");
break;
}
}
getch();}
void insert_beg()
{
int num;
temp=(struct node*)malloc(sizeof(struct node));
20
printf("Enter data:");
scanf("%d",&num);
temp->data=num;
if(head==NULL)
{
temp->next=NULL;
head=temp;
}
else{
temp->next=head;
head=temp;
}
}
void insert_end()
{
int num;
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&num);
temp->data=num;
temp->next=NULL;
if(head==NULL)
{
head=temp;

21
}
else{
q=head;
while(q->next!=NULL)
q=q->next;
q->next=temp;
}
}
int insert_pos()
{
int pos,i,num;
if(head==NULL)
{
printf("List is empty!!");
getch();
}
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&num);
printf("Enter position to insert:");
scanf("%d",&pos);
temp->data=num;
q=head;
for(i=1;i<pos-1;pos++)

22
{
if(q->next==NULL)
{
printf("There are less elements!!");
getch();
}
q=q->next;
}
temp->next=q->next;
q->next=temp;
getch();
}
void display(){
if(head==NULL){
printf("List is empty!!");}
else{
q=head;
printf("The linked list is:\n");
while(q!=NULL){
printf("%d->",q->data);
q=q->next;
}
}
}

23
OUTPUT:

24
25
PROGRAM 10
Ques 10. write a menu based program for deletion of an element in a
singly link list.
1.deletion of an element in the beginning of the list.
2. deletion of an element in the end of the list.
3.deletion of an element at a particular position in the list.
Coding:
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
}*temp,*q,*head;
void display(){
q=head;
printf("the linked list is as follow:-\n");
while(q!=NULL){
printf("%d\n",q->data);
q=q->next;}}
void delbeg(){
if(head==NULL){

26
printf("there are no element in the list");}
else {
q=head;
head=head->next;
printf("the delete data is %d",q->data);
free(q);}}
void delend(){
if(head==NULL){
printf("there are no element in the list");}
else {
q=head;
while(q->next->next!=NULL)
q=q->next;
temp=q->next;
q->next=NULL;
printf("the delete data is %d",temp->data);
free(temp); }}
int delpos(){
int pos,i;
if(head==NULL){
printf("List is empty!!");
return 0;}
printf("Enter position to delete:");
scanf("%d",&pos);

27
for(i=1;i<pos-1;pos++){
if(q->next==NULL) {
printf("There are less elements!!");
return 0;}
q=q->next;}
temp=q->next;
q->next=temp->next;
printf("Deleted element is %d",temp->data);
free(temp);
return 0;}
void main(){
int n,m,i,ch;
clrscr();
printf("how many nodes do you want:-");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("take the demand:-");
scanf("%d",&m);
temp=(struct node *) malloc(sizeof(struct node));
temp->data=m;
temp->next=NULL;
if(head==NULL){
head=temp;}
else{

28
q=head;
while(q->next!=NULL)
q=q->next;
q->next=temp; }}
do{
printf("\n1. delete element at begning\n2. delete element at end\n3. delete
element from position\n4. display list\n5. exit\n");
printf("select from 1 to 5:-");
scanf("%d",&ch);
switch(ch){
case 1:delbeg();
break;
case 2:delend();
break;
case 3:delpos();
break;
case 4:display();
break;
case 5:exit(0);
break;
default:printf("invalid option");}
}while(ch!=5);
getch();}

29
OUTPUT:

30
PROGRAM 11
Ques 11. Create a c program to create and display a doubly linked list.
 display the list in original form.
 display the list in reverse form.
Coding:
#include <stdio.h>
#include <conio.h>
#include<malloc.h>
struct node {
int data;
struct node * prev;
struct node * next;
}*head, *last;
void createList(int n);
void displayListFromFirst();
void displayListFromEnd();
void main(){
int n, choice;
clrscr();
head = NULL;
last = NULL;
printf("Enter the number of nodes you want to create: ");
scanf("%d", &n);
createList(n);
printf("\nPress 1 to display list from First");
31
printf("\nPress 2 to display list from End : ");
scanf("%d", &choice);
if(choice==1){
displayListFromFirst();}
else if(choice == 2) {
displayListFromEnd();}
getch();}
void createList(int n){
int i, data;
struct node *newNode;
if(n >= 1){
head = (struct node *)malloc(sizeof(struct node));
if(head != NULL){
printf("Enter data of 1 node: ");
scanf("%d", &data);
head->data = data;
head->prev = NULL;
head->next = NULL;
last = head;
for(i=2; i<=n; i++){
newNode = (struct node *)malloc(sizeof(struct node));
if(newNode != NULL){
printf("Enter data of %d node: ", i);
scanf("%d", &data);
newNode->data = data;
newNode->prev = last;

32
newNode->next = NULL;
last->next = newNode;
last = newNode;}else{
printf("Unable to allocate memory.");
break;}}
printf("\nDOUBLY LINKED LIST CREATED SUCCESSFULLY\n");}
else{
printf("Unable to allocate memory");}}}
void displayListFromFirst(){
struct node * temp;
int n = 1;
if(head == NULL){
printf("List is empty.");}else{
temp = head;
printf("\n\nDATA IN THE LIST:\n");
while(temp != NULL) {
printf("DATA of %d node = %d\n", n, temp->data);
n++;
temp = temp->next;}}}
void displayListFromEnd(){
struct node * temp;
int n = 0;
if(last == NULL){
printf("List is empty.");}else{
temp = last;
printf("\n\nDATA IN THE LIST:\n");

33
while(temp != NULL){
printf("DATA of last-%d node = %d\n", n, temp->data);
n++;
temp = temp->prev;}}}

OUTPUT:

34
PROGRAM 12
Ques 12. Write a menu based program for insertion of element in
doubly linked list and display the list in original form as well as in a
reverse manner.
1. Inserting an element in the beginning of a doubly link list.
2. Inserting an element in the end of a doubly link list.
3. Inserting an element at a particular position in the doubly link list.
Coding:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>
void insertbeg();
void insertend();
void insertpos();
void display(struct node *n);
void display1(struct node *n);
struct node{
int data;
struct node*next,*prev;
}*head,*q,*tail,*temp,*prev;
void main()
{int i,n,op;
clrscr();

35
printf("how many nodes you want \n");
scanf("%d",&n);
for(i=0;i<n;i++){
temp=(struct node*)malloc(sizeof(struct node));
temp->prev=NULL;
printf("enter the data for %d node:- ",i+1);
scanf("%d",&temp->data);
temp->next=NULL;
if(head==NULL){
head=tail=temp;}
else{
temp->prev=tail;
tail->next=temp;
tail=temp;}}
printf("\n");
while(1){
printf("\n\n1.Insert the element at beg\n2.Insert the element at end\n3.Insert
the element at desired postion\n4.Display the list\n5.Display the list in reverse
manner\n6.Exit\n\n");
scanf("%d",&op);
switch(op){
case 1:insertbeg();
break;
case 2:insertend();
break;

36
case 3:insertpos();
break;
case 4:display(head);
break;
case 5:display1(tail);
break;
case 6:exit(0);
break;}}
getch();}
void display(struct node* n){
while(n!=NULL){
printf("\n\n Resultant List:-\n");
while(n!=NULL){
printf("%d\t",n->data);
n=n->next;}}}
void display1(struct node*n){
printf("\n\nResultant list in reverse manner:-\n");
while(n!=NULL){
printf("%d\t",n->data);
n=n->prev;}}
void insertbeg(){
int m;
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter element you want insert at beg:- ");

37
scanf("%d",&m);
temp->data=m;
if(head==NULL){
head=temp;}
else{
temp->next=head;
head->prev=temp;
head=temp;}}
void insertend(){
int a;
temp=(struct node *)malloc(sizeof(struct node));
printf("Enter element you want to insert at end:- ");
scanf("%d",&a);
temp->data=a;
temp->next=NULL;
if(head==NULL){
head=tail=temp;}
else{
tail->next=temp;
temp->prev=tail;
tail=temp;}
while(q->next!=NULL){
q=q->next;}
q->next=temp;

38
}
void insertpos()
{
int pos,i,num;
if(head==NULL){
printf("list is empty");}
temp=(struct node *)malloc(sizeof(struct node));
printf("Enter element you want to insert:- ");
scanf("%d",&num);
printf("Enter the desired position:- ");
scanf("%d",&pos);
temp->data=num;
q=head;
for(i=1;i<pos-1;i++){
if(q->next==NULL){
printf("there are less element");
}
q=q->next;
}
temp->next=q->next;
q->next->prev=temp;
q->next=temp;
temp->prev=q;
}

39
OUTPUT:

40
41
PROGRAM 13
Ques 13. Write a menu based program for deletion of element in
doubly linked list and display the list in original form as well as in a
reverse manner.
1. Deleting an element in the beginning of a doubly link list.
2. Deleting an element in the end of a doubly link list.
3. Deleting an element at a particular position in the doubly link list.

Coding:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>
void delbeg();
void delend();
void delpos();
void display(struct node *n);
void display1(struct node *n);
struct node{
int data;
struct node*next,*prev;
}*head,*q,*tail,*temp,*prev;
void main()
{int i,n,op;
clrscr();

42
printf("how many nodes you want \n");
scanf("%d",&n);
for(i=0;i<n;i++){
temp=(struct node*)malloc(sizeof(struct node));
temp->prev=NULL;
printf("enter the data for %d node:- ",i+1);
scanf("%d",&temp->data);
temp->next=NULL;
if(head==NULL){
head=tail=temp;}
else{
temp->prev=tail;
tail->next=temp;
tail=temp;}}
do{
printf("\n\n1.Delete the element from beg\n2.Delete the element from
end\n3.Delete the element from desired postion\n4.Display the list\n5.Display
the list in reverse manner\n6.Exit\n\n");
scanf("%d",&op);
switch(op){
case 1:delbeg();
break;
case 2:delend();
break;
case 3:delpos();

43
break;
case 4:display(head);
break;
case 5:display1(tail);
break;
case 6:exit(0);
break;}
}while(op!=6);
getch();}
void display(struct node* n){
while(n!=NULL){
printf("\n\n Resultant List:-\n");
while(n!=NULL){
printf("%d\t",n->data);
n=n->next;}}}
void display1(struct node*n){
printf("\n\nResultant list in reverse manner:-\n");
while(n!=NULL){
printf("%d\t",n->data);
n=n->prev;}}
void delbeg()
{
if(head==NULL)
{

44
printf("there is no element to delete");}
else
{
q=head;
head=head->next;
head->prev=NULL;
printf("%d is deleted from beg\n",q->data);
free(q);
}
display(head);
}
void delend()
{
if(head==NULL){
printf("There is no element to delete");}
else if(head==tail)
{
temp=head;
head=tail=NULL;
free(temp);}
else
{
temp=tail;
tail=temp->prev;

45
tail->next=NULL;
printf("%d is deleted from end\n",temp->data);
free(temp);}
display(head);
}
void delpos(){
int pos,i;
if(head==NULL){
printf("link list is empty");}
printf("enter the position of element to delete:-");
scanf("%d",&pos);
q=head;
for(i=1;i<pos-1;i++)
{
if(q->next==NULL){
printf("there are less elememt");}
q=q->next;
}
temp=q->next;
q->next=temp->next;
temp->next->prev=q;
printf("%d element is deleted",temp->data);
free(temp);
}

46
OUTPUT:

47
48
PROGRAM 14
Ques 14. Write a program for executing a stack in c using link list
which includes push , pop, peep, display and exit fucntions.
Coding:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>
#define max_size 5
int stack[max_size],top=-1;
struct node
{
int data;
struct node*next;
}
*temp,*q,*head;
void push();
void pop();
void peep();
void display();
void main()
{
clrscr();
int n,m,i,choice;

49
printf("how many nodes you want");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the elements");
scanf("%d",&m);
temp=(node*)malloc(sizeof(struct node));
temp->data=m;
temp->next=NULL;
if(head==NULL){
head=temp;}
else{
q=head;
while(q->next!=NULL)
q=q->next;
q->next=temp;}}
do
{
printf("\n 1.push \n 2.pop \n 3.peep \n 4.display \n 5.exit \n");
printf("Enter choice");
scanf("%d",&choice);
switch(choice){
case 1:push();
break;

50
case 2:pop();
break;
case 3:peep();
break;
case 4:display();
break;
case 5:exit(0);
break;
default:printf("invalid choice");
break;
}}
while(choice!=5);
getch();
}
void push()
{
int item;
if(top==(max_size-1))
{
printf("stack overflow");}
else{
printf("enter item to be pushed");
scanf("%d",&item);
top++;

51
stack[top]=item;
}}
void pop()
{
int item;
if(top==-1)
printf("stack underflow");
else
{
item=stack[top];
top--;
printf("the deleted item is %d",item);
}}
void peep()
{
printf("the topmost element of stock is %d", stack[top]);
}
void display(){
q=head;
printf("the linked list is as follow:-\n");
while(q!=NULL){
printf("%d\n",q->data);
q=q->next;
}}

52
OUTPUT:

53
54
PROGRAM 15
Ques 15. Write a program for executing a stack in c using array which
includes push , pop, peep, display and exit fucntions.
Coding:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define max_size 5
int stack[max_size],top=-1;
void push();
void pop();
void peep();
void display();
void main()
{
clrscr();
int choice;
do
{
printf("\n 1.push \n 2.pop \n 3.peep \n 4.display \n 5.exit \n");
printf("Enter choice");
scanf("%d",&choice);
switch(choice){
case 1:push();

55
break;
case 2:pop();
break;
case 3:peep();
break;
case 4:display();
break;
case 5:exit(0);
break;
default:printf("invalid choice");
break;
}}
while(choice!=5);
}
void push()
{
int item;
if(top==(max_size-1))
{
printf("stack overflow");}
else{
printf("enter item to be pushed");
scanf("%d",&item);
top++;

56
stack[top]=item;
}}
void pop()
{
int item;
if(top==-1)
printf("stack underflow");
else
{
item=stack[top];
top--;
printf("the deleted item is %d",item);
}}
void peep()
{
printf("the topmost element of stock is %d", stack[top]);
}
void display()
{
int i;
for(i=top;i>=0;i--)
{
printf("%d\n",stack[i]);
}}

57
OUTPUT:

58
59
PROGRAM 16
Ques 16. Write a program to perform QUEUE in c by using link list.
Coding:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct Node{
int data;
struct Node *next;
}*front = NULL,*rear = NULL;
void enqueue(int);
void dequeue();
void display();
void main(){
int choice, value;
clrscr();
printf("\n:: Queue Implementation using Linked List ::\n");
while(1){
printf("1. Insert\n2. Delete\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert: ");
scanf("%d", &value);
60
enqueue(value);
break;
case 2: dequeue();
break;
case 3: display();
break;
case 4: exit(0);
break;
default: printf("\nWrong selection!!! Please try again!!!\n");
break; } }
}
void enqueue(int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode -> next = NULL;
if(front == NULL)
front = rear = newNode;
else{
rear -> next = newNode;
rear = newNode; }
printf("\nInsertion is Success!!!\n");
}

61
void dequeue()
{
if(front == NULL)
printf("\nQueue is Empty!!!\n");
else
{
struct Node *temp = front;
front = front -> next;
printf("\nDeleted element: %d\n", temp->data);
free(temp);}
}
void display()
{
if(front == NULL)
printf("\nQueue is Empty!!!\n");
else
{
struct Node *temp = front;
while(temp->next != NULL){
printf("%d--->",temp->data);
temp = temp -> next;
}
printf("%d--->NULL\n",temp->data);
}}

62
OUTPUT:

63
PROGRAM 17
Ques 17. Write a program to perform QUEUE in c by using array.
Coding:
#include <stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 50
void enqueue();
void dequeue();
void display();
int element[MAX];
int rear = - 1;
int front = - 1;
void main()
{
int choice;
clrscr();
while (1){
printf("1.enqueue\n2.dequeue\n3.display\n4.exit");
printf("\nEnter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1: enqueue();
64
break;
case 2: dequeue();
break;
case 3: display();
break;
case 4: exit(0);
break;
default: printf("Wrong choice \n");
break;}
}
getch();}
void enqueue()
{
int ele;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else{
if (front == - 1)
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &ele);
rear = rear + 1;
element[rear] = ele;
}}

65
void dequeue(){
if (front == - 1 || front > rear)
{
printf("Queue Underflow \n");
return ;
}
else
{
printf("Element deleted from queue is : %d\n", element[front]);
front = front + 1;
}
}
void display()
{
int i;
if (front == - 1)
printf("Queue is empty \n");
else
{
printf("Queue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", element[i]);
printf("\n");
}}

66
OUTPUT:

67
68
PROGRAM 18
Ques 18. Write a c program for Priority Queue using link list.
Coding:
# include<stdio.h>
# include<conio.h>
#include<malloc.h>
#include<stdlib.h>
void enqueue();
void dequeue();
void display();
struct node{
int priority;
int info;
struct node *link;
}*front = NULL;
void main(){
int choice;
clrscr();
while(1){
printf("1.Insert.\n2.Delete.\n3.Display.\n4.Exit.\n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch(choice){
case 1: enqueue();
69
break;
case 2: dequeue();
break;
case 3: display();
break;
case 4: exit(0);
default : printf("INVALID CHOICE TRY AGAIN!!!!!!1\n");
break;}}
getch();}
void enqueue(){
struct node *tmp,*q;
int added_item,item_priority;
tmp = (struct node *)malloc(sizeof(struct node));
printf("\nInput the data to be added in the queue : ");
scanf("%d",&added_item);
printf("\n\nEnter its priority : ");
scanf("%d",&item_priority);
tmp->info = added_item;
tmp->priority = item_priority;
if( front == NULL || item_priority < front->priority ){
tmp->link = front;
front = tmp;}
else{
q = front;

70
while( q->link != NULL && q->link->priority <= item_priority ) q=q->link;
tmp->link = q->link;
q->link = tmp;}}
void dequeue(){
struct node *tmp;
if(front == NULL){
printf("\n\nQueue Underflow\n");}
else{
tmp = front;
printf("\n\nDeleted data is %d\n",tmp->info);
front = front->link;
free(tmp);}}
void display(){
struct node *ptr;
ptr = front;
if(front == NULL){
printf("\n\nQueue is empty\n");}
else{
printf("Priority\tData\n\n");
while(ptr != NULL){
printf("%d \t\t%d\n\n",ptr->priority,ptr->info);
ptr = ptr->link;
}
}}

71
OUTPUT:

72
73
PROGRAM 19
Ques 19. Write a c program showing the execution of Circular Queue.
Coding:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define max 5
int q[10],front=0,rear=-1;
void main(){
int ch;
void enqueue();
void dequeue();
void display();
clrscr();
printf("\nCircular Queue operations\n");
printf("1.insert\n2.delete\n3.display\n4.exit\n");
while(1){
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch){
case 1: enqueue();
break;
case 2: dequeue();
break;
case 3:display();

74
break;
case 4:exit(0);
break;
default:printf("Invalid option\n");
break; }}
getch();}
void enqueue(){
int x;
if((front==0&&rear==max-1)||(front>0&&rear==front-1))
printf("Queue is overflow\n");
else{
printf("Enter element to be insert:");
scanf("%d",&x);
if(rear==max-1&&front>0){
rear=0;
q[rear]=x;}
else{
if((front==0&&rear==-1)||(rear!=front-1))
q[++rear]=x;}}}
void dequeue(){
int a;
if((front==0)&&(rear==-1)) {
printf("Queue is underflow\n"); }
if(front==rear) {
a=q[front];
rear=-1;

75
front=0;}
else
if(front==max-1) {
a=q[front];
front=0;}
else a=q[front++];
printf("Deleted element is:%d\n",a);}
void display(){
int i,j;
if(front==0&&rear==-1){
printf("Queue is underflow\n");}
if(front>rear) {
for(i=0;i<=rear;i++)
printf("\t%d",q[i]);
for(j=front;j<=max-1;j++)
printf("\t%d",q[j]);
printf("\nrear is at %d\n",q[rear]);
printf("\nfront is at %d\n",q[front]);}
else{
for(i=front;i<=rear;i++)
{
printf("\t%d",q[i]);}
printf("\nrear is at %d\n",q[rear]);
printf("\nfront is at %d\n",q[front]);
}
printf("\n");}

76
OUTPUT:

77
PROGRAM 20
Ques 20. Write a c program for implementation of DEQUEUE using a
doubly linked list.
Coding:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 10
int q[MAX],front=0,rear=0;
void add_rear();
void add_front();
void delete_rear();
void delete_front();
void display();
void main(){
int ch;
clrscr();
printf(" DQueue Menu");
printf("\n 1. Insert Rear");
printf("\n 2. Insert Front");
printf("\n 3. Delete Rear");
printf("\n 4. Delete Front");
printf("\n 5. Display");
printf("\n 6. Exit\n");

78
while(ch!=6){
printf(" Enter your choice:-");
scanf("%d",&ch);
switch(ch){
case 1:add_rear();
break;
case 2:add_front();
break;
case 3:delete_rear();
break;
case 4:delete_front();
break;
case 5:display();
break;
case 6:exit(0);
break;
default:printf(" Wrong Choice\n");}}}
void add_rear(){
int no;
printf("\nEnter value to insert : ");
scanf("%d",&no);
if(rear==MAX){
printf(" Queue is Overflow\n");}
else{

79
rear++;
q[rear]=no;
if(rear==0)
rear=1;
if(front==0)
front=1;}}
void add_front(){
int no;
printf("\n Enter value to insert:-");
scanf("%d",&no);
if(front<=1){
printf(" Cannot add value at front end\n"); }
else {
front--;
q[front]=no;} }
void delete_front(){
int no;
if(front==0){
printf(" Queue is Underflow\n");}
else {
no=q[front];
printf("Deleted element is %d\n",no);
if(front==rear){
front=0;

80
rear=0; }
else{
front++; }}}
void delete_rear(){
int no;
if(rear==0){
printf(" Cannot delete value at rear end\n");}
else{
no=q[rear];
if(front==rear){
front=0;
rear=0;}
else {
rear--;
printf(" Deleted element is %d\n",no); }}}
void display()
{
int i;
if(front==0{
printf(" Queue is Underflow\n");}
else{
printf("\n Output");
for(i=front;i<=rear;i++){
printf(" %d",q[i]);}}}

81
OUTPUT:

82
83

You might also like