Dsaassf

Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

Enrollment no.

:0801IT221017

Program 9
9. Write a program to implement stack using array.

#include <stdio.h>

#include<stdlib.h>

#define size 5

struct stack{ int

A[size]; int top;

}s;

void push(){ if(s.top==size-1)

{ 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--; } } void display() { if

(s.top == -1) {

printf("Underflow\n"); } else

{ for (int i = 0; i <= s.top; i++)

{ printf("%d ", s.A[i]); }

printf("\n"); } } void main() {

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

1:push(); break; case

2:pop(); break; case

3:display();break; case

0:exit(0);

2
Enrollment no.:0801IT221017

OUTPUT: Stack implemented using array.

3
Enrollment no.:0801IT221017

Program 10

10. Program to implement stack using linked list.

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

struct Node* top = NULL; void push(int value) { struct Node*

newNode = (struct Node*)malloc(sizeof(struct Node)); if (newNode

== NULL) {

printf("Overflow.\n");

return; }

newNode->data = value;

newNode->next = top; top =

newNode; printf("Pushed

%d.\n", value);

} void pop() { if (top

== NULL) {

printf("Underflow.\n");

}
int value = top->data;

struct Node* temp = top;

top = top->next;

4
Enrollment no.:0801IT221017

free(temp); printf("Popped

%d",value);

} void display() { struct

Node* current = top; if

(current == NULL) {

printf("Underflow.\n");

} else { printf("Stack

contents: "); while (current

!= NULL) { printf("%d ",

current->data); current =

current->next;

} printf("\n");

} } void main() { printf("AMAN

SHARMA\n0801IT221017\n");

int choice, data; while (1) { printf("Enter 1 to push,2 to

pop,3 to display and 0 to exit "); scanf("%d", &choice);

switch (choice) { case 1: printf("Enter

the data to push: "); scanf("%d", &data);

push(data); break; case 2: pop(); break;

case 3: display(); break; case 0: exit(0);

default: printf("Invalid choice. \n");

5
Enrollment no.:0801IT221017

OUTPUT: Stack implemented using linked list.

6
Enrollment no.:0801IT221017

Program 11

11. Infix to postfix conversion using array.

#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 preci(char ch)


{ switch (ch) {
case '#': return 0;
case '+': case '-':
return 1; case '*':
case '/': return 2;
default: return 0;
}
}

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);

while ((ch = infix[i++]) != '\0') {


if (ch == '(') {
push(ch);
} else if (isalnum(ch)) {
postfix[k++] = ch; }
else if (ch == ')') {

7
Enrollment no.:0801IT221017

while (s.a[s.top] != '(')


{ postfix[k++] = pop();
}
char trash = pop(); }
else {
while (preci(s.a[s.top]) >= preci(ch)) {
postfix[k++] = pop();
}
push(ch);
} } while (s.a[s.top] !=
'#') { postfix[k++] =
pop();
} postfix[k] =
'\0';
printf("Postfix is - %s\n", postfix);
}

OUTPUT: Infix to postfix converted using array.

8
Enrollment no.:0801IT221017

Program 12

12. Infix to postfix conversion using linked list.

#include <stdio.h>

#include <ctype.h>

#include <stdlib.h>

#include <string.h>

struct Stack { char*

data;

int top; int size; };

struct Stack stack;

char* postfix; int

postfixIndex = 0;

void initStack(int stackSize) { stack.size =

stackSize; stack.data = (char*)malloc(stackSize *

sizeof(char)); stack.top = -1; } void push(char elem)

if (stack.top < stack.size - 1) { stack.data[++stack.top]

= elem; } else { printf("Error: Stack is full\n");

} } char pop() { if (stack.top

>= 0) { return

stack.data[stack.top--];

} else { printf("Error: Stack is

empty\n");

9
Enrollment no.:0801IT221017

return '\0'; } } int

precedence(char op) {

switch (op) {

case '+': case '-': return 1; case '*': case

'/': return 2; default: return 0; } } void

infixToPostfix(const char* infix) {

initStack(strlen(infix)); // Initialize the

stack with a dynamic size postfix =

(char*)malloc(strlen(infix) *

sizeof(char)); postfixIndex = 0;

int i = 0; while (infix[i] !=

'\0') { char ch = infix[i]; if

(isalnum(ch)) {

postfix[postfixIndex++] = ch;

} else if (ch == '(') {

push(ch);

} else if (ch == ')') { while (stack.top >= 0 &&

stack.data[stack.top] != '(') { postfix[postfixIndex++]

= pop();

} if (stack.top >= 0 && stack.data[stack.top] == '(')

{ pop(); }

} else {

while (stack.top >= 0 && precedence(stack.data[stack.top]) >= precedence(ch)) {

postfix[postfixIndex++] = pop();

10
Enrollment no.:0801IT221017

}push(ch); } i++; }

while (stack.top >= 0) {

postfix[postfixIndex++

] = pop();

} postfix[postfixIndex] = '\0';

printf("Postfix is: %s\n", postfix);

free(stack.data); free(postfix);

void main() { printf("AMAN

SHARMA\n0801IT221017\nProgram no.:12\n");

char infix[50]; printf("Enter infix

expression: "); scanf("%s",

&infix); infixToPostfix(infix);

OUTPUT: Infix to postfix converted using linked list.

11
Enrollment no.:0801IT221017

Program 13

13. Implementation of queue using array.

#include <stdio.h>
#include <stdlib.h>
#define size 100

struct Queue {
int
array[size];
int front; int
rear;
} q;

void enqueue(int data)


{ if (q.rear == size - 1)
{ printf("Overflow.\n");
} else { if (q.front == -
1) { q.front = 0;
}
q.rear++;
q.array[q.rear] = data;
}
}

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;
}

printf("Queue elements: "); for (int i


= q.front; i <= q.rear; i++) {
printf("%d ", q.array[i]);
} printf("\n");
}

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

OUTPUT: Queue implemented successfully using array.

14
Enrollment no.:0801IT221017

Program 14

14. Implementation of queue using linked list.

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

struct q { int
data; struct q
*next;
};

struct q *rear, *newnode,*temp;

void enqueue(int data) { newnode = (struct


q*)malloc(sizeof(struct q));
newnode->data = data;
newnode->next = NULL; if
(rear == NULL) { rear =
newnode; } else { temp = rear;
while (temp->next != NULL)
{ temp = temp->next;
}
temp->next = newnode;
}
}

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:

printf("Enter a valid choice\n");


}
}
}

16
Enrollment no.:0801IT221017

OUTPUT: Queue implemented successfully using linked list.

17
Enrollment no.:0801IT221017

Program 15

15. Implementation of circular queue using array.

#include <stdio.h>

#include <stdlib.h>

#define size 100

int queue[size]; int

front = -1; int rear

= -1;

void enqueue(int data) { if ((front == 0 && rear == size -

1) || (rear == front - 1)) { printf("Queue is full. Cannot

enqueue.\n");

} else { if (front == -1)

{ front = 0; } rear =

(rear + 1) % size;

queue[rear] = data;

void dequeue() {

if (front == -1) { printf("Queue is empty.

Cannot dequeue.\n"); } else { if (front ==

18
Enrollment no.:0801IT221017

rear) { front = rear = -1; } else { front =

(front + 1) % size;

void display() {

int i;

if (front == -1) { printf("Queue

is empty.\n");

} else { printf("Queue

elements: ");

for (i = front; i != rear; i = (i + 1) % size) { printf("%d

", queue[i]);

} printf("%d\n",

queue[i]);

void main() { int

choice, data;

printf("AMAN SHARMA\n0801IT221017\nProgram no.:15\n"); while (1) { printf("Enter 1 for enqueue,

2 for dequeue, 3 for display, and 0 to exit: "); scanf("%d", &choice); switch (choice) { case 1:

printf("Enter data: ");

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

OUTPUT: Circular queue implemented successfully using array.

21
Enrollment no.:0801IT221017

Program 16

16. Implementation of circular queue using linked list.

#include <stdio.h>

#include <stdlib.h> struct

Node {

int data;

struct Node* next;

};

struct Node* front = NULL; struct

Node* rear = NULL;

void enqueue(int data) { struct Node* newnode = (struct

Node*)malloc(sizeof(struct Node)); newnode->data = data;

newnode->next = NULL;

if (front == NULL) {

front = newnode; }

else {

rear->next = newnode;

rear = newnode; rear->next = front;

// Make it circular

22
Enrollment no.:0801IT221017

void dequeue() { if (front == NULL) {

printf("Queue is empty. Cannot dequeue.\n");

return; }

if (front == rear) { free(front);

front = rear = NULL; } else { struct

Node* temp = front; front = front-

>next; rear->next = front; // Make it

circular free(temp);

void display() { if (front ==

NULL) { printf("Queue is

empty.\n");

} else { struct Node* current

= front; printf("Queue

elements: "); do {

printf("%d ", current->data);

current = current->next; }

while (current != front);

printf("\n");

23
Enrollment no.:0801IT221017

int main() { int

choice, data;

printf("AMAN SHARMA\n0801IT221017\nProgram
no.:16\n");

while (1) { printf("Enter 1 for enqueue, 2 for dequeue, 3 for display, and

0 to exit: "); scanf("%d", &choice); switch (choice) { case 1:

printf("Enter data: ");

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

OUTPUT: Circular queue implemented successfully using linked list.

25
Enrollment no.:0801IT221017

Program 17

17. Implement bubble sort.

#include <stdio.h> void

bubbleSort(int arr[], int n) {

int i, j;

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

for (j = 0; j < n - i - 1; j++) {

if (arr[j] > arr[j + 1]) { int

temp = arr[j]; arr[j] = arr[j +

1]; arr[j + 1] = temp;

void main() { printf("AMAN

SHARMA\n0801IT221017\nProgram no.:17\n");

int arr[] = {84,32,91,36,22,74,};

int n = sizeof(arr) / sizeof(arr[0]);

printf("Unsorted array: "); for

(int i = 0; i < n; i++) { printf("%d

", arr[i]);

}
printf("\n");

bubbleSort(arr, n);
26
Enrollment no.:0801IT221017

printf("Sorted array: "); for

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

printf("%d ", arr[i]);

} printf("\n");

OUTPUT: Bubble sort successfully implemented.

27
Enrollment no.:0801IT221017

Program 18

18. Implement selection sort.

#include <stdio.h>

void selectionSort(int arr[], int n)

{ int i, j, index;

for (i = 0; i < n - 1; i++)

{ index = i;

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

{ if (arr[j] < arr[index])

{ index = j;

}}

int temp = arr[i];

arr[i] = arr[index];

arr[index] = temp;

void main() {

printf("AMAN SHARMA\n0801IT221017\nProgram

no.:18\n");

int arr[] = {45,11,62,28,33,70}; int

n = sizeof(arr) / sizeof(arr[0]);

printf("Unsorted array: ");

28
Enrollment no.:0801IT221017

for (int i = 0; i < n; i++) { printf("%d

", arr[i]);

} printf("\n");

selectionSort(arr, n);

printf("Sorted array: ");

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

printf("%d ", arr[i]);

} printf("\n");

OUTPUT: Selection sort successfully implemented.

29

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy