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

This file contains c++ code for Insertion in linked list

Typology: Exercises

2019/2020

Uploaded on 03/28/2020

Seema.Ahmad
Seema.Ahmad 🇵🇰

3 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
#include <iostream>
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;
pf3
pf4

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; }