Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cmath>
- using namespace std;
- struct node {
- int data;
- node* next;
- };
- struct list {
- node* head;
- node* tail;
- };
- void add_node(int value, list* listx) {
- node *temp = new node;
- temp->data = value;
- temp->next = NULL;
- if(listx->head == NULL) {
- listx->head = temp;
- listx->tail = temp;
- temp = NULL;
- }
- else {
- listx->tail->next = temp;
- listx-> tail = temp;
- }
- }
- void add_first(int value, list* listx) {
- node *temp = new node;
- temp->data = value;
- temp->next = listx->head;
- listx->head = temp;
- }
- void display(list* listx) {
- node *temp = new node;
- temp = listx->head;
- while(temp != NULL) {
- cout << "(" << (temp->data + 1) << ")";
- if(temp->next != NULL) {
- cout << ", ";
- }
- temp = temp->next;
- }
- }
- int main() {
- int n;
- cin >> n;
- list listar[n];
- for(int i = 0; i < n; i++) {
- list *listx = new list;
- listx->head = NULL;
- listx->tail = NULL;
- int inserted;
- for(cin >> inserted; inserted != 777; cin >> inserted) {
- if(inserted < 0) {
- add_first(abs(inserted - 1), listx);
- } else {
- add_node(inserted - 1, listx);
- }
- }
- listar[i] = *listx;
- }
- cout << "Inserted Graph: " << endl;
- cout << "----------------" << endl;
- for(int i = 0; i < n; i++) {
- cout << "(" << i + 1 << "): ";
- display(&listar[i]);
- cout << endl;
- }
- cout << "---------------" << endl;
- cout << "Converting to matrix..." << endl;
- bool matrix[n][n];
- for(int i = 0; i < n; i++) {
- bool row[n];
- for(int j = 0; j < n; j++) {
- row[i] = false;
- }
- list *list_i = &listar[i];
- for(node *nodex = list_i->head; nodex != NULL; nodex = nodex->next) {
- row[nodex->data] = true;
- }
- memcpy(matrix + i, row, sizeof(row));
- }
- cout << "-----------" << endl;
- cout << "Converted matrix: " << endl;
- cout << " |";
- for(int i = 0; i < n; i++) {
- cout << " " << i + 1 << " |";
- }
- cout << endl;
- for(int i = 0; i < n; i++) {
- cout << i + 1 << " |";
- for(int j = 0; j < n; j++) {
- cout << " " << (matrix[i][j] ? 1 : 0) << " |";
- }
- cout << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement