Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Implement the following functions and show their working.

1. void Push(element) – pushes an element on the top of stack


2. element Pop() – removes and display the element on the top of stack
3. bool isEmpty() – checks if the stack is empty or not
4. bool isFull() – checks if the stack is full or not
5. void Clear() – release the memory allocated by stack
6. void Top() – display the contents of top element of stack

/* lab 5(a) submitted by ***Isra Imran


************Warda tu Zahra*/
#include <bits/stdc++.h>

using namespace std;

#define MAX 5

class Stack {
int top;
int *a;
public:
// Maximum size of Stack

Stack() { top = -1;


a= new int[MAX];}
~Stack(){ // Destructor to free memory allocated to the stack
delete[] a;
}
bool push(int x);
int pop();
int Top();
bool isEmpty();
bool isFull();
void clear();
};
bool Stack::push(int x)
{
if (top >= (MAX - 1)) {
cout << "Stack Overflow\n";
return false;
}
else {
a[++top] = x;
cout << x << " pushed into stack\n";
return true;
}
}
void Stack::clear(){
delete a;
top=-1;
//a=new int[MAX];
}

int Stack::pop()
{
if (top < 0) {
cout << "Stack Underflow";
return 0;
}
else {
int x = a[top--];
return x;
}
}
int Stack::Top()
{
if (top < 0) {
cout << "Stack is Empty\n";
return 0;
}
else {
int x = a[top];
return x;
}
}

bool Stack::isEmpty()
{
return (top < 0);
}

bool Stack::isFull()
{
return (top >= MAX-1);
}

// Driver program to test above functions


int main()
{
Stack s;
int choose;int no;
while(1){
cout<<"enter 1 to push\n---enter 2 to pop\n--enter 3 to see if stack is empty-\n-enter 4 to see if stack
is ful1-\n-0 to exit-\n-5 to check topmost element of stack\n --6 to clear stack";
cin>>choose;
if(choose==0){
return 0;
}
if (choose==1){
cout<<"enter no to push";
cin>>no;
s.push(no);
}
if (choose==2){
cout <<s.pop() << " Popped from stack\n";
}
if (choose==3){
if (s.isEmpty())
cout<<"stack is empty\n";
else
cout<<"stack is not empty\n";
}
if (choose==4){
if (s.isFull())
cout<<"stack is full\n";
else
cout<<"stack is not full\n";
}
if (choose==5){
cout <<s.Top() << " is topmost element of stack\n";
}
if (choose==6){
s.clear();
cout << "Stack cleared.\n";
s.Top();

}
return 0;
}
Implement the Josephus problem as follows.

1. Decide the number of participants ‘n’ and generate a doubly linked list.
2. Modify the Doubly linked list to circular linked list by pointing the first
element of the list by
the last element.
3. Choose the number of participants ‘k’ to be skipped.
4. Pick a starting point and remove every (k+1) th participant.
5. Continue until a single participant remains. That’s the leader.

//SUBMITTED BY: ISRA IMRAN


/////// WARDA TU ZAHRA

#include <bits/stdc++.h>

using namespace std;


typedef struct Node{
int data;
struct Node *next, *prev;
}node;

node *createDCLL(int n)
{
node *head = NULL;
node *ptr = head, *ptr_prev = head;

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


if(i == 0){
head = (node*)malloc(sizeof(node));
head->data = i+1;
head->next = head->prev = head;
ptr_prev = head;
}
else{
ptr = (node*)malloc(sizeof(node));
ptr->data = i+1;
ptr_prev->next = ptr;
ptr->prev = ptr_prev;
ptr_prev = ptr;
}
}
ptr->next = head;
head->prev = ptr;
head=ptr;
return head;
}

void display(node *head)


{
node *ptr = head;
cout<<" Components of list 1 is :"<<endl;

do {
cout<<ptr->data <<" ";
ptr = ptr->next;
} while(ptr != head);

cout<<endl;
}

int josephus(node *head, int k)


{

node *ptr = head;


if(ptr->next == head)
return ptr->data;
else{
node *todel=ptr, *todel_prev = todel->prev;
for(int i=0; i< k+1 ; i++){
todel_prev = todel;
todel = todel->next;
}
node *new_head = todel->next;
new_head->prev = todel_prev;
todel_prev->next = new_head;
cout<<" remove : "<< todel->data<<endl;
free(todel);
return josephus(new_head, k);}
}

int main()
{
node *leader = NULL;
int n, k;

cout<<"Enter number of participants: ";


cin>> n;
leader = createDCLL(n);
// display(head);
// display_rev(head->prev);

cout<<"Enter k : ";
cin>> k;
display(leader);
cout<<"leader is : " <<josephus(leader, k);
return 0;
}

You might also like