Pages

Monday 1 November 2021

Introduction to Single Linked List in c++

What is Linked List?

When we want to work with an unknown number of data values, we use a linked list data structure to organize that data. The linked list is a linear data structure that contains a sequence of elements such that each element links to its next element in the sequence. Each element in a linked list is called "Node".

What is Single Linked List?

Simply a list is a sequence of data, and the linked list is a sequence of data linked with each other.

In any single linked list, the individual element is called as "Node". Every "Node" contains two fields, data field, and the next field. The data field is used to store actual value of the node and next field is used to store the address of next node in the sequence.

The graphical representation of a node in a single linked list is as follows...


Important  Points to be Remembered

In a single linked list, the address of the first node is always stored in a reference node known as "front" (Some times it is also known as "head").

Always next part (reference part) of the last node must be NULL.

Program to illustrate Linked list:
/* Program to create n nodes in single linked list*/
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<mem.h>

struct node
{
  int info;
  struct node *link;
};
struct node *start,*q,*tmp;

class rkslcreate
{
public:
void create_list(int);
void display(void);
};

void rkslcreate::create_list(int data)
{
  struct node* newnode = new node;
  tmp=newnode;
  tmp->info=data;
  tmp->link=NULL;
  if(start==NULL) /*If list is empty */
 {
 start=tmp;
 cout<<"\n start :="<<start<<" || "<<"tmp->info :="<<tmp->info;
 }
  else
  {       /*Element inserted at the end */
    q=start;
    cout<<"\n start :="<<q<<" || "<<"q->info :="<<q->info;
    while(q->link!=NULL)
    {
     q=q->link;
     cout<<"\nq->link :="<<q<<" || "<<"q->info :="<<q->info;
    }
    q->link=tmp;
    cout<<"\nq->link :="<<tmp<<" || "<<"q->info :="<<tmp->info;
  }
}/*End of create_list()*/

void rkslcreate::display()
{
if(start == NULL)
{
cout<<"\nList is empty";
}
q=start;
cout<<"\n singly linked List is :\n";
cout<<start<<" ---->"<<start->info<<"||";
while(q->link!=NULL)
{
q=q->link;
cout<<q<<"----->"<<q->info<<"||";
}
cout<<NULL;
}/*End of display() */

void main()
{
int choice,n,item,i;
start=NULL;
rkslcreate ls;
clrscr();
while(1)
{
cout<<"\n MENU \n";
cout<<"1.Create List\n";
cout<<"2.Display\n";
cout<<"3.Quit\n";
cout<<"Enter your choice : " ;
cin>>choice;
switch(choice)
{
case 1:
cout<<"\nHow many nodes you want : ";
cin>>n;
for(i=0;i<n;i++)
{
  cout<<"\nEnter the element : ";
  cin>>item;
  ls.create_list(item);
}
break;
case 2:
ls.display();
break;
case 3:
exit(1);
default:
cout<<"Wrong choice\n";
}/*End of switch */
}/*End of while */
}/*End of main()*/

Output:

ANNIMATED VIDEO LINK:
https://www.youtube.com/watch?v=R9PTBwOzceo

No comments:

Post a Comment

Constructors & Destructors in c++

  Constructors :  A Constructor is a special member function, which is used to initialize the objects of its class. The Constructor is invok...