Dsaassf
Dsaassf
Dsaassf
:0801IT221017
Program 9
9. Write a program to implement stack using array.
#include <stdio.h>
#include<stdlib.h>
#define size 5
}s;
{ printf("Overflow");
} else{
int x;
printf("Enter value");
scanf("%d",&x);
s.top++;
s.A[s.top]=x;
} } void
pop() {
if(s.top==-1)
printf("Unde
rflow"); }
else{
printf("%d\n
1
Enrollment no.:0801IT221017
",s.A[s.top])
(s.top == -1) {
printf("Underflow\n"); } else
printf("AMAN SHARMA\n");
printf("0801IT221017\n");
s.top=-1;
int c;
while(1) { printf("Enter 1 for push,2 for pop,3 for display and
0 to exit.");
scanf("%d",&c);
switch(c) { case
3:display();break; case
0:exit(0);
2
Enrollment no.:0801IT221017
3
Enrollment no.:0801IT221017
Program 10
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
== NULL) {
printf("Overflow.\n");
return; }
newNode->data = value;
newNode; printf("Pushed
%d.\n", value);
== NULL) {
printf("Underflow.\n");
}
int value = top->data;
top = top->next;
4
Enrollment no.:0801IT221017
free(temp); printf("Popped
%d",value);
(current == NULL) {
printf("Underflow.\n");
} else { printf("Stack
current->data); current =
current->next;
} printf("\n");
SHARMA\n0801IT221017\n");
5
Enrollment no.:0801IT221017
6
Enrollment no.:0801IT221017
Program 11
#include <stdio.h>
#include <ctype.h>
#define SIZE 50
struct stack {
int top; char
a[SIZE];
} s; void push(char
elem) { s.a[++s.top] =
elem; } char pop() {
return s.a[s.top--];
}
int main() {
printf("AMAN SHARMA\n0801IT221017\nProgram
no.:11\n"); s.top = -1; push('#'); int i = 0, k = 0;
char infix[SIZE], postfix[SIZE], ch;
printf("Enter infix - ");
scanf("%s", infix);
7
Enrollment no.:0801IT221017
8
Enrollment no.:0801IT221017
Program 12
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
data;
postfixIndex = 0;
>= 0) { return
stack.data[stack.top--];
empty\n");
9
Enrollment no.:0801IT221017
precedence(char op) {
switch (op) {
(char*)malloc(strlen(infix) *
sizeof(char)); postfixIndex = 0;
(isalnum(ch)) {
postfix[postfixIndex++] = ch;
push(ch);
= pop();
{ pop(); }
} else {
postfix[postfixIndex++] = pop();
10
Enrollment no.:0801IT221017
}push(ch); } i++; }
postfix[postfixIndex++
] = pop();
} postfix[postfixIndex] = '\0';
free(stack.data); free(postfix);
SHARMA\n0801IT221017\nProgram no.:12\n");
&infix); infixToPostfix(infix);
11
Enrollment no.:0801IT221017
Program 13
#include <stdio.h>
#include <stdlib.h>
#define size 100
struct Queue {
int
array[size];
int front; int
rear;
} q;
void dequeue() { if
(q.front == -1) {
printf("Underflow.\n");
} else {
int dequeuedItem =
q.array[q.front]; if (q.front ==
q.rear) { q.front = q.rear = -1;
} else {
q.front++;
}
}
}
void display() {
if (q.front == -1) { printf("Underflow.\n");
12
Enrollment no.:0801IT221017
return;
}
int main() {
q.front = -1;
q.rear = -1; int
choice;
int data;
printf("AMAN SHARMA\n0801IT221017\nProgram no.:13\n");
while (1) {
printf("Enter 1 for enqueue,2 for dequeue,3 for display and 4 to exit\n");
scanf("%d", &choice); switch (choice) { case 1: printf("Enter an
element to enqueue: ");
scanf("%d", &data);
enqueue(data); break; case
2: dequeue(); break; case
3: display(); break; case 4:
exit(0); default:
printf("Invalid
choice.\n");
}
}
}
13
Enrollment no.:0801IT221017
14
Enrollment no.:0801IT221017
Program 14
#include <stdio.h>
#include <stdlib.h>
struct q { int
data; struct q
*next;
};
void dequeue() { if
(rear == NULL) {
printf("Underflow.\n");
return;
} temp = rear;
rear = rear-
>next;
free(temp);
}
void display() {
temp = rear; while (temp
!= NULL) { printf("%d ",
15
Enrollment no.:0801IT221017
temp->data); temp =
temp->next;
} printf("\n");
}
int main() {
printf("AMAN SHARMA\n0801IT221017\nProgram
no.:14\n"); int choice; rear = NULL; while (1) {
printf("Enter 1 for enqueue, 2 for dequeue, 3 for display, and 0 to exit: \n");
scanf("%d", &choice); switch (choice) { case 1: int data; printf("Enter
data:\n"); scanf("%d", &data); enqueue(data); break; case 2: dequeue();
break; case 3:
if (rear == NULL) { printf("Linked
list is empty.\n");
} else {
display()
; } break;
case 0:
exit(0);
default:
16
Enrollment no.:0801IT221017
17
Enrollment no.:0801IT221017
Program 15
#include <stdio.h>
#include <stdlib.h>
= -1;
enqueue.\n");
{ front = 0; } rear =
(rear + 1) % size;
queue[rear] = data;
void dequeue() {
18
Enrollment no.:0801IT221017
(front + 1) % size;
void display() {
int i;
is empty.\n");
} else { printf("Queue
elements: ");
", queue[i]);
} printf("%d\n",
queue[i]);
choice, data;
2 for dequeue, 3 for display, and 0 to exit: "); scanf("%d", &choice); switch (choice) { case 1:
19
Enrollment no.:0801IT221017
scanf("%d", &data);
enqueue(data);
break; case 2:
dequeue(); break;
case 3: display();
break; case 0:
exit(0); default:
printf("Invalid choice.\n");
20
Enrollment no.:0801IT221017
21
Enrollment no.:0801IT221017
Program 16
#include <stdio.h>
Node {
int data;
};
newnode->next = NULL;
if (front == NULL) {
front = newnode; }
else {
rear->next = newnode;
// Make it circular
22
Enrollment no.:0801IT221017
return; }
circular free(temp);
NULL) { printf("Queue is
empty.\n");
= front; printf("Queue
elements: "); do {
current = current->next; }
printf("\n");
23
Enrollment no.:0801IT221017
choice, data;
printf("AMAN SHARMA\n0801IT221017\nProgram
no.:16\n");
while (1) { printf("Enter 1 for enqueue, 2 for dequeue, 3 for display, and
scanf("%d", &data);
enqueue(data);
break; case 2:
dequeue(); break;
case 3: display();
break;
case 0: exit(0);
default:
printf("Invalid choice.\n");
24
Enrollment no.:0801IT221017
25
Enrollment no.:0801IT221017
Program 17
int i, j;
SHARMA\n0801IT221017\nProgram no.:17\n");
", arr[i]);
}
printf("\n");
bubbleSort(arr, n);
26
Enrollment no.:0801IT221017
} printf("\n");
27
Enrollment no.:0801IT221017
Program 18
#include <stdio.h>
{ int i, j, index;
{ index = i;
{ index = j;
}}
arr[i] = arr[index];
arr[index] = temp;
void main() {
printf("AMAN SHARMA\n0801IT221017\nProgram
no.:18\n");
n = sizeof(arr) / sizeof(arr[0]);
28
Enrollment no.:0801IT221017
", arr[i]);
} printf("\n");
selectionSort(arr, n);
} printf("\n");
29