0% found this document useful (0 votes)
113 views

Program:: #Include Using Namespace STD

The second document asks to write a C++ program to implement a queue using an array. It lists the required operations as initializing the queue, checking if it is empty or full, accessing the front or back, adding to the rear, and removing from the front. The program
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views

Program:: #Include Using Namespace STD

The second document asks to write a C++ program to implement a queue using an array. It lists the required operations as initializing the queue, checking if it is empty or full, accessing the front or back, adding to the rear, and removing from the front. The program
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Q#1:

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;

cout << "****Elements : ";

while (tmp) {

cout << tmp->data << " ";

tmp = tmp->nxt;

}
cout << "\n";

bool contained(int data)


{

node *tmp = h;

while (tmp) {

if (tmp->data == data) {

return true;

tmp = tmp->nxt;
}

return false;

void add(int data)


{

node *tmp = new node;

tmp->data = data;

tmp->nxt = h;

h = tmp;

}
void remove(int data)
{

node *tmp1 = h;

node *tmp2 = tmp1;

if (h->data == data) {

h = h->nxt;

delete tmp1;

else {

while (tmp1->data != data || tmp1 != NULL) {


temp2 = temp1;

tmp1 = tmp1->nxt;

if (!tmp1) {

tmp2->nxt = tmp1->nxt;

delete tmp1;

}
};

int main() {

LL li;

for (int i = 0; i < 10; ++i) {

li.add(i);
}

cout << "SIZE: " << li.s() << "\n";

li.print();

if (li.contained(9)) {

cout << "9 IS PRESENT \n";

else {

cout << "9 IS NOT PRESENT \n";

li.remove(9);
cout << " AFTER DELETING 9:\n";

li.print();

cout << "NEW SIZE : " << li.s() << "\n";

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:

#include <iostream >


using namespace std;

struct Queue {
int f, r, tp;
int* queue;
Queue(int t)
{
f = r = 0;
tp = t;
queue = new int;
}

~Queue() { delete[] queue; }

void Enqueue(int data)


{

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

for (i = f; i < r; i++) {


cout<<" <-- "<< queue[i];
}
return;
}
void Front()
{
if (f == r) {
cout<<"\n QUEUE IS EMPTY"<<endl;
return;
}
cout<<"\n FRONT ELEMENT :" << queue[f];
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();

cout<<"\n AFTER DELETION"<<endl;


q.Display();

q.Front();

return 0;
}

Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy