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

DS Codes

Uploaded by

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

DS Codes

Uploaded by

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

DS Codes

Single Link list


Deleting Part
Doubly link list
Deleting Part
Postfix Evalution

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

#define MAX 100

int evaluatePostfix(const char *exp) {


int stack[MAX], top = -1;

for (int i = 0; exp[i] != '\0'; i++) {


char ch = exp[i];

if (isdigit(ch)) {
stack[++top] = ch - '0';
} else {
int b = stack[top--];
int a = stack[top--];

switch (ch) {
case '+': stack[++top] = a + b; break;
case '-': stack[++top] = a - b; break;
case '*': stack[++top] = a * b; break;
case '/': stack[++top] = a / b; break;
default:
printf("Invalid operator: %c\n", ch);
exit(1);
}
}
}
return stack[top];
}

int main() {
char exp[MAX];
printf("Enter a postfix expression: ");
scanf("%s", exp);
printf("The result is: %d\n", evaluatePostfix(exp));
return 0;
}

Input :
Enter a postfix expression: 53+62/*

Output :
The result is: 8
To implement stack using array

#include <stdio.h>
#define SIZE 10

// Function prototypes
void push(int value);
void pop();
void display();
void peek();

int stack[SIZE], top = -1;

int main() {
int value, choice;

do {
printf("\n\n***** MENU *****\n");
printf("1. Push\n2. Pop\n3. Display\n4. Peek\n5. Exit\n");
printf("\nEnter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter the value to be inserted: ");
scanf("%d", &value);
push(value);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
peek();
break;
case 5:
printf("\nExiting...\n");
break;
default:
printf("\nInvalid choice! Please try again.\n");
}
} while (choice != 5);

return 0;
}

void push(int value) {


if (top == SIZE - 1) {
printf("\nStack is Full! Insertion is not possible!\n");
} else {
top++;
stack[top] = value;
printf("\nInsertion successful! %d is inserted into the stack.\n", value);
}
}

void pop() {
if (top == -1) {
printf("\nStack is Empty! Deletion is not possible!\n");
} else {
printf("\nDeleted: %d\n", stack[top]);
top--;
}
}

void peek() {
if (top == -1) {
printf("\nStack is Empty!\n");
} else {
printf("\nStack top is %d\n", stack[top]);
}
}

void display() {
if (top == -1) {
printf("\nStack is Empty!\n");
} else {
printf("\nStack elements are:\n");
for (int i = top; i >= 0; i--) {
printf("%d\n", stack[i]);
}
}
}
Array implementation of queue

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

#define MAX 50

int queue_array[MAX];
int rear = -1;
int front = -1;

void insert();
void delete();
void display();

int main() {
int choice;
while (1) {
printf("\nQueue Operations:\n");
printf("1. Insert element to queue\n");
printf("2. Delete element from queue\n");
printf("3. Display all elements of queue\n");
printf("4. Quit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Wrong choice. Please try again.\n");
}
}
return 0;
}

void insert() {
int add_item;
if (rear == MAX - 1) {
printf("Queue Overflow\n");
} else {
if (front == -1) {
front = 0;
}
printf("Insert the element in queue: ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
printf("%d added to the queue.\n", add_item);
}
}

void delete() {
if (front == -1) {
printf("Queue Underflow\n");
return;
}
printf("Element deleted from queue is: %d\n", queue_array[front]);
if (front == rear) {
front = -1;
rear = -1;
} else {
front = front + 1;
}
}

void display() {
if (front == -1) {
printf("Queue is empty\n");
} else {
printf("Queue elements are: ");
for (int i = front; i <= rear; i++) {
printf("%d ", queue_array[i]);
}
printf("\n");
}
}

You might also like