Lab Assigment Data Structure
Lab Assigment Data Structure
#include <iostream>
using namespace std;
int main()
{
int temperature[CITY][WEEK];
cout << "Enter all temperature for a week of first city and then second city. \n";
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
#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}
}
};
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>
int main()
int n, s,i;
int ar[n];
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<<ar[i]<<endl;
return 0;
class Stack
{
int top;
public:
int myStack[MAX]; //stack array
#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;
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;
}
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;
struct Node {
int data;
int size = 0;
// allocating space
newNode->data = data;
newNode->next = NULL;
return newNode;
}
else {
while (pos--) {
if (pos == 0) {
temp->next = *current;
*current = temp;
else
size++;
}
}
head = head->next;
// Driver Code
int main()
{
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);
printList(head);
data = 1, pos = 1;
printList(head);
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;
}
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;
}
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.
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.
Algorithm:
Program:
#include<iostream>
using namespace std;
//main program
int main()
{
int n;
return 0;
}
Output