- Linked lists are linear data structures made up of nodes that contain data and a pointer to the next node. This allows them to be linked together to form a list.
- There are two types of linked lists: singly linked lists where each node has a pointer to the next node, and doubly linked lists where each node has pointers to both the next and previous nodes.
- Basic operations on linked lists include creating and inserting nodes, deleting nodes, searching the list, and reversing the list. Nodes are created dynamically and inserted at the beginning, end, or after a specified node using pointer manipulation.
3. What are linked lists?
A Linked Lists is a linear data structure.
Nodes make up linked lists.
Nodes are structures made up of data and a
pointer to another node.
Usually the pointer is called next.
5 * 6 * 7 * 8*
Null
head
4. Real Life Example:
A linked list is like scavenger hunt. You have a clue, and that
clue has a pointer to the next place having the next clue. So
you go to the next place and get another piece of data and
another pointer. To get something in the middle or at the end,
the only way is to follow the list from beginning.
5. Types of lists:
There are two types of lists:
1. Singly linked lists 2. Doubly linked lists
5 * 6*
Head
Node
NULLSingly list
5 * 6
Doubly list
5 *
Head
* *
NULL
prev
next
prev prev
next next
NULL
6. Basic Operation on a list
• Creating a list
• Inserting an element in a list
• Deleting an element in a list
• Searching a list
• Reversing a list
7. Creating a node
Struct node{
int data; //A simple node of a linked list
node*next;
}*start //Start points at the first node
Start =NULL; initialized to NULL at beginning
8. Node*create(int num) //say num=1 is passed from main
Node*ptr;
Ptr = new node; //memory located dynamically
if (ptr==NULL)
“OVERFLOW” //no memory available
exit(1);
Else {
ptr->data= num;
ptr->next=NULL;
return ptr;
}
10. Inserting the nod in singly
linked list:
There are 3 cases here:
1. Insertion at the beginning
2. Insertion at the end
3. Insertion at the particular node
11. Insertion at the beginning:
There are two steps to be followed:
1. Make the next pointer of the node point towards the first node of the list.
2. Make the start pointer point towards this new node
If the list is empty simply make the start pointer point towards the new node;
5
65 5
Head
New
node
12. void insert_beg(node* p)
{ node* temp;
if(start==NULL) //if the list is empty
{
start=p;
cout<<”nNode inserted successfully at the beginning”;
}
else {
temp=start;
start=p;
p->next=temp; //making new node point at
} the first node of the list
}
13. Insertion at the end:
Here we simply need to make the next pointer of the last
node point to the new node
96 100 102
prev New
node
Formerly null
15. Inserting after an element:
Here we again need to do 2 steps :-
1. Make the next pointer of the node to be inserted point to
the next node of the node after which you want to insert the
node
2. Make the next pointer of the node after which the node is
to be inserted, point to the node to be inserted