16.
Program to create a linked list anad delete a Node* prev = NULL;
node at the end. while (temp->next != NULL)
{
#include <iostream.h> prev = temp;
#include <conio.h> temp = temp->next;
#include <stdlib.h> }
struct Node { cout << "Deleted item is: " << temp->info << "\n";
int info; prev->next = NULL;
Node* next; delete temp;
}; }
Node* head = NULL; // Display the list
// Insert at the END of the list void ldisplay()
void linsert() {
{ if (head == NULL)
Node* q = new Node(); {
int item; cout << "List is empty\n";
cout << "Enter the element to be inserted at the return;
end: "; }
cin >> item; Node* temp = head;
q->info = item; cout << "Contents of the list are:"<<endl;
q->next = NULL; while (temp != NULL)
if (head == NULL) {
{ cout << temp->info << " -> ";
head = q; temp = temp->next;
} }
else cout << "NULL\n";
{ }
Node* temp = head; // Main function
while (temp->next != NULL) int main()
{ {
temp = temp->next; int ch;
} while (1)
temp->next = q; {
} cout << "\nLINKED LIST MENU"<<endl;
} cout << "1. Insert at end"<<endl;
// Delete from the END of the list cout << "2. Delete at end" <<endl;
void ldelete() cout << "3. Display "<<endl;
{ cout << "4. Exit"<<endl;
if (head == NULL) cout << "Enter your choice: ";
{ cin >> ch;
cout << "List is empty\n"; switch (ch)
return; {
} case 1: linsert();
if (head->next == NULL) break;
{ case 2: ldelete();
cout << "Deleted item is: " << head->info << "\n"; break;
delete head; case 3: ldisplay();
head = NULL; break;
return; case 4: exit(0);
} default: cout << "Invalid choice\n";
Node* temp = head; }}}