Professional Documents
Culture Documents
Unit Iv
Unit Iv
List – a number of connected items or names written or printed consecutively, typically one below the other.
List Structure – an abstract concept denoting an indexed collection of entities with a fixed length (ex. master
list of students) which is either an array or a linked list of any sort.
- It allows data to be linked to each other and handled in one lump.
Data section – that contains data element (usually a record type but possibilities are endless)
Pointer section – that contains an address; reference to the next node.
Linked list – can be visualized as a chain of nodes, where every node points to the next node.
Recall:
Pointer variable – is just a variable that contains the address where some other data is stored.
If p is declared to be a pointer to a record, then the value stored in p is interpreted as the location
where a record can be found.
A linked list is represented graphically by boxes and arrows.
Arrays – are static structures and therefore cannot be easily extended or reduced to fit the data set; expensive
to maintain new insertions and deletions.
Linked list – is a linear data structure where each element is a separate object connected together via links.
Self-referential Structures
- Structure that contains a pointer to a structure of the same type
- Can be linked together to form useful data structures such as lists, queues, stacks, and trees.
- Terminated with a NULL pointer (0)
*nextPtr – points to an object of type node; referred to as a link (ties one node to another node)
node – first data member of the structure; is an integer to hold an integer and the second data member is the
pointer to a node (same structure) which holds the address of the next node.
Linked list – does not store its node in consecutive memory locations. The number of nodes in a list is not fixed
and can grow and shrink on demand.
Linked list – does not allow random access of data or direct access to the individual elements. If you want to
access a particular item, then you have to start at the head and follow the references until you get to that item.
Dynamic Memory Allocation – obtain and release memory during execution
malloc
o takes number of bytes to allocate
sizeof – use to determine the size of an object
o returns pointer of type void *
a void * pointer may be assigned to any pointer
if no memory available, returns NULL
o example: newPtr = malloc(sizeof(struct node));
free
o deallocates memory allocated by malloc
o takes a pointer as an argument
o free (newPtr);
Inserting an array element
Uni-directional List
- Also called one-way list, simple linked list or singly-linked list
- Pointer section of a cell contains address of a cell, which the next data is stored.
- Search on data is done by tracking the addresses one by one.
Bi-directional List
– Also called doubly/double-linked list
– 2 pointer sections
– Data can be tracked from either the head or the tail of cells
- Navigation through the list can be both way – forward and backward.
- There are two references associated with each node. One of the reference points to the next node and
one to the previous node.
- Advantage: we can traverse in both directions and for deletion, we don’t need to have explicit access
to the previous node.
Ring List
– Also called circular list
– A bi-directional list that contains NULL in the first cell
– Data can be searched in similar way as that of bi-directional.
- The last item (TAIL) in the list contains a link to the first element (HEAD) as next and the first element
has a link to the last element as previous.
- Linked list where all nodes are connected to form a circle.
- There is no NULL at the end.
- Can be a singly circular linked list or doubly circular linked list.
- Advantage: Any node can be made as starting node. This is useful in implementation of circular queue
in linked list.
Addition/insertion
Deletion
Search (always begins at the head)
Addition/Insertion Operation
1. Adding a node at the beginning (prepend)
Deletion Operation
Search Operation