Basic single linked list in c++

In this program one single linked listed is used.This program can create link list, display link list, add elements in available list and checks if link list is empty or not.
Brief explanation of program is given below:

First of all we have to create the structure of the link list.A single link list contains two parts, a data a reference to next node.So we will create structure of link list using struct keyword.We will declare head as null at the end of structure so it can be accessed through out the program.

Create Method:
create method will execute if user tries to enter an element is a list and there is no list available.Create method will create a list a node a promote the user to enter a value.Once user entered a value.That value will be stored at first node of list.

Empty Method
Empty method is used to check if link list contains any element or not.We will check a condition if head is not null, then list contains some data, otherwise list is empty.Did you remember when we create structure of list we initialized head variable to null.That's why we use head variable to check list elements.

Display Method
Display method is used to display the whole link list to the user.Link list is displayed by using a loop.This method will move head to next node every time after printing an element.And continue to print elements while head variable not equals to null.Obviously  after last element head will point to null.

Delete Method
Delete method is used to delete single element from the end of node.This method will delete last element from list.However we can delete any element from the list by changing our code.

ADD Method
Add method is used to add single element in the list.If link list does't exists.It will invoke create method to create a link list and ask the user to enter a value.

Below you can see the complete code of single link list in C++ 

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

//structure of single link list
struct node
{
int data;
node *address; 
}*head=NULL;

//Function to check empty link list
void isEmpty(){
if(head==NULL){
cout<<"Linklist is empty:"; 
}else
cout<<"Link list is not empty"; 
}

//Function to display Link list
void display()
{
if(head==NULL){
cout<<"Nothing to display!"; 
}else{

cout<<"\n Your link-list is"<<endl;
node *v=head;
while(v!=NULL)
{
cout<<"  "<<v->data;
v=v->address;
}
delete(v);  
}}

//Funtion to delete element
void del()
{
if(head==NULL){
cout<<"Nothing to delete"; 
}
else if(head->address==NULL){
head=NULL;
cout<<"link list deleted";}

else{
node *p;
node *q=p;
p=head;
while(p->address!=NULL)
{
 q=p;
p=p->address;
 
 }

q->address=NULL;
cout<<"\nLast element is deleted:"<<endl;
 


delete(p);
}}


//Function to create Link list
void create(node *new1 , node *end)
{

int info;

cout<<"\n Enter your data in integer only: ";
cin>>info;
new1=new node;
new1->data=info;
head=new1;
new1->address=NULL;
cout<<info<<" is added to link list"<<endl;}
 




//Funtion to  add element
void add()
{
if(head==NULL){ 
  node *new1=NULL;
  node *end=new1;   
  create(new1,end);  
}

else{
  
int d;
node *p=head;
node *q=new node;
while(p->address!=NULL)
p=p->address;

p->address=q;
cout<<"\nEnter your data in integer only: ";
cin>>d;
q->data=d;
q->address=NULL;
cout<<d<<" is added to link list"<<endl;
}
}

//Main function
main()
{
node *new1=NULL;
node *end=new1;
int choice;
cout<<"\t***Wellcome to Link list***"<<endl;
while(choice!=99)
{ 
cout<<"\n\nTo add element link list 1: "<<endl;
cout<<"To display link list 2: "<<endl;
cout<<"To delete single element 3:"<<endl;
cout<<"To ccheck if link list is empty 4:\n";

cin>>choice;
switch(choice)
{
case 1:
add();
break;

case 2:
display();
break;

case 3:
del();
break;

case 4:
isEmpty();
break; 

default:
cout<<"Wrong value entered. "<<endl; 
 
}//switch end 
}//while end
 
getch(); 

}

Reactions

Post a Comment

0 Comments