Docsity

C++ code for linked list node deletion, Exercises of Data Structures and Algorithms

Toggle sidebar
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;
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;
}
else
{
cout << head->data << endl;
display(head->next);
}
}
static void concatenate(node *a,node *b)
pf2

Partial preview of the text

Download C++ code for linked list node deletion 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; } else { cout << head->data << endl; display(head->next); } } static void concatenate(node *a,node *b)

if( a != NULL && b!= NULL ) { if (a->next == NULL) a->next = b; else concatenate(a->next,b); } else { cout << "Either a or b is NULL\n"; } } void front(int n) { node *tmp = new node; tmp -> data = n; tmp -> next = head; head = tmp; } void after(node a, int value) { node p = new node; p->data = value; p->next = a->next; a->next = p; } void del (node before_del) { node temp; temp = before_del->next; before_del->next = temp->next; delete temp; } }; int main() { linked_list a; a.add_node(1); a.add_node(2); a.front(3); a.add_node(5); a.add_node(15); cout<<"Elements of Linked List "<<endl; linked_list::display(a.gethead()); a.del(a.gethead()->next); cout<<"Elements of Linked List Afetr Deletion"<<endl; linked_list::display(a.gethead()); return 0; }