0% found this document useful (0 votes)
23 views28 pages

DS Practicals - 1-4

Uploaded by

Shreya Painter
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views28 pages

DS Practicals - 1-4

Uploaded by

Shreya Painter
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Government of Gujarat

L. D. College Of Engineering

LABORATORY MANUAL

Information Technology Department


Semester III

3130702 - DATA STRUCTURES

Year 2023-24

Name : Jency Maheshwari


Enrollment No. : 220280116056
Division : A

L.D. College Of Engineering, Ahmedabad - 380015


Government of Gujarat
L. D. College of Engineering

This is to certify that Mr./Mrs.

Enrollment No. of Semester III Information


Technology has successfully completed the prescribed term
work/laboratory work of DATA STRUCTURES 3130702 course within
the four walls of L. D. College of Engineering. This is required as a
partial fulfillment of the said course of Gujarat Technological
University.

Date:

Course In-Charge HOD


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

7 Write a menu driven program to implement following operationson the


singly linked 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 Reverse the
list
Count total number of nodes
8 Write a program to implement stack using linked list.
9 Write a program to implement queue using linked list.
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
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:

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);
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:

Step 1: Start the program.


Step 2: Read x and y
Step 3: Call the function swap(&x,&y)
Step 3a: Start function
Step 3b: Assign temp ← *x
Step 3c: Assign *x← *y
Step 3d: Assign *x← temp
Step 3e: End function
Step 4: Print a and y.
Step 5: Stop the program.
3) Call By Reference

: #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.

 Dynamic Memory Allocation can be defined as a procedure in which the


size of a data structure (like Array) is changed during the runtime.
C provides some functions to achieve these tasks.

Q) Dynamic Memoery Allocation Functions.

1) malloc() :

#include<stdio.h>
#include<stdlib.h
> int main(){

int *ptr;

int n,i,sum=0;

printf("Enter Number of Elements: ");

scanf("%d",&n);

ptr=(int*)malloc(n*sizeof(int));

printf("Enter Elements of Array: ");

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 main(){

int n,i,*ptr,sum=0;

printf("Enter Number of Elements: ");

scanf("%d",&n);

ptr=(int*)calloc(n,sizeof(int)); //memory allocated using calloc

printf("Enter elements of array: ");

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.

The syntax of free() function is


:
free(ptr);
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 :

1) PUSH()

1. [Check for stack


overflow?] if top=MAXSIZE-1
then
print “Stack Overflow” and Exit
else
Set top=top+1
[Increase top by 1]
Set Stack[top]:= item
[Inserts item in new top position]
2. Exit

2) POP()

1. [Check for the stack


Underflow] If top == -1 then
Print “Stack Underflow” and Exit
Else
[Remove the top element]
Set item=Stack [top]
[Decrement top by 1]
Set top=top-1
Return item
2. Exit

3) DISPLAY()

1. [Check for the stack


underflow] If top==-1 then
Print “Stack underflow” and
Exit Else
[Display all the items of stack]
Set i=stack[top]
Repeat below steps until i>=0
Print stack[i]
Set i=i-1
2. Exit

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()

1. if top <= -1:


print "Stack is underflow"
2. else:
input i
print "Enter the new value:"
input new_value
stack[i] = new_value
print "Value of i’th element is changed"
7) COUNT()

1. if top <= -1:


print "Stack is underflow"
2. else:
m=0
for i from 0 to top:
m=m+1
print "The number of elements in the stack are:", m

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.

2. Initialize an empty stack.

3. If the scanned character is an operand, add it to postfix sting

4. If the scanned character is an operator, PUSH the character to stack.

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.

6. If the scanned character is a left parenthesis ‘(‘, PUSH it 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 ')'.

8. Repeat step 3-7 till all the characters are scanned.

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 :

You might also like