Mca Dsa Assignment-2
Mca Dsa Assignment-2
19
1.
Wr i te a menu dri ven prog ram to per form the fol l ow ing operati ons on a SIN G LYLI N
KED LI ST
i)
C reate a li st
#include <stdlib.h>
/ / D efine no de structure
struct No de {
int data;
};
if(newNo de == NULL ){
exit( 0);
scanf("%d",&value);
new No de
-
>data = value;
new No de
-
>next = NULL ;
if (head == NULL ) {
tem p
-
>next = newNo de;
printf("%d
-
> " ,current
-
>data);
current= current
-
>next;
printf(" NUL L
\
n");
/ / M ain functio n
int m ain() {
int n;
Page |
20
scanf("%d",&n);
display L ist(head);
ret urn 0;
}
Output
:
ii)
#include <stdlib.h>
/ / D efine no de structure
struct No de {
int data;
};
/ / Insert at beginning
if(newNo de = = NULL ) {
new No de
-
>data = value;
new No de
-
>next = head;
return head;
/ / D isplay t he list
printf("%d
-
> " ,current
-
>data);
current= current
-
>next;
printf(" NUL L
\
n");
/ / M ain functio n
int m ain() {
int n, val;
scanf("%d",&n);
fo r (int i = 0 ; i < n; i+ +) {
Page |
21
scanf("%d",&val);
scanf("%d",&val);
head = insertAtBeginning(head,val);
ret urn0 ;
Output
:
i i i)
I ns er t a rec ord atthe end of the l ist
#include <stdlib.h>
/ / D efine no de structure
struct No de {
int data;
};
new No de
-
>data = value;
new No de
-
>next = NUL L ;
if (head == NULL ) {
} e lse {
struct No de* tem p= head;
while (tem p
-
>next != NULL)
tem p = tem p
-
>next ;
tem p
-
>next = newNo de;
return head;
printf("%d
-
> " ,tem p
-
>data);
tem p = tem p
-
>next ;
Page |
22
printf(" NUL L
\
n");
/ / M ain functio n
int m ain() {
int n, val;
scanf("%d",&n);
fo r (int i = 0 ; i < n; i+ +) {
printf("Enter data fo r no de%d: ", i + 1 );
scanf("%d",&val);
scanf("%d",&val);
return 0;
Output
:
i v)
/ / D efine no de structure
struct No de {
int data;
};
struct No de* insertAtPo sitio n(struct No de* head, intvalue, int positio n) {
new No de
-
>data = value;
new No de
-
>next = NUL L ;
new No de
-
>next = head;
tem p = tem p
-
>next ;
Page |
23
free(newNo de);
} e lse {
new No de
-
>next = tem p
-
>next ;
tem p
-
>next = newNo de;
return head;
printf("%d
-
> " ,tem p
-
>data);
tem p = tem p
-
>next ;
}
printf(" NUL L
\
n");
/ / M ain functio n
int m ain() {
scanf("%d",&n);
fo r (int i = 0 ; i < n; i+ +) {
scanf("%d",&val);
scanf("%d",&po s);
return 0;
Output
:
v)
#include <stdlib.h>
/ / No destructure
struct No de {
Page |
24
int data;
};
tem p = tem p
-
>next ;
} e lse {
new No de
-
>next = temp
-
>next;
tem p
-
>next = newNo de;
return head;
printf("%d
-
> " ,tem p
-
>data);
tem p = tem p
-
>next ;
printf(" NUL L
\
n");
new No de
-
>data = value;
new No de
-
>next = NUL L ;
while (tem p
-
>next != NUL L )
tem p = tem p
-
>next ;
tem p
-
>next = newNo de;
return head;
/ / M ain
int m ain() {
scanf("%d",&n);
fo r (int i = 0 ; i < n; i+ +) {
scanf("%d",&val);
head = insertAtEnd(head, val);
Page |
25
scanf("%d",&after);
ret urn0 ;
Output
:
v i)
/ / No destructure
struct No de {
int data;
};
struct No de* insertBefo reValue( struct No de* head, int target , intvalue) {
new No de
-
>data = value;
new No de
-
>next = NUL L ;
if(head == NULL ){
return head;
if (head
-
>data == target) {
new No de
-
>next = head;
return head;
while (tem p
-
>next != NUL L && tem p
-
>next
-
>data != target ) {
tem p = tem p
-
>next ;
if (tem p
-
>next == NUL L) {
Page |
26
} e lse {
new No de
-
>next = temp
-
>next;
tem p
-
>next = newNo de;
printf("%d
-
> " ,tem p
-
>data);
tem p = tem p
-
>next ;
printf(" NUL L
\
n");
new No de
-
>data = value;
new No de
-
>next = NUL L ;
while (tem p
-
>next != NUL L )tem p = tem p
-
>next;
tem p
-
>next = newNo de;
return head;
/ / M ain functio n
int m ain() {
fo r (int i = 0 ; i < n; i+ +) {
scanf("%d",&val);
scanf("%d",&befo re);
scanf("%d",&val);
ret urn0 ;
Output
:
Page |
27
v ii )
#include <stdlib.h>
/ / No destructure
struct No de {
int data;
};
new No de
-
>data = value;
new No de
-
>next = NUL L ;
new No de
-
>next = head;
return head;
while (tem p
-
>next != NUL L && tem p
-
>next
-
>data < value) {
tem p = tem p
-
>next ;
new No de
-
>next = tem p
-
>next;
tem p
-
>next = newNo de;
printf("%d
-
> " ,tem p
-
>data);
tem p = tem p
-
>next ;
printf(" NUL L
\
n");
/ / M ain functio n
int m ain() {
int n,val;
Page |
28
scanf("%d",&n);
fo r (int i = 0 ; i < n; i+ +) {
scanf("%d",&val);
head = insertInSo rtedOrder(head,val);
ret urn0 ;
Output
:
v ii i)
#include <stdlib.h>
/ / No destructure
struct No de {
int data;
};
if(head == NULL ){
printf("L ist is em pty. No thing to delete.
\
n");
return NULL ;
head = head
-
>next;
return head;
printf("%d
-
> " ,tem p
-
>data);
tem p = tem p
-
>next ;
printf(" NUL L
\
n");
Page |
29
new No de
-
>data = value;
new No de
-
>next = NUL L ;
while (tem p
-
>next != NUL L )tem p = tem p
-
>next;
tem p
-
>next = newNo de;
/ / M ain
int m ain() {
int n, val;
scanf("%d",&n);
fo r (int i = 0 ; i < n; i+ +) {
scanf("%d",&val);
ret urn0 ;
Output
:
i x)
#include <stdlib.h>
/ / No destructure
struct No de {
int data;
};
if(head == NULL ){
return NULL ;
if (head
-
>next = = NULL ) {
free (head);
Page |
30
return NULL ;
while (tem p
-
>next
-
>next!= NULL ) {
tem p = tem p
-
>next ;
free (tem p
-
>next );
tem p
-
>next = NUL L;
return head;
printf("%d
-
> " ,tem p
-
>data);
tem p = tem p
-
>next ;
printf(" NUL L
\
n");
new No de
-
>data = value;
new No de
-
>next = NUL L ;
while (tem p
-
>next != NUL L )tem p = tem p
-
>next;
tem p
-
>next = newNo de;
/ / M ain functio n
int m ain() {
scanf("%d",&n);
fo r (int i = 0 ; i < n; i+ +) {
scanf("%d",&val);
return 0;
Output
:
Page |
31
x)
#include <stdlib.h>
/ / No destructure
struct No de {
int data;
};
/ / D elete no de by value
if(head == NULL ){
return NULL ;
}
if(head
-
>data = =target ){
head = head
-
>next;
return head;
// Search fo r t he no de to delete
while (tem p != NUL L &&tem p
-
>data != target) {
prev = tem p;
tem p = tem p
-
>next ;
return head;
prev
-
>next = tem p
-
>next;
return head;
}/ / D isplay list
printf("%d
-
> " ,tem p
-
>data);
tem p = tem p
-
>next ;
Page |
32
printf(" NUL L
\
n");
}
new No de
-
>data = value;
new No de
-
>next = NUL L ;
while (tem p
-
>next != NUL L )tem p = tem p
-
>next;
tem p
-
>next = newNo de;
return head;
}/ / M ain
int m ain() {
struct No de* head = NULL ;
scanf("%d",&n);
fo r (int i = 0 ; i < n; i+ +) {
scanf("%d",&val);
scanf("%d",&target );
return 0;
}
Output
:
2.
Wr i te a menu dri ven prog ram to per form the fol l ow ing operati ons on a CI RC ULAR
LIN KED
LI ST
i)
C reate a circ ul ar l i st
ii)
i i i)
i v)
v)
v i)
Di s pl ay t he circ ul ar l i st
Page |
33
#include <stdlib.h>
struct No de {
int data;
};
new No de
-
>data = data;
new No de
-
>next = new No de;
/ / Functio n to insert a no de
vo id insert( int data) {
if(last== NUL L ) {
return;
}struct No de* newNo de= (struct No de* )m allo c(sizeo f(struct No de) );
new No de
-
>data = data;
new No de
-
>next = last
-
>next;
last
-
>next = newNo de;
return;
if(last== last
-
>next && last
-
>data== key ){
free (last);
last = NULL ;
return;
} do {
if(tem p
-
>data = = key){
last= prev;
prev
-
>next = tem p
-
>next ;
free(tem p);
return;
}prev = tem p;
tem p = tem p
-
>next ;
} w hile(tem p != last
-
>next );
printf(" Key %d not fo und.
\
n", key );
vo id deleteAll(){
Page |
34
if(last== NUL L ) {
return;
next No de = tem p
-
>next;
free (tem p);
free (last);
last = NUL L;
printf("Entirecircular listdeleted.
\
n");
vo id co untNodes() {
if(last== NUL L ) {
return;
} int co unt =0 ;
co unt+ +;
tem p = tem p
-
>next ;
} w hile(tem p != last
-
>next );
vo id display () {
if(last== NUL L ) {
return;
tem p = tem p
-
>next ;
} w hile(tem p != last
-
>next );
printf("
\
n");
}int m ain() {
do {
printf("
\
n
---
printf(" 0 . Ex it
\
n");
scanf("%d",&c ho ice);
Page |
35
case1 :
if ( last != NUL L)
else {
break;
case2 :
printf("Enter datato insert: " );
scanf("% d",&data);
insert( data);
break;
case3 :
scanf("% d",&data);
break;
case4 :
deleteAll();
break;
case5 :
co untNodes( );
break;
case6 :
display();
break;
case0 :
break;
default:
printf("Invalid c hoice!
\
n");
ret urn0 ;
Output
:
Page |
36
3.
Wr i te a menu dri ven prog ram to per form the fol l ow ing operati ons on a DOUB LY LIN
KED
LI ST
i)
ii)
i i i)
i v)
v)
v i)
v ii )
i x)
x)
xi)
Page |
37
xii )
Di s pl ay t he li st
#include <stdlib.h>
struct No de {
int data;
};
new No de
-
>data = data;
new No de
-
>prev = newNo de
-
>next = NULL ;
if (head == NUL L ) {
} e lse {
while (tem p
-
>next !=NUL L )
tem p = tem p
-
>next ;
tem p
-
>next = newNo de;
new No de
-
>prev = temp;
vo id insertAtBeginning(int data) {
new No de
-
>data = data;
new No de
-
>prev = NULL ;
new No de
-
>next = head;
if (head != NULL )
head
-
>prev = new No de;
vo id insertAtEnd(int data){
create( data);
if(po s<= 0 ){
return;
if(po s== 1 ){
insertAtBeginning(data);
return;
1 ; i++ )
tem p = tem p
-
>next ;
if (tem p = = NUL L) {
Page |
38
return;
new No de
-
>next = tem p
-
>next;
new No de
-
>prev = tem p;
if (tem p
-
>next != NUL L)
tem p
-
>next
-
>prev = newNo de;
tem p
-
>next = newNo de;
vo id deleteFrom Beginning() {
if(head == NULL ){
printf("L ist is em pty.
\
n");
return;
head = head
-
>next;
if (head != NULL )
head
-
>prev = NULL ;
if(head == NULL ){
return;
if(tem p
-
>next == NUL L ) {
head = NUL L;
return;
while (tem p
-
>next != NULL)
tem p = tem p
-
>next ;
tem p
-
>prev
-
>next = NULL ;
tem p = tem p
-
>next ;
printf("Value no t fo und.
\
n");
return;
}
if (tem p
-
>prev != NUL L )
tem p
-
>prev
-
>next =tem p
-
>next ;
else
head = tem p
-
>next;
Page |
39
5 4 |Page
if(tem p
-
>next != NUL L )
tem p
-
>next
-
>prev = tem p
-
>prev;
return;
tem p = tem p
-
>next ;
if (tem p == NULL ){
printf("Po sitio n not found.
\
n");
return;
if (tem p
-
>prev != NUL L)
tem p
-
>prev
-
>next =tem p
-
>next ;
else
head = tem p
-
>next;
if (tem p
-
>next != NUL L )
tem p
-
>next
-
>prev = tem p
-
>prev;
}vo id deleteAll() {
tem p = head;
head = head
-
>next;
vo id co untNodes() {
int co unt = 0;
co unt+ +;
tem p = tem p
-
>next ;
int po s= 1;
if(tem p
-
>data = = key){
return;
Page |
40
po s+ +;
tem p = tem p
-
>next ;
}vo id display() {
if(tem p == NUL L ){
tem p = tem p
-
>next ;
printf("
\
n");
int m ain() {
do {
printf("
\
n
---
scanf("%d",&c ho ice);
case1 :
case3 :
scanf("% d",&data);
create( data);
break;
case2 :
scanf("% d",&data);
insertAtBe ginning(data);
break;
case4 :
break;
case5 :
deleteFrom Beginning();
break;
case6 :
deleteFrom End();
Page |
41
break;
case7 :
scanf("% d",&data);
break;
case8 :
scanf("% d",&pos);
break;
case9 :
deleteAll();
break;
case1 0:
co untNodes( );
break;
case1 1:
scanf("% d",&data);
search(data);
break;
case1 2:
display();
break;
case0 :
printf("Ex iting.
\
n");
break;
default:
printf("Invalid c hoice.
\
n");
ret urn0 ;
Output
:
Page |
42
4.
Wr i te a menu dri ven prog ram to i mpl ement aSTAC Kus i ng an array of si ze1 0.
#define SIZE1 0
int stack[SIZE];
int to p=
-
1;
/ / P ush functio n
vo id push(intvalue) {
if(to p = = SIZE
-
1){
} e lse {
to p+ +;
stack[to p]=value;
Page |
43
/ / Po p functio n
vo id po p() {
if(to p = =
-
1 ){
} e lse {
to p
--
;
}/ / Peek functio n
vo id peek () {
if(to p = =
-
1 ){
} e lse {
printf("To p e lement is%d
\
n",stack[to p]);
}/ / D isplay functio n
vo id display () {
if(to p = =
-
1 ){
} e lse {
fo r (int i = to p; i >=0 ; i
--
){
printf("
\
n");
int m ain() {
intcho ice,value;
do {
printf("
\
n
---
printf("1 .P ush
\
n2 . Po p
\
n3 .Peek
\
n4 . D isplay
\
n0 . Exit
\
n");
scanf("%d",&c ho ice);
switch ( cho ice) {
case1 :
scanf("% d",&value) ;
push(value);
break;
case2 :
po p();
break;
case3 :
pee k();
break;
case4 :
display();
break;
Page |
44
case0 :
break;
default:
ret urn0 ;
Output
:
5.
Wr i te a menu dri ven prog ram to i mpl ement aSTAC Kus i ng al i nked
-
l ist.
#include <stdlib.h>
/ / D efine a no de structure
struct No de {
int data;
};
/ / P ush functio n
vo id push(intvalue) {
if(!newNo de) {
Page |
45
return;
new No de
-
>data = value;
new No de
-
>next = to p;
to p = newNo de;
}/ / Po p functio n
vo id po p() {
if(to p = = NUL L) {
return;
}
struct No de* tem p = to p;
to p = to p
-
>next;
/ / Peek functio n
vo id peek () {
if(to p = = NUL L) {
} e lse {
/ / D isplay functio n
vo id display () {
if(to p = = NUL L) {
return;
tem p = tem p
-
>next ;
}
printf("
\
n");
int m ain() {
intcho ice,value;
do {
printf("
\
n
---
printf("1 .P ush
\
n2 . Po p
\
n3 .Peek
\
n4 . D isplay
\
n0 . Exit
\
n");
case1 :
scanf("% d",&value) ;
push(value);
Page |
46
break;
case2 :
po p();
break;
case3 :
pee k();
break;
case4 :
display();
break;
case0 :
break;
default:
ret urn 0;
Output
:
6.
Wr i te a menu dri ven prog ram to i mpl ement a QUEUE usi ng an array ofs i ze 10 .
#define SIZE1 0
int queue[SIZE];
int fro nt =
-
1 , rear=
-
1;
/ / Enqueue functio n
vo idenqueue(intvalue) {
Page |
47
if(rear = = SIZE
-
1){
} e lse {
if(fro nt ==
-
1)
fro nt = 0 ;
rear+ +;
queue[rear]=value;
printf("Enqueued %d to t he queue.
\
n",value);
/ / D equeue functio n
vo id dequeue( ){
if(fro nt ==
-
1 || fro nt > rear) {
} e lse {
fro nt+ +;
fro nt = rear=
-
1;
/ / Peek functio n
vo id peek () {
if(fro nt ==
-
1 || fro nt > rear) {
printf("Queue is em pty.
\
n");
} e lse {
/ / D isplay functio n
vo id display () {
if(fro nt ==
-
1 || fro nt > rear) {
printf("Queue is em pty.
\
n");
} e lse {
printf("
\
n");
}
}int m ain() {
intcho ice,value;
do {
printf("
\
n
---
printf("1 . Enqueue
\
n2 . D equeue
\
n3 . Pee k
\
n4 .D isplay
\
n0 . Exit
\
n");
scanf("%d",&c ho ice);
switch(cho ice) {
case1 :
printf("Entervalue to enqueue: " );
Page |
48
scanf("% d",&value) ;
enqueue(value);
break;
case2 :
dequeue();
break;
case3 :
pee k();
break;
case4 :
display();
break;
case0 :
break;
default:
ret urn 0;
Output
:
7.
Wr i te a menu dri ven prog ram to i mpl ement a QUEUE usi ng a li nked l i st.
/ / D efine a no de structure
struct No de {
int data;
Page |
49
};
/ / Enqueue functio n
vo idenqueue(intvalue) {
if(!newNo de) {
return;
}
new No de
-
>data = value;
new No de
-
>next = NUL L ;
if (rear== NUL L) {
} e lse {
rear
-
>next = newNo de;
}/ / Dequeue functio n
vo id dequeue( ){
if(fro nt == NULL ){
return;
fro nt = fro nt
-
>next;
if(fro nt == NULL )
rear= NULL ;
}/ / Peek functio n
vo id peek () {
if(fro nt == NULL ){
printf("Queue is em pty.
\
n");
} e lse {
/ / D isplay functio n
vo id display () {
if(fro nt == NULL ){
printf("Queue is em pty.
\
n");
return;
tem p = tem p
-
>next ;
Page |
50
printf("
\
n");
}int m ain() {
intcho ice,value;
do {
printf("
\
n
---
printf("1 . Enqueue
\
n2 . D equeue
\
n3 . Pee k
\
n4 .D isplay
\
n0 . Exit
\
n");
scanf("%d",&c ho ice);
case1 :
scanf("% d",&value) ;
enqueue(value);
break;
case2 :
dequeue();
break;
case3 :
pee k();
break;
case4 :
display();
break;
case0 :
break;
default:
Output
:
Page |
51
8.
Wr i te a menu dri ven prog ram to i mpl ement aC I RC ULAR QUEUE us i ng a li nked l ist.
#include <stdlib.h>
/ / No de definitio n
struct No de {
int data;
};
/ / Enqueueo peratio n
vo idenqueue(intvalue) {
return;
new No de
-
>data = value;
new No de
-
>next = NUL L ;
if(fro nt == NULL ){
rear
-
>next = fro nt; // Circular link
} e lse {
rear
-
>next = newNo de;
rear
-
>next = fro nt;
Page |
52
}/ / Dequeueo peratio n
vo id dequeue( ){
if(fro nt == NULL ){
return;
}
if (fro nt== rear){ // Only one no de
} e lse {
fro nt = fro nt
-
>next;
rear
-
>next = fro nt;
}/ / Peek o peratio n
vo id peek () {
if(fro nt == NULL ){
printf("CircularQueueis em pty.
\
n");
} e lse {
/ / D isplay o peratio n
vo id display () {
if(fro nt == NULL ){
printf("CircularQueueis em pty.
\
n");
return;
}
printf("CircularQueueelements: ");
do {
tem p = tem p
-
>next ;
printf("
\
n");
int m ain() {
intcho ice,value;
do {
printf("
\
n
---
printf("1 . Enqueue
\
n2 . D equeue
\
n3 . Pee k
\
n4 .D isplay
\
n0 . Exit
\
n");
scanf("%d",&c ho ice);
case1 :
Page |
53
scanf("% d",&value) ;
enqueue(value);
break;
case2 :
dequeue();
break;
case3 :
pee k();
break;
case4 :
display();
break;
case0 :
default:
return 0;
Output
:
9.
Wr i te a menu dri ven prog ram to i mpl ement a PRI ORIT Y QUEUE usi ng a li nked l ist.
#include <stdlib.h>
/ / No destructurewith priority
Page |
54
struct No de {
int data;
};
vo idenqueue(intvalue, intpriority ) {
new No de
-
>data = value;
new No de
-
>prio rity = prio rity;
new No de
-
>next = NUL L ;
// Insert at beginning if it's t he highest priority
new No de
-
>next = fro nt;
} e lse {
while (tem p
-
>next !=NUL L && tem p
-
>next
-
>priority <= prio rity)
tem p = tem p
-
>next ;
new No de
-
>next = tem p
-
>next;
tem p
-
>next = newNo de;
vo id dequeue( ){
if(fro nt == NULL ){
return;
fro nt = fro nt
-
>next;
free (tem p);
vo id peek () {
if(fro nt == NULL ){
} e lse {
}/ / D isplay queue
vo id display () {
if(fro nt == NULL ){
tem p = tem p
-
>next ;
Page |
55
}int m ain() {
printf("
\
n
---
printf("1 . Enqueue
\
n2 . D equeue
\
n3 . Pee k
\
n4 .D isplay
\
n0 . Exit
\
n");
scanf("%d",&c ho ice);
case1 :
break;
case2 :
dequeue();
break;
case3 :
pee k();
break;
case4 :
display();
break;
case0 :
break;
default:
return 0;
Output
:
Page |
56
1 0.
Wr i te a menu dri ven prog ram to i mpl ement a DEQUEUE usi ng al i nked l ist.
#include <stdlib.h>
/ / No de definitio n
struct No de {
int data;
};
/ / Insert at fro nt
new No de
-
>data = value;
new No de
-
>prev = NULL ;
new No de
-
>next = fro nt;
else {
fro nt
-
>prev = new No de;
Page |
57
/ / Insert at rear
vo id insertRear(intvalue) {
new No de
-
>data = value;
new No de
-
>next = NUL L ;
new No de
-
>prev = rear;
if (rear = = NULL )
else {
rear
-
>next = newNo de;
vo id deleteFro nt( ) {
if(fro nt == NULL ){
printf("D equeue Underflow! Cannot delete from fro nt.
\
n");
return;
fro nt = fro nt
-
>next;
rear= NULL ;
else
fro nt
-
>prev = NULL ;
free(tem p);
}
/ / D elete from rear
vo id deleteRear() {
if(rear = = NUL L) {
return;
rear = rear
-
>prev ;
if(rear = = NUL L)
fro nt = NUL L;
else
rear
-
>next = NULL ;
}/ / D isplay DEQUEUE
vo id display () {
if(fro nt == NULL ){
return;
Page |
58
tem p = tem p
-
>next ;
printf("
\
n");
}int m ain() {
intcho ice,value;
do {
printf("
\
n
---
printf("5 .D isplay
\
n0 . Exit
\
n");
scanf("%d",&c ho ice);
switch(cho ice) {
case1 :
scanf("% d",&value) ;
insertFro nt(value) ;
break;
case2 :
printf("Entervalue to insert at rear:");
scanf("% d",&value) ;
insertRear(value) ;
break;
case3 :
deleteFro nt();
break;
case4 :
deleteRear();
break;
case5 :
display();
break;
case0 :
printf("Ex iting program .
\
n");
break;
default:
return 0;
Output
:
Page |
59
1 1.
Wr i te a program toreverse a stri ng us i ng stac k.
#include <string.h>
#define SIZE1 00
/ / Stack im plementatio n
char stack[SIZE];
int to p=
-
1;
/ / P usho peratio n
vo id push(char c h) {
if(to p = = SIZE
-
1){
} e lse {
}/ / Po po peratio n
char po p(){
if(to p = =
-
1 ){
return '
\
0 ';
} e lse {
return stack[to p
--
];
Page |
60
int m ain() {
char str[SIZE],reversed[SIZE];
int i;
printf(" Enter a string: " );
get s(str); // No te: get s() is unsafe; yo u can use fget s() instead
push(str[i]);
reversed[i] = po p();
reversed[i] = '
\
0 '; / / Null
-
term inate the reversed string
ret urn 0;
}
Output
:
1 2.
Wr i te a program toc onvert an i nfi x express i on toi tsequi val ent postfi x form.
#include <string.h>
#define SIZE1 00
char stack[SIZE];
int to p=
-
1;
/ / P usho peratio n
vo id push(char c h) {
if(to p = = SIZE
-
1)
else
stack[+ +to p]= ch;
/ / Po p o peratio n
char po p(){
if(to p = =
-
1)
return
-
1;
else
return stack[to p
--
];
/ / Peek to po f stack
char peek() {
if(to p = =
-
1)
return
-
1;
else
Page |
61
switch (ch) {
case '*':
case '+':
case '
-
': return 1;
}
}
int m ain() {
int i = 0 , j=0 ;
scanf("%s" , infix);
po stfix[j++ ] = to ken;
push(to ken);
po stfix[j++ ] = po p();
while (to p !=
-
1 &&prece dence( peek ()) >= precedence(token))
po stfix[j++ ] = po p();
push(to ken);
i+ +;
while (to p !=
-
1)
po stfix[j+ +] = po p();
po stfix[j] = '
\
0 ';
printf(" Postfixexpressio n: %s
\
n", po stfix);
return 0;
Output
:
1 3.
Page |
62
#include <stdlib.h>
#define SIZE1 00
int stack[SIZE];
int to p=
-
1;
/ / P usho peratio n
vo id push(intvalue) {
if(to p = = SIZE
-
1)
else
/ / Po p o peratio n
int po p() {
if(to p = =
-
1 ){
exit( 1 );
} e lse {
return stack[to p
--
];
int m ain() {
char po stfix[SIZE];
int i, a, b,result;
scanf("%s" , po stfix) ;
char c h = po stfix[i];
if (isdigit(ch)) {
push(ch
-
} e lse {
b= po p();
a = po p();
switch(ch) {
case '
-
': result = a
-
b; break;
if ( b ==0 ) {
ret urn1 ;
result = a / b; break;
default:
printf("Invalidoperato r%c
\
n", ch);
ret urn 1;
push(result);
return 0;
Page |
63
Output
:
1 4.
Wr i te a program toc onvert an i nfi x express i on toi tsequi val ent prefi x for m.
#include <string.h>
#include <stdlib.h>
#define M AX1 00
char stack[MAX];
int to p=
-
1;
/ / Stack o peratio ns
vo id push(char c ) {
stack[+ +to p] = c ;
char po p(){
return stack[to p
--
];
char peek() {
}
int isEmpty() {
return to p==
-
1;
switch (o p) {
case '*':
case '+':
case '
-
': return 1;
/ / Reversea string
vo id reverse(char* str) {
str[i] =str[len
-
i
-
1 ];
str[len
-
i
-
1 ]= tem p;
}
Page |
64
int i, j = 0;
char c h;
fo r (i=0 ; infix[i]; i+ +) {
ch = infix[i];
if (isalnum (ch)) {
po stfix[j++ ] =ch;
push(ch);
po stfix[j++ ] = po p();
}
while (!isEm pty ()&& precedence( peek() ) > = prece dence( ch)) {
po stfix[j++ ] = po p();
push(ch);
po stfix[j+ +] = po p();
/ / Swap ( and)
}char po stfix[MAX];
reverse( po stfix) ;
}/ / M ain functio n
int m ain() {
scanf("%s" , infix);
printf("P refixexpressio n: % s
\
n", prefix) ;
return 0;
Output
:
Page |
65
1 5.
1;
while (low <= high & & key>= arr[lo w] && key <= arr[high]) {
return
-
1;
lo w) / ( arr[high]
-
arr[low]);
if (arr[po s] == key )
return po s;
low = po s + 1 ;
else
high = po s
-
1;
} ret urn
-
1; / / No t fo und
}/ / M ain functio n
int m ain() {
scanf("%d",&n);
printf("Enter%d so rtedelements:
\
n", n);
fo r (int i = 0 ; i < n; i+ +) {
scanf("%d",&arr[i]);
scanf("%d",&key);
if (index !=
-
1)
printf("Elem ent%d fo und at index%d.
\
n", key, index);
else
ret urn0 ;
Output
:
1 6.
Page |
66
lo ng lo ng facto rial(int n) {
if (n ==0 || n == 1)
return 1;
else
1 );
/ / M ain functio n
int m ain() {
int num ;
printf("Enter a no n
-
negative integer: ");
scanf("%d",&num );
if (num <0 ) {
} e lse {
lo ng lo ngresult= factorial(num );
printf("Facto rialo f%dis % lld
\
n", num , result);
return 0;
Output
:
1 7.
1 ) + fibo nacci(n
-
2 );
}
/ / M ain functio n
int m ain() {
int n;
printf(" Enter the num berof term s inthe Fibo nacci series: ");
scanf("%d",&n);
if (n <= 0) {
} e lse {
printf("
\
n");
ret urn 0;
Output
:
Page |
67
1 8.
if(b == 0 )
return a;
/ / M ain functio n
int m ain() {
printf("GCD o f % d and%d is %d
\
n", num1 , num 2 , result) ;
ret urn0 ;}
Output
:
1 9.
lo ng lo ng po wer(int x, inty ) {
if(y == 0 )
return 1;
else
return x * po wer(x ,y
-
1 );
/ / M ain functio n
int m ain() {
scanf("%d",&base);
scanf("%d",&expo nent);
} e lse {
lo ng lo ngresult= po wer(base,expo nent);
printf("%d^%d = %lld
\
n", base,expo nent, result);
ret urn 0;
Page |
68
Output
:
2 0.
if(n == 1 ) {
return;
towerOfHano i(n
-
towerOfHano i(n
-
/ / M ain functio n
int m ain() {
int n;
scanf("%d",&n);
return 0;
Output
:
2 1.
a)
B ubbl e S ort
b)
S el ecti onS or t
c)
I ns er ti on S ort
9 4 |Page
Page |
69
printf("
\
n");
/ / B ubbl eS or t
printf("
\
nBubble So rt:
\
n");
fo r (int i = 0 ; i < n
-
1; i+ +) {
i
-
1; j+ +) {
if ( arr[j] > arr[j + 1 ]) {
// Swap
arr[j] = arr[j + 1 ];
arr[j + 1 ] = tem p;
/ / S el ec ti on S ort
printf("
\
nSelectio n So rt:
\
n");
fo r (int i = 0 ; i < n
-
1; i+ +) {
intm in= i;
m in= j;
/ / Swap
arr[i]= tem p;
/ / I ns er ti onS or t
fo r (int i = 1 ; i < n; i+ +) {
intkey = arr[i];
int j = i
-
1;
arr[j + 1 ] = arr[j];
j
--
;
arr[j +1 ] =key;
}
/ / M ain functio n
int m ain() {
Page |
70
scanf("%d",&n);
into riginal[10 0 ];
fo r (int i = 0 ; i < n; i+ +) {
scanf("%d",&arr[i]);
return 0;
Output
: