Facebook
From Speedy Tapir, 2 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 138
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. class SinglyLinkedListNode {
  6.     public:
  7.         int data;
  8.         SinglyLinkedListNode *next;
  9.  
  10.         SinglyLinkedListNode(int node_data) {
  11.             this->data = node_data;
  12.             this->next = nullptr;
  13.         }
  14. };
  15.  
  16. class SinglyLinkedList {
  17.     public:
  18.         SinglyLinkedListNode *head;
  19.  
  20.         SinglyLinkedList() {
  21.             this->head = nullptr;
  22.         }
  23.  
  24. };
  25.  
  26. void print_singly_linked_list(SinglyLinkedListNode* node, string sep, ofstream& fout) {
  27.     while (node) {
  28.         fout << node->data;
  29.  
  30.         node = node->next;
  31.  
  32.         if (node) {
  33.             fout << sep;
  34.         }
  35.     }
  36. }
  37.  
  38. void free_singly_linked_list(SinglyLinkedListNode* node) {
  39.     while (node) {
  40.         SinglyLinkedListNode* temp = node;
  41.         node = node->next;
  42.  
  43.         free(temp);
  44.     }
  45. }
  46.  
  47. // Complete the insertNodeAtTail function below.
  48.  
  49. /*
  50.  * For your reference:
  51.  *
  52.  * SinglyLinkedListNode {
  53.  *     int data;
  54.  *     SinglyLinkedListNode* next;
  55.  * };
  56.  *
  57.  */
  58. SinglyLinkedListNode* insertNodeAtTail(SinglyLinkedListNode* head, int data) {
  59.        
  60.     // SinglyLinkedListNode* temp,*ptr;
  61.    
  62.     // if(head==NULL){
  63.     //     ptr->data=data;
  64.     //     ptr->next=NULL;
  65.     //     head=ptr;
  66.     // }else{
  67.     //     temp=head;
  68.     //     while(temp->next!=NULL){
  69.     //         temp=temp->next;
  70.     //     }
  71.     //     temp->next=NULL;
  72.     //     temp->data=data;
  73.     // }
  74.    
  75.    
  76.     // return head;
  77.    
  78.    SinglyLinkedListNode *ptr = new SinglyLinkedListNode(data);
  79.    
  80.     if(head==NULL){
  81.         head=ptr;
  82.         return head;
  83.     }
  84.     else{
  85.         SinglyLinkedListNode *temp = head;
  86.        
  87.         while(head->next !=NULL){
  88.             head=head->next;
  89.         }
  90.         head->next = ptr;
  91.         return temp;
  92.     }
  93.  
  94.            
  95. }
  96.  
  97. int main()
  98. {
  99.     ofstream fout(getenv("OUTPUT_PATH"));
  100.  
  101.     SinglyLinkedList* llist = new SinglyLinkedList();
  102.  
  103.     int llist_count;
  104.     cin >> llist_count;
  105.     cin.ignore(numeric_limits<streamsize>::max(), 'n');
  106.  
  107.  
  108.     for (int i = 0; i < llist_count; i++) {
  109.         int llist_item;
  110.         cin >> llist_item;
  111.         cin.ignore(numeric_limits<streamsize>::max(), 'n');
  112.  
  113.         SinglyLinkedListNode* llist_head = insertNodeAtTail(llist->head, llist_item);
  114.         llist->head = llist_head;
  115.     }
  116.  
  117.  
  118.     print_singly_linked_list(llist->head, "n", fout);
  119.     fout << "n";
  120.  
  121.     free_singly_linked_list(llist->head);
  122.  
  123.     fout.close();
  124.  
  125.     return 0;
  126. }
  127.  

Replies to Untitled rss

Title Name Language When
Insert node at tail Sweet Mockingbird cpp 2 Years ago.