0% found this document useful (0 votes)
75 views

Lab Assigment Data Structure

The document contains 8 questions and solutions related to data structures and algorithms using C++. Question 1 involves storing temperature data of two cities over a week in a 2D array and displaying it. Question 2 stores user input values in a 3D array and displays it. Question 3 swaps the first and last elements of a 1D integer array. Question 4 demonstrates pushing elements onto a stack implemented using an array. Question 5 transverses a stack using an array. Question 6 displays the nodes of a linked list implemented using an array. Question 7 inserts a node at a particular position in a linked list. Question 8 deletes the last node of a linked list.

Uploaded by

Pankaj Kohli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

Lab Assigment Data Structure

The document contains 8 questions and solutions related to data structures and algorithms using C++. Question 1 involves storing temperature data of two cities over a week in a 2D array and displaying it. Question 2 stores user input values in a 3D array and displays it. Question 3 swaps the first and last elements of a 1D integer array. Question 4 demonstrates pushing elements onto a stack implemented using an array. Question 5 transverses a stack using an array. Question 6 displays the nodes of a linked list implemented using an array. Question 7 inserts a node at a particular position in a linked list. Question 8 deletes the last node of a linked list.

Uploaded by

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

ASSIGMENT LABS

SUBJECT DATA STRUCTURE USING C++


PROGRAM MCA
SEMESTER 3 ( session 2020)
MAX MARKS 100
REFERNCE / 2556944
ENROLLMENT
NUMBER
STUDENT NAME PANKAJ KUMAR KOHLI

QUESTION 1: Program to store the temperature of two differnt cities for a


week and display it
solution:

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

// Inserting the values into the temperature array


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

// Accessing the values from the temperature array


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

Output

Enter all temperature for a week of first city and then second city.
City 1, Day 1 : 32
City 1, Day 2 : 33
City 1, Day 3 : 32
City 1, Day 4 : 34
City 1, Day 5 : 35
City 1, Day 6 : 36
City 1, Day 7 : 38
City 2, Day 1 : 23
City 2, Day 2 : 24
City 2, Day 3 : 26
City 2, Day 4 : 22
City 2, Day 5 : 29
City 2, Day 6 : 27
City 2, Day 7 : 23

Displaying Values:
City 1, Day 1 = 32
City 1, Day 2 = 33
City 1, Day 3 = 32
City 1, Day 4 = 34
City 1, Day 5 = 35
City 1, Day 6 = 36
City 1, Day 7 = 38
City 2, Day 1 = 23
City 2, Day 2 = 24
City 2, Day 3 = 26
City 2, Day 4 = 22
City 2, Day 5 = 29
City 2, Day 6 = 27
City 2, Day 7 = 23

Question 2: Program to store value entered by user in three dimensional array


and display it
Solution :

// C++ Program to Store value entered by user in


// three dimensional array and display it.

#include <iostream>
using namespace std;

int main() {
// This array can store upto 12 elements (2x3x2)
int test[2][3][2] = {
{
{1, 2},
{3, 4},
{5, 6}
},
{
{7, 8},
{9, 10},
{11, 12}
}
};

// Displaying the values with proper index.


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

QUESTION 3 Program to swap first and last element of an integer 1-d array
SOLUTION :

#include<iostream>

using namespace std;

int main()

int n, s,i;

cout<<"Enter Number of elements you want to enter : ";


cin>>n;

int ar[n];

cout<<"Enter the elements of the array "<<endl;

for( i = 0;i<n;i++)//loop to input elements from user

cin>>ar[i];

// Now swapping the first and last element of the array using swap metheod.

s = ar[0];

ar[0] = ar[n-1];

ar[n-1] = s;

cout<<"Array after swapping first and last elements "<<endl;

for(i = 0;i<n;i++) // loop to display the elements after swapping

cout<<ar[i]<<endl;

return 0;

Question 4 : Code to PUSH element in a stack using Array


Solution :
#include<iostream>
using namespace std;

#define MAX 1000 //max size for stack

class Stack
{
int top;
public:
int myStack[MAX]; //stack array

Stack() { top = -1; }


bool push(int x);
int pop();
bool isEmpty();
};
//pushes element on to the stack
bool Stack::push(int item)
{
if (top >= (MAX-1)) {
cout << "Stack Overflow!!!";
return false;
}
else {
myStack[++top] = item;
cout<<item<<endl;
return true;
}
}

//removes or pops elements out of the stack


int Stack::pop()
{
if (top < 0) {
cout << "Stack Underflow!!";
return 0;
}
else {
int item = myStack[top--];
return item;
}
}

//check if stack is empty


bool Stack::isEmpty()
{
return (top < 0);
}

// main program to demonstrate stack functions


int main()
{
class Stack stack;
cout<<"The Stack Push "<<endl;
stack.push(2);
stack.push(4);
stack.push(6);
cout<<"The Stack Pop : "<<endl;
while(!stack.isEmpty())
{
cout<<stack.pop()<<endl;
}
return 0;
}
Output:
The Stack Push

The Stack Pop:

Question 5: code to transverse the stack using array


Solution :

#include<iostream>
#define SIZE 5
namespace std {}
using namespace std;
class STACK
{
private:
int num[SIZE];
int top;
public:
STACK();
int push(int);
int pop();
int isEmpty();
int isFull();
void displayItems();
};
STACK::STACK()
{
top=-1;
}
int STACK::isEmpty()
{
if(top==-1)
return 1;
else
return 0;
}
int STACK::isFull()
{
if(top==(SIZE-1))
return 1;
else
return 0;
}
int STACK::push(int n)
{
if(isFull()){
return 0;
}
++top;
num [top]=n;
return n;
}
int STACK::pop()
{
int temp;
if(isEmpty())
return 0;
temp=num[top];
--top;
return temp;
}
void STACK::displayItems(){
int i;
cout<<"STACK is:";
for(i=(top);i>=0;i--)
cout<<num[i]<<"";
cout<<endl;
}
int main()
{
STACK stk;
int choice,n,temp;
do {
cout<<endl;
cout<<"0-Exit."<<endl;
cout<<"1-Push Item."<<endl;
cout<<"2-Pop Item."<<endl;
cout<<"3 - Display Items (Print STACK)."<<endl;

cout<<"Enter your choice: ";


cin>>choice;

switch(choice){
case 0: break;

case 1:
cout<<"Enter item to insert: ";
cin>>n;
temp=stk.push(n);
if(temp==0)
cout<<"STACK is FULL."<<endl;
else
cout<<temp<<" inserted."<<endl;
break;

case 2:
temp=stk.pop();
if(temp==0)
cout<<"STACK IS EMPTY."<<endl;
else
cout<<temp<<" is removed (popped)."<<endl;
break;

case 3:
stk.displayItems();
break;

default:
cout<<"An Invalid choice."<<endl;
}
}while(choice!=0);

return 0;
}

Question 6: Code to displaying Nodes of linked list of Array


Solution :
include <iostream>
using namespace std;
struct Node {
int data;
struct Node *next;
};
struct Node* head = NULL;
void insert(int new_data) {
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = head;
head = new_node;
}
void display() {
struct Node* ptr;
ptr = head;
while (ptr != NULL) {
cout<< ptr->data <<" ";
ptr = ptr->next;
}
}
int main() {
insert(3);
insert(1);
insert(7);
insert(2);
insert(9);
cout<<"The linked list is: ";
display();
return 0;

Output
The linked list is: 9 2 7 1 3

Question 7
Code to insert the node at particular position in a linked list
Solution:

include <iostream>
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

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

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

}
}

// This function prints contents


// of the linked list

void printList(struct Node* head)


{

while (head != NULL) {

cout << " " << head->data;

head = head->next;

cout << endl;


}

// Driver Code

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

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

Question: 8
Code to delete the last node of linked list
Solution:
#include <iostream>
using namespace std;

//node structure
struct Node {
int data;
Node* next;
};

class LinkedList {
private:
Node* head;
public:
LinkedList(){
head = NULL;
}

//Add new element at the end of the list


void push_back(int newElement) {
Node* newNode = new Node();
newNode->data = newElement;
newNode->next = NULL;
if(head == NULL) {
//for first element in the list
head = newNode;
} else {
Node* temp = head;
while(temp->next != NULL)
temp = temp->next;
temp->next = newNode;
}
}

//Delete last node of the list


void pop_back() {
if(head != NULL) {
if(head->next == NULL) {
head = NULL;
} else {
Node* temp = head;
while(temp->next->next != NULL)
temp = temp->next;
Node* lastNode = temp->next;
temp->next = NULL;
free(lastNode);
}
}
}

//display the content of the list


void PrintList() {
Node* temp = head;
if(temp != NULL) {
cout<<"\nThe list contains: ";
while(temp != NULL) {
cout<<temp->data<<" ";
temp = temp->next;
}
} else {
cout<<"\nThe list is empty.";
}
}
};

// test the code


int main() {
LinkedList MyList;

//Add four elements in the list.


MyList.push_back(10);
MyList.push_back(20);
MyList.push_back(30);
MyList.push_back(40);
MyList.PrintList();

//Delete the last node


MyList.pop_back();
MyList.PrintList();
return 0;
}

Output
The list contains: 10 20 30 40
The list contains: 10 20 30

Question 9
Program to calulate the factorial of given number using recursion in c
Solution:
#include<stdio.h>
// declaring the function
int fact(int);

int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int num, f;
printf("\n\nEnter a number: ");
scanf("%d", &num);
f= fact(num);
printf("\n\nFactorial of %d is %d\n\n", num, f);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}

int fact(int aj)


{
if(aj==1 || aj==0)
return 1;
else
return (aj*fact(aj-1));
}
Output:
enter number :5
factorial of 5 is 120
coding is fun !

Question 10: Program to implement Tower Of Hanoi problem


Solution:

The Tower of Hanoi is a mathematical puzzle invented by the French mathematician Edouard Lucas
in 1883.

There are three pegs, source(A), Auxiliary (B) and Destination(C). Peg A contains a set of disks
stacked to resemble a tower, with the largest disk at the bottom and the smallest disk at the top.
Illustrate the initial configuration of the pegs for 3 disks. The objective is to transfer the entire tower
of disks in peg A to peg C maintaining the same order of the disks.

Obeying the following rules:

1.Only one disk can be transfer at a time.

2.Each move consists of taking the upper disk from one of the peg and placing it on the top of
another peg i.e. a disk can only be moved if it is the uppermost disk of the peg.

3.Never a larger disk is placed on a smaller disk during the transfer

Algorithm:

TOH( n, Sour, Aux , Des)


If(n=1)
Write ("Move Disk “, n ," from ", Sour ," to ",Des)
Else
TOH(n-1,Sour,Des,Aux);
Write ("Move Disk “, n ," from ", Sour ," to ",Des)
TOH(n-1,Aux,Sour,Des);
END

Program:

#include<iostream>
using namespace std;

//tower of HANOI function implementation


void TOH(int n,char Sour, char Aux,char Des)
{
if(n==1)
{
cout<<"Move Disk "<<n<<" from "<<Sour<<" to "<<Des<<endl;
return;
}
TOH(n-1,Sour,Des,Aux);
cout<<"Move Disk "<<n<<" from "<<Sour<<" to "<<Des<<endl;
TOH(n-1,Aux,Sour,Des);
}

//main program
int main()
{
int n;

cout<<"Enter no. of disks:";


cin>>n;
//calling the TOH
TOH(n,'A','B','C');

return 0;
}

Output

Enter no. of disks:3


Move Disk 1 from A to C
Move Disk 2 from A to B
Move Disk 1 from C to B
Move Disk 3 from A to C
Move Disk 1 from B to A
Move Disk 2 from B to C
Move Disk 1 from A

You might also like