You are on page 1of 23

1.

Program to store temperature of two different cities for a week and display
it

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

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;

}
2. Program to Store value entered by user in three dimensional array and display
it
#include<iostream>
using namespace std;

int main()
{.
int x[3][2] = {{0,1}, {2,3}, {4,5}};

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


{
for (int j = 0; j < 2; j++)
{
cout << "Element at x[" << i
<< "][" << j << "]: ";
cout << x[i][j]<<endl;
}
}

return 0;
}
3. Program to swap first and last element of an integer 1-d array

#include<iostream>
using namespace std;
int main()
{
int Arr[100],n,i,temp;

cout<<"Enter number of elements you want to insert ";


cin>>n;

for(i=0;i<n;i++)
{

cout<<"Enter element "<<i+1<<":";


cin>>Arr[i];
}
temp=Arr[0];
Arr[0]=Arr[n-1];

Arr[n-1]=temp;

cout<<"\nArray after swapping"<<endl;

for(i=0;i<n;i++)
cout<<Arr[i]<<" ";
return 0;
}

Output:
4. Code to PUSH Element in a stack using Array

#include <bits/stdc++.h>

using namespace std;

#define MAX 1000


class Stack {
int top;

public:
int a[MAX];

Stack() { top = -1; }


bool push(int x);
int pop();
int peek();
bool isEmpty();
};

bool Stack::push(int x)
{
if (top >= (MAX - 1)) {
cout << "Stack Overflow";
return false;
}
else {
a[++top] = x;
cout << x << " pushed into stack\n";
return true;
}
}

int Stack::pop()
{
if (top < 0) {
cout << "Stack Underflow";
return 0;
}
else {
int x = a[top--];
return x;
}
}
int Stack::peek()
{
if (top < 0) {
cout << "Stack is Empty";
return 0;
}
else {
int x = a[top];
return x;
}
}
bool Stack::isEmpty()
{
return (top < 0);
}

int main()
{
class Stack s;
s.push(10);
s.push(20);
s.push(30);
cout << s.pop() << " Popped from stack\n";

return 0;
}
5. Code to traverse the stack using Array

#include <iostream>
#include <stack>
usingnamespacestd;
  
intmain()
{
    stack<int>mystack;
    mystack.push(5);
    mystack.push(1);
    mystack.push(2);
  
    cout <<mystack.top();
    return0;
}
Output :-

6. Code for displaying nodes of linked list

#include <iostream.h>
#include <conio.h>

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

class linklist
{
node *head;
public:
linklist()
{head=NULL;}
void create(void);
void display(void);
};

void linklist :: create(void)


{
node *newl=NULL,*end=newl;
int info;
while(1)
{
cout<<"\n\nenter -999 to terminate\n";
cout<<"enter info:-";
cin>>info;
if(info==-999)
break;
else
{
newl=new node;
newl->data=info;
if(head==NULL)
head=newl;
else
end->next=newl;
end=newl;
end->next=NULL;
}
}
}

void linklist :: display(void)


{
cout<<"\n\n\nDisplay Function\n";
node *temp=head;
while(temp!=NULL)
{
cout<<temp->data;
if(temp->next!=NULL)
cout<<"==>";
temp=temp->next;
}
delete(temp);
}

void main()
{
clrscr();
linklist o1;
o1.create();
o1.display();
getch();
}
Output :
7. Code to insert the node at the particular position in the linked list

#include <iostream>

using namespace std;

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

struct Node *head=NULL;

void insertNode(int n){


struct Node *newNode=new Node;
newNode->num=n;
newNode->next=head;
head=newNode;
}

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

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

8. Code to delete the last node from the linked list

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

class Node
{
public:
int data;
Node *next;
};

void deleteNode(Node *head, Node *n)


{
if(head == n)
{
if(head->next == NULL)
{
cout << "There is only one node." <<
" The list can't be made empty ";
return;
}

head->data = head->next->data;
n = head->next;
head->next = head->next->next;
free(n);

return;
}
Node *prev = head;
while(prev->next != NULL && prev->next != n)
prev = prev->next;

if(prev->next == NULL)
{
cout << "\nGiven node is not present in Linked List";
return;
}

prev->next = prev->next->next;
free(n);

return;
}
void push(Node **head_ref, int new_data)
{
Node *new_node = new Node();
new_node->data = new_data;
new_node->next = *head_ref;
*head_ref = new_node;
}
void printList(Node *head)
{
while(head!=NULL)
{
cout<<head->data<<" ";
head=head->next;
}
cout<<endl;
}
int main()
{
Node *head = NULL;

push(&head,3);
push(&head,2);
push(&head,6);
push(&head,5);
push(&head,11);
push(&head,10);
push(&head,15);
push(&head,12);

cout<<"Given Linked List: ";


printList(head);
cout<<"\nDeleting node "<< head->next->next->data<<" ";
deleteNode(head, head->next->next);

cout<<"\nModified Linked List: ";


printList(head);
cout<<"\nDeleting first node ";
deleteNode(head, head);

cout<<"\nModified Linked List: ";


printList(head);
return 0;
}
9. Program to calculate factorial of a given number using recursion in ‘C’

#include<stdio.h>

unsigned int factorial(unsigned int n)


{
if (n == 0)
return 1;
return n * factorial(n - 1);
}

int main()
{
int num = 5;
printf("Factorial of %d is %d", num, factorial(num));
return 0;

}
10. Program to implement Tower of Hanoi problem

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>

struct Stack
{
unsigned capacity;
int top;
int *array;
};
struct Stack* createStack(unsigned capacity)
{
struct Stack* stack =
(struct Stack*) malloc(sizeof(struct Stack));
stack -> capacity = capacity;
stack -> top = -1;
stack -> array =
(int*) malloc(stack -> capacity * sizeof(int));
return stack;
}
int isFull(struct Stack* stack)
{
return (stack->top == stack->capacity - 1);
}
{
return (stack->top == -1);
}
void push(struct Stack *stack, int item)
{
if (isFull(stack))
return;
stack -> array[++stack -> top] = item;
}

int pop(struct Stack* stack)


{
if (isEmpty(stack))
return INT_MIN;
return stack -> array[stack -> top--];
}
void moveDisk(char fromPeg, char toPeg, int disk)
{
printf("Move the disk %d from \'%c\' to \'%c\'\n",
disk, fromPeg, toPeg);
}

void moveDisksBetweenTwoPoles(struct Stack *src,


struct Stack *dest, char s, char d)
{
int pole1TopDisk = pop(src);
int pole2TopDisk = pop(dest);
if (pole1TopDisk == INT_MIN)
{
push(src, pole2TopDisk);
moveDisk(d, s, pole2TopDisk);
}
else if (pole2TopDisk == INT_MIN)
{
push(dest, pole1TopDisk);
moveDisk(s, d, pole1TopDisk);
}
else if (pole1TopDisk > pole2TopDisk)
{
push(src, pole1TopDisk);
push(src, pole2TopDisk);
moveDisk(d, s, pole2TopDisk);
}

else
{
push(dest, pole2TopDisk);
push(dest, pole1TopDisk);
moveDisk(s, d, pole1TopDisk);
}
}
void tohIterative(int num_of_disks, struct Stack
*src, struct Stack *aux,
struct Stack *dest)
{
int i, total_num_of_moves;
char s = 'S', d = 'D', a = 'A';

if (num_of_disks % 2 == 0)
{
char temp = d;
d = a;
a = temp;
}
total_num_of_moves = pow(2, num_of_disks) - 1;
for (i = num_of_disks; i >= 1; i--)
push(src, i);

for (i = 1; i <= total_num_of_moves; i++)


{
if (i % 3 == 1)
moveDisksBetweenTwoPoles(src, dest, s, d);

else if (i % 3 == 2)
moveDisksBetweenTwoPoles(src, aux, s, a);

else if (i % 3 == 0)
moveDisksBetweenTwoPoles(aux, dest, a, d);
}
}
int main()
{
unsigned num_of_disks = 3;

struct Stack *src, *dest, *aux;


src = createStack(num_of_disks);
aux = createStack(num_of_disks);
dest = createStack(num_of_disks);

tohIterative(num_of_disks, src, aux, dest);


return 0;
}

You might also like