Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 31

Suresh Gyan Vihar University

Data Structure Using C++ , Lab Answer

Reference / Enrolment 2753496


Number :
Student Name : Kanhu Charan Sahoo
Subject Name : Data Structure using C++
Program : MCA
Semester / Year : III

1. Program to store temperature of two different cities


for a week and display it ?
Answer
#include <iostream>
using namespace std;
const int CITY = 2;
const int WEEK = 7;
int main()
{
int temperature[CITY][WEEK];
cout << "Enter all temperature for a week of first city
and then second city. \n";

Sensitivity: Internal & Restricted


Suresh Gyan Vihar University

for (int i = 0; i < CITY; ++i)


{
for(int j = 0; j < WEEK; ++j)
{
cout << "City " << i + 1 << ", Day " << j + 1 << " : ";
cin >> temperature[i][j];
}
}
cout << "\n\nDisplaying Values:\n";
for (int i = 0; i < CITY; ++i)
{
for(int j = 0; j < WEEK; ++j)
{
cout << "City " << i + 1 << ", Day " << j + 1 << " = "
<< temperature[i][j] << endl;
}
}
return 0;
}

Sensitivity: Internal & Restricted


Suresh Gyan Vihar University

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

2.Program to Store value entered by user


in three dimensional array and display
it ?
Answer

Sensitivity: Internal & Restricted


Suresh Gyan Vihar University

#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

Sensitivity: Internal & Restricted


Suresh Gyan Vihar University

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

3. Program to swap first and last element


of an integer 1-d array ?
Answer
#include<iostream>
using namespace std;
int main()
{
int n, temp;
cout<<"Enter Number of elements you
want to enter : ";
cin>>n;
int arr[n];

Sensitivity: Internal & Restricted


Suresh Gyan Vihar University

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

Sensitivity: Internal & Restricted


Suresh Gyan Vihar University

4. Code to PUSH Element in a stack using


Array ?
Answer
#include<iostream>
#define MAX 5
using namespace std;
int STACK[MAX],TOP;
void initStack(){
TOP=-1;
}
int isEmpty(){
if(TOP==-1)
return 1;
else
return 0;
}
int isFull(){
if(TOP==MAX-1)
return 1;

Sensitivity: Internal & Restricted


Suresh Gyan Vihar University

else
return 0;
}

void push(int num){


if(isFull()){cout<<"STACK is FULL."<<endl;
return;
}
++TOP;
STACK[TOP]=num;
cout<<num<<" has been inserted."<<endl;
}
void display(){
int i;
if(isEmpty()){
cout<<"STACK is EMPTY."<<endl;
return;
}
for(i=TOP;i>=0;i--){

Sensitivity: Internal & Restricted


Suresh Gyan Vihar University

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{

Sensitivity: Internal & Restricted


Suresh Gyan Vihar University

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

Sensitivity: Internal & Restricted


Suresh Gyan Vihar University

}
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

Sensitivity: Internal & Restricted


Suresh Gyan Vihar University

Please enter your choice: 1


Enter an Integer Number: 20
20 has been inserted.
Do you want to continue ? y
Chosse
1.push
2.pop
3.display
Please enter your choice: 3
20 10
Do you want to continue ?
5. Code to traverse the stack using
Array ?
Answer
#include <stdio.h>
// Function to traverse and print the array
void printArray(int* arr, int n)
{
int i;

Sensitivity: Internal & Restricted


Suresh Gyan Vihar University

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

Sensitivity: Internal & Restricted


Suresh Gyan Vihar University

6. Code for displaying nodes of linked


list ?
Answer
#include <iostream>

using namespace std;

//Declare Node
struct Node{
int num;
Node *next;
};

//Declare starting (Head) node


struct Node *head=NULL;

//Insert node at start


void insertNode(int n){
struct Node *newNode=new Node;
newNode->num=n;

Sensitivity: Internal & Restricted


Suresh Gyan Vihar University

newNode->next=head;
head=newNode;
}

//Traverse/ display all nodes (print items)


void display(){
if(head==NULL){
cout<<"List is empty!"<<endl;
return;
}
struct Node *temp=head;
while(temp!=NULL){
cout<<temp->num<<" ";
temp=temp->next;
}
cout<<endl;
}

//delete node from start

Sensitivity: Internal & Restricted


Suresh Gyan Vihar University

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

Sensitivity: Internal & Restricted


Suresh Gyan Vihar University

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!

7. Code to insert the node that particular


position in the linked list ?
Answer
// C++ program for insertion in a single linked

Sensitivity: Internal & Restricted


Suresh Gyan Vihar University

// list at a specified position


#include <bits/stdc++.h>
using namespace std;

// A linked list Node


struct Node {
int data;
struct Node* next;
};

// Size of linked list


int size = 0;

// function to create and return a Node


Node* getNode(int data)
{
// allocating space
Node* newNode = new Node();

// inserting the required data

Sensitivity: Internal & Restricted


Suresh Gyan Vihar University

newNode->data = data;
newNode->next = NULL;
return newNode;
}

// function to insert a Node at required position


void insertPos(Node** current, int pos, int data)
{
// This condition to check whether the
// position given is valid or not.
if (pos < 1 || pos > size + 1)
cout << "Invalid position!" << endl;
else {

// Keep looping until the pos is zero


while (pos--) {

if (pos == 0) {

// adding Node at required position

Sensitivity: Internal & Restricted


Suresh Gyan Vihar University

Node* temp = getNode(data);

// Making the new Node to point to


// the old Node at the same position
temp->next = *current;

// Changing the pointer of the Node previous


// to the old Node to point to the new Node
*current = temp;
}
else
// Assign double pointer variable to point to the
// pointer pointing to the address of next Node
current = &(*current)->next;
}
size++;
}
}

void printList(struct Node* head)

Sensitivity: Internal & Restricted


Suresh Gyan Vihar University

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

cout << "Linked list before insertion: ";

Sensitivity: Internal & Restricted


Suresh Gyan Vihar University

printList(head);

int data = 12, pos = 3;


insertPos(&head, pos, data);
cout << "Linked list after insertion of 12 at position 3: ";
printList(head);

// front of the linked list


data = 1, pos = 1;
insertPos(&head, pos, data);
cout << "Linked list after insertion of 1 at position 1: ";
printList(head);

// insetion at end of the linked list


data = 15, pos = 7;
insertPos(&head, pos, data);
cout << "Linked list after insertion of 15 at position 7: ";
printList(head);
return 0;
}

Sensitivity: Internal & Restricted


Suresh Gyan Vihar University

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

8. Code to delete the last node from the


linked list ?
Answer
// CPP program to remove last node of
// linked list.
#include <iostream>
using namespace std;

/* Link list node */


struct Node {
int data;
struct Node* next;
};

Sensitivity: Internal & Restricted


Suresh Gyan Vihar University

/* Function to remove the last node


of the linked list */
Node* removeLastNode(struct Node* head)
{
if (head == NULL)
return NULL;

if (head->next == NULL) {
delete head;
return NULL;
}

// Find the second last node


Node* second_last = head;
while (second_last->next->next != NULL)
second_last = second_last->next;

// Delete last node


delete (second_last->next);

Sensitivity: Internal & Restricted


Suresh Gyan Vihar University

// Change next of second last


second_last->next = NULL;

return head;
}

// Function to push node at head


void push(struct Node** head_ref, int new_data)
{
struct Node* new_node = new Node;
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}

// Driver code
int main()
{
/* Start with the empty list */

Sensitivity: Internal & Restricted


Suresh Gyan Vihar University

Node* head = NULL;

/* Use push() function to construct


the below list 8 -> 23 -> 11 -> 29 -> 12 */
push(&head, 12);
push(&head, 29);
push(&head, 11);
push(&head, 23);
push(&head, 8);

head = removeLastNode(head);
for (Node* temp = head; temp != NULL; temp = temp-
>next)
cout << temp->data << " ";

return 0;
}

Output
8 23 11 29

Sensitivity: Internal & Restricted


Suresh Gyan Vihar University

9. Program to calculate factorial of a


given number using recursion in ‘C’ ?
Answer
#include<stdio.h>
int fact(int);
int main(){
int x,n;

printf(" Enter the Number to Find Factorial


:");
scanf("%d",&n);
x=fact(n);

printf(" Factorial of %d is %d",n,x);


return 0;
}
int fact(int n)
{
if(n==0)
return(1);

Sensitivity: Internal & Restricted


Suresh Gyan Vihar University

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;

void towerOfHanoi(int n, char from_rod,


char to_rod, char aux_rod)
{
if (n == 1)
{
cout << "Move disk 1 from rod "
<< from_rod
<< " to rod "
<< to_rod<<endl;

Sensitivity: Internal & Restricted


Suresh Gyan Vihar University

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

Sensitivity: Internal & Restricted


Suresh Gyan Vihar University

// This is code is contributed by


rathbhupendra

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

Sensitivity: Internal & Restricted


Suresh Gyan Vihar University

Sensitivity: Internal & Restricted

You might also like