Created
November 24, 2010 12:51
-
-
Save drewlesueur/713603 to your computer and use it in GitHub Desktop.
my cse310 assignment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <sstream> | |
using namespace std; | |
typedef struct record{ | |
int key; | |
int data; | |
} record; | |
//allocate new record | |
record* new_record() { | |
record *ret; | |
ret = (record *) malloc(sizeof(record)); | |
return ret; | |
} | |
//array of records | |
record** new_record_array(int len) { | |
//0th element will be a record, with the data as the length | |
int i = 0; | |
record** my_list = (record**) malloc(len * sizeof(record)); | |
for (i=0; i < len; i++) { | |
my_list[i] = new_record(); | |
} | |
return my_list; | |
} | |
void Print(record** A, int len) { | |
int i; | |
//don't include the 0th element | |
for (i=1; i<len; i++) { | |
cout << "(" << A[i]->key << ", " << A[i]->data << ")" << endl; | |
} | |
} | |
int Find1(int x, record** A) { | |
if (A[x]->key <=0) { | |
return x; | |
} else { | |
return Find1(A[x]->key, A); | |
} | |
} | |
int Find2(int x, record** A) { | |
if (A[x]->key <= 0) { | |
return x; | |
} else { | |
A[x]->key = Find2(A[x]->key, A); | |
return A[x]->key; | |
} | |
} | |
void Link(int x, int y, record** A) { | |
if (-A[x]->key > -A[y]->key) { | |
record* yRoot = A[y]; | |
yRoot->key = x; | |
} else if (-A[x]->key == -A[y]->key){ | |
A[y]->key = A[y]->key - 1; | |
record* xRoot = A[x]; | |
xRoot->key = y; | |
} | |
} | |
void Union(int x, int y, record** A, int flag) { | |
int xRootIndex; | |
int yRootIndex; | |
if (flag == 1) { | |
xRootIndex = Find1(x, A); | |
yRootIndex = Find1(y, A); | |
} else { | |
xRootIndex = Find2(x, A); | |
yRootIndex = Find2(y, A); | |
} | |
Link(xRootIndex, yRootIndex, A); | |
} | |
int main() { | |
int arr[] = {12, 11,1,3,2,4,3,5,4,6,5,7,6}; | |
int len = 12; | |
int i=0; | |
string input = ""; | |
record** A = new_record_array(len); | |
for (i=1; i < len; i++) { | |
A[i]->key = 0; | |
A[i]->data = arr[i]; | |
} | |
Print(A, len); | |
while (1) { | |
getline(cin, input); | |
cout << "you said" << input << endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment