Introduction:
Linked list is one of the fundamental data structures, and can be used to implement other data structures. In a linked list there are different numbers of nodes. Each node is consists of two fields. The first field holds the value or data and the second field holds the reference to the next node or null if the linked list is empty.
typedef struct node
{
int data;
node *next;
};
Complete Structure of linklist..
1) Insert from front.
At first initialize node type.
node *head = NULL; // because Link List is empty.....
Then we take the data input from the user and store in the
node linkdata
variable. Create a temporary node node *temp
and allocate space for it.
hen place
info
to temp->data
. So the first field of the node *temp
is filled. Now temp->next
must become a part of the remaining linked list (although now linked list is empty but imagine that we have a 2 node linked list and head is pointed at the front) So temp->next
must copy the address of the *head
(Because we want insert at first) and we also want that *head
will always point at front. So *head
must copy the address of the node *temp
.
temp->data=linkdata;
temp->next=head;
head=temp;
Insert Example:
#include<iostream>
#include<conio.h>
using namespace std;
typedef struct node
{
int data;
node *next;
};
int main()
{
node *head = NULL; // because Link List is empty.....
int linkdata;
cout << "Enter Number ....";
cin >> linkdata;
// first creat temp memory for store data.......
node *temp;
temp = (node*)malloc(sizeof(node));
temp->data=linkdata;
temp->next=head;
head=temp;
cout << "your Value succesfully Stored in LinkList \n\nYour Value is ....." << linkdata;
getch();
}