Advertisement
HakdogForce

Linked List to Array - Force Porquillo

Jun 2nd, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <sstream>
  4. using namespace std;
  5.  
  6. struct Node{
  7.     int data;
  8.     Node* next;
  9.    
  10. };
  11.  
  12. Node *head = NULL;
  13. Node *tail = NULL;
  14.  
  15. void exitMessage(){
  16.     system("cls");
  17.     cout << "Programmed by: Aljan A. Porquillo\n"
  18.          << "FEU - Insitute of Technology\n"
  19.          << "BS Computer Science with Specialization in Software Engineering\n"
  20.          << "Last compiled: 06 - 02 - 2019\n" << endl;
  21. }
  22.  
  23. void addList(int val){
  24.     Node* node = new Node;
  25.     node -> data = val;
  26.     node -> next = NULL;
  27.    
  28.     if(head == NULL){
  29.         head = tail = node;
  30.     }else{
  31.         tail -> next = node;
  32.         tail = node;
  33.     }
  34. }
  35.  
  36. void traverseList(){
  37.     Node *node = head;
  38.    
  39.     cout << "\nLinked list: ";
  40.     while(node != NULL){
  41.         cout << node -> data << " ";
  42.         node = node -> next;
  43.     }
  44. }
  45.  
  46. void list_Arr(){
  47.     Node* node = head;
  48.    
  49.     static int counter = 0;
  50.     int *arr = new int[counter];
  51.    
  52.     while(node != NULL){
  53.         arr[counter] = node -> data;
  54.         node = node -> next;
  55.         counter++;
  56.     }
  57.    
  58.     delete node;
  59.     node = NULL;
  60.    
  61.     cout << "\nArray List: ";
  62.     for(int i = 0; i < counter; i++){
  63.         cout << arr[i] << " ";
  64.     }
  65.    
  66.     cout << endl;
  67.    
  68.     delete [] arr;
  69.     arr = NULL;
  70.  
  71. }
  72.  
  73. int main(){
  74.     static int c = 1;
  75.     string x;
  76.     int num;
  77.    
  78.     cout << "Linked list to Array\n";
  79.     cout << "Enter [~] to stop inputing values. Do you understand? [Y/N]: ";
  80.     getch();
  81.    
  82.     if(getch() == 'y'){
  83.         system("cls");
  84.         while(x != "~"){
  85.             cout << "Enter value #" << c++ << ": ";
  86.             cin >> x;
  87.        
  88.             if(x == "~"){
  89.                 break;
  90.             }else{
  91.                 istringstream(x) >> num;
  92.                 addList(num);
  93.             }  
  94.         }  
  95.     }else if(getch() == 'n'){
  96.         exit;
  97.     }else{
  98.         system("cls");
  99.         main();
  100.     }
  101.    
  102.     traverseList();
  103.         list_Arr();
  104.        
  105.         cout << "\nPress [X] to exit" << endl;
  106.         getch();
  107.    
  108.         if(getch() == 'x'){
  109.             exitMessage();
  110.             system("pause");
  111.         }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement