0% found this document useful (0 votes)
7 views40 pages

Shreya PPT Final

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 PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views40 pages

Shreya PPT Final

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 PDF, TXT or read online on Scribd
You are on page 1/ 40

Painter Shreya 220280116070

Government of Gujarat

L. D. College Of Engineering

LABORATORY MANUAL

Information Technology Department


Semester III

3130702 - DATA STRUCTURES

Year 2023-24

Painter Shreya Prashant


220280116070

L.D. College Of Engineering, Ahmedabad - 380015

1|P ag e
Painter Shreya 220280116070

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

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

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.

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

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
6|P ag e
Painter Shreya 220280116070

Step 3d: Assign *x← temp


Step 3e: End function
Step 4: Print a and y.
Step 5: 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);
}

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.

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

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

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

ptr=(int*)calloc(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;
}

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.

The syntax of free() function is :


free(ptr);

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

Algorithm for change Operation:


Step 1: START
Step 2: Read an integer.
Step 3: If TOS == -1, Stack is empty, goto step 5.
Step 4: If TOS - i + 1 > -1 and TOS – i + 1 <= TOS, Replace value of Stack[TOS-i+1]
with new value.
Step 5: exit from the code.
Step 6: STOP

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]);
}

void change(int idx, int new_element){


int i, j = -1;
printf("\n\tTop: %d", s.top);
for(i=s.top; i>idx; i--){
s.temp[++j] = s.a[s.top--];
}
s.a[s.top] = new_element;

for(i = j; i>-1; i--){


s.a[++s.top] = s.temp[j--];
}
}

int main(){
s.top = -1;
int item, choice, row, new_element;
char ans;
do{
printf("\n-----------------------------");
15 | P a g e
Painter Shreya 220280116070

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 idx no : ");
scanf("%d",&row);
printf("\n\tEnter new element: ");
scanf("%d", &new_element);
change(row, new_element );
break;
case 6:
16 | 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 2: Push ( on to the stack

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.

IF a ") is encountered, then;


a. Repeatedly pop from stack and add it to the postfix expression

until a "(" is encountered.


b. Discard the (". That is, remove the "(" from stack and do not

add it to the postfix Expression


If an operator 0 is encountered, then;

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

b. Push the operator 0 to the stack

[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

Aim : Write a program to implement QUEUE using arrays that


performs following operations (a) INSERT (b) DELETE (c) DISPLAY.

ALGORITHM:

INSERT

Step 1: IF REAR MAX-1, then; Write OVERFLOW


[END OF IF]
Step 2: IF FRONT-1 and REAR = -1, then SET FRONT REAR = 0
ELSE
SET REAR REAR + 1
[END OF IF]
Step 3: SET QUEUE [REAR] = NUM
Step 4: Exit

DELETE

Step 1: IF FRONT = -1 OR FRONT > REAR, then; rear is set to zero, s


ELSE
SET FRONT FRONT + 1 SET VAL= QUEUE [FRONT]
[END OF IF]
Step 2: Exit

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

Aim : Write a program to implement Circular Queue using arrays that


performs following operations. (a) INSERT (b) DELETE (c) DISPLAY

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

You might also like