0% found this document useful (0 votes)
19 views5 pages

Linkedlist Program Practical

Uploaded by

R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views5 pages

Linkedlist Program Practical

Uploaded by

R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Program 1: Write a program in C++ Create a linked list with 5 elements:

#include<iostream>
using namespace std;
struct node
{
int data;
node *next;
};
int main()
{
node *head=new node;
head->data=0;
head->next=NULL;
node *current=head;
for(int i=1;i<=5;i++)
{
node *N=new node;
N->data=i;
N->next=NULL;
current->next=N;
current=current->next;
}

current=head;
while(current->next!=NULL)
{
cout<<current->data<<"-->";
current=current->next;
}
cout<<current->data;
return 0;
}

Head ->

0 1 2 3 4 5
Program 2: Write C++ program of single linked list to Insert a node at the beginning:
#include<iostream>
using namespace std;
struct node
{
int data;
node *next;
};
//------------------------------------------------------
Void display(node* current)
{
while(current->next!=NULL)
{
cout<<current->data<<"-->";
current=current->next;
}
cout<<current->data;
}
//---------------------------------------------------------
node* inserHead(node* cur, int x)
{
node* N=new node;
N->data=x;
N->next=cur;
cur=N;
return cur;
}
int main()
{
node *head=new node;
head->data=0;
head->next=NULL;
node *current=head;
for(int i=1;i<=5;i++)
{
node *N=new node;
N->data=i;
N->next=NULL;
current->next=N;
current=current->next;
}
display(head);
node* H=new node;
H=inserHead(head, -1);
head=H;
cout<<”\nthe elements of the list after inserting a node at the head:\n”;
display(head);

return 0;
}

1- 0 1 2 3 4 5

Program 3: Write C++ program of single linked list to Insert a node at the end:
#include<iostream>
using namespace std;
struct node
{
int data;
node *next;
};
//----------------------------------------
void display (node* current)
{
while(current -> next!=NULL)
{
cout<<current ->data<<"--->";
current=current->next;
}
cout<<current->data;
}
//--------------------------
node* insertHead (node* cur, int x)
{
node* N=new node;
N->data=x;
N->next=cur;
cur=N;
return cur;
}
//-----------------------
void insertLast(node* cur, int x)
{
node* N=new node;
N->data=x;
N->next=NULL;
node* current=cur;
while (current->next!=NULL)
current =current->next;
current->next=N;
}
//-------------------------
int main()
{
node *head=new node;
head->data=0;
head->next=NULL;
node* current=head;
for(int i=1;i<=5; i++)
insertLast (head,i);
display(head);

node* H=new node;


H=insertHead(head, -1);
head=H;
cout<<"\nthe element of the list after inserting a node at the head:\n";
display(head);

H=insertHead(head, -2);
head=H;
cout<<"\nthe element of the list after inserting a node at the head\n";
display(head);
insertLast(head, 6);
insertLast(head, 7);
cout<<"\nthe element of the list after inserting a node at the end\n";
display(head);

return 0;
}
What is Node:
A node is a collection of two sub-elements or parts. A data part that stores the element and a next part that stores
the link to the next node.

Find out the Head and NULL?

You might also like