0% found this document useful (0 votes)
42 views12 pages

DS 1-5 Experiments

The document contains code for implementing various stack and queue operations using arrays and linked lists. These include push, pop, and peek operations for stacks, as well as enqueue and dequeue operations for queues. The code is written by Shaikh Saaiem Salaar Mohammed Saleem and includes their name, roll number, and branch at the top of each program.

Uploaded by

saaiem.221846.ci
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)
42 views12 pages

DS 1-5 Experiments

The document contains code for implementing various stack and queue operations using arrays and linked lists. These include push, pop, and peek operations for stacks, as well as enqueue and dequeue operations for queues. The code is written by Shaikh Saaiem Salaar Mohammed Saleem and includes their name, roll number, and branch at the top of each program.

Uploaded by

saaiem.221846.ci
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

NAME : SHAIKH SAAIEM SALAAR MOHAMMED SALEEM

ROLLNO : 221846 BRANCH : CSE-IOTCSBCT


EXPERIMENT - 01 = IMPLEMENT STACK ADT USING ARRAY

CODE:
//SAAIEM SALAAR
#include <stdio.h>
// Function declarations
void push();
void pop();
void peek();

// Taking stack array of size 50


int stack[50], i, j, option, n, value, top = -1;

void main()
{
printf("NAME : SAAIEM SALAAR\t\t ROLL : 221846\n\n");
// Size of stack
printf("Enter the number of elements in the stack : ");
scanf("%d", &n);
printf("Stack Implementation using array \n");

// Taking user input for the operation to be performed


while (option != 4)
{
printf("Choose one from the below options...\n");
printf("\n 1.Push\n 2.Pop\n 3.Peek\n 4.Exit");
printf("\n Enter your option : ");
scanf("%d", &option);
switch (option)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
peek();
break;
}
default:
{
printf("Please Enter a valid option\n");
}
};
}
}

// Push operation function


void push()
{
int value;
if (top == n - 1) // Check if the stack is full
printf("Stack overloaded\n");
else
{
printf(" Enter the value : ");
scanf("%d", &value);
top = top + 1;
stack[top] = value;
}
}

// Pop operation function


void pop()
{
if (top == -1)
printf("Underflow\n");
else
{
printf("Popped element is %d\n", stack[top]);
top = top - 1;
}
}

// Peek operation function (only showing the top element)


void peek()
{
if (top == -1)
{
printf("Stack is empty\n");
}
else
{
printf("Top element of the stack: %d\n", stack[top]);
}
}
OUTPUT :
NAME : SHAIKH SAAIEM SALAAR MOHAMMED SALEEM
ROLLNO : 221846 BRANCH : CSE-IOTCSBCT
EXPERIMENT – 2 = INFIX TO POSTFIX CONVERSION USING STACK

CODE:

//SAAIEM SALAAR
#include <stdio.h>
#include <ctype.h>

char stack[100]; // Stack to hold operators


int top = -1; // Top of the stack

void push(char x) {
stack[++top] = x; // Push an operator onto the stack
}

char pop() {
if (top == -1)
return -1;
else
return stack[top--]; // Pop an operator from the stack
}

int priority(char x) {
if (x == '(')
return 0; // Priority for opening parenthesis
if (x == '+' || x == '-')
return 1; // Priority for addition and subtraction
if (x == '*' || x == '/')
return 2; // Priority for multiplication and division
return 0;
}

int main() {
char exp[100];
char *e, x;

printf("NAME : SAAIEM SALAAR\t\t ROLL : 221846\n\n");


printf("ENTER THE INFIX EXPRESSION : ");
scanf("%s", exp);
printf("\nPOSTFIX EXPRESSION : ");
e = exp;

while (*e != '\0') {


if (isalnum(*e)) {
printf("%c", *e); // If it's an operand, print it
} else if (*e == '(') {
push(*e); // If it's an opening parenthesis, push it onto the stack
} else if (*e == ')') {
while ((x = pop()) != '(') {
printf("%c", x); // Pop and print operators until a matching '(' is found
}
} else {
while (priority(stack[top]) >= priority(*e)) {
printf("%c", pop()); // Pop and print operators with higher or equal priority
}
push(*e); // Push the current operator onto the stack
}
e++;
}

while (top != -1) {


printf("%c", pop()); // Pop and print any remaining operators
}

return 0;
}

OUTPUT :
NAME : SHAIKH SAAIEM SALAAR MOHAMMED SALEEM
ROLLNO : 221846 BRANCH : CSE-IOTCSBCT
EXPERIMENT – 3 = EVALUATE POSTFIX EXPRESSION USING STACK

CODE:
//SAAIEM SALAAR
#include <stdio.h>
#include <ctype.h> // Include this header for isdigit function

int stack[20];
int top = -1;

// Function to push an element onto the stack


void push(int x) {
stack[++top] = x;
}

// Function to pop an element from the stack


int pop() {
return stack[top--];
}

int main() {
char exp[20];
char *e;
int n1, n2, n3, num;

printf("NAME : SAAIEM SALAAR\t\t ROLL : 221846\n\n");


printf("ENTER THE POSTFIX EXPRESSION : ");
scanf("%s", exp);
e = exp;

while (*e != '\0') {


if (isdigit(*e)) { // Check if the character is a digit
num = *e - '0'; // Convert character digit to integer
push(num);
} else {
n1 = pop();
n2 = pop();
switch (*e) {
case '+':
n3 = n1 + n2;
break;
case '-':
n3 = n2 - n1;
break;
case '*':
n3 = n1 * n2;
break;
case '/':
n3 = n2 / n1;
break;
}
push(n3);
}
e++;
}

printf("\nEVALUATED POSTFIX EXPRESSION : %s = %d\n\n", exp, pop());


return 0;
}

OUTPUT :
NAME : SHAIKH SAAIEM SALAAR MOHAMMED SALEEM
ROLLNO : 221846 BRANCH : CSE-IOTCSBCT
EXPERIMENT – 4 = IMPLEMENT STACK USING LINKED LIST

CODE:
//SAAIEM SALAAR
#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node* next;
} *first = NULL;

// Function to create a linked list from an array


void create(int A[], int n) {
int i;
struct Node* t, * last;
first = (struct Node*)malloc(sizeof(struct Node));
first->data = A[0];
first->next = NULL;
last = first;

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


t = (struct Node*)malloc(sizeof(struct Node));
t->data = A[i];
t->next = NULL;
last->next = t;
last = t;
}
}

// Function to display the linked list


void Display(struct Node* p) {
while (p != NULL) {
printf("%d ", p->data);
p = p->next;
}
}

// Recursive function to display the linked list in reverse


void RDisplay(struct Node* p) {
if (p != NULL) {
RDisplay(p->next);
printf("%d ", p->data);
}
}

int main() {
struct Node* temp;
int A[] = {11, 32, 34, 46, 41, 13, 38, 29};
create(A, 8);
printf("NAME : SAAIEM SALAAR\t\t ROLL : 221846\n\n");
printf("GIVEN LINKED LIST : ");
Display(first);
printf("\nREVERSED LINKED LIST : ");
RDisplay(first);
return 0;
}

OUTPUT :
NAME : SHAIKH SAAIEM SALAAR MOHAMMED SALEEM
ROLLNO : 221846 BRANCH : CSE-IOTCSBCT
EXPERIMENT – 5 = IMPLEMENT LINEAR QUEUE USING ARRAY

CODE:
//SAAIEM SALAAR
#include <stdio.h>
#include <stdlib.h>

// Structure to represent a queue


struct Queue {
int size; // Maximum size of the queue
int front; // Index of the front element
int rear; // Index of the rear element
int *Q; // Pointer to the array for storing queue elements
};

// Function to create a queue with the given size


void create(struct Queue *q, int size) {
q->size = size;
q->front = q->rear = -1;
q->Q = (int *)malloc(q->size * sizeof(int)); // Allocate memory for the queue
}

// Function to add an element to the queue (enqueue)


void enqueue(struct Queue *q, int x) {
if (q->rear == q->size - 1) {
printf("Queue is Full\n"); // Check if the queue is full
} else {
q->rear++;
q->Q[q->rear] = x; // Add the element to the rear of the queue
}
}

// Function to remove an element from the queue (dequeue)


int dequeue(struct Queue *q) {
int x = -1;
if (q->front == q->rear) {
printf("Queue is Empty\n"); // Check if the queue is empty
} else {
q->front++;
x = q->Q[q->front]; // Remove the element from the front of the queue
}
return x; // Return the removed element
}

// Function to display the contents of the queue


void Display(struct Queue q) {
int i;
printf("NAME : SAAIEM SALAAR\t\t ROLL : 221846\n\n");
printf("ElEMENTS IN QUEUE : ");
for (i = q.front + 1; i <= q.rear; i++) {
printf(" %d ", q.Q[i]); // Print the elements in the queue
}
printf("\n");
}

int main() {
struct Queue q;
create(&q, 5); // Create a queue with a maximum size of 5

enqueue(&q, 46); // Add elements to the queue


enqueue(&q, 32);
enqueue(&q, 525);

Display(q); // Display the contents of the queue

printf("DE-QUEUED ELEMENT : %d ", dequeue(&q));


// Remove an element from the queue and print it

free(q.Q); // Free the memory allocated for the queue


return 0;
}

OUTPUT :

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