Professional Documents
Culture Documents
#include <iostream>
#include <string.h>
int roll_num;
char name[20];
float marks;
} stud;
int main()
stud s[20];
char x[20];
do
switch (ch)
case 1:
cin >> n;
create(s, n);
break;
case 2:
display(s, n);
break;
case 3:
bubble_sort(s, n);
break;
case 4:
insertionSort(s, n);
break;
case 5:
quick_sort(s, 0, n - 1);
<< "\t"
<< "\t"
<< "Marks";
cout << "\t " << s[i].roll_num << "\t " << s[i].name << "\t " << s[i].marks;
break;
case 6:
search(s, n, key);
break;
case 7:
cout << "\n Enter the name of student which u want to search:=";
cin >> x;
insertionSort(s, n);
if (result == -1)
cout << " \n Student name you want to search for is not present ! \n";
else
break;
case 8:
return 0;
default:
cout << "\n Invalid choice !! Please enter your choice again." << endl;
int i;
int i;
<< "\t"
<< "\t"
<< "\t"
<< "Marks";
cout << "\t " << s[i].roll_num << "\t " << s[i].name << "\t " << s[i].marks;
int i, j;
stud temp;
temp = s[j];
s[j + 1] = temp;
int i, j;
stud key;
key = s[i];
j = i - 1;
/* Move elements of arr[0..i-1], that are
s[j + 1] = s[j];
j = j - 1;
s[j + 1] = key;
int j;
if (l < u)
j = partition(s, l, u);
quick_sort(s, l, j - 1);
quick_sort(s, j + 1, u);
int i, j;
stud temp, v;
v = s[l];
i = l;
j = u + 1;
do
{
do
i++;
do
j--;
if (i < j)
temp = s[i];
s[i] = s[j];
s[j] = temp;
s[l] = s[j];
s[j] = v;
return (j);
// linear search for marks if more than one student having same marks print all of them
int i;
<< "\t"
<< "\t"
<< "\t"
<< "Marks";
if (key == s[i].marks)
cout << "\n\t " << s[i].roll_num << "\t " << s[i].name << "\t " << s[i].marks;
int mid;
if (strcmp(x, s[mid].name) == 0)
return mid;
high = mid - 1;
else
low = mid + 1;
-1;
}
2. Department of Computer Engineering has student's club named ‘COMET’. Students of Second,
third and final year of department can be granted membership on request. Similarly one may cancel
the membership of club. First node is reserved for president of club and last node is reserved for
secretary of club. Write program to maintain club member‘s information using singly linked list.
Store student MIS Registration No. and Name. Write functions to a) Add and delete the members as
well as president or even secretary. b) Compute total number of members of club c) Display
members d) Display list in reverse order using recursion e) Two linked lists exists for two divisions.
Concatenate two lists.
#include<iostream>
#include<string.h>
struct node
char name[50];
};
class info
node
*s = NULL,
*head1 = NULL, *temp1 = NULL, *head2 = NULL, *temp2 = NULL, *head = NULL, *temp = NULL;
int b, c, i, j, ct;
char a[20];
public:
node *create();
void insertp();
void insertm();
void delm();
void delp();
void dels();
void display();
void count();
void reverse();
void concat();
};
node *info::create()
cin >> a;
strcpy(p->name, a);
cin >> b;
p->prn = b;
cin >> c;
p->rollno = c;
p->next = NULL;
return p;
void info::insertm()
node *p = create();
if (head == NULL)
head = p;
else
temp = head;
while (temp->next != NULL)
temp = temp->next;
temp->next = p;
void info::insertp()
node *p = create();
if (head == NULL)
head = p;
else
temp = head;
head = p;
head->next = temp->next;
void info::display()
if (head == NULL)
else
temp = head;
<< temp->prn << " " << temp->rollno << " " << temp->name;
temp = temp->next;
cout << " " << temp->prn << " " << temp->rollno << " " << temp->name;
void info::delm()
int m, f = 0;
cout << "\n enter the prn no. of student whose data you want to delete";
cin >> m;
temp = head;
if (temp->prn == m)
s->next = temp->next;
delete (temp);
f = 1;
s = temp;
temp = temp->next;
if (f == 0)
}
}
void info::delp()
temp = head;
head = head->next;
delete (temp);
void info::dels()
temp = head;
s = temp;
temp = temp->next;
s->next = NULL;
delete (temp);
void info::count()
temp = head;
ct = 0;
temp = temp->next;
ct++;
ct++;
}
void info::reverse()
rev(head);
if (temp == NULL)
return;
else
rev(temp->next);
cout << " " << temp->prn << " " << temp->rollno << " " << temp->name;
void info::concat()
int k, j;
cin >> k;
head = NULL;
insertm();
head1 = head;
head = NULL;
cin >> j;
insertm();
head2 = head;
head = NULL;
temp1 = head1;
temp1 = temp1->next;
temp1->next = head2;
temp2 = head1;
cout << "\n " << temp2->prn << "temp2 = temp2->next;" << temp2->rollno << "" << temp2-
>name << "\n ";
cout << "\n " << temp2->prn << " " << temp2->rollno << " " << temp2->name << "\n";
int main()
info s;
int i;
char ch;
do
cin >> i;
switch (i)
case 1:
s.insertp();
break;
case 2:
s.insertm();
break;
case 3:
s.insertm();
break;
case 4:
s.delp();
break;
case 5:
s.delm();
break;
case 6:
s.dels();
break;
case 7:
s.display();
break;
case 8:
s.count();
break;
case 9:
s.reverse();
break;
case 10:
s.concat();
break;
default:
return 0;
3. Implement Stack using a linked list. Use this stack to perform evaluation of a postfix
expression#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
struct node
int data;
ptr->data = data;
ptr->next = NULL;
return ptr;
if (ptr == NULL)
exit(EXIT_FAILURE);
if (top == NULL)
top = ptr;
return;
ptr->next = top;
top = ptr;
}
int pop()
int data;
if (top == NULL)
return -1;
data = top->data;
temp = top;
top = top->next;
free(temp);
return data;
int main()
char str[100];
if (isdigit(str[i]))
if (data != -1)
push(data);
operand2 = pop();
operand1 = pop();
break;
switch (str[i])
case '+':
push(result);
break;
case '-':
push(result);
break;
case '*':
push(result);
break;
case '/':
break;
data = -1;
else
return 0;
4. Queues are frequently used in computer programming, and a typical example is the creation of a
job queue by an operating system. If the operating system does not use priorities, then the jobs are
processed in the order they enter the system. Write C++ program for simulating job queue. Write
functions to add job and delete job from queue
#include<iostream>
#define MAX 10
struct queue
int data[MAX];
};
class Queue
struct queue q;
public:
int isempty();
int isfull();
void enqueue(int);
int delqueue();
void display();
};
int Queue::isempty()
int Queue::isfull()
void Queue::enqueue(int x)
q.data[++q.rear] = x;
int Queue::delqueue()
return q.data[++q.front];
void Queue::display()
int i;
int main()
Queue obj;
int ch, x;
do
cout << "\n 1. insert job\n 2.delete job\n 3.display\n 4.Exit\n Enter your choice:";
switch (ch)
case 1:
if (!obj.isfull())
cin >> x;
obj.enqueue(x);
else
break;
case 2:
if (!obj.isempty())
else
obj.display();
break;
case 3:
if (!obj.isempty())
obj.display();
else
break;
case 4:
return 0;
5. A double-ended queue(deque) is a linear list in which additions and deletions may be made at
either end. Obtain a data representation mapping a deque into a one-dimensional array. Write C++
program to simulate deque with functions to add and delete elements from either end of the deque.
#include<iostream>
#define SIZE 5
class dequeue
public:
dequeue();
void add_at_beg(int);
void add_at_end(int);
void delete_fr_front();
void delete_fr_rear();
void display();
};
dequeue::dequeue()
front = -1;
rear = -1;
if (front == -1)
front++;
rear++;
a[rear] = item;
else
a[i + 1] = a[i];
}
a[front] = item;
rear++;
if (front == -1)
front++;
rear++;
a[rear] = item;
else
a[++rear] = item;
void dequeue::display()
if (front == -1)
return;
}
for (int i = front; i <= rear; i++)
void dequeue::delete_fr_front()
if (front == -1)
return;
if (front == rear)
else
front = front + 1;
void dequeue::delete_fr_rear()
if (front == -1)
{
cout << "Deletion is not possible: Dequeue is empty";
return;
if (front == rear)
else
rear = rear - 1;
int main()
int c, item;
dequeue d1;
do
switch (c)
case 1:
d1.add_at_beg(item);
break;
case 2:
d1.add_at_end(item);
break;
case 3:
d1.display();
break;
case 4:
d1.delete_fr_front();
break;
case 5:
d1.delete_fr_rear();
break;
case 6:
break;
default:
break;
} while (c != 6);
return 0;
6. A book consists of chapters, chapters consist of sections and sections consist of subsections.
Construct a tree and print the nodes. Find the time and space requirements of your method.
#include<iostream>
#include<cstdlib>
#include<string.h>
/*
* Node Declaration
*/
struct node
char label[10];
int ch_count;
} *root;
/*
* Class Declaration
*/
class BST
BST()
root = NULL;
}
};
void BST::create_tree()
root->ch_count = tchapters;
// cin>>r1->child[i]->ch_count;
int i, j, k, tchapters;
if (r1 != NULL)
cout<<" "<<r1->child[i]->label;
// cin>>r1->child[i]->child[j]->label;
/*
*/
int main()
int choice;
BST bst;
while (1)
switch (choice)
case 1:
bst.create_tree();
case 2:
bst.display(root);
break;
case 3:
exit(1);
default:
7. Beginning with an empty binary search tree, Construct binary search tree by inserting the values
in the order given. After constructing a binary tree - i. Insert new node ii. Find number of nodes in
longest path iii. Minimum data value found in the tree iv. Change a tree so that the roles of the left
and right pointers are swapped at every node v. Search a value
#include<iostream>
struct node
int data;
node *L;
node *R;
};
node *root, *temp;
class bst
public:
void create();
bst()
root = NULL;
count = 0;
};
void bst::create()
char ans;
do
temp->L = NULL;
temp->R = NULL;
if (root == NULL)
{
root = temp;
else
insert(root, temp);
cout << "Do you want to insert more value : " << endl;
count++;
if (root->R == NULL)
root->R = temp;
else
insert(root->R, temp);
else
if (root->L == NULL)
root->L = temp;
else
insert(root->L, temp);
}
if (root != NULL)
disin(root
->L);
->data
count++;
if (root != NULL)
->data
dispre(root
->L);
dispre(root
->R);
if (root != NULL)
{
dispost(root
->L);
dispost(root
->R);
int flag = 0;
temp = root;
if (key == temp->data)
flag = 1;
break;
temp = temp->R;
else
{
temp = temp->L;
if (flag == 0)
if (root == NULL)
return 0;
return 0;
hr = height(root->R);
hl = height(root->L);
return (1 + hr);
else
return (1 + hl);
}
void bst::min(node *root)
temp = root;
temp = temp->L;
temp = root;
if (root != NULL)
mirror(root->L);
mirror(root->R);
temp = root->L;
root->L = root->R;
root->R = temp;
int main()
bst t;
int ch;
char ans;
do
cout<<"\n1) Insert new node 2)number of nodes in longest path 3) minimum 4) mirror 5) search
6) inorder 7) preorder 8) postorder"<<endl;
cin>>ch;
switch (ch)
case 1:
t.create();
break;
case 2:
break;
case 3:
t.min(root);
break;
case 4:
t.mirror(root);
t.disin(root);
break;
case 5:
t.search(root, key);
break;
case 6:
t.disin(root);
break;
case 7:
t.dispre(root);
break;
case 8:
break;
return 0;
8. Implement graph using adjacency list or matrix and perform DFS or BFS.
#include<bits/stdc++.h>
class Graph
// Number of vertex
int v;
// Number of edges
int e;
// Adjacency matrix
int **adj;
public:
Graph::Graph(int v, int e)
this->v = v;
this->e = e;
adj[row][column] = 0;
adj[start][e] = 1;
adj[e][start] = 1;
vector<bool>visited(v,false);
vector<int>q;
q.push_back(start);
visited[start] = true;
int vis;
while (!q.empty())
vis = q[0];
q.erase(q.begin());
q.push_back(i);
// Set
visited[i] = true;
// Driver code
int main()
int v = 5, e = 4;
G.addEdge(0, 2);
G.addEdge(1, 3);
G.BFS(0);