Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views13 pages

DSA Assignment

Assignment and practice questions of data structures

Uploaded by

ghimireurbish
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)
5 views13 pages

DSA Assignment

Assignment and practice questions of data structures

Uploaded by

ghimireurbish
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/ 13

Lab Report

of
Data Structure And Algorithm

Submitted To
Butwal Multiple Campus
Butwal, Rupandehi

Submitted B

Roll Number: 1

Program: Bachelor of Science in Computer Science and


Information Technology (B.Sc.CSIT)
Semester: First (3)
S/N Topic Remarks

Implementation of Stack

Implementation of Queue

Factorial of Number Using Recursion

GCD of Two Numbers Using Recursion

Find Fibonacci Series upto Nth term


1/3/25, 8:23 AM Stack.c

~\OneDrive\Desktop\Stack.c

1| #include <stdio.h>
2| #include <stdlib.h>
3| #include <stdbool.h>
4
5| #define N 10
6
7
8| int top = -1;
9
10
11| int stack[N];
12
13
14| void push();
15 int pop();
16 | int peek();
17 | bool isEmpty();
18 | bool isFull();
19
20| int main(){
21 printf("STATIC ARRAY (Total Capacity: %d)\n", N);
22 int choice;
23 while(1){
24 printf("\nChoose any of the following options:\n");
25 printf(" @: Exit 1: Push 2: Pop 3: Peek\n");
26 printf(" 4: Check if the stack is empty 5: Check if the stack is full\n\n");
27 scanf("%d", &choice);
28
29 switch(choice){
30 case 0: exit(@);
31 case 1: push(); break;
32 case 2: pop(); break;
33 case 3: peek(); break;
34 case 4: isEmpty(); break;
35 case 5: isFull(); break;
36 default: printf("Please choose a correct option!");
37 }
38 }
39 return 0;
40| }
41| void push(){
42 if(top == N-1)
43 printf("Overflow State: can't add more elements into the stack\n");
44 else{
45 int x;
46 printf("Enter element to be pushed into the stack: ");
47 scanf("%d", &x);
48 top+=1;
49 stack[top] = x;
50 }
51| }

localhost:50384/df2e293c-1fc8-4246-abbb-df5f7c23a9ad/
1/3/25, 8:23 AM Stack.c

52
53| int pop(){
54 if(top == -1)
55 printf("Underflow State: Stack already empty, can't remove any element\n");
56 else{
57 int x = stack[top];
58 printf("Popping %d out of the stack\n", x);
59 top-=1;
60
61 return x;
62 }
63 return -1;
64 }
65
66 int peek(){
67 int x = stack[top];
68 printf("%d is the top most element of the stack\n", x);
69 return x;
70 }
71
72| bool isEmpty(){
73 if(top == -1){
74 printf("Stack is empty: Underflow State\n");
75 return true;
76 }
77
78 printf("Stack is not empty\n");
79 return false;
80| }
81
82 bool isFull(){
83 if(top == N-1){
84 printf("Stack is full: Overflow State\n");
85 return true;
86 }
87 printf("Stack is not full\n");
88 return false;
89 }

localhost:50384/df2e293c-1fc8-4246-abbb-df5f7c23a9ad/ 22
STATIC ARRAY (Total Capacity: 10)

Choose any of the following options:


0: Exit 1: Push 2: Pop 3: Peek
4: Check if the stack is empty 5: Check if the stack is full

1
Enter element to be pushed into the stack: 5

Choose any of the following options:


0: Exit 1: Push 2: Pop 3: Peek
4: Check if the stack is empty 5: Check if the stack is full

3
5 is the top most element of the stack

Choose any of the following options:


0: Exit 1: Push 2: Pop 3: Peek
4: Check if the stack is empty 5: Check if the stack is full

2
Popping 5 out of the stack

Choose any of the following options:


0: Exit 1: Push 2: Pop 3: Peek
4: Check if the stack is empty 5: Check if the stack is full

4
Stack is empty: Underflow State

Choose any of the following options:


0: Exit 1: Push 2: Pop 3: Peek
4: Check if the stack is empty 5: Check if the stack is full

== Code Execution Successful ===


1/3/25, 8:44 AM queue.c

~\OneDrive\Desktop\queue.c

1| #include<stdio.h>
2| #include<stdlib.h>
3
4 void enqueue();
5| void dequeue();
6 void display();
7
8| struct node{
9 int data;
10 struct node* next;
1}
12
13 struct node *rear = NULL;
14| struct node *front = NULL;
15
16 | int main()
17| {
18 printf("Queue implementation using link list\n");
19 while(1)
20 {
21 int choice;
22 printf("1. for enqueue\n 2. for dequeue\n 8. for exit\n 3. for display \n");
23 printf("\n enter your choice \n");
24 scanf("%d",&choice);
25 switch(choice)
26 {
27 case 0: exit(@); break;
28 case 1: enqueue(); break;
29 case 2: dequeue(); break;
30 case 3: display();
31 default: printf("wrong choice \n");
32 }
33 }
34 return 0;
35}
36
37| void enqueue()
38| {
39 struct node* new_node;
40 new_node = (struct node*)malloc(sizeof(struct node));
41 printf("enter element to enqueue \n");
42 scanf("%d",&new_node->data);
43 new_node->next=NULL;
44
45 if(front==NULL)
46 {
47 front= new_node;
48 rear = new_node;
49 printf("successfully inserted first element\n");
50 }
51 else

localhost:50384/3e515f95-74b9-4a7a-803a-5da97e7cf090/
1/3/25, 8:44 AM queue.c

52 {
53 rear->next = new_node;
54 rear = new_node;
55 printf("inserted element \n");
56 }
57 }
58
59| void dequeue()
60 {
61 if(front==NULL)
62 {
63 printf("no elements to dequeue \n");
64 return;
65 }
66 else if(front==rear)
67 {
68 struct node* current;
69 current=front;
70 front=rear=NULL;
71 free(current);
72 }
73 else
74 {
75 struct node* current;
76 current = front;
77 front=front->next;
78 free(current);
79 printf("elements successfully dequeued \n");
80 }
81 }
82
83| void display()
84 {
85 if(front==NULL)
86 {
87 printf("queue List is Empty\n");
88 return;
89 }
90 printf("queue elements data elements are:\n ");
91 struct node* current = front;
92 while(current!=NULL)
93 {
94 printf("%d ",current->data);
95 current = current->next;
96 }
97 printf("\n");
98 }

localhost:50384/3e515f95-74b9-4a7a-803a-5da97e7cf090/ 212
Queue implementation using link list
1. for enqueue
2. for dequeue
0. for exit
3. for display

enter your choice


1
enter element to enqueue
10
successfully inserted first element
1. for enqueue
2. for dequeue
0. for exit
3. for display

enter your choice


3
queue elements data elements are:
10
wrong choice
1. for enqueue
2. for dequeue
0. for exit
3. for display

enter your choice


2
1. for enqueue
2. for dequeue
0. for exit
3. for display

enter your choice


0

=== Code Execution Successful ===


1/3/25, 8:49 AM Factorial.c

~\OneDrive\Desktop\Factorial.c

1 #include <stdio.h>
2
3 int factorial(int n);
4
5 int main()
6 {
7 int num;
8 printf("Enter a positive integer:
9 scanf("%d", &num);
10
11 if (num < @) {
12 printf("Error: Negative Number.\n");
13 ¥
14 else
15 {
16 int result = factorial(num);
17 printf("Factorial of %d = %d\n", num, result);
18 }
19
20 return 0;
21
22
23
24 int factorial(int n)
25 {
26 if (n==9 || n
27 {
28 return 1;
29 }
30 else
31 {
return n * factorial(n - 1);
}
}

Enter a positive integer: 9


Factorial of 9 = 362880

=== Code Execution Successful ===

localhost:50932/f5102ef1-2309-4414-adfa-1b253e257292/ n
1/3/25, 9:05 AM GCD.c

~\OneDrive\Desktop\GCD.c

1| #include <stdio.h>
2
3| int ged(int a, int b) {
4 if (b == 0) {
5 return a;

6 }
7 return gcd(b, a % b);
8 }
9
10| int main() {
11 int numil, num2;
12
13 printf("Enter two integers: ");
14 scanf("%d %d", &numl, &num2);
15
16 int result = gcd(numl, num2);
17
18 printf("GCD of %d and %d is: %d\n", numl, num2, result);
19 return 0;
2 }

Enter two integers: 77


84
GCD of 77 and 84 is: 7

=== Code Execution Successful ===

localhost:50932/eaa9d30f-b1ad-410a-abfe-6b869ef70635/ n
1/3/25, 8:52 AM Fibonacci.c

~\OneDrive\Desktop\Fibonacci.c

1 #include <stdio.h>
2
3 int fibonacci(int n);
4
5 int main()
6 {
7 int num;
8 printf("Enter the number of Fibonacci terms you want to generate:
9 scanf("%d", &num);
10
11 if (num < @)
12 {
13 printf("Number of terms cannot be negative.\n");
14 }
15 else
16 {
17 printf("Fibonacci series:\n");
18 for (int i = @; i < num; i++)
19 {
20 printf("%d ", fibonacci(i));
21 }
22 printf("\n");
23 }
24
25 return 0;
26
27
28
29 int fibonacci(int n)
30 {
31 if (n == 0)
32 {
33 return 0;
34 }
35 else if (n == 1)
36 {
37 return 1;
38 3
39 else
40 {
41 return fibonacci(n - 1) + fibonacci(n - 2);
42 }
43 ¥

Enter the number of Fibonacci terms you want to generate: 9


Fibonacci series:
1123581321

=== Code Execution Successful ===

localhost:50932/ebb92d56-b751-4662-a9d4-89da6a4
98d3d/ n
1/3/25, 10:30 AM TOH.c

~\OneDrive\Documents\DSA\TOH.c

1 #include <stdio.h>
2
3 void toH(int n, char S, char A, char D)
4 {
5 if (n == 1)
6 {
7 printf("\n Move disk 1 from rod %c to rod %c",S ,D );
8 return;
9 }
10 toH(n-1, S, D, A);
11 printf("\n Move disk %d from rod %c to rod %c", n, S, D);
12 toH(n-1, A, S,D);
13 ¥
14 int main()
15 {
16 int no_of_disks ;
17 printf("Enter number of disks:
18 scanf("%d", &no_of_disks);
19 toH(no_of_disks, ,'AY,'DY);
20 return 0;
21

localhost:49924/04b49d19-6e40-4404-8bfa-5043784b74ab/ n
Enter number of disks: 4

Move disk 1 from rod S to rod A


Move disk 2 from rod S to rod D
Move disk 1 from rod A to rod D
Move disk 3 from rod S to rod A
Move disk 1 from rod D to rod S
Move disk 2 from rod D to rod A
Move disk 1 from rod S to rod A
Move disk 4 from rod S to rod D
Move disk 1 from rod A to rod D
Move disk 2 from rod A to rod S
Move disk 1 from rod D to rod S
Move disk 3 from rod A to rod D
Move disk 1 from rod S to rod A
Move disk 2 from rod S to rod D
Move disk 1 from rod A to rod D

== Code Execution Successful ===

You might also like