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

Sneh

Uploaded by

vp367961
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)
16 views

Sneh

Uploaded by

vp367961
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/ 31

Enrollment No:- 202303103510342

Practical-1
Aim:- Implement the stack operations for the stack of elements, for the followings:
a) Push 5 elements
b) Pop 2 elements
c) Peep the 2nd element
d) Change 3rd element
e) Display all elements from stack

Program Code:-

#include <stdio.h>
#include <stdlib.h>
int top = -1,stack[10],x,i,n,top,v;
void push();
void pop();
void display();
int main()
{
printf("Enter the stack of stack[10] ");
scanf("%d", &n);
int choice;
while (choice)
{
printf("\nPerform operations on the stack:");
printf("\n\t1.Push the element\n\t2.Pop the element\n\t3.Display\n\t4.End");
printf("\nEnter the choice: ");
scanf("%d", &choice);
switch (choice)
Enrollment No:- 202303103510342

{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\nInvalid choice!!");
}
}
}
void push()
{
int x;
if (top >=n-1)
{
printf("\nOverflow!!");
}
else
{
printf("\nEnter the element to be added onto the stack: ");
scanf("%d", &x);
top++;
Enrollment No:- 202303103510342

stack
}
}
void pop()
{
if (top <= -1)
{
printf("\nUnderflow!!");
}
else
{
printf("\nPopped element: %d", stack[top]);
top = top - 1;
}
}
void display()
{
printf("\nElements present in the stack: \n");
for (int i = top; i >= 0; --i)
printf("%d\n", stack[i]);
}
Out Put:-
Enrollment No:- 202303103510342

Practical-2
Aim:- Write a program to convert an infix operation to its postfix operation using stack.

Program Code:-

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

struct Stack {
int top;
int maxSize;
int* array;
};

struct Stack* create(int max)


{
struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
stack -> maxSize = max;
stack -> top = -1;
stack -> array = (int*)malloc(stack -> maxSize * sizeof(int));
return stack;
}

int isFull(struct Stack* stack)


{
Enrollment No:- 202303103510342

if(stack -> top == stack -> maxSize - 1)


{
printf("Will not be able to push maxSize reached\n");
}
return stack -> top == stack -> maxSize - 1;
}

int isEmpty(struct Stack* stack)


{
return stack -> top == -1;
}

void push(struct Stack* stack, int item)


{
if (isFull(stack))
return;
stack -> array[++stack -> top] = item;
}

int pop(struct Stack* stack)


{
if (isEmpty(stack))
return INT_MIN;
return stack -> array[stack -> top--];
}

int peek(struct Stack* stack)


{
if (isEmpty(stack))
Enrollment No:- 202303103510342

return INT_MIN;
return stack->array[stack->top];
}

int checkIfOperand(char ch)


{
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
}
int precedence(char ch)
{
switch (ch)
{
case '+':
case '-':
return 1;

case '*':
case '/':
return 2;

case '^':
return 3;
}
return -1;
}

int covertInfixToPostfix(char* expression)


