Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
// Language C++
#include <bits/stdc++.h>
class stac
int stk[5];
int top;
public:
stac()
top=-1;
void push(int x)
if(top > 4)
return;
stk[++top]=x;
void pop()
{
if(top <0)
return;
void display()
if(top<0)
return;
for(int i=top;i>=0;i--)
};
int main()
ios_base::sync_with_stdio(0);
cin.tie(0);
int ch;
stac st;
do
st.push(ch);
break;
case 3: st.display();break;
case 4: exit(0);
}while(ch!=4);
return 0;
Output:
! !
Program 5(B)
// AIMWrite a program to a implement stack using Linked List.
// Language C++
#include<iostream>
struct node
int data;
};
class stack
public:
stack() // constructure
top=NULL;
};
// PUSH Operation
void stack::push()
int value;
cout<<"\nPUSH Operationn";
cin>>value;
ptr=new node;
ptr->data=value;
ptr->next=NULL;
if(top!=NULL)
ptr->next=top;
top=ptr;
// POP Operation
void stack::pop()
if(top==NULL)
temp=top;
top=top->next;
// Show stack
void stack::show()
while(ptr1!=NULL)
cout<<ptr1->data<<" ->";
ptr1=ptr1->next;
cout<<"NULL\n";
// Main function
int main()
stack s;
int choice;
while(1)
cout<<"1:PUSH\n2:POP\n3:DISPLAY STACK\n4:EXIT";
cin>>choice;
switch(choice)
{
case 1:
s.push();
break;
case 2:
s.pop();
break;
case 3:
s.show();
break;
case 4:
return 0;
break;
default:
break;
return 0;
}
Output:
!
Program 6
// AIMWrite a program to convert infix to postfix notation.
// Language C++
#include<bits/stdc++.h>
int prec(char c)
if(c == '^')
return 3;
return 2;
return 1;
else
return -1;
void infixToPostfix(string s)
std::stack<char> st;
st.push('N');
int l = s.length();
string ns;
if((s[i] >= 'a' && s[i] <= 'z')||(s[i] >= 'A' && s[i] <= 'Z'))
ns+=s[i];
st.push('(');
{
while(st.top() != 'N' && st.top() != '(')
char c = st.top();
st.pop();
ns += c;
if(st.top() == '(')
char c = st.top();
st.pop();
else{
char c = st.top();
st.pop();
ns += c;
st.push(s[i]);
while(st.top() != 'N')
char c = st.top();
st.pop();
ns += c;
}
int main()
infixToPostfix(exp);
return 0;
Output:
Program 7
// AIMWrite a program to implement doubly linked list.
// Language C
#include <stdio.h>
#include <stdlib.h>
struct node
int n;
}*h,*temp,*temp1,*temp2,*temp4;
void insert1();
void insert2();
void insert3();
void traversebeg();
void traverseend(int);
void sort();
void search();
void update();
void delete();
int count = 0;
void main()
int ch;
h = NULL;
printf("\n 10 - Exit");
while (1)
scanf("%d", &ch);
switch (ch)
case 1:
insert1();
break;
case 2:
insert2();
break;
case 3:
insert3();
break;
case 4:
delete();
break;
case 5:
traversebeg();
break;
case 6:
temp2 = h;
if (temp2 == NULL)
else
traverseend(temp2->n);
break;
case 7:
search();
break;
case 8:
sort();
break;
case 9:
update();
break;
case 10:
exit(0);
default:
}
}
void create()
int data;
temp->prev = NULL;
temp->next = NULL;
scanf("%d", &data);
temp->n = data;
count++;
/* TO insert at beginning */
void insert1()
if (h == NULL)
create();
h = temp;
temp1 = h;
else
create();
temp->next = h;
h->prev = temp;
h = temp;
/* To insert at end */
void insert2()
if (h == NULL)
create();
h = temp;
temp1 = h;
else
create();
temp1->next = temp;
temp->prev = temp1;
temp1 = temp;
void insert3()
int pos, i = 2;
temp2 = h;
return;
return;
create();
h = temp;
temp1 = h;
return;
else
temp2 = temp2->next;
i++;
create();
temp->prev = temp2;
temp->next = temp2->next;
temp2->next->prev = temp;
temp2->next = temp;
/* To delete an element */
void delete()
int i = 1, pos;
scanf("%d", &pos);
temp2 = h;
return;
if (h == NULL)
return;
else
{
temp2 = temp2->next;
i++;
if (i == 1)
if (temp2->next == NULL)
free(temp2);
temp2 = h = NULL;
return;
if (temp2->next == NULL)
temp2->prev->next = NULL;
free(temp2);
return;
temp2->next->prev = temp2->prev;
if (i != 1)
if (i == 1)
h = temp2->next;
free(temp2);
count--;
}
void traversebeg()
temp2 = h;
if (temp2 == NULL)
return;
temp2 = temp2->next;
void traverseend(int i)
if (temp2 != NULL)
i = temp2->n;
temp2 = temp2->next;
traverseend(i);
void search()
temp2 = h;
if (temp2 == NULL)
return;
scanf("%d", &data);
if (temp2->n == data)
return;
else
temp2 = temp2->next;
count++;
}
printf("\n Error : %d not found in list", data);
void update()
scanf("%d", &data);
scanf("%d", &data1);
temp2 = h;
if (temp2 == NULL)
return;
if (temp2->n == data)
temp2->n = data1;
traversebeg();
return;
else
temp2 = temp2->next;
}
void sort()
int i, j, x;
temp2 = h;
temp4 = h;
if (temp2 == NULL)
return;
x = temp2->n;
temp2->n = temp4->n;
temp4->n = x;
}
}
traversebeg();
Output:
!
Program 8(A)
// AIMWrite a program to implement simple queue using array.
// Language C
#include <stdio.h>
#define MAX 50
int queue_array[MAX];
int rear = - 1;
int front = - 1;
main()
int choice;
while (1)
{
printf("1.Insert element to queue \n");
printf("4.Quit \n");
scanf("%d", &choice);
switch (choice)
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
} /*End of switch*/
} /*End of while*/
} /*End of main()*/
insert()
int add_item;
if (rear == MAX - 1)
if (front == - 1)
front = 0;
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
} /*End of insert()*/
delete()
return ;
else
front = front + 1;
} /*End of delete() */
display()
int i;
if (front == - 1)
printf("Queue is empty \n");
else
printf("Queue is : \n");
printf("\n");
Output:
!
Program 8(B)
// AIMWrite a program to implement simple queue using linked list.
// Language C
#include <stdio.h>
#include <stdlib.h>
struct node
int info;
*front,*rear,*temp,*front1;
int frontelement();
void deq();
void empty();
void display();
void create();
void queuesize();
int count = 0;
void main()
printf("\n 1 - Enque");
printf("\n 2 - Deque");
printf("\n 4 - Empty");
printf("\n 5 - Exit");
printf("\n 6 - Display");
create();
while (1)
scanf("%d", &ch);
switch (ch)
case 1:
scanf("%d", &no);
enq(no);
break;
case 2:
deq();
break;
case 3:
e = frontelement();
if (e != 0)
else
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
queuesize();
break;
default:
break;
void create()
{
void queuesize()
if (rear == NULL)
rear->ptr = NULL;
rear->info = data;
front = rear;
else
rear->ptr = temp;
temp->info = data;
temp->ptr = NULL;
rear = temp;
}
count++;
void display()
front1 = front;
printf("Queue is empty");
return;
front1 = front1->ptr;
if (front1 == rear)
printf("%d", front1->info);
void deq()
front1 = front;
if (front1 == NULL)
{
printf("\n Error: Trying to display elements from empty queue");
return;
else
if (front1->ptr != NULL)
front1 = front1->ptr;
free(front);
front = front1;
else
free(front);
front = NULL;
rear = NULL;
count--;
int frontelement()
return(front->info);
else
return 0;
}
/* Display if queue is empty or not */
void empty()
else
Output:
!
Program 8(C)
// AIMWrite a program to implement priority queue using linked list.
// Language C++
#include<stdio.h>
#include<stdlib.h>
struct node
int priority;
int info;
}*front=NULL;
int del();
void display();
int isEmpty();
main()
int choice,item,item_priority;
while(1)
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch(choice)
case 1:
scanf("%d",&item);
scanf("%d",&item_priority);
insert(item, item_priority);
break;
case 2:
break;
case 3:
display();
break;
case 4:
exit(1);
default :
printf("Wrong choice\n");
if(tmp==NULL)
return;
tmp->info=item;
tmp->priority=item_priority;
tmp->link=front;
front=tmp;
else
p = front;
p=p->link;
tmp->link=p->link;
p->link=tmp;
}/*End of insert()*/
int del()
int item;
if( isEmpty() )
printf("Queue Underflow\n");
exit(1);
else
tmp=front;
item=tmp->info;
front=front->link;
free(tmp);
return item;
}/*End of del()*/
int isEmpty()
return 1;
else
return 0;
}/*End of isEmpty()*/
void display()
ptr=front;
if( isEmpty() )
printf("Queue is empty\n");
else
{ printf("Queue is :\n");
printf("Priority Item\n");
while(ptr!=NULL)
{
printf("%5d %5d\n",ptr->priority,ptr->info);
ptr=ptr->link;
Output:
Program 9
// AIMWrite a program to implement (perform insertion, deletion and modification of a node
and // transversal) a binary search tree.
// Language C++
#include <stdio.h>
#include <stdlib.h>
struct btnode
int value;
void delete1();
void insert();
void delete();
void create();
int flag = 1;
void main()
int ch;
printf("\nOPERATIONS ---");
printf("6 - Exit\n");
while(1)
scanf("%d", &ch);
switch (ch)
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
inorder(root);
break;
case 4:
preorder(root);
break;
case 5:
postorder(root);
break;
case 6:
exit(0);
default :
break;
void insert()
create();
if (root == NULL)
root = temp;
else
search(root);
/* To create a node */
void create()
int data;
scanf("%d", &data);
temp->value = data;
if ((temp->value > t->value) && (t->r != NULL)) /* value more than root node value insert at right */
search(t->r);
t->r = temp;
else if ((temp->value < t->value) && (t->l != NULL)) /* value less than root node value insert at left */
search(t->l);
t->l = temp;
if (root == NULL)
return;
if (t->l != NULL)
inorder(t->l);
if (t->r != NULL)
inorder(t->r);
}
/* To check for the deleted node */
void delete()
int data;
if (root == NULL)
return;
scanf("%d", &data);
t1 = root;
t2 = root;
search1(root, data);
if (root == NULL)
return;
if (t->l != NULL)
preorder(t->l);
if (t->r != NULL)
preorder(t->r);
if (root == NULL)
return;
if (t->l != NULL)
postorder(t->l);
if (t->r != NULL)
postorder(t->r);
if ((data>t->value))
t1 = t;
search1(t->r, data);
{
t1 = t;
search1(t->l, data);
else if ((data==t->value))
delete1(t);
/* To delete a node */
int k;
if (t1->l == t)
t1->l = NULL;
else
t1->r = NULL;
t = NULL;
free(t);
return;
}
/* To delete node having one left hand child */
if (t1 == t)
root = t->l;
t1 = root;
else if (t1->l == t)
t1->l = t->l;
else
t1->r = t->l;
t = NULL;
free(t);
return;
if (t1 == t)
root = t->r;
t1 = root;
else if (t1->r == t)
t1->r = t->r;
else
t1->l = t->r;
t == NULL;
free(t);
return;
t2 = root;
if (t->r != NULL)
k = smallest(t->r);
flag = 1;
else
k =largest(t->l);
flag = 2;
search1(root, k);
t->value = k;
}
}
t2 = t;
if (t->l != NULL)
t2 = t;
return(smallest(t->l));
else
return (t->value);
if (t->r != NULL)
t2 = t;
return(largest(t->r));
else
return(t->value);
Output:
!
Program 10
// AIMWrite a program to create the mirror image of a binary tree.
// Language C++
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
if (node==NULL)
return;
else
/* do the subtrees */
mirror(node->left);
mirror(node->right);
temp = node->left;
node->left = node->right;
node->right = temp;
}
void inOrder(struct node* node)
if (node == NULL)
return;
inOrder(node->left);
inOrder(node->right);
int main()
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
inOrder(root);
mirror(root);
inOrder(root);
getchar();
return 0;
Output:
!
Program 11
// AIMWrite a program to implement heap sort.
// Language C++
#include <bits/stdc++.h>
largest = l;
largest = r;
if (largest != i)
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
heapify(arr, n, i);
swap(arr[0], arr[i]);
heapify(arr, i, 0);
}
/* A utility function to print array of size n */
// Driver program
int main()
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
heapSort(arr, n);
printArray(arr, n);
Output:
!
Program 12(A)
// AIMWrite a program to perform a depth first search in a graph.
// Language C++
#include<bits/stdc++.h>
int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10];
int main()
int m;
cin >> n;
cin >> m;
for(k=1;k<=m;k++)
cin >>i>>j;
cost[i][j]=1;
cin >>v;
visited[v]=1;
k=1;
while(k<n)
for(j=n;j>=1;j--)
visit[j]=1;
stk[top]=j;
top++;
}
v=stk[--top];
k++;
visit[v]=0; visited[v]=1;
Output:
Program 12(B)
// AIMWrite a program to perform a breadth first search in a graph.
// Language C++
#include <bits/stdc++.h>
list<int>::iterator itr;
lev = 0;
level[1] = lev;
while (flag) {
flag = false;
if (level[i] == lev) {
flag = true;
itr = adjacencyList[i].begin();
par = i;
if (level[*itr] != -1) {
++itr;
continue;
level[*itr] = lev + 1;
parent[*itr] = par;
++itr;
}
++lev;
int main()
scanf("%d", &vertices);
scanf("%d", &edges);
adjacencyList[v1].push_back(v2);
adjacencyList[v2].push_back(v1);
++itr;
cout<<endl;
//Each element of Parent Array holds the Node value of its parent
//Each element of Level Array holds the Level value of that node
parent[i] = 0;
level[i] = -1;
//Level Array
}
return 0;
Output: