Printing a powerset from a linked list.

So I am trying to get to print a powerset from my linked list however I am not sure how to go about it. I keep running into this issue with however many elements I would need so many for loops so I don't think my way of thinking is right and I have do some research but I can't make sense of it. So here is my code hopefully someone can help me. I need to create a void function for it so I know that much. Any ways this is my code.
If you do not know what a powerset is here is an example.
say you have a set {1,2,3}the power set would be the following:{{}, {1}, {2}, {3}, {1,2}, {1,3}, {2,3}, {1,2,3}} another example say you have the power set {1,2,3,4,5} the result should be this:{{}, {1}, {2}, {3}, {4}, {5}, {1,2}, {1,3}, {1,4}, {1,5}, {2,3}, {2,4}, {2,5}, {3,4}, {3,5}, {4,5}, {1,2,3}, {1,2,4}, {1,2,5}, {1,3,4}, {1,3,5}, {1,4,5}, {2,3,4}, {2,3,5}, {2,4,5}, {3,4,5}, {1,2,3,4}, {1,2,3,5}, {1,2,4,5}, {1,3,4,5}, {2,3,4,5}, {1,2,3,4,5}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#ifndef TEST1_H_INCLUDED
#define TEST1_H_INCLUDED

class List{
private:

   typedef struct node{
        int data;
        node* next;
    }* nodePtr;

    nodePtr head;
    nodePtr curr;
    nodePtr temp;

public: // This is were my functions go
    List();
    void AddNode(int addData);
    void DeleteNode(int delData);
    void PrintList();

};

#endif // TEST1_H_INCLUDED


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <cstdlib>
#include <iostream>
#include "List.h"

using namespace std;

List::List(){
    head = NULL;
    curr = NULL;
    temp = NULL;
}

void List::AddNode(int addData){
    nodePtr n = new node;
    n->next = NULL;
    n->data = addData;

    if(head != NULL){
        curr = head;
        while(curr->next !=NULL){
            curr = curr->next;
        }
        curr->next =n;
    }
    else
    {
        head = n;
    }
}

void List::DeleteNode(int delData){
    nodePtr delPtr = NULL;
    temp = head;
    curr = head;
    while(curr != NULL && curr->data != delData){
        temp = curr;
        curr = curr->next;
    }
    if(curr == NULL){
        cout << delData << "was not in the list\n";
        delete delPtr;
    }
    else{
        delPtr = curr;
        curr = curr->next;
        temp->next = curr;
        if(delPtr == head){
            head = head->next;
            temp = NULL;
        }
        delete delPtr;
        cout << "The value " << delData << "was deleted\n";
    }
}

void List::PrintList(){
    curr = head;
    while(curr != NULL){
        cout << curr->data <<endl;
        curr = curr->next;
    }
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include "List.h"


using namespace std;

int main()
{
    int i =0;
    int k =0;
    int m =0;
    List element;
    cout << "How many distinct elements are in your set?\n";
    cin >> k;
    for(int j=0;j<k;j++)
    {
        cout << "Please enter the "<< j+1 <<" distinct element in your set?\n";
        cin >> i;
        element.AddNode(i);
    }
    do{
    cout << "Please make sure the numbers below are the elements in your set.\n";
    element.PrintList();
    cout << "If this is correct press '1' if you need to add an element press '2' or if you need to delete an element press '3'.\n";
    cin >> m;
    switch(m){
        case 1:
            break;
        case 2:
            cout << "Please enter the number you wish to add.\n";
            cin >> i;
            element.AddNode(i);
            k++;
            break;
        case 3:
            cout << "Please enter the number you wish to delete.\n";
            cin >> i;
            element.DeleteNode(i);
            k--;
            break;
        default:
            cout << "The value you have entered is not valid.\n";
    }
    }while(m !=1);

    return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// you need <math.h> for pow-function!
void quicktest() {

	vector<vector<int>*> a; // container I save all subsets
        int countNumbers = 5;  // well 5 = {1, 2, 3, 4, 5}

	for(int i = 0; i < (int) pow(2, countNumbers) ; i++) { // count from 0 to all possibilites (32 in this case with 5 numbers)
		vector<int>* te = new vector<int>(); // temp vector to contain our subset we create in each iteration
		for(int j = 1; j <= countNumbers; j++) { //go from 1 to 5 (alle numbers in the array (just a simple way since we have excatly number 1-5)
			if(i % (int) pow(2, j) >= (int) pow(2, j-1) && i % (int) pow(2, j) <= (int) pow(2, j) - 1) 
// so this if is probably the interesting part .. I'd need 15 minutes now to explain so I won't
// You probably smart enough to figure it out
				te->push_back(j);  // add number to subset
		}

		a.push_back(te);  //add subset to set of subsets
	}

// print everything
	for(int i = 0; i < a.size(); i++) {
		for(int j = 0; j < a.at(i)->size(); j++)
			cout << a.at(i)->at(j) << " | ";
		cout << endl;
	}

	cout << endl;
}


Well I don't like posting solutions but since this is not with linked list you still have to use ur brain and do something else

But you get an idea I guess on how-to

ps: I hope I don't answer too late ;x
pps: my result List is not in the nice order you had in your examples. Sorry for that ^.^
Last edited on
Lol this isn't for class my math teacher mentioned you could do this and I went about it for my own curiosity but thank you I'll see how it can be done with a linked list I appreciate your help I'll see if I can get any more thoughts from it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// you need <math.h> for pow-function!
void quicktest() {

	vector<vector<int>*> a; // container I save all subsets
        int countNumbers = 5;  // well 5 = {1, 2, 3, 4, 5}

	for(int i = 0; i < (int) pow(2, countNumbers) ; i++) { // count from 0 to all possibilites (32 in this case with 5 numbers)
		vector<int>* te = new vector<int>(); // temp vector to contain our subset we create in each iteration
		for(int j = 1; j <= countNumbers; j++) { //go from 1 to 5 (alle numbers in the array (just a simple way since we have excatly number 1-5)
			if(i % (int) pow(2, j) >= (int) pow(2, j-1) && i % (int) pow(2, j) <= (int) pow(2, j) - 1) 
// so this if is probably the interesting part .. I'd need 15 minutes now to explain so I won't
// You probably smart enough to figure it out
				te->push_back(j);  // add number to subset
		}

		a.push_back(te);  //add subset to set of subsets
	}

// print everything
	for(int i = 0; i < a.size(); i++) {
		for(int j = 0; j < a.at(i)->size(); j++)
			cout << a.at(i)->at(j) << " | ";
		cout << endl;
	}

	cout << endl;
}


Well I don't like posting solutions but since this is not with linked list you still have to use ur brain and do something else

But you get an idea I guess on how-to

ps: I hope I don't answer too late ;x
pps: my result List is not in the nice order you had in your examples. Sorry for that ^.^


Is there a better way than linked lists? If so how?
Maybe try the set class from STL.
I do not know anything about sets or STL?
Topic archived. No new replies allowed.