Linked List II
Linked List
Head : address of the head node gives us access of the complete list.
Example of linked list implementation in C based on the picture above
struct node{
int data;
struct node *next;
};
node *A;
A = NULL; //empty list
node *temp = (node*)malloc(sizeof(node));
temp->data=2;
temp->next=NULL;
A=temp;
node *temp1 = A;
while(temp1->next!=NULL)
{
temp1=temp1->next;
temp1->next=temp;
}
Circular Singly Linked List
Main difference in a single linked list is that it doesn't have of NULL values in the list. The last node will link to the head node so it's circular.
Doubly Linked List
Every node is a data structure that contains two links, 1 line contains reference to both previous and next data/node.
In code it can be written like :
struct t_node{
int value;
struct node* next;
struct node* prev;
};
typedef struct t_node tnode;
Examples of inserting new node :
tnode *head=0;
tnode *tail=0;
tnode *node=(tnode*)malloc(sizeof(tnode)); //allocating new code
if(head==NULL) // if there is no existing node then the head is NULL.
{
node->value = x;
node->next = NULL;
node->prev = NULL;
head = node;
tail = node;
}// inserting the node in head position
else {
node->value = x2;
node->next = NULL;
node->prev = tail;
tail->next = node;
tail = node;
} // inserting new node after tail
Deleting a node :
// only deleting the head
free(head);
head=head->next;
// deleting the only node
head = head->next;
free(head->prev);
head->prev = NULL;
// deleting the tail node
tail = tail-> prev;
free(trail->next);
tail->next = NULL;
// Deleting a node that is not either in the head or tail
tnode *curr = head;
while (curr->next->value !=x)
curr = curr-> next;
tnode *a = curr;
tnode *del = curr->next;
tnode *b = curr->next->next;
a -> next = b;
b -> prev = a;
free(del);
source : https://www.youtube.com/watch?v=JdQeNxWCguQ
https://www.youtube.com/watch?v=MuwxQ2IB8lQ
Head : address of the head node gives us access of the complete list.
Example of linked list implementation in C based on the picture above
struct node{
int data;
struct node *next;
};
node *A;
A = NULL; //empty list
node *temp = (node*)malloc(sizeof(node));
temp->data=2;
temp->next=NULL;
A=temp;
node *temp1 = A;
while(temp1->next!=NULL)
{
temp1=temp1->next;
temp1->next=temp;
}
Circular Singly Linked List
Main difference in a single linked list is that it doesn't have of NULL values in the list. The last node will link to the head node so it's circular.
Doubly Linked List
Every node is a data structure that contains two links, 1 line contains reference to both previous and next data/node.
In code it can be written like :
struct t_node{
int value;
struct node* next;
struct node* prev;
};
typedef struct t_node tnode;
Examples of inserting new node :
tnode *head=0;
tnode *tail=0;
tnode *node=(tnode*)malloc(sizeof(tnode)); //allocating new code
if(head==NULL) // if there is no existing node then the head is NULL.
{
node->value = x;
node->next = NULL;
node->prev = NULL;
head = node;
tail = node;
}// inserting the node in head position
else {
node->value = x2;
node->next = NULL;
node->prev = tail;
tail->next = node;
tail = node;
} // inserting new node after tail
Deleting a node :
// only deleting the head
free(head);
head=head->next;
// deleting the only node
head = head->next;
free(head->prev);
head->prev = NULL;
// deleting the tail node
tail = tail-> prev;
free(trail->next);
tail->next = NULL;
// Deleting a node that is not either in the head or tail
tnode *curr = head;
while (curr->next->value !=x)
curr = curr-> next;
tnode *a = curr;
tnode *del = curr->next;
tnode *b = curr->next->next;
a -> next = b;
b -> prev = a;
free(del);
source : https://www.youtube.com/watch?v=JdQeNxWCguQ
https://www.youtube.com/watch?v=MuwxQ2IB8lQ



Comments
Post a Comment