0% found this document useful (0 votes)
13 views6 pages

C++ Factorial and Doubly Linked List

Uploaded by

k230534
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)
13 views6 pages

C++ Factorial and Doubly Linked List

Uploaded by

k230534
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

#include <iostream>

using namespace std;

int factorial(int num) {

if (num <= 1) return 1;

return num * factorial(num - 1);

void printCombinations(int k, int n, int current[], int index, int start) {

if (index == k) {

cout << "(";

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

cout << current[i];

if (i < k - 1) cout << ", ";

cout << ")" << endl;

return;

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

current[index] = i;

printCombinations(k, n, current, index + 1, i + 1);

void combination(int k, int n) {

if (k > n) {
cout << "Invalid input: k cannot be greater than n." << endl;

return;

int current[100];

printCombinations(k, n, current, 0, 1);

int main() {

int k = 2; // Length of combination

int n = 3; // Numbers from 1 to n

combination(k, n);

return 0;

Queston 2:

#include <iostream>

using namespace std;

class Node {

public:
int Value;

Node *Next;

Node *Prev;

Node(int Value) {

this->Value = Value;

Next = NULL;

Prev = NULL;

};

class DoublyLL {

public:

Node *Head;

Node *Tail;

DoublyLL() {

Head = NULL;

Tail = NULL;

void AddToTail(int Value) {

Node *newNode = new Node(Value);

if (Head == NULL) {

Head = newNode;

Tail = newNode;

} else {
Tail->Next = newNode;

newNode->Prev = Tail;

Tail = newNode;

cout << Value << " has been added!" << endl;

void DeleteFromFront() {

if (Head == NULL) {

cout << "No Node in LL." << endl;

return;

Node *temp = Head;

Head = Head->Next;

if (Head != NULL) {

Head->Prev = NULL;

} else {

Tail = NULL;

cout << temp->Value << " has been removed from LL!" << endl;

delete temp;

int countNodes(Node *head) {

int count = 0;

Node *curr = head;

while (curr != nullptr) {


count++;

curr = curr->Next;

return count;

void add(Node *Head1, Node *Head2, DoublyLL &LL) {

Node *curr1 = Head1;

Node *curr2 = Head2;

while (curr1 != nullptr || curr2 != nullptr) {

int x = 0;

if (curr1 != nullptr) {

x += curr1->Value;

curr1 = curr1->Next;

if (curr2 != nullptr) {

x += curr2->Value;

curr2 = curr2->Next;

[Link](x);

void printList() {

Node *curr = Head;

while (curr != nullptr) {

cout << curr->Value << " ";


curr = curr->Next;

cout << endl;

};

int main() {

DoublyLL list1, list2, result;

[Link](1);

[Link](2);

[Link](3);

[Link](4);

[Link]([Link], [Link], result);

[Link]();

return 0;

You might also like