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

Code Commandos #287

Open
wants to merge 30 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
2 changes: 1 addition & 1 deletion c/p1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@ int main(){
cout << "target: ";
cin >> t;

checkSum(a, n, t);
checkSum(a, n, t);
}
124 changes: 59 additions & 65 deletions c/p10.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@

#include<bits/stdc++.h>
using namespace std;
struct node{ //defining the structure of the node

struct node {
int data;
struct node *next;
};
//class definition for circular linked list
class CLL{

class CLL {
private:
struct node *tail;
public:
CLL(){
this->tail=NULL; //constructor to initialize the tail pointer
this->tail=NULL;
}
void insert(int,bool);
void deletenode(); //member function
void deletenode();
void display();
~CLL(){ //destructor to delete the list
~CLL(){
struct node *ptr=tail->next;
while(ptr!=tail){
struct node *temp=ptr;
Expand All @@ -27,110 +28,103 @@ class CLL{
delete ptr;
}
};
//member function definitions
void CLL::insert(int a,bool x)
{
struct node *temp=new struct node;
temp->data=a;
temp->next=nullptr;
if(tail==nullptr){
tail=temp;
tail->next=temp;

void CLL::insert(int a, bool x) {
struct node *temp = new struct node;
temp->data = a;
temp->next = nullptr;
if (tail == nullptr) {
tail = temp;
tail->next = temp;
return;
}
temp->next=tail->next;
tail->next=temp;
if(x){
tail=temp;
temp->next = tail->next;
tail->next = temp;
if (x) {
tail = temp;
}
}

//delete function to delete at end
void CLL::deletenode()
{
if(tail==nullptr)
{
void CLL::deletenode() {
if (tail == nullptr) {
cout << "Invalid, List is already empty" << endl;
return;
}
if(tail->next==tail)
{
struct node *ptr=tail;
tail=nullptr;
if (tail->next == tail) {
struct node *ptr = tail;
tail = nullptr;
delete ptr;
return;
}
struct node *temp=tail;
struct node *ptr=tail->next;
while(ptr->next!=tail)
ptr=ptr->next;
ptr->next=tail->next;
tail=ptr;
struct node *temp = tail;
struct node *ptr = tail->next;
while (ptr->next != tail)
ptr = ptr->next;
ptr->next = tail->next;
tail = ptr;
delete temp;
return;
}

//display function to display the list
void CLL::display()
{
if (tail==nullptr)
{
void CLL::display() {
if (tail == nullptr) {
cout << "List is empty" << endl;
return;
}
struct node *ptr=tail->next;
while(ptr->next!=tail->next)
{
struct node *ptr = tail->next;
while (ptr->next != tail->next) {
cout << ptr->data << " -> ";
ptr=ptr->next;
ptr = ptr->next;
}
cout << ptr->data << endl;
}

int main(){
int choice=1;CLL list1;
//menu driven program
do{
int choice=1;
CLL list1;
do {
cout << "1.Insert"<<endl;
cout << "2.Delete"<<endl;
cout << "3.Display"<<endl;
cout << "4.Exit the menu"<<endl;
cout << "Enter your choice: ";
cin>> choice;
int a,b;bool x;
switch(choice)
{
cin >> choice;
int a;
bool x;
switch (choice) {
case 1:{
cout << "Enter the element to be inserted: ";
cin>>a;
cin >> a;
cout << "0. Insert the element at beginning"<<endl;
cout << "1. Insert the element at end"<<endl;
cout << "Choice: ";
cin>>choice;
if(choice==0 || choice==1) //inserting at beginning or end
{
x=choice;
list1.insert(a,x);
choice=-1;
cin >> choice;
if (choice == 0 || choice == 1) {
x = (choice == 1); // Convert choice to boolean
list1.insert(a, x);
choice = -1;
}
else{
else {
cout << "Invalid choice" << endl;
}
break;
}
case 2:{ //deleting at end
case 2:{
list1.deletenode();
break;
}
case 3:{ //displaying the list
cout << "List"<<endl;
case 3:{
cout << "List" << endl;
list1.display();
break;
}
case 4:{ //exiting the menu
cout << "Exiting the menu..."<<endl;
case 4:{
cout << "Exiting the menu..." << endl;
}
default: cout << "Invalid choice"<<endl; break;
default:
cout << "Invalid choice" << endl;
break;
}
}while(choice!=4);
} while(choice != 4);
}

5 changes: 2 additions & 3 deletions c/p11.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//program to check if two binary trees are the same, given the roots

#include <stdio.h>
//Definition for a binary tree node.
struct TreeNode {
int val;
struct TreeNode *left;
Expand All @@ -12,8 +11,8 @@ int isSameTree(struct TreeNode* p, struct TreeNode* q){
if(p == NULL && q == NULL){
return 1;
}
if(p->val == q->val){
return isSameTree(p->left, q->left) || isSameTree(p->right, q->right);
if(p != NULL && q != NULL && p->val == q->val){
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
}
return 0;
}
70 changes: 36 additions & 34 deletions c/p12.cpp
Original file line number Diff line number Diff line change
@@ -1,49 +1,51 @@
//program to find the longest common subsequence (LCS) in two strings
//e.g. ABCDE and AFCZE have a common subsequence of ACE

#include <bits/stdc++.h>
#include <iostream>
using namespace std;

void LCS(string a, string b, int m, int n) {
int dp[m + 1][n + 1];

for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++) {
if (i == 0 || j == 0)
dp[i][j] = 0;
else if (a[i - 1] == b[j - 1])
dp[i][j] = dp[i - 1][j - 1] + 1;
else
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
int dp[m + 1][n + 1];

for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++) {
if (i == 0 || j == 0)
dp[i][j] = 0;
else if (a[i - 1] == b[j - 1])
dp[i][j] = dp[i - 1][j - 1] + 1;
else
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
}

cout << dp[m][n] << endl; //length of LCS

int index = dp[m][n];
char lcs[index + 1];
lcs[index] = '\0';

int i = m, j = n;
while (i > 0 && j > 0) {
if (a[i - 1] == b[j - 1]) {
lcs[index - 1] = a[i - 1];
i--;
j--;
cout << dp[m][n] << endl;

int index = dp[m][n];
char lcs[index + 1];
lcs[index] = '\0';

int i = m, j = n;
while (i > 0 && j > 0) {
if (a[i - 1] == b[j - 1]) {
lcs[index - 1] = a[i - 1];
i--;
j--;
index--;
} else if (dp[i - 1][j] > dp[i][j - 1])
i--;
else
j--;
}
else if (dp[i - 1][j] > dp[i][j - 1]) i--;
else j--;
index--;
}

cout << "LCS: " << lcs << endl;
cout << "LCS: " << lcs << endl;
}

int main() {
string a = "ABFCDE";
string b = "AFCZE";
int m = a.length();
int n = b.length();
string a = "ABFCDE";
string b = "AFCZE";
int m = a.length();
int n = b.length();

LCS(a, b, m, n);
LCS(a, b, m, n);
    return 0;
}
8 changes: 3 additions & 5 deletions c/p13.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@
#include <stdio.h>
#include <stdlib.h>

int i = 0;

int main(){
int i = i;
int a[] = {1, 2, 3, 4, 5, 6};
int b[] = {7, 8, 9, 10, 11, 12, 13};

int n = sizeof(a)/sizeof(a[0]);
int m = sizeof(b)/sizeof(b[0]);

for(int i=0;i<n;i++);{
for(int i=0; i<n; i++) {
printf("%d ", a[i]);
}
printf("\n");
for(i;i<m;i++){
for(int i=0; i<m; i++) {
printf("%d ", b[i]);
}
    return 0;
}
19 changes: 13 additions & 6 deletions c/p14.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
#include<string.h>

int romanToInt(char * s){
int *a = (int *)malloc(strlen(s) * sizeof(int));
for(int i=0;i<strlen(s);i++){
int *a = (int *)malloc((strlen(s) + 1) * sizeof(int));

for(int i = 0; i < strlen(s); i++) {
switch(s[i]){
case 'I': a[i] = 1; break;
case 'V': a[i] = 5; break;
Expand All @@ -17,18 +18,24 @@ int romanToInt(char * s){
case 'M': a[i] = 1000; break;
}
}
int num=0;
for(int i=0;i<strlen(s)-1;i++){
if(a[i] < a[i+1]){
a[i+1] -= a[i];

int num = 0;

for(int i = 0; i < strlen(s) - 1; i++) {
if(a[i] < a[i + 1]) {
a[i + 1] -= a[i];
}
num += a[i];
}
num += a[strlen(s) - 1];

free(a);

return num;
}

int main() {
char roman[10] = "MMXXIV";
printf("%d", romanToInt(roman));
    return 0;
}
Loading