Advertisement
ChaeYuriya

Assessment praktikum Struktur Data Sub CLO 2 ADITYA GALANG ADZANA 103042310028

Dec 29th, 2024
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.93 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct Edge;
  6.  
  7. struct Node{
  8.     string id;
  9.     string name;
  10.     int followers;
  11.     int following;
  12.     Node* next;
  13.     Edge* edgeHead;
  14. };
  15.  
  16. struct Edge{
  17.     string id;
  18.     Edge* next;
  19.     Edge* prev;
  20. };
  21.  
  22. struct Graph{
  23.     Node* head;
  24. };
  25.  
  26. Node* createNode(string id,string name){
  27.     Node* n = new Node;
  28.     n->id = id;
  29.     n->name = name;
  30.     n->followers = 0;
  31.     n->following = 0;
  32.     n->next = NULL;
  33.     n->edgeHead = NULL;
  34.     return n;
  35. }
  36.  
  37. void insertLastNode(Graph &G,Node* n){
  38.     if(G.head == NULL){
  39.         G.head = n;
  40.     }else{
  41.         Node* temp = G.head;
  42.         while(temp->next != NULL){
  43.             temp = temp->next;
  44.         }
  45.         temp->next = n;
  46.     }
  47. }
  48.  
  49. Edge* createEdge(string id){
  50.     Edge* newEdge = new Edge;
  51.     newEdge->id = id;
  52.     newEdge->next = NULL;
  53.     newEdge->prev = NULL;
  54.     return newEdge;
  55. }
  56.  
  57. void insertLastEdge(Graph &G,Node* n,Edge* e){
  58.     if(n->edgeHead == NULL){
  59.         n->edgeHead = e;
  60.     }else{
  61.         Edge* temp = n->edgeHead;
  62.         while(temp->next != NULL){
  63.             temp = temp->next;
  64.         }
  65.         temp->next = e;
  66.         e->prev = temp;
  67.     }
  68. }
  69.  
  70. Node* findNode(Graph &G, string id){
  71.     Node* temp = G.head;
  72.     while(temp != NULL){
  73.         if(temp->id == id){
  74.             return temp;
  75.         }
  76.         temp = temp->next;
  77.     }
  78.     return NULL;
  79. }
  80.  
  81. void showAkun(Graph G){
  82.     Node* temp = G.head;
  83.     while(temp != NULL){
  84.         cout << "=>" << temp->id << "," << temp->name << "," << temp->followers << "," << temp->following <<endl;
  85.         temp = temp->next;
  86.     }
  87. }
  88.  
  89. void follow(Graph &G, string idA,string idB){
  90.     Node* a = findNode(G,idA);
  91.     Node* b = findNode(G,idB);
  92.  
  93.     if(a != NULL && b != NULL){
  94.         Edge* e = createEdge(idB);
  95.         insertLastEdge(G,a,e);
  96.  
  97.         a->following += 1;
  98.         b->followers +=1;
  99.     }else{
  100.         cout << "ID TIDAK DITEMUKAN" <<endl;
  101.     }
  102. }
  103.  
  104. void showFollowing(Graph g, string id){
  105.     Node *a = findNode(g,id);
  106.  
  107.     if(a != NULL){
  108.         Edge* e = a->edgeHead;
  109.  
  110.         if(e != NULL){
  111.             while(e != NULL){
  112.                 Node* temp = findNode(g,e->id);
  113.                 cout << "=>" << temp->id << "," << temp->name <<endl;
  114.                 e = e->next;
  115.             }
  116.         }else{
  117.             cout << "Tidak memfollow akun" << endl;
  118.         }
  119.     }
  120. }
  121.  
  122. int main(){
  123.     Graph G;
  124.     G.head = NULL;
  125.  
  126.     Node* akun1 = createNode("jake123","Jaka");
  127.     Node* akun2 = createNode("lovelySun","Chika");
  128.     Node* akun3 = createNode("gummyS","Ghea");
  129.  
  130.     insertLastNode(G,akun1);
  131.     insertLastNode(G,akun2);
  132.     insertLastNode(G,akun3);
  133.  
  134.     follow(G,"jake123","lovelySun");
  135.     follow(G,"jake123","gummyS");
  136.     follow(G,"gummyS","lovelySun");
  137.  
  138.     cout<<"Data sosial media : " <<endl;
  139.     showAkun(G);
  140.  
  141.     cout<<"Data Akun : " <<endl;
  142.     showFollowing(G,"jake123");
  143.     return 0;
  144. }
  145.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement