Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add files via upload #20

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions advance_recursion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@



#include<iostream>
using namespace std;

int knapsack(int value[],int wt[],int n,int w)
{
if(n==0 || w==0){
return 0;
}
if(wt[n-1]>w){
return knapsack(value,wt,n-1,w);
}
return max(knapsack(value,wt,n-1,w-wt[n-1])+value[n-1], knapsack(value,wt,n-1,w));
}

int main()
{
int value[]={100,50,150};
int wt[]={10,20,30};
int w = 50;
cout<<knapsack(value,wt,3,w);
return 0;
}
118 changes: 118 additions & 0 deletions find m in stack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@

/* C++ Program to implement a stack
that supports findMiddle() and
deleteMiddle in O(1) time */
#include <bits/stdc++.h>
using namespace std;

class myStack {
struct Node {
int num;
Node* next;
Node* prev;

Node(int num) { this->num = num; }
};

// Members of stack
Node* head = NULL;
Node* mid = NULL;
int size = 0;

public:
void push(int data)
{
Node* temp = new Node(data);
if (size == 0) {
head = temp;
mid = temp;
size++;
return;
}

head->next = temp;
temp->prev = head;

// update the pointers
head = head->next;
if (size % 2 == 1) {
mid = mid->next;
}
size++;
}

int pop()
{
int data=-1;
if (size != 0) {
data=head->num;
if (size == 1) {
head = NULL;
mid = NULL;
}
else {
head = head->prev;
head->next = NULL;
if (size % 2 == 0) {
mid = mid->prev;
}
}
size--;
}
return data;
}

int findMiddle()
{
if (size == 0) {
return -1;
}
return mid->num;
}

void deleteMiddle()
{
if (size != 0) {
if (size == 1) {
head = NULL;
mid = NULL;
}
else if (size == 2) {
head = head->prev;
mid = mid->prev;
head->next = NULL;
}
else {
mid->next->prev = mid->prev;
mid->prev->next = mid->next;
if (size % 2 == 0) {
mid = mid->prev;
}
else {
mid = mid->next;
}
}
size--;
}
}
};

int main()
{
myStack st;
st.push(11);
st.push(22);
st.push(33);
st.push(44);
st.push(55);
st.push(66);
st.push(77);
st.push(88);
st.push(99);
cout <<"Popped : "<< st.pop() << endl;
cout <<"Popped : "<< st.pop() << endl;
cout <<"Middle Element : "<< st.findMiddle() << endl;
st.deleteMiddle();
cout <<"New Middle Element : "<< st.findMiddle() << endl;
return 0;
}