Docsity

C++ code for Insertion in linked list, Exercises of Data Structures and Algorithms

Partial preview of the text

Download C++ code for Insertion in linked list and more Exercises Data Structures and Algorithms in PDF only on Docsity!

#include using namespace std; struct node { int data; node *next; }; class linked_list { private: node head,tail; public: linked_list() { head = NULL; tail = NULL; } void add_node(int n) { node *tmp = new node; tmp->data = n;

tmp->next = NULL; if(head == NULL) { head = tmp; tail = tmp; } else { tail->next = tmp; tail = tail->next; } } node* gethead() { return head; } static void display(node *head) { if(head == NULL) { cout << "NULL" << endl; }

linked_list a; a.add_node(1); a.add_node(2); linked_list b; b.add_node(3); b.add_node(4); linked_list::concatenate(a.gethead(),b.gethead()); a.after(a.gethead()->next, 10); linked_list::display(a.gethead()); return 0; }