Program:: #Include Using Namespace STD
Program:: #Include Using Namespace STD
Assume that a singly linked list is implemented with a header node, but
no tail node, and that it maintains only a pointer to the header node.
Write a class that includes methods to:
a) return the size of the linked list
b) print the linked list
c) test if a value x is contained in the linked list
d) add a value x if it is not already contained in the linked list
e) Remove a value x if it is contained in the linked list.
Program:
#include <iostream>
using namespace std;
struct node {
int data;
struct node *nxt;
};
class LL {
private:
node * h = NULL;
public:
int s()
node *tmp = h;
int con = 0;
while (tmp) {
con++;
tmp = tmp->nxt;
}
return con;
void print()
{
node *tmp = h;
while (tmp) {
tmp = tmp->nxt;
}
cout << "\n";
node *tmp = h;
while (tmp) {
if (tmp->data == data) {
return true;
tmp = tmp->nxt;
}
return false;
tmp->data = data;
tmp->nxt = h;
h = tmp;
}
void remove(int data)
{
node *tmp1 = h;
if (h->data == data) {
h = h->nxt;
delete tmp1;
else {
tmp1 = tmp1->nxt;
if (!tmp1) {
tmp2->nxt = tmp1->nxt;
delete tmp1;
}
};
int main() {
LL li;
li.add(i);
}
li.print();
if (li.contained(9)) {
else {
li.remove(9);
cout << " AFTER DELETING 9:\n";
li.print();
return 0;
Output:
Q#2:
Write a C++ program which will build and implemented all the
operations of Queue array wise, use appropriate classes, data members and
member functions for this task.
Operations of Queue should be:
• initializeQueue—Initializes the queue to an empty state.
• isEmptyQueue—Determines whether the queue is empty. If the
queue is empty, it returns the value true; otherwise, it returns the value
false.
• isFullQueue—Determines whether the queue is full. If the queue is
full, it returns the value true; otherwise, it returns the value false.
• front—Returns the front, that is, the first element of the queue.
Prior to this operation, the queue must exist and must not be empty.
• back—Returns the last element of the queue. Prior to this
operation, the queue must exist and must not be empty.
• addQueue—Adds a new element to the rear of the queue. Prior to
this operation, the queue must exist and must not be full.
• deleteQueue—Removes the front element from the queue. Prior to
this operation, the queue must exist and must not be empty.
Program:
struct Queue {
int f, r, tp;
int* queue;
Queue(int t)
{
f = r = 0;
tp = t;
queue = new int;
}
if (tp == r) {
cout<<"\n QUEUE IS FULL"<<endl;
return;
}
else {
queue[r] = data;
r++;
}
return;
}
void Dequeue()
{
if (f == r) {
cout<<"\n QUEUE IS EMPTY"<<endl;
return;
}
else {
for (int i = 0; i < r - 1; i++) {
queue[i] = queue[i + 1];
}
r--;
}
return;
}
void Display()
{
int i;
if (f == r) {
cout<<"\n QUEUE IS EMPTY"<<endl;
return;
}
int main()
{
Queue q(4);
q.Display();
q.Enqueue(2);
q.Enqueue(3);
q.Enqueue(4);
q.Enqueue(5);
q.Display();
q.Enqueue(6);
q.Display();
q.Dequeue();
q.Dequeue();
q.Front();
return 0;
}
Output: