Professional Documents
Culture Documents
Kanhu Charan Sahoo - Data Structure Lab Record - Mca - Lateral
Kanhu Charan Sahoo - Data Structure Lab Record - Mca - Lateral
Output
Bhubaneswar, Sunday= 32
Bhubaneswar, Monday= 33
Bhubaneswar, Tuesday = 32
Bhubaneswar, Wednesday = 36
Bhubaneswar, Thursday = 34
Bhubaneswar, Friday = 35
Bhubaneswar, Saturday= 38
Cuttack, Sunday = 23
Cuttack, Monday = 24
Cuttack, Tuesday = 26
Cuttack, Wednesday = 22
Cuttack, Thursday = 29
Cuttack, Friday = 27
Cuttack, Saturday = 23
#include <iostream>
using namespace std;
int main(){ int test[2][3][2]; cout
<< "Enter 12 values: \n"; for(int i =
0; i < 2; ++i) { for (int j = 0;
j < 3; ++j) { for(int k
= 0; k < 2; ++k) {
cin >> test[i][j]
[k]; } } }
cout<<"\nDisplaying Value stored:"<<endl;
for(int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
for(int k = 0; k < 2; ++k) {
cout << "test[" << i << "][" << j << "]["
<< k << "] = " << test[i][j][k] << endl;
} } } return 0;}
Output
test[0][0][0] = 1
test[0][0][1] = 2
test[0][1][0] = 3
test[0][1][1] = 4
test[0][2][0] = 5
test[0][2][1] = 6
test[1][0][0] = 7
test[1][0][1] = 8
test[1][1][0] = 9
test[1][1][1] = 10
test[1][2][0] = 11
test[1][2][1] = 12
for(int i = 0;i<n;i++){
cin>>arr[i];
}
temp = arr[0];
arr[0] = arr[n-1];
arr[n-1] = temp;
cout<<"Array after swapping first and
last elements "<<endl;
for(int i = 0;i<n;i++){
cout<<arr[i]<<"\t";
}
return 0;
}
Output
Enter Number of elements you want to enter
:10 20 30 40 50
Array after swapping first and last
elements 50 20 30 40 10
else
return 0;
}
cout<<STACK[i]<<" ";
}
cout<<endl;
}
void pop(){
int temp;
if(isEmpty()){
cout<<"STACK is EMPTY."<<endl;
return;
}
temp=STACK[TOP];
TOP--;
cout<<temp<<" has been deleted."<<endl
}
int main(){
int num;
initStack();
char ch;
do{
int a;
cout<<"Chosse \n1.push\n"<<"2.pop\n"<<"3.display\n";
cout<<"Please enter your choice: ";
cin>>a;
switch(a)
{
case 1:
cout<<"Enter an Integer Number: ";
cin>>num;
push(num);
break;
case 2:
pop();
break;
case 3:
display();
break;
default :
cout<<"An Invalid Choice!!!\n";
}
cout<<"Do you want to continue ? ";
cin>>ch
}while(ch=='Y'||ch=='y');
return 0;
}
Output
Chosse
1.push
2.pop
3.display
Please enter your choice: 1
Enter an Integer Number: 10
10 has been inserted.
Do you want to continue ? y
Chosse
1.push
2.pop
3.display
printf("Array: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
// Driver program
int main()
{
int arr[] = { 2, -1, 5, 6, 0, -3 };
int n = sizeof(arr) / sizeof(arr[0]);
printArray(arr, n);
return 0;
}
Output
Array: 2 -1 5 6 0 -3
//Declare Node
struct Node{
int num;
Node *next;
};
newNode->next=head;
head=newNode;
}
void deleteItem(){
if(head==NULL){
cout<<"List is empty!"<<endl;
return;
}
cout<<head->num<<" is removed."<<endl;
head=head->next;
}
int main(){
display();
insertNode(10);
insertNode(20);
insertNode(30);
insertNode(40);
insertNode(50);
display();
deleteItem(); deleteItem(); deleteItem(); deleteItem();
deleteItem();
deleteItem();
display();
return 0;
}
Output
List is empty!
50 40 30 20 10
50 is removed.
40 is removed.
30 is removed.
20 is removed.
10 is removed.
List is empty!
List is empty!
newNode->data = data;
newNode->next = NULL;
return newNode;
}
if (pos == 0) {
{
while (head != NULL) {
cout << " " << head->data;
head = head->next;
}
cout << endl;
}
int main()
{
// Creating the list 3->5->8->10
Node* head = NULL;
head = getNode(3);
head->next = getNode(5);
head->next->next = getNode(8);
head->next->next->next = getNode(10);
size = 4;
printList(head);
Output
Linked list before insertion: 3 5 8 10
Linked list after insertion of 12 at position 3: 3 5 12 8 10
Linked list after insertion of 1 at position 1: 1 3 5 12 8 10
Linked list after insertion of 15 at position 7: 1 3 5 12 8
10 15
if (head->next == NULL) {
delete head;
return NULL;
}
return head;
}
// Driver code
int main()
{
/* Start with the empty list */
head = removeLastNode(head);
for (Node* temp = head; temp != NULL; temp = temp-
>next)
cout << temp->data << " ";
return 0;
}
Output
8 23 11 29
return(n*fact(n-1));
}
Output
Enter the Number to Find
Factorial :5Factorial of 5 is 120
10. Program to implement Tower of Hanoi
problem ?
Answer
#include <bits/stdc++.h>
using namespace std;
return;
}
towerOfHanoi(n - 1, from_rod, aux_rod,
to_rod);
cout << "Move disk " << n << " from rod "
<< from_rod <<" to rod " << to_rod <<
endl;
towerOfHanoi(n - 1, aux_rod, to_rod,
from_rod);
}
// Driver code
int main()
{
int n = 4; // Number of disks
towerOfHanoi(n, 'A', 'C', 'B'); // A, B
and C are names of rods
return 0;
}
Output
Move disk 1 from rod A to rod B
Move disk 2 from rod A to rod C
Move disk 1 from rod B to rod C
Move disk 3 from rod A to rod B
Move disk 1 from rod C to rod A
Move disk 2 from rod C to rod B
Move disk 1 from rod A to rod B
Move disk 4 from rod A to rod C
Move disk 1 from rod B to rod C
Move disk 2 from rod B to rod A
Move disk 1 from rod C to rod A
Move disk 3 from rod B to rod C
Move disk 1 from rod A to rod B
Move disk 2 from rod A to rod C
Move disk 1 from rod B to rod C