Advertisement
hadiuzzaman65

insert link at position

Mar 10th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.66 KB | None | 0 0
  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.         SinglyLinkedListNode *tail;
  20.  
  21.         SinglyLinkedList() {
  22.             this->head = nullptr;
  23.             this->tail = nullptr;
  24.         }
  25.  
  26.         void insert_node(int node_data) {
  27.             SinglyLinkedListNode* node = new SinglyLinkedListNode(node_data);
  28.  
  29.             if (!this->head) {
  30.                 this->head = node;
  31.             } else {
  32.                 this->tail->next = node;
  33.             }
  34.  
  35.             this->tail = node;
  36.         }
  37. };
  38.  
  39. void print_singly_linked_list(SinglyLinkedListNode* node, string sep, ofstream& fout) {
  40.     while (node) {
  41.         fout << node->data;
  42.  
  43.         node = node->next;
  44.  
  45.         if (node) {
  46.             fout << sep;
  47.         }
  48.     }
  49. }
  50.  
  51. void free_singly_linked_list(SinglyLinkedListNode* node) {
  52.     while (node) {
  53.         SinglyLinkedListNode* temp = node;
  54.         node = node->next;
  55.  
  56.         free(temp);
  57.     }
  58. }
  59.  
  60. // Complete the insertNodeAtPosition function below.
  61.  
  62. /*
  63.  * For your reference:
  64.  *
  65.  * SinglyLinkedListNode {
  66.  *     int data;
  67.  *     SinglyLinkedListNode* next;
  68.  * };
  69.  *
  70.  */
  71.  
  72. SinglyLinkedListNode* insertNodeAtPosition(SinglyLinkedListNode* head, int data, int position)
  73. {
  74.    SinglyLinkedListNode *p,*q;
  75.    p=head;
  76.    q=new SinglyLinkedListNode;
  77.    q->data=data;
  78.    q->next=NULL;
  79.    int i=1;
  80.    while(i<position)
  81.    {
  82.        p=p->next;
  83.        i++;
  84.    }
  85.   SinglyLinkedListNode *temp=p->next;
  86.    p->next=q;
  87.    q->next=temp;
  88.      return head;
  89.  
  90. }
  91.  
  92. int main()
  93. {
  94.     ofstream fout(getenv("OUTPUT_PATH"));
  95.  
  96.     SinglyLinkedList* llist = new SinglyLinkedList();
  97.  
  98.     int llist_count;
  99.     cin >> llist_count;
  100.     cin.ignore(numeric_limits<streamsize>::max(), '\n');
  101.  
  102.     for (int i = 0; i < llist_count; i++) {
  103.         int llist_item;
  104.         cin >> llist_item;
  105.         cin.ignore(numeric_limits<streamsize>::max(), '\n');
  106.  
  107.         llist->insert_node(llist_item);
  108.     }
  109.  
  110.     int data;
  111.     cin >> data;
  112.     cin.ignore(numeric_limits<streamsize>::max(), '\n');
  113.  
  114.     int position;
  115.     cin >> position;
  116.     cin.ignore(numeric_limits<streamsize>::max(), '\n');
  117.  
  118.     SinglyLinkedListNode* llist_head = insertNodeAtPosition(llist->head, data, position);
  119.  
  120.     print_singly_linked_list(llist_head, " ", fout);
  121.     fout << "\n";
  122.  
  123.     free_singly_linked_list(llist_head);
  124.  
  125.     fout.close();
  126.  
  127.     return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement