0% found this document useful (0 votes)
5 views14 pages

Static Q Unit 3

The document contains multiple static implementations of data structures including stacks, queues, and deques using both arrays and linked lists. Each implementation provides functions for basic operations such as push, pop, enqueue, dequeue, and display. Additionally, there are examples of task management using priority and deadline, as well as memory management for dynamic structures.

Uploaded by

muzatanatswa037
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views14 pages

Static Q Unit 3

The document contains multiple static implementations of data structures including stacks, queues, and deques using both arrays and linked lists. Each implementation provides functions for basic operations such as push, pop, enqueue, dequeue, and display. Additionally, there are examples of task management using priority and deadline, as well as memory management for dynamic structures.

Uploaded by

muzatanatswa037
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

// Static 1

void push(char value) {

if (isFull()) {

cout << "Stack is full" << endl;

} else {

top++;

items[top] = value;

cout << "Pushed: " << value << endl;

void pop() {

if (isEmpty()) {

cout << "Stack is empty. Nothing to pop." << endl;

} else {

cout << "Popped: " << items[top] << endl;

top--;

void display() {

if (isEmpty()) {

cout << "Stack is empty." << endl;

} else {

cout << "Stack elements: ";

for (int i = top; i >= 0; i--) {

cout << items[i] << " ";

cout << endl;

// static 2

#include <iostream>
using namespace std;

struct Node {

int data;

Node* next;

};

Node* top = NULL; // top of stack

void push(int value) {

Node* newNode = new Node();

newNode->data = value;

newNode->next = top;

top = newNode;

cout << value << " is pushed onto the stack" << endl;

void pop() {

if (top == NULL) {

cout << "Stack Underflow" << endl;

return;

cout << top->data << " is popped from the stack" << endl;

Node* temp = top;

top = top->next;

delete temp;

void displayStack() {

if (top == NULL) {

cout << "Stack is empty" << endl;


return;

cout << "Elements in the stack: ";

Node* temp = top;

while (temp != NULL) {

cout << temp->data << " ";

temp = temp->next;

cout << endl;

// clearing memory (optional based on given main function)

void initStack() {

Node* temp;

while (top != NULL) {

temp = top;

top = top->next;

delete temp;

int main() {

int choice, value;

do {

cin >> choice;

switch (choice) {

case 1:

cin >> value;

push(value);

break;

case 2:
pop();

break;

case 3:

displayStack();

break;

case 4:

cout << "Exiting the program" << endl;

initStack();

break;

default:

cout << "Invalid choice" << endl;

} while (choice != 4);

return 0;

//static 3

void initializeQueue(){

front=-1;

rear=-1;

int addCustomer(int id){

if(rear==MAX_CUSTOMERS -1)

return 0;

if(front==-1)

front=0;

customerIDs[++rear]=id;

return 1;

int processCustomer(){

if(front==-1 || front>rear)
return -1;

int id=customerIDs[front++];

if(front>rear)

front=rear=-1;

return id;

void displayQueue(){

if(front==-1 || front>rear){

cout<<"Checkout queue is empty."<<endl;

return;

cout<<"Customers waiting in the checkout queue: ";

for(int i=front; i<=rear; i++){

cout<<customerIDs[i]<<" ";

cout<<endl;

// Static 4
void enqueue(int value){

Node* newNode=new Node();

newNode->data=value;

newNode->next=nullptr;

if(front==nullptr){

front=rear=newNode;

}else{

rear->next=newNode;

rear=newNode;

void dequeue(){
if(front==nullptr)

return;

Node* temp=front;

front=front->next;

delete temp;

if(front==nullptr)

rear=nullptr;

void printFrontRear(){

cout<<"Front:"<<front->data<<", Rear:"<<rear->data;

//Static 5
#include<iostream>

#include<algorithm>

#include<string>

using namespace std;

struct Task{

int priority;

int deadline;

string name;

};

bool cmp(const Task &a, const Task &b){

if([Link]==[Link])

return [Link]<[Link];

return [Link]>[Link];

int main(){

int n;
cin>>n;

Task *arr=new Task[n];

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

cin>>arr[i].priority>>arr[i].deadline>>arr[i].name;

sort(arr, arr+n, cmp);

cout<<"Executed Tasks:"<<endl;

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

cout<<arr[i].name<<endl;

delete[]arr;

return 0;

//Static question 6
#include <iostream>

using namespace std;

struct Node {

int data;

Node* next;

Node* prev;

};

Node* frontNode = NULL;

Node* rearNode = NULL;

void insertFront(int x) {

Node* newNode = new Node();


newNode->data = x;

newNode->prev = NULL;

newNode->next = frontNode;

if (frontNode == NULL)

rearNode = newNode;

else

frontNode->prev = newNode;

frontNode = newNode;

void insertRear(int x) {

Node* newNode = new Node();

newNode->data = x;

newNode->next = NULL;

newNode->prev = rearNode;

if (rearNode == NULL)

frontNode = newNode;

else

rearNode->next = newNode;

rearNode = newNode;

void deleteFront() {

if (frontNode == NULL) return;

Node* temp = frontNode;

frontNode = frontNode->next;

if (frontNode == NULL)
rearNode = NULL;

else

frontNode->prev = NULL;

delete temp;

void deleteRear() {

if (rearNode == NULL) return;

Node* temp = rearNode;

rearNode = rearNode->prev;

if (rearNode == NULL)

frontNode = NULL;

else

rearNode->next = NULL;

delete temp;

void display() {

Node* temp = frontNode;

while (temp != NULL) {

cout << temp->data << " ";

temp = temp->next;

int main() {

int n;

cin >> n;

int choice, value;


// Perform operations in same input order

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

cin >> choice >> value;

if (choice == 1)

insertFront(value);

else

insertRear(value);

cout << "Original Deque: ";

display();

cout << endl;

deleteFront();

deleteRear();

cout << "Deque after removing front and rear elements: ";

display();

cout << endl;

int frontVal, rearVal;

cin >> frontVal >> rearVal;

insertFront(frontVal);

insertRear(rearVal);

cout << "Deque after adding new front and rear elements: ";

display();

cout << endl;

return 0;

}
//STATIC 7
struct Node{

int data;

Node* prev;

Node* next;

};

class Deque{

private:

Node* frontNode;

Node* rearNode;

int sizeDeque;

int capacity;

public:

Deque(int cap){

capacity=cap;

sizeDeque=0;

frontNode=NULL;

rearNode=NULL;

void insertFront(int x){

if(sizeDeque==capacity){

cout<<"Overflow\n";

return;

Node* newNode= new Node();

newNode->data=x;

newNode->prev=NULL;

newNode->next=frontNode;

if(frontNode!=NULL)

frontNode->prev=newNode;
else

rearNode=newNode;

frontNode=newNode;

sizeDeque++;

cout<<x<<"\n";

void insertRear(int x){

if(sizeDeque==capacity){

cout<<"Overflow\n";

return;

Node* newNode= new Node();

newNode->data=x;

newNode->next=NULL;

newNode->prev=rearNode;

if(rearNode!=NULL)

rearNode->next=newNode;

else

frontNode=newNode;

rearNode=newNode;

sizeDeque++;

cout<<x<<"\n";

void deleteFront(){

if(sizeDeque==0){

cout<<"Underflow\n";

return;

Node* temp=frontNode;

frontNode=frontNode->next;

if(frontNode!=NULL)
frontNode->prev=NULL;

else

rearNode=NULL;

delete temp;

sizeDeque--;

void deleteRear(){

if(sizeDeque==0){

cout<<"Underflow\n";

return;

Node* temp=rearNode;

rearNode=rearNode->prev;

if(rearNode!=NULL)

rearNode->next=NULL;

else

frontNode=NULL;

delete temp;

sizeDeque--;

void getFront(){

if(sizeDeque==0){

cout<<"Queue is empty\n";

return;

cout<<frontNode->data<<"\n";

void getRear(){

if(sizeDeque==0){

cout<<"Queue is empty\n";

return;
}

cout<<rearNode->data<<"\n";

void printDeque(){

if(sizeDeque==0){

cout<<"Queue is empty\n";

return;

Node* temp=frontNode;

while(temp!=NULL){

cout<<temp->data;

if(temp->next!=NULL)

cout<<" ";

temp=temp->next;

cout<<"\n";

};

You might also like