Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct Edge;
- struct Node{
- string id;
- string name;
- int followers;
- int following;
- Node* next;
- Edge* edgeHead;
- };
- struct Edge{
- string id;
- Edge* next;
- Edge* prev;
- };
- struct Graph{
- Node* head;
- };
- Node* createNode(string id,string name){
- Node* n = new Node;
- n->id = id;
- n->name = name;
- n->followers = 0;
- n->following = 0;
- n->next = NULL;
- n->edgeHead = NULL;
- return n;
- }
- void insertLastNode(Graph &G,Node* n){
- if(G.head == NULL){
- G.head = n;
- }else{
- Node* temp = G.head;
- while(temp->next != NULL){
- temp = temp->next;
- }
- temp->next = n;
- }
- }
- Edge* createEdge(string id){
- Edge* newEdge = new Edge;
- newEdge->id = id;
- newEdge->next = NULL;
- newEdge->prev = NULL;
- return newEdge;
- }
- void insertLastEdge(Graph &G,Node* n,Edge* e){
- if(n->edgeHead == NULL){
- n->edgeHead = e;
- }else{
- Edge* temp = n->edgeHead;
- while(temp->next != NULL){
- temp = temp->next;
- }
- temp->next = e;
- e->prev = temp;
- }
- }
- Node* findNode(Graph &G, string id){
- Node* temp = G.head;
- while(temp != NULL){
- if(temp->id == id){
- return temp;
- }
- temp = temp->next;
- }
- return NULL;
- }
- void showAkun(Graph G){
- Node* temp = G.head;
- while(temp != NULL){
- cout << "=>" << temp->id << "," << temp->name << "," << temp->followers << "," << temp->following <<endl;
- temp = temp->next;
- }
- }
- void follow(Graph &G, string idA,string idB){
- Node* a = findNode(G,idA);
- Node* b = findNode(G,idB);
- if(a != NULL && b != NULL){
- Edge* e = createEdge(idB);
- insertLastEdge(G,a,e);
- a->following += 1;
- b->followers +=1;
- }else{
- cout << "ID TIDAK DITEMUKAN" <<endl;
- }
- }
- void showFollowing(Graph g, string id){
- Node *a = findNode(g,id);
- if(a != NULL){
- Edge* e = a->edgeHead;
- if(e != NULL){
- while(e != NULL){
- Node* temp = findNode(g,e->id);
- cout << "=>" << temp->id << "," << temp->name <<endl;
- e = e->next;
- }
- }else{
- cout << "Tidak memfollow akun" << endl;
- }
- }
- }
- int main(){
- Graph G;
- G.head = NULL;
- Node* akun1 = createNode("jake123","Jaka");
- Node* akun2 = createNode("lovelySun","Chika");
- Node* akun3 = createNode("gummyS","Ghea");
- insertLastNode(G,akun1);
- insertLastNode(G,akun2);
- insertLastNode(G,akun3);
- follow(G,"jake123","lovelySun");
- follow(G,"jake123","gummyS");
- follow(G,"gummyS","lovelySun");
- cout<<"Data sosial media : " <<endl;
- showAkun(G);
- cout<<"Data Akun : " <<endl;
- showFollowing(G,"jake123");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement