Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <conio.h>
- #include <sstream>
- using namespace std;
- struct Node{
- int data;
- Node* next;
- };
- Node *head = NULL;
- Node *tail = NULL;
- void exitMessage(){
- system("cls");
- cout << "Programmed by: Aljan A. Porquillo\n"
- << "FEU - Insitute of Technology\n"
- << "BS Computer Science with Specialization in Software Engineering\n"
- << "Last compiled: 06 - 02 - 2019\n" << endl;
- }
- void addList(int val){
- Node* node = new Node;
- node -> data = val;
- node -> next = NULL;
- if(head == NULL){
- head = tail = node;
- }else{
- tail -> next = node;
- tail = node;
- }
- }
- void traverseList(){
- Node *node = head;
- cout << "\nLinked list: ";
- while(node != NULL){
- cout << node -> data << " ";
- node = node -> next;
- }
- }
- void list_Arr(){
- Node* node = head;
- static int counter = 0;
- int *arr = new int[counter];
- while(node != NULL){
- arr[counter] = node -> data;
- node = node -> next;
- counter++;
- }
- delete node;
- node = NULL;
- cout << "\nArray List: ";
- for(int i = 0; i < counter; i++){
- cout << arr[i] << " ";
- }
- cout << endl;
- delete [] arr;
- arr = NULL;
- }
- int main(){
- static int c = 1;
- string x;
- int num;
- cout << "Linked list to Array\n";
- cout << "Enter [~] to stop inputing values. Do you understand? [Y/N]: ";
- getch();
- if(getch() == 'y'){
- system("cls");
- while(x != "~"){
- cout << "Enter value #" << c++ << ": ";
- cin >> x;
- if(x == "~"){
- break;
- }else{
- istringstream(x) >> num;
- addList(num);
- }
- }
- }else if(getch() == 'n'){
- exit;
- }else{
- system("cls");
- main();
- }
- traverseList();
- list_Arr();
- cout << "\nPress [X] to exit" << endl;
- getch();
- if(getch() == 'x'){
- exitMessage();
- system("pause");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement