DS Practicals - 1-4
DS Practicals - 1-4
L. D. College Of Engineering
LABORATORY MANUAL
Year 2023-24
Date:
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
Initial of
Exp. Page
Title Date Course In
No. No. -charge
Write a program to implement following operations on the
10 doublylinked 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.
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:
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);
}
OUTPUT:
2) Call By Reference :
ALGORITHM:
: #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);
}
Output :
Practical : 2
AIM : Introduction to Dynamic Memory Allocation. DMA function
malloc(), calloc(), free(), etc.
1) malloc() :
#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);
printf("Sum=%d",sum);
free(ptr);
return 0;
}
Output :
2) calloc() :
#include<stdio.h>
#include<stdlib.h
int n,i,*ptr,sum=0;
scanf("%d",&n);
for(i=0;i<n;++i)
scanf("%d",ptr+i);
sum+=*(ptr+i);
printf("Sum=%d",sum);
free(ptr);
return 0;
}
Output:
3) free() :
The memory occupied by malloc() or calloc() functions must be released by
calling free() function. Otherwise, it will consume memory until program exit.
Algorithm :
1) PUSH()
2) POP()
3) DISPLAY()
4) PEEK()
Step 1: Start
Step 2: Declare Stack[MAX]
Step 3: Push the elements into the stack
Step 4: Print the value stored in the stack pointed by top.
Step 5: Stop
5) PEEP()
1. Initialization:
Initialize a stack and set the initial PEEP value to zero.
2. Push Operation:
Add a new value to the top of the stack.
3. Pop Operation:
Remove and return the top value from the stack, if the stack is not empty.
4. Checking if Stack is Empty:
Determine whether the stack is empty.
5. Setting PEEP Value:
Set a new PEEP value.
6. Getting PEEP Value:
Retrieve the current PEEP value.
7. Applying PEEP:
Add the PEEP value to the top element of the stack.
6) CHANGE()
Program :
#include<stdio.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;
}
// Pop operation....
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]);
}
}
// Peep operation....
void peep(){
printf("\n\tTop : %d", s.top); printf("\
n\tValue: %d",s.a[s.top]);
}
void change(int row, int new_element){
int i;
int j = -1;
printf("\n\tTop: %d", s.top);
for(i=s.top; i>row; i--){
s.temp[++j] = s.a[s.top--];
/*
display();
printf("\n\tTop: %d", s.top);
printf("\n\t j : %d", j);
*/
}
s.a[s.top] = new_element;
/*
display();
printf("\n\tTop: %d", s.top);
printf("\n\t j : %d", j);
*/
for(i = j; i>-1; i--){
s.a[++s.top] = s.temp[j--];
/*
display();
printf("\n\tTop: %d", s.top);
printf("\n\t j : %d", j);
*/
}
}
int main()
{ s.top = -1;
int item, choice, row, new_element;
char ans;
do{
printf("\n ");
printf("\nSTACK IMPLEMENTATION PROGRAM\n");
printf(" ");
printf("\n 1. Push\n 2. Pop\n 3. Display\n 4. Peep\n 5. Change\n
6. Exit\n");
printf(" \n");
printf("\n Enter your choice: ");
scanf("%d", &choice);
switch(choice){
case 1:
if(s.top >= size-1){ printf("\
nStack overflow..\n"); break;
}
printf("\nEnter item to be pushed: ");
scanf("%d", &item);
push(item);
break;
case 2:
if(s.top == -1){
printf("\n..Stack underflow..\n");
break;
}
pop();
break;
case 3:
display();
break;
case 4:
peep();
break;
case 5:
printf("\n\tEnter row no : ");
scanf("%d",&row); printf("\n\
tEnter new element: ");
scanf("%d", &new_element);
change(row, new_element );
break;
case 6:
return 0;
}
}while(choice != 6);
return 0;
}
Output :
Practical : 4
AIM : Implement a program to convert infix notation to postfix notation
using stack.
Algorithm :
1. Scan the infix string from left to right.
5. If the top operator in stack has equal or higher precedence than scanned
operator then POP the operator present in stack and add it to postfix string else
PUSH the scanned character to stack.
7. If the scanned character is a right parenthesis ‘)’, POP and add to postfix
string from stack until an ‘(‘ is encountered. Ignore both '(' and ')'.
9. After all character are scanned POP the characters and add to postfix string
from the stack until it is not empty.
Program :
1)
#include<stdio.h>
#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)
{
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 == '/')
{
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 != '<pre data-line="" class="highlight-height language-c line-numbers">
<code readonly="true" class="language-c">
<xmp>#include<stdio.h>
2)
#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)
{
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 == '/')
{
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++;
}
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];
}
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;
}
Output :