Linked List
Linked List is a type of data structure that is made up of nodes that are created using self-referential structures. Each of these nodes contains two parts, namely the data and the reference to the next list node. Only the reference to the first list node is required to access the whole linked list. This is known as the head. The last node in the list points to nothing so it stores NULL in that part.
Linked List
Linked List comprises nodes where every node contains a data field and a reference(link) to the following node in the list. A linked list is a linear data structure, in which the components are not put away at adjacent memory locations.
Following is a simple linked list example with using the template function to demonstrate Linked List C++.
Linked List C++
using namespace std;
template<typename T>
class list
{ int n;
struct NODE
{T data;
NODE *next;
}*start;
public:
list()
{ start=NULL;}
void create()
{T x;
NODE *q,*temp;
cout<<“Enter the number of nodes : “;
cin>>n;
cout<<“Enter the data : “;
for(int i=0;i<n;i++)
{
cin>>x;
temp = new NODE;
temp->data=x;
temp->next=NULL;
if(start==NULL) start=temp;
else
{q=start;
while(q->next != NULL) q = q->next;
q->next = temp;}
}}
void display()
{NODE *q;
if(start==NULL) cout<<“List is empty\n”;
else
{cout<<“List : “;
q=start;
while(q!=NULL)
{
cout<<q->data<<” “;
q=q->next;}
cout<<endl;}
}
void del()
{NODE *p,*temp;
int loc;
cout<<“Enter the postion to delete\n”;
cin>>loc;
if(start == NULL)
{cout<<“List empty.\n”; return;}
else if(loc == 1)
{
p=start;
start=start->next;
cout<<“Deleted element is “<<p->data<<endl;
delete p;
n–;}
else if(loc<=n)
{
p=start;
for(int i=1;i<=loc-1;++i)
{
temp=p;
p=p->next;
}
cout<<“Deleted element is “<<p->data<<endl;
temp->next=p->next;
delete p;
n–;
}
else cout<<“Invalid Location.\n”;
display();
}};
int main()
{
list<int> z;
int ch;
do{
cout<<“\nEnter your choice.\n”;
cout<<“1:Create\n2:Display\n3:Delete\n4:Exit\n”;
cin>>ch;
switch(ch)
{
case 1: z.create(); break;
case 2: z.display(); break;
case 3: z.del(); break;
case 4: break;
default: cout<<“Invalid Choice.\n”;
}
}while(ch!=4);
return 0;
}
OUTPUT:
Enter your choice.
1: Create
2: Display
3: Delete
4: Exit
1
Enter the number of nodes: 5
Enter the data: 1 2 3 4 5
Enter your choice.
1: Create
2: Display
3: Delete
4: Exit
2
List : 1 2 3 4 5
1: Create
2: Display
3: Delete
4: Exit
2
List : 1 2 3 4 5
Enter your choice.
1: Create
2: Display
3: Delete
4: Exit
3
Enter the position to delete
3
Deleted element is 3
List : 1 2 4 5
1: Create
2: Display
3: Delete
4: Exit
3
Enter the position to delete
3
Deleted element is 3
List : 1 2 4 5
Enter your choice.
1: Create
2: Display
3: Delete
4: Exit
4
1: Create
2: Display
3: Delete
4: Exit
4
You must be also searching for these programming languages :
tags: linked list, Linked List in C, LinkedList in Java, linked list implementation, Linked List C++, linked list implementation C++, learn C programming language, C programs for beginners.
tags: linked list, Linked List in C, LinkedList in Java, linked list implementation, Linked List C++, linked list implementation C++, learn C programming language, C programs for beginners.