#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node{
int data;
char str[10];//char *str; and we can avoid strcpy but its not proper way
struct node *next;
};
int main(){
struct node *a = NULL;//struct POINTER is initialized to NULL, SInce pointer
a = (struct node*)malloc(sizeof(struct node));
a->data=5;
strcpy(a->str,"adarsh");
a->next = NULL;
struct node *tmp = a;
while(tmp!= NULL){
printf("%d",a->data);
printf("%s",a->str);
tmp = tmp->next;
}
}
/* https://[Log in to view URL]
linked list elements are not stored at a contiguous location;
the elements are linked using pointers. They include a series of connected nodes.
Here, each node stores the data and the address of the next node.
Why Linked List?
Arrays can be used to store linear data of similar types,
but arrays have the following limitations:
The size of the arrays is fixed: So we must know the upper limit on the number
of elements in advance. Also, generally, the allocated memory is equal to the
upper limit irrespective of the usage.
Insertion of a new element / Deletion of a existing element in an array of
is expensive: The room has to be created for the new elements and to create
room existing elements have to be shifted but in Linked list
if we have the head node then we can traverse to any node through it
and insert new node at the required position.
Example:
In a system, if we maintain a sorted list of IDs in an array
id[] = [1000, 1010, 1050, 2000, 2040].
If we want to insert a new ID 1005, then to maintain the sorted order,
we have to move all the elements after 1000 (excluding 1000).
Deletion is also expensive with arrays until unless some special
techniques are used. For example, to delete 1010 in id[],
everything after 1010 has to be moved due to this so much work is being done
which affects the efficiency of the code.
Advantages of Linked Lists over arrays:
Dynamic Array.
Ease of Insertion/Deletion.
Drawbacks of Linked Lists:
Random access is not allowed. We have to access elements sequentially
starting from the first node(head node). So we cannot do a binary search
with linked lists efficiently with its default implementation.
Extra memory space for a pointer is required with each element of the list.
Not cache friendly. Since array elements are contiguous locations,
there is locality of reference which is not there in case of linked lists.
Types of Linked Lists:
Simple Linked List – In this type of linked list, one can move or traverse
the linked list in only one direction
Doubly Linked List – In this type of linked list, one can move or traverse
the linked list in both directions (Forward and Backward)
Circular Linked List – In this type of linked list, the last node
of the linked list contains the link of the first/head node of the linked
list in its next pointer and the first/head node contains the link of
the last node of the linked list in its prev pointer
Basic operations on Linked Lists:
Deletion
Insertion
Search
Display
Representation of Linked Lists:
A linked list is represented by a pointer to the first node of the linked list. The first node is called the head of the linked list. If the linked list is empty, then the value of the head points to NULL.
Each node in a list consists of at least two parts:
A Data Item (we can store integer, strings, or any type of data).
Pointer (Or Reference) to the next node (connects one node to another)
or An address of another node
In C, we can represent a node using structures.
Below is an example of a linked list node with integer data.
In Java or C#, LinkedList can be represented as a class and a Node as a
separate class. The LinkedList class contains a reference of Node class type.
The linked list is a linear data structure where each node has two parts.
1. Data
2. Reference to the next node
*/
// C program to implement a
// linked list
#include <stdio.h>
#include <stdlib.h>
/* Data
In this section, we can store the required information. It can be any data type.
data - used to store the integer information.
struct node *next - It is used to refer to the next node.
It will hold the address of the next node.
The line struct node *next; is read as "next is a pointer to another struct node".
*/
struct Node {
int data;
struct Node* next;//This is just a recursive structure declaration (definition):
};
// Driver's code
/*
Data - it can be any data type. int,char,float,double etc.
Reference Part - It will hold the address of the next node.
So, it is a type pointer.
Reference to the next node
It will hold the next nodes address.
Head Node - Starting node of a linked list.
Last Node - Node with reference pointer as NULL.
*/
int main()
{
struct Node* head = NULL;
struct Node* second = NULL;
struct Node* third = NULL; // or struct Node *head,*middle,*last;
// allocate 3 nodes in the heap
head = (struct Node*)malloc(sizeof(struct Node));
second = (struct Node*)malloc(sizeof(struct Node));
third = (struct Node*)malloc(sizeof(struct Node));
/* Three blocks have been allocated dynamically.
We have pointers to these three blocks as head,
second and third
head second third
| | |
| | |
+---+-----+ +----+----+ +----+----+
| # | # | | # | # | | # | # |
+---+-----+ +----+----+ +----+----+
# represents any random value.
Data is random because we haven’t assigned
anything yet */
head->data = 1; // assign data in first node
head->next = second; // Link first node with
// the second node
/* data has been assigned to the data part of the first
block (block pointed by the head). And next
pointer of first block points to second.
So they both are linked.
head second third
| | |
| | |
+---+---+ +----+----+ +-----+----+
| 1 | o----->| # | # | | # | # |
+---+---+ +----+----+ +-----+----+
*/
// assign data to second node
second->data = 2;
// Link second node with the third node
second->next = third;
/* data has been assigned to the data part of the second
block (block pointed by second). And next
pointer of the second block points to the third
block. So all three blocks are linked.
head second third
| | |
| | |
+---+---+ +---+---+ +----+----+
| 1 | o----->| 2 | o-----> | # | # |
+---+---+ +---+---+ +----+----+ */
third->data = 3; // assign data to third node
third->next = NULL;
/* data has been assigned to data part of third
block (block pointed by third). And next pointer
of the third block is made NULL to indicate
that the linked list is terminated here.
We have the linked list ready.
head
|
|
+---+---+ +---+---+ +----+------+
| 1 | o----->| 2 | o-----> | 3 | NULL |
+---+---+ +---+---+ +----+------+
Note that only head is sufficient to represent
the whole list. We can traverse the complete
list by following next pointers. */
/* To print each node's data, we have to traverse the linked list till the end.
Algorithm
1. Create a temporary node(temp) and assign the head node's address.
2. Print the data which present in the temp node.
3. After printing the data, move the temp pointer to the next node.
4. Do the above process until we reach the end.*/
//temp is a reference for head pointer.
struct Node *temp = head;
//till the node becomes null, printing each nodes data
while(temp != NULL)
{
printf("%d->",temp->data);
temp = temp->next;
}
//printf("NULL"); its ok
return 0;
}
To embed this program on your website, copy the following code and paste it into your website's HTML: