Linked List Lecture Note
Linked List Lecture Note
In sequential representation of a data structure, the items are implicitly ordered by the sequential
order of storage. Thus, if items[x] represent an element in a data structure the next item in the data
structure will be items[x+1]. Since the storage structure of the data structure helps to know the
address of a particular item within the data structure there is no need to worry about the address of
the item in the data structure. Suppose that the items of the data structure are explicitly ordered,
that means, each item contains within itself the address of the next item. Such explicit ordering
gives rise to a new data structure known as Linked List. And the resulting representation is called
Linked List Representation.
Definition
A Linked List is a collection of elements called nodes. Each node contains a data portion and a
pointer that contains the location of (points to) the node that logically follows this node. The entire
list is referenced by a separate pointer to the first element of the list.
head …
Data Pointer
Portion part
Node
Linked list can also be represented in the following way. Where start is a pointer to the first node
of the list and the pointer part of the first node points to the second node. The pointer part of the
last node points to NULL.
In sequential representation of lists one major problem is limitation in size. The most important
advantage of linked lists in dynamic storage is flexibility. Overflow is no problem until the
computer memory is exhausted. The other important advantage of linked lists is that insertions and
deletions can be made in the middle of the linked list more quickly than in the middle of a
contiguous list. If the records are large it is much quicker to change the values of a few pointers
than to copy the records themselves from one location to another. If the data items are arranged in
some type of order and it becomes necessary to insert into or delete from the middle of the list, a
considerable amount of data movement is involved in the case of contiguous lists. This data
movement requires computer time and decreases program efficiency. The central motivation
behind the linked list data structure is to eliminate the data movement associated with insertion
into and deletions from the middle of the list.
The first drawback of Linked Lists is the links (pointers) themselves take space. However, the
space used for a list node is not usually twice the space used by an array element, since the elements
in such a list usually consists of structures with many sub-fields. The other disadvantage of
linked lists is that they are not suited to random access. With sequential storage, the program can
refer to any position within the list as quickly as to any other position. With a linked list, it may be
necessary to traverse a long path to reach the desired node.
Operation Explanation
Initializes an empty linked list
1. create_list(head)
referenced by head
Checks whether the linked list
referenced by head is empty or not;
2. is_empty(head)
does it contain
any active node?
Checks whether the linked
list referenced by head is full; for
3. is_full(head)
array
implementation of a linked list
Inserts the node pointed by p into the
4. insert_node(head,p)
linked list referenced by head
Deletes the node pointed by p from
5. delete_node(head,p)
the linked list referenced by head
Searches the linked list referenced by
6. search_node(head,target) head for the node whose data field
matches a specified target value
//Global declaration
#include<iostream.h>
#include<stdlib.h>
#define MAX 100
#define null -1
struct node
{
[data type] data; //eg. int data, stores the data part of the node
int next; //next stores the address of the next node.
};