Shreya PPT Final
Shreya PPT Final
Government of Gujarat
L. D. College Of Engineering
LABORATORY MANUAL
Year 2023-24
1|P ag e
Painter Shreya 220280116070
Government of Gujarat
L. D. College of Engineering
Date:
2|P ag e
Painter Shreya 220280116070
Government of Gujarat
L. D. College of Engineering
Initial of
Exp. Page
Title Date Course In
No. No. -charge
1 Introduction to pointers. Call by Value and Call by reference.
2 Introduction to Dynamic Memory Allocation. DMA functions
malloc(), calloc(), free() etc.
3 Implement a program for stack that performs followingoperations
using array.
(a) PUSH (b) POP (c) PEEP (d) PEEK (e) CHANGE (f) Count (g)
DISPLAY
4 Implement a program to convert infix notation to postfix notation
using stack.
5 Write a program to implement QUEUE using arrays that performs
following operations (a) (a) INSERT (b) DELETE (c) DISPLAY
6 Write a program to implement Circular Queue using arrays that
performs following operations. (a) INSERT (b) DELETE (c)DISPLAY
3|P ag e
Painter Shreya 220280116070
Government of Gujarat
L. D. College of Engineering
Initial of
Exp. Page
Title Date Course In
No. No. -charge
Write a program to implement following operations on the doublylinked
10 list.
Insert a node
◦ At beginning of the list
◦ At end of the list
◦ After specific node
◦ Before specific node
Delete a node
◦ From begin of the list
◦ From end of the list
◦ Specific node
Searching a node List
traversal
Count total number of nodes
Write a program to implement following operations on thecircular
linked list.
11 Insert a node
◦ At beginning of the list
◦ At end of the list
◦ After specific node
◦ Before specific node
Delete a node
◦ From begin of the list
◦ From end of the list
◦ Specific node
Searching a node List
traversal
Count total number of nodes
12 Write a program to implement Bubble Sort and Selection Sort.
13 Write a program to implement Insertion Sort and Quick Sort.
14 Write a program to implement Merge Sort.
15 Write a program to implement Linear Search and Binary Search.
16 Write a program which create binary search tree.
17 Implement recursive and non-recursive tree traversing methods
inorder, preorder and post-order traversal.
4|P ag e
Painter Shreya 220280116070
Practical : 1
AIM : Introduction to pointers. Call by Value and Call by reference.
Pointers :
A pointer is defined as a derived data type that can store the address
of other C variables or a memory location. We can access and manipulate the data
stored in that memory location using pointers.
Q) Program to swap the value of two variables using user defined function.
1) Call By Value :
ALGORITHM:
Step 1: Start the program.
Step 2: Read a and b
Step 3: Call the function swap(a,b)
Step 3a: Start function.
Step 3b: Assign temp ← a
Step 3c: Assign a ← b
Step 3d: Assign b ← temp
Step 3e: Print a and b.
Step 3f: End function.
Step 4: Stop the program.
CODE:
#include<stdio.h>
void swap(int,int);
int main()
{
int a,b;
printf("\nEnter a Value of A=");
scanf("%d",&a);
5|P ag e
Painter Shreya 220280116070
OUTPUT:
2) Call By Reference :
ALGORITHM:
CODE:
#include<stdio.h>
void swap(int*,int*);
int main()
{
int a,b;
printf("\nEnter a Value of A=");
scanf("%d",&a);
printf("\nEnter a Value of B=");
scanf("%d",&b);
swap(&a,&b);
printf("\nOld Values:");
printf("A=%d B=%d \n",a,b);
}
void swap(int *p , int *q)
{
int tmp;
tmp=*p;
*p=*q;
*q=tmp;
printf("New Values After Swap:");
printf("A=%d B=%d",*p,*q);
}
7|P ag e
Painter Shreya 220280116070
OUTPUT:
8|P ag e
Painter Shreya 220280116070
Practical : 2
AIM : Introduction to Dynamic Memory Allocation. DMA function
malloc(), calloc(), free(), etc.
1) malloc() :
CODE:
#include<stdio.h>
#include<stdlib.h>
int main(){
int *ptr;
int n,i,sum=0;
scanf("%d",&n);
ptr=(int*)malloc(n*sizeof(int));
for(i=0;i<n;++i)
{
scanf("%d",ptr+i);
sum+=*(ptr+i);
9|P ag e
Painter Shreya 220280116070
printf("Sum=%d",sum);
free(ptr);
return 0;
}
OUTPUT:
10 | P a g e
Painter Shreya 220280116070
2) calloc() :
CODE:
#include<stdio.h>
#include<stdlib.h>
int main(){
int n,i,*ptr,sum=0;
printf("Enter Number of Elements: ");
scanf("%d",&n);
for(i=0;i<n;++i)
{
scanf("%d",ptr+i);
sum+=*(ptr+i);
printf("Sum=%d",sum);
free(ptr);
return 0;
}
11 | P a g e
Painter Shreya 220280116070
OUTPUT:
Q) free() :
The memory occupied by malloc() or calloc() functions must be released by
calling free() function. Otherwise, it will consume memory until program exit.
12 | P a g e
Painter Shreya 220280116070
Practical : 3
AIM : Implement a program for stack that performs following operations using
array : (1) PUSH (2) POP (3) PEEP (4) CHANGE
(5) COUNT (6) DISPLAY.
ALGORITHM:
PUSH()
Step 1: START
Step 2: Store the element to push into array
Step 3: Check if top== (MAXSIZE-1) then stack is full else goto step 4
Step 4: Increment top as tos = tos+1
Step 5: Add element to the position stack[tos]=num
Step 6: STOP
POP()
Step 1: START
Step 2: Check if tos== (-1) then stack is empty else goto step 4
Step 3: Access the element top is pointing num = stack[tos];
Step 4: Decrease the top by 1 tos = tos-1;
Step 6: STOP
DISPLAY()
Step 1: START
Step 2: print from Stack[0] to Stack[TOS]
Step 3: STOP
PEEP()
Step 1: START
13 | P a g e
Painter Shreya 220280116070
Step 2: Read a number from the user.
Step 3: If TOS == -1, Stack is empty goto step 5 else if TOS = MAX – 1, Stack is full.
goto step 5
Step 4: if TOS – i + 1 > 0 and TOS – i +1 < TOS, print TOS – i + 1 th element of
stack. goto step 6.
Step 5: exit from the code.
Step 6: STOP
CHANGE()
CODE:
#include<stdio.h>
#include<conio.h>
#define size 5
struct stack{
int a[size],top;
int temp[size], tos;
}s;
// Push operation....
void push(int item){
s.a[++s.top] = item;
}
14 | P a g e
Painter Shreya 220280116070
int pop(){
return s.a[s.top--];
}
// Display operation....
void display(){
int i;
printf("\nThe stack contains: ");
for(i = s.top; i>=0; i--){
printf("\n\t%d", s.a[i]);
}
}
void peep(){
printf("\n\tTop : %d", s.top);
printf("\n\tValue: %d",s.a[s.top]);
}
int main(){
s.top = -1;
int item, choice, row, new_element;
char ans;
do{
printf("\n-----------------------------");
15 | P a g e
Painter Shreya 220280116070
return 0;
}
}while(choice != 6);
return 0;
}
OUTPUT:
17 | P a g e
Painter Shreya 220280116070
18 | P a g e
Painter Shreya 220280116070
19 | P a g e
Painter Shreya 220280116070
Practical : 4
AIM : Implement a program to convert infix notation to postfix notation
using stack.
ALGORITHM:
Step 1: Add ")" to the end of the infix expression
Step 3: Repeat until each character in the infix notation is scanned IF a "(" is
encountered, push it on the stack IF an operand (whether a digit or an alphabet)
is encountered, add it
to the postfix expression.
a. Repeatedly pop from stack and add each operator (Popped from the stack) to
the postfix expression until it has the same precedence or a Higher precedence
than o
[END OF If]
20 | P a g e
Painter Shreya 220280116070
Step 4: Repeatedly pop from the stack and add it to the postfix expression until
the stack is empty
Step 5: EXIT
CODE:
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#define SIZE 100
char stack[SIZE];
int top = -1;
void push(char item)
{
if(top >= SIZE-1)
{
printf("\nStack Overflow.");
}
else
{
top = top+1;
stack[top] = item;
}
}
char pop()
{
char item ;
if(top <0)
{
21 | P a g e
Painter Shreya 220280116070
printf("stack under flow: invalid infix expression");
getchar();
/* underflow may occur for invalid expression */
/* where ( and ) are not matched */
exit(1);
}
else
{
item = stack[top];
top = top-1;
return(item);
}
}
int is_operator(char symbol)
{
if(symbol == '^' || symbol == '*' || symbol == '/' || symbol == '+' || symbol =='-')
{
return 1;
}
else
{
return 0;
}
}
int precedence(char symbol)
{
if(symbol == '^')
{
return(3);
}
else if(symbol == '*' || symbol == '/')
{
22 | P a g e
Painter Shreya 220280116070
return(2);
}
else if(symbol == '+' || symbol == '-')
{
return(1);
}
else
{
return(0);
}
}
void InfixToPostfix(char infix_exp[], char postfix_exp[])
{
int i, j;
char item;
char x;
push('(');
strcat(infix_exp,")");
i=0;
j=0;
item=infix_exp[i];
while(item != '\0')
{
if(item == '(')
{
push(item);
}
else if( isdigit(item) || isalpha(item))
{
postfix_exp[j] = item;
j++;
}
23 | P a g e
Painter Shreya 220280116070
else if(is_operator(item) == 1)
{
x=pop();
while(is_operator(x) == 1 && precedence(x)>= precedence(item))
{
postfix_exp[j] = x;
j++;
x = pop();
}
push(x)
push(item);
}
else if(item == ')')
{
x = pop();
while(x != '(')
{
postfix_exp[j] = x;
j++;
x = pop();
}
}
else
{
printf("\nInvalid infix Expression.\n");
getchar();
exit(1);
}
i++;
item = infix_exp[i];
}
24 | P a g e
Painter Shreya 220280116070
if(top>0)
{
printf("\nInvalid infix Expression.\n");
getchar();
exit(1);
}
if(top>0)
{
printf("\nInvalid infix Expression.\n");
getchar();
exit(1);
}
postfix_exp[j] = '\0';
}
int main()
{
char infix[SIZE], postfix[SIZE];
printf("ASSUMPTION: The infix expression contains single letter variables and
single digit constants only.\n");
printf("\nEnter Infix expression : ");
gets(infix);
InfixToPostfix(infix,postfix);
printf("Postfix Expression: ");
puts(postfix);
return 0;
}
25 | P a g e
Painter Shreya 220280116070
OUTPUT:
26 | P a g e
Painter Shreya 220280116070
Practical : 5
ALGORITHM:
INSERT
DELETE
DISPLAY
Step 1: start
27 | P a g e
Painter Shreya 220280116070
i= FRONT;
Step 2:repeat step 3 until i<= REAR
Step 3: print qeu[i];
Step 4: exit
CODE:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct queue{
int que[10];
int front,rear;
}q;
void main()
{
void insert();
void del();
void display();
int choice;
q.front=-1;
q.rear=-1;
char ans='n';
do{
printf("------------------\n MAIN MENU\n------------------\n
1.\tinsert\n 2.\tdelete\n 3.\tdisplay\n 4.\texit \n------------------\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
28 | P a g e
Painter Shreya 220280116070
case 2:
del();
break;
case 3:
display();
break;
case 4:
exit(0);
}
}while(1);
}
void insert()
{
int item;
if(q.rear>=10)
{
printf("queue is full!");
}
if(q.front==-1)
{
q.front++;
}
printf("enter the item\n");
scanf("%d",&item);
q.que[++q.rear]=item;
}
void del()
{
int item=0;
if(q.front==-1||q.front>q.rear)
{
printf("queue is empty!\n");
}
else{
item=q.que[q.front++];
29 | P a g e
Painter Shreya 220280116070
printf("the deleted item is %d\n",item);
}
}
void display()
{
int i;
printf("The elements are:\n");
for(i=q.front;i<=q.rear;i++)
{
printf("%d\n",q.que[i]);
}
}
30 | P a g e
Painter Shreya 220280116070
OUTPUT:
31 | P a g e
Painter Shreya 220280116070
32 | P a g e
Painter Shreya 220280116070
Practical : 6
ALGORITHM:
INSERT
Step 1:start
Step 2: if front== (rear+1)%max then
Print overflow
Exit
Else if
Step 3:front== -1 and rear == -1 then
Front= front+1 and rear= rear+1
Que[rear]=num
Else
Step 4: rear =rear+1
And que[rear]=num;
Step 5:exit
DELETE
Step 1: start
Step 2:if front= -1 then
Print underflow
Else if
33 | P a g e
Painter Shreya 220280116070
Step 3: front == rear then
Front= -1 and rear= -1
Else
Step 4: print que[front] is deleted item
Front=(front+10%max)
Step 5: exit
DISPLAY
Step 1: start
I= front
Step 2: if front>rear
Then repeat step 3 until front<max
Step 3: print que[i]
I=0
And repeat step 4 until I<=rear
Step 4: print que[i]
Else
I=front
Step 5:repeat step 6 until I<=rear
Step 6: print que[i]
Step 7: exit
CODE:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define N 5
34 | P a g e
Painter Shreya 220280116070
int queue[N];
int front=-1;
int rear=-1;
int count=0;
void isfull()
{
printf("queue overflow");
}
void isempty()
{
printf("queue underflow");
}
void enqueue(int x)
{
if((rear+1)%N==front)
isfull();
else if(front==-1 && rear==-1)
{
printf("enter data which you want to insert:- ");
scanf("%d",&x);
front=rear=0;
queue[rear]=x;
}
else
{
printf("enter data which you want to insert:- ");
scanf("%d",&x);
rear=(rear+1)%N;
queue[rear]=x;
}
count++;
}
void dequeue()
{
if(front==-1 && rear==-1)
isempty();
else if(front==rear)
35 | P a g e
Painter Shreya 220280116070
{
printf("dequeued iteam from queue is %d",queue[front]);
front=rear=-1;
}
else
{
printf("dequeued iteam from queue is %d",queue[front]);
front=(front+1)%N;
}
count--;
}
void display()
{
int i=front;
if(front==-1 && rear==-1)
isempty();
else
{
printf("queue is: ");
while(i!=rear)
{
printf("%d ",queue[i]);
i=(i+1)%N;
}
printf("%d",queue[rear]);
}
}
void main()
{
int choice,i,x;
char ch;
do
{
printf("\n----------\n");
printf("MAIN MENU");
printf("\n----------");
printf("\n1.enqueue");
36 | P a g e
Painter Shreya 220280116070
printf("\n2.dequeue");
printf("\n3.display");
printf("\n4.count");
printf("\n5.exit");
printf("\n----------");
printf("\nenter your choice:-");
scanf("%d",&choice);
switch(choice)
{
case 1:enqueue(x);
break;
case 2:dequeue();
break;
case 3:display();
break;
case 4:printf("total number of iteam in the queue is %d",count);
break;
case 5:exit(0);
break;
default:printf("\nwrong choice");
}
printf("\n countinue(Y/N):- ");
fflush(stdin);
scanf("%c",&ch);
}while(ch=='Y'|| ch=='y');
}
37 | P a g e
Painter Shreya 220280116070
OUTPUT:
38 | P a g e
Painter Shreya 220280116070
39 | P a g e
Painter Shreya 220280116070
40 | P a g e