Sneh
Sneh
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;
};
return INT_MIN;
return stack->array[stack->top];
}
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
}
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()
{
}
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 cQueDel() {
if (front == -1) {
Enrollment No:- 202303103510342
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;
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
2
while (temp != NULL)
{
printf("%d\t", temp->INFO);
temp = temp->LINK;
}
printf("\n");
}
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;
if (F == NULL)
{
return New;
}
Save = F;
while (Save->LINK != NULL)
{
Save = Save->LINK;
}
Save->LINK = New;
return F;
}
Save = F;
while (Save->LINK != NULL && (Save->LINK)->INFO <= New->INFO)
{
Save = Save->LINK;
}
New->LINK = Save->LINK;
Save->LINK = New;
return F;
}
{
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
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