Skip to content
site-logo
  • Home
  • Blog
  • JavaScript
  • Machine Learning
  • Numerical Techniques C++
  • Data Structures C++
  • Typing Guide
site-logo
Home / Data Structures C++ / Linked Lists C++ Program | Insertion Deletion & Example

Linked Lists C++ Program | Insertion Deletion & Example

ByLonz Updated onSeptember 22, 2021
Data Structures C++
linked list c++ example
Ad
Table of contents
  1. Linked Lists C++ Introduction
  2. Linked List C++ Example
    1. Step 1: Create a structure ( Node ) with two fields.
    2. Step 2. Insertion in linked list Nodes.
    3. Step 3: Display Data
  3. Linked Lists C++ Program
    1. Difference between Linked List and Array
  4. Types Of Linked Lists
    1. Singly Linked List
    2. Doubly Linked List
    3. Circular Linked List
    4. Doubly Circular Linked List

Linked Lists C++ Introduction

A linked list is a linear type of data structure, which is a sequence of objects or elements which are connected to each other through pointers. It stores the actual data and a link to another node.

The linked list or singly linked list is a data structure or a Node which has two parts data and link.

Ad
  • Data Part
  • Link Part
Linked Lists C++ Example graphic
linked list example

The data part stores the actual data e.g Roll No., Name, etc. And the 2nd part is a pointer to another node, i.e, it stores the address of the next node.

Also Read: Doubly Linked List C++

Linked List C++ Example

Following are the Simple and Easy Steps to Learn actually how we
create and modify linked lists data elements

Step 1: Create a structure ( Node ) with two fields.

  • Liike int data which stores the actual data elements.
  • Node *Next is a pointer that holds the memory address of another node.
  • Now just make a class where we call functions like adding or displaying elements of a linked list.
Linked Lists C++ Program Example
Create a Node with data and a next pointer part C/C++

See Also: Stack using Linked List C++

Step 2. Insertion in linked list Nodes.

Define a function which adds data to the Node.

add nodes Linked Lists C++ Program Example

Step 3: Display Data

display function to show the node elements Linked Lists C++ Program Example

Suggestion: Doubly Circular Linked List C++

Linked Lists C++ Program

  //Linked List C++ Program
  //techindetail.com

#include<iostream>
using namespace std;

 struct node
 {
 	int data;
 	node *next;
 };

 class linked_list
 {
 	node *head;
 	public:
 		linked_list()
 		{
 			head=NULL;
 		}
	void add_node();
	void display();
	void add_node_begining();
	void add_at_pos();
 };
 
 void linked_list::add_node()
 {
 	int n;
 	node *p;
 	node *temp=new node;
 	cout<<"Enter data: \n";
 	cin>>n;
 	temp->data=n;
 	temp->next=NULL;
 if(head==NULL)
 {
 	head=temp;
 	head->next=NULL;
 }
 else
 {
 	p=head;
 	while(p->next!=NULL)
 	{
 		p=p->next;
 	}
	 	p->next=temp;
	 
 }
 }
   //function to display elements
  void linked_list::display()
  	{
  		node *temp;
  		temp=head;
  	cout<<"values in linked list are:\n";
  	cout<<endl;
  	while(temp!=NULL)
  	{
  		cout<<" "<<temp->data<<" -->";
  		temp=temp->next;
	  }
	  cout<<endl;
	  cout<<endl;
	  }
	  
 void linked_list::add_node_begining()
 {
 	int n;
 	node *p;
 	node *temp=new node;
 	cout<<"enter value: ";
 	cin>>n;
 	temp->data=n;
 	temp->next=NULL;
 if(head==NULL)
 {
 	head=temp;
 	head->next=NULL;
 }
 else
 {
 	p=head;
 	temp->next=p;
 	head=temp;
 }
}
 
 void linked_list::add_at_pos()
 {
 	int n, i, pos, counter=0;
 	node *temp=new node;
 	node *p,*ptr;
 	cout<<"enter postion at which to insert: ";
 	cin>>pos;
 	cout<<"enter value: ";
 	cin>>n;
 	temp->data=n;
 	temp->next=NULL;
 	p=head;
 while(p!=NULL)
 {
 	p=p->next;
 	counter ++;
 }
 if(pos==1)
 {
 	if(head==NULL)
 	{
 		head=temp;
 		head->next=NULL;
	 }
	 else
	 {
	 	ptr=head;
	 	head=temp;
	 	head->next=ptr;
	 }
	 
 }else if(pos>1 || pos<=counter)
 {
 	p=head;
  for(i=1; i<pos; i++)
   {
   	ptr=p;
   	p=p->next;
   }
   ptr->next=temp;
   temp->next=p;
 }else
  cout<<"position out of order: \n";
}
 

 int main()
 {
 	linked_list object;
 	while(1){
	 cout<<"enter 1 for add: 2 for display:3 for add-begining";
	 cout<<" 4 for add-pos: 5 for exit: \n";
 	int ch;
 	cin>>ch;
 	
	 switch(ch)
	 {
	 case 1:
 	   object.add_node();
     break;
    case 2:
 	object.display();
 	break;
 	    case 3:
 		object.add_node_begining();
 		break;
 	case 4:
 			object.add_at_pos();
 	break;
 	  case 5:
 		return 0;
 	default:
 		cout<<"wrong choice:\n";
 }
	 }
	 return 0;
  //techindetail.com
 }Code language: C++ (cpp)

Suggested: Circular Linked List C++

Difference between Linked List and Array

Linked List or Singly Linked list is a linear data structure like Arrays. But in Arrays, the data is stored in contiguous locations and the size is fixed.

Ad

But in linked lists, the data is stored at different locations and is linked or connected through Pointers thus making it a dynamic data structure that grows and shrinks at the run time with our needs.

Also Read: Binary Search Tree

Types Of Linked Lists

  1. Singly-linked list
  2. Doubly linked list
  3. Circular linked list
  4. Doubly Circular Linked List

Singly Linked List

When we say linked list, we are talking about a singly linked list. It has only two parts i.e, the data part and a link part

Doubly Linked List

Every node in the doubly linked list has three fields that are Left_Pointer, Right_Pointer, and Data. It stores the data as well as links to the next and previous nodes.

Circular Linked List

It is just like a singly linked list, except the last node in the list stores the address of the first node of the list instead of the NULL value.

Doubly Circular Linked List

A doubly circular linked list is a double linked list in which the last node stores the address of the first node, and the first node stores the address of the last node.

Some Related Articles

  • Doubly Linked Lists
  • Stack using Arrays
  • Queue Using Arrays
  • Circular Linked List
  • Tree Data structure
  • Graph Data structure

Post navigation

NextContinue
Doubly Linked List C++ | Insert at Front, Middle and at End
Search

Ad

Categories

  • C++ Programing
  • C++ Programs
  • Computer Graphics
  • Data Structures C++
  • JavaScript
  • Machine Learning
  • Numerical Techniques C++
  • Processor
  • Tech Updates
  • Typing Guide
  • Un Categorised

Recent Posts

  • NetSuite Cloud ERP Features
  • Is i7 Better Than i5
  • setTimeout vs setInterval JavaScript Methods
  • String Formatting In JavaScript

Ad


  • Home
  • Blog
  • Privacy Policy
  • Disclaimer
  • Sitemap

Copyright © 2023 Tech In Detail

  • Home
  • Blog
  • JavaScript
  • Machine Learning
  • Numerical Techniques C++
  • Data Structures C++
  • Typing Guide
Search