{
int i, j;
Enrollment No:- 202303103510342

struct Stack* stack = create(strlen(expression));


if(!stack)
return -1 ;

for (i = 0, j = -1; expression[i]; ++i)


{
if (checkIfOperand(expression[i]))
expression[++j] = expression[i];

else if (expression[i] == '(')


push(stack, expression[i]);

else if (expression[i] == ')')


{
while (!isEmpty(stack) && peek(stack) != '(')
expression[++j] = pop(stack);
if (!isEmpty(stack) && peek(stack) != '(')
return -1;
else
pop(stack);
}
else
{
while (!isEmpty(stack) && precedence(expression[i]) <=
precedence(peek(stack)))
expression[++j] = pop(stack);
push(stack, expression[i]);
}
Enrollment No:- 202303103510342

while (!isEmpty(stack))
expression[++j] = pop(stack);

expression[++j] = '\0';
printf( "%s", expression);
}

int main()
{
char expression[] = "((x+(y*z))-w)";
covertInfixToPostfix(expression);
return 0;
}

Out Put:-
Enrollment No:- 202303103510342

Practical-3
Aim:- Implement queue operations for library window of five people.

Program Code:-
#include<stdio.h>
#include <stdlib.h>
int queue[100],front=-1,rear=-1,n=100;

void Insert()
{
int val;
if(rear>=n)
{
printf("Queue is Overflow\n");
}
else
{
if(front==-1)
front==0;
printf("Insert the element in queue:\n");
scanf("%d",&val);
rear++;
queue[rear]=val;
}
}

void Delete()
Enrollment No:- 202303103510342

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

void Display()
{

printf("Queue element are: ");

for(int i=front; i<=rear; i++)


{
printf(" %d", queue[i]);
}
printf("\n");

}
Enrollment No:- 202303103510342

int main()
{
int ch;
printf("Insert element ti queue\n");
printf("Delete element from queue\n");
printf("Display all the element of queue\n");
printf("Exit\n");

do
{
printf("Enter your choice:\n");
scanf("%d",&ch);

switch (ch)
{
case 1:
Insert();
break;

case 2:
Delete();
break;

case 3:
Display();
break;

case 4:
printf("Exit\n");
Enrollment No:- 202303103510342

break;

default:
printf("Invalid choice\n");
}
}
while (ch!=4);
return 0;
}

Out Put:-
Enrollment No:- 202303103510342

Practical-4
Aim:- : Implement a circular queue for buffering system which performs the following
operations: insert, delete, get_front, get_rear.

Program Code:-
#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE 52

int queue[MAX_SIZE];
int front = -1, rear = -1;

void cQueIns(int num) {


if ((rear + 1) % MAX_SIZE == front) {
printf("!!Queue Overflow!! Cannot insert %d, buffer is full.\n", num);
return;
}
if (front == -1) {
front = 0;
}

rear = (rear + 1) % MAX_SIZE;


queue[rear] = num;
printf("Inserted %d into the buffer.\n", num);
}

void cQueDel() {
if (front == -1) {
Enrollment No:- 202303103510342

printf("!!Queue Underflow!! No elements to delete.\n");


return;
}

int temp = queue[front];


printf("The value deleted is %d.\n", temp);

if (front == rear) {
front = rear = -1;
} else {
front = (front + 1) % MAX_SIZE;
}
}

void display() {
if (front == -1) {
printf("Queue is empty!\n");
return;
}

printf("Queue: |");
int i = front;
while (1) {
printf("\t%d", queue[i]);
if (i == rear) break;
i = (i + 1) % MAX_SIZE;
}
printf("\t|\n");
}
Enrollment No:- 202303103510342

void getLR() {
if (front == -1) {
printf("Queue is empty! No front or rear pointer.\n");
} else {
printf("The front pointer is %d (value = %d) and the rear pointer is %d
(value = %d).\n",front, queue[front], rear, queue[rear]);
}
}

int main() {
int choice, num;

printf("This is a program to understand Circular Queue (Buffering system)\n");


while (1) {
printf("------------------------ - -- - - - - -- - -----------------------\n");
printf("1. Insert\n2. Delete\n3. Display\n4. Get Front and Rear\n5. Exit\nEnter a
choice to perform a task: ");
scanf("%d", &choice);
printf(" -----------------------------------------------------------------------------\n");

switch (choice) {
case 1:
printf("Enter a value to be inserted: ");
scanf("%d", &num);
cQueIns(num);
break;
case 2:
cQueDel();
break;
Enrollment No:- 202303103510342

case 3:
display();
break;
case 4:
getLR();
break;
case 5:
exit(0);
default:
printf("!!Enter a valid choice (1 - 5) only!!\n");
}
}
}

Out Put:-
Enrollment No:- 202303103510342

2
Enrollment No:- 202303103510342

Practical-5
Aim:- Write a menu driven program to implement following operations for train coach
using singly linked list.
1. Insert a node at the front of the linked list
2. Delete a node from linked list

Program Code:-
#include <stdio.h>
#include<stdlib.h>

struct node
{
int INFO;
struct node *LINK;
}
*First = NULL;
struct node *INSFRONT(int X, struct node *F)
{
struct node *New = (struct node *)malloc(sizeof(struct node)); // memory
allocation for New Node

New->INFO = X; // storing data in INFO part of Node


New->LINK = F; // storing memory location of next element in LINK part
return New;
}

void display(struct node *F)


{
struct node *temp = F;
Enrollment No:- 202303103510342

2
while (temp != NULL)
{
printf("%d\t", temp->INFO);
temp = temp->LINK;
}
printf("\n");
}

void Del(int X, struct node *F)


{
struct node *temp, *Pred;
if (F == NULL)
{
printf("Linked List UNderflown\n");
}
else
{
temp = F;
while (temp->INFO != X && temp->LINK != NULL)
{
Pred = temp;
temp = temp->LINK;
}
if (temp->INFO != X)
{
printf("Node not found\n");
}
else
{
Enrollment No:- 202303103510342

if (X == F->INFO)
{
F = F->LINK;
}
else
{
Pred->LINK = temp->LINK;
}
}
}
}

void main()
{
int X, opt;
while (1)
{
printf(" ----------------------------------------------------------------------------- \n");
printf("1. Insert(Front)\n2. Delete\n3. Display\n4. Exit\nEnter a choice to
perform a task:");
scanf("%d", &opt);
printf(" ----------------------------------------------------------------------------- \n");

switch (opt)
{
case 1:
{
printf("Enter a value to insert:");
scanf("%d", &X);
Enrollment No:- 202303103510342

2
First = INSFRONT(X, First);
}
break;
case 2:
{
printf("Enter a value to delete:");
scanf("%d", &X);
Del(X, First);
}
break;
case 3:
{
display(First);
}
break;
case 4:
{
exit(0);
}
break;

default:
{
printf("!!Enter a proper option from (1-4) only!!\n");
}
break;
}
}
}
Enrollment No:- 202303103510342

Out Put:-
Enrollment No:- 202303103510342

Practical-6
Aim:- Write a menu driven program to implement following operations for train coach
using singly linked list.
1. Insert a node at the end of the linked list
2. Insert a node in sorted order in linked list

Program Code:-
#include <stdio.h>
#include<stdlib.h>

struct node
{
int INFO;
struct node *LINK;
}
*First = NULL;

struct node *INSFRONT(int X, struct node *F)


{
struct node *New = (struct node *)malloc(sizeof(struct node));
New->INFO = X;
New->LINK = F;
return New;
}

struct node *INSEND(int X, struct node *F)


{
struct node *Save;
Enrollment No:- 202303103510342

struct node *New = (struct node *)malloc(sizeof(struct node));


New->INFO = X;
New->LINK = NULL;

if (F == NULL)
{
return New;
}

Save = F;
while (Save->LINK != NULL)
{
Save = Save->LINK;
}
Save->LINK = New;
return F;
}

struct node *INSORD(int X, struct node *F)


{
struct node *Save;
struct node *New = (struct node *)malloc(sizeof(struct node));
New->INFO = X;

if (New->INFO <= F->INFO)


{
New->LINK = F;
return New;
}
Enrollment No:- 202303103510342

Save = F;
while (Save->LINK != NULL && (Save->LINK)->INFO <= New->INFO)
{
Save = Save->LINK;
}
New->LINK = Save->LINK;
Save->LINK = New;
return F;
}

void display(struct node *F)


{
struct node *temp = F;
while (temp != NULL)
{
printf("%d\t", temp->INFO);
temp = temp->LINK;
}
printf("\n");
}

void Del(int X, struct node *F)


{
struct node *temp, *Pred;
if (F == NULL)
{
printf("Linked List UNderflown\n");
}
else
Enrollment No:- 202303103510342

{
temp = F;
while (temp->INFO != X && temp->LINK != NULL)
{
Pred = temp;
temp = temp->LINK;
}
if (temp->INFO != X)
{
printf("Node not found\n");
}
else
{
if (X == F->INFO)
{
F = F->LINK;
}
else
{
Pred->LINK = temp->LINK;
}
}
}
}

void main()
{
int X, opt;
while (1)
Enrollment No:- 202303103510342

{
printf(" ---------------------------------------------------------------------------- \n");
printf("1. Insert(Front)\n2. Insert(End)\n3. Insert(Ordered)\n4. Delete\n5.
Display\n6. Exit\nEnter a choice to perform a task:");
scanf("%d", &opt);
printf("------- - - - - -- - - - - -- - - - ---------------------- \n");

switch (opt)
{
case 1:
{
printf("Enter a value to insert(First):");
scanf("%d", &X);
First = INSFRONT(X, First);
}
break;
case 2:
{
printf("Enter a value to insert(End):");
scanf("%d", &X);
First = INSEND(X, First);
}
break;
case 3:
{
for (int i = 0; i < 5; i++)
{
printf("Enter a value to insert(Ordered):");
scanf("%d", &X);
Enrollment No:- 202303103510342

First = INSORD(X, First);


}
}
break;
case 4:
{

printf("Enter a value to delete:");


scanf("%d", &X);
Del(X, First);
}
break;

case 5:
{
display(First);
}
break;
case 6:
{
exit(0);
}
break;

default:
{
printf("!!Enter a proper option from (1-4) only!!\n");
}
break;
}
}
}
Enrollment No:- 202303103510342

Out Put:-
Enrollment No:- 202303103510342

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