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;
}
|