Professional Documents
Culture Documents
Linked List: - Linked List Is A Set of Nodes Where Each Node Has Two Fields Data and Next
Linked List: - Linked List Is A Set of Nodes Where Each Node Has Two Fields Data and Next
• Pointer :- A pointer always points to the first node of the linked list. The
first node to which the pointer points is called header node.
• Empty List :- when there is no node in any linked list , then it is called as
empty linked list.
• Null Pointer : - when linked list is empty , then null value is set to the
pointer.
• E.g Head = NULL;
Array supports Random Access, which means elements can be Linked List supports Sequential Access, which means to
accessed directly using their index, like arr[0] for 1st access any element/node in a linked list, we have to
element, arr[6] for 7th element etc. sequentially traverse the complete linked list, upto that element.
Hence, accessing elements in an array is fast with a constant To access nth element of a linked list, time complexity is O(n).
time complexity of O(1).
Memory is allocated as soon as the array is declared, Memory is allocated at runtime, as and when a new node is
at compile time. It's also known as Static Memory Allocation. added. It's also known as Dynamic Memory Allocatio
Size of the array must be specified at time of array decalaration. Size of a Linked list is variable. It grows at runtime, as more
nodes are added to it.
In an array, elements are stored in contiguous memory In a linked list, new elements can be stored anywhere in the
location or consecutive manner in the memory. memory.
Address of the memory location allocated to the new element is
stored in the previous node of linked list, hence formaing a link
between the two nodes/elements.
In array, Insertion and Deletion operation takes more time, as In case of linked list, a new element is stored at the first free
the memory locations are consecutive and fixed. and available memory location, with only a single overhead
step of storing the address of memory location in the previous
node of linked list.
Insertion and Deletion operations are fast in linked list.
• Size of linked lists is not fixed, they can expand and shrink
during run time.
• Insertion and Deletion Operations are fast and easier in Linked
Lists.
• Memory allocation is done during run-time (no need to
allocate any fixed memory).
• Data Structures like Stacks, Queues, and trees can be easily
implemented using Linked list.
Struct node
{
int data;
struct node *next;
};
struct node
{
int data;
struct node *next;
struct node *prev;
};
• Creation
• Traversing
• Searching
• Insertion
• Deletion
void create()
{ struct Node *head,*newnode,*temp;
head = 0;
newnode = (struct Node*) malloc(sizeof(struct Node));
cout<<“Enter the data”
cin>> newnode->data ;
newnode->next = 0;
if(head = = 0)
{
head=temp=newnode;
}
else
{
temp->next=newnode;
temp=newnode;
}
} PROF.SHOBHA PATIL,JSPM NTC
PROF.SHOBHA PATIL,JSPM NTC
Display linked list
struct Node
{ int data;
struct Node *next;
};
Void disply()
{
struct Node *head,* newnode,*temp;
temp=head;
while(temp!=0)
{
temp->data;
temp=temp->next;
}
The left (previous) node of the target node now should point to the next node of the target node −
LeftNode.next −> TargetNode.next;
Void addatbeg()
{
struct Node *head,* newnode;
Void addatend()
{
struct Node *head,* newnode,*temp;
void create()
{ struct Node *head,*newnode,*temp;
Void disply()
{
struct Node *head,* newnode,*temp;
temp=head;
while(temp->next!=head)
{ temp->data;
temp=temp->next;
}
temp->data;
} PROF.SHOBHA PATIL,JSPM NTC
Inserting node at beginning
struct Node
{ int data;
struct Node *next;
};
Void addatbeg()
{
struct Node *tail,* newnode;
Void addatbeg()
{
struct Node *tail,* newnode;
void create()
{ struct Node *head,*newnode,*temp;
head = 0;
newnode = (struct Node*) malloc(sizeof(struct Node));
cout<<“Enter the data”
cin>> newnode->data ;
newnode->next = 0;
newnode->prev= 0
if(head = = 0)
{
head=temp=newnode;
}
else
{
temp->next=newnode;
newnode->prev=temp;
temp= newnode;
}
}
PROF.SHOBHA PATIL,JSPM NTC
Display doubly linked list
struct Node
{ int data;
struct Node *next;
};
Void disply()
{
struct Node *head,* newnode,*temp;
temp=head;
while(temp!=0)
{
temp->data;
temp=temp->next;
}
Void addatbeg()
{
struct Node *head,* newnode,*tail;
Void addatbeg()
{
struct Node *head,* newnode,*temp;
4x3+6x2+7x+9
5x2+4x+2
struct node
{
int c; //Data
int index; //Flag
struct node *next, *down; //Next & Down pointer
};