Singly linked list Program C++

In this tutorial, we will write a C++ program to implement singly linked list.

In this program, we will create a separate function for each operation that is to be performed in a linked list. This operation includes:

  • create()
  • insert(): this includes insetion at the begining, at the end or at a given position.
  • delet() : delete is also the same that is at the begining, at the end or at a given position.
  • display()
  • search()

The program takes user input at every step necessary from the user and performed accordingly. The following are the various C++ programming topics used in the program.

Singly linked list in C++

#include <iostream>
#include <conio.h>
#include <stdlib.h>
using namespace std;

class Node
{
  public_colon
  int info;
  Node * next;
};

class List: public Node
{

  Node *first, *last;
  public_colon
  List()
  {
    first = NULL;
    last = NULL;
  }
  void create();
  void insert();
  void delet();
  void display();
  void search();
};

//create function
void List::create()
{
  Node * temp;
  temp = new Node;
  int n;

  cout << "\nEnter an Element: ";
  cin >> n;

  temp->info = n;
  temp->next = NULL;
  if (first == NULL)
  {
    first = temp;
    last = first;
  }
  else
  {
    last->next = temp;
    last = temp;
  }
}

//insert function
//at chosen position
void List::insert()
{
  Node *prev, *cur;
  prev = NULL;
  cur = first;
  int count = 1, pos, ch, n;
  Node *temp = new Node;

  cout << "\nEnter an Element: ";
  cin >> n;

  temp->info = n;
  temp->next = NULL;
  cout << "\nChoose the position:";
  cout << "\n1 for the beginning.";
  cout << "\n2 for the end.";
  cout << "\n3 for in between.";
  cout << "\nEnter Your Choice: ";
  cin >> ch;

  switch (ch)
  {
    case 1:
      temp->next = first;
      first = temp;
      break;
    case 2:
      last->next = temp;
      last = temp;
      break;
    case 3:
      cout << "\nEnter the Position to Insert: ";
      cin >> pos;
      while (count != pos)
      {
        prev = cur;
        cur = cur->next;
        count++;
      }
      if (count == pos)
      {
        prev->next = temp;
        temp->next = cur;
      }
      else
        cout << "\nNot Able to Insert";
      break;
  }
}

//function to delete
//at chosen position
void List::delet()
{
  Node *prev = NULL, *cur = first;
  int count = 1, pos, ch;
  cout << "Select the position for deletion: \n";
  cout << "\n1 for the beginning.";
  cout << "\n2 for the end.";
  cout << "\n3 for in between.";
  cout << "\nEnter Your Choice:";
  cin >> ch;

  switch (ch)
  {
    case 1:
      if (first != NULL)
      {
        cout << "\nDeleted Element is " << first->info;
        first = first->next;
      }
      else
        cout << "\nOperation Failed";
      break;
    case 2:
      while (cur != last)
      {
        prev = cur;
        cur = cur->next;
      }
      if (cur == last)
      {
        cout << "\nDeleted Element is: " << cur->info;
        prev->next = NULL;
        last = prev;
      }
      else
        cout << "\nOperation Failed";
      break;
    case 3:
      cout << "\nEnter the Position of Deletion: ";
      cin >> pos;
      while (count != pos)
      {
        prev = cur;
        cur = cur->next;
        count++;
      }

      if (count == pos)
      {
        cout << "\nDeleted Element is: " << cur->info;
        prev->next = cur->next;
      }
      else
      {
        cout << "\nOperation Failed";
      }
      break;
  }
}

//display function
//check for empty list first
void List::display()
{
  Node *temp = first;
  if (temp == NULL)
  {
    cout << "\nList is Empty";
  }
  while (temp != NULL)
  {
    cout << temp->info;
    cout << "-->";
    temp = temp->next;
  }
  cout << "NULL";
}

//search function
//check for empty list first
void List::search()
{
  int value, pos = 0;
  bool flag = false;

  if (first == NULL)
  {
    cout << "List is Empty";
    return;
  }
  cout << "Enter the Value to be Searched: ";
  cin >> value;

  Node * temp;
  temp = first;
  while (temp != NULL)
  {
    pos++;
    if (temp->info == value)
    {
      flag = true;
      cout << "Element" << value << "is Found at " << pos << " Position";
      return;
    }
    temp = temp->next;
  }
  if (!flag)
  {
    cout << value << " is not present in the List";
  }
}

//drive function
int main()
{
  List ls;
  int ch;
  while (1)
  {
    cout << "\nChoose an Operation: ";
    cout << "\n1 to CREATE.";
    cout << "\n2 to INSERT.";
    cout << "\n3 to DELETE.";
    cout << "\n4 to SEARCH.";
    cout << "\n5 to DISPLAY.";
    cout << "\n6 to EXIT.";
    cout << "\nEnter Your Choice: ";
    cin >> ch;

    switch (ch)
    {
      case 1:
        ls.create();
        break;
      case 2:
        ls.insert();
        break;
      case 3:
        ls.delet();
        break;
      case 4:
        ls.search();
        break;
      case 5:
        ls.display();
        break;
      case 6:
        return 0;
    }
  }
  return 0;
}

Output:

Choose an Operation:
1 to CREATE.
2 to INSERT.
3 to DELETE.
4 to SEARCH.
5 to DISPLAY.
6 to EXIT.
Enter Your Choice: 1

Enter an Element: 2

Choose an Operation:
1 to CREATE.
2 to INSERT.
3 to DELETE.
4 to SEARCH.
5 to DISPLAY.
6 to EXIT.
Enter Your Choice: 2

Enter an Element: 4

Choose the position:
1 for the beginning.
2 for the end.
3 for in between.
Enter Your Choice: 2

Choose an Operation:
1 to CREATE.
2 to INSERT.
3 to DELETE.
4 to SEARCH.
5 to DISPLAY.
6 to EXIT.
Enter Your Choice: 5
2-->4-->NULL
Choose an Operation:
1 to CREATE.
2 to INSERT.
3 to DELETE.
4 to SEARCH.
5 to DISPLAY.
6 to EXIT.
Enter Your Choice: 6

Similarly, you can follow the option, and then you can search and delete an element from the list.