Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

UNIT IV: Lists

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.

Linked List Implementation


- Linear collection of self-referential class objects, called nodes.
- Connected by pointer links.
- Accessed via a pointer to the first node of the list. (HEAD)
- Subsequent nodes are accessed via the link-pointer member of the current node.
- Link pointer in the last node is set to null to mark the list’s end. (TAIL)
*Head is not a separated node, but the reference to the first node. If the list is empty, then the head is a null
reference.
The list contains cells (nodes) and each cell consists of the following:

 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.

 Shifting backward/forward for insertion/deletion respectively

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

 Inserting data into a list


o Replacing pointers preceding and subsequent to data to be inserted
o Do not have to shift elements as in the case of array-type data (easy and quick)

 Deleting data from a list


o Replace pointers (as for inserting data) into a list
o Cells that contain data remain in a memory area as garbage after data is deleted.

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.

– Navigation in this list is forward only.


– Every node stores address of reference of next node in list and the last node has next address or
reference as NULL.

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.

BASIC OPERATIONS (supported by linked list)

 Addition/insertion
 Deletion
 Search (always begins at the head)

Addition/Insertion Operation
1. Adding a node at the beginning (prepend)

2. Adding a node at the end (append)

3. Adding a node in between nodes (insert after prev)

Deletion Operation
Search Operation

LIST STRUCTURE SUMMARY

 Implemented using Array and Pointers (Linked List)


 Direction is usually one-way (unidirectional) but can be two-way (bi-directional) or circular.
 List structure allows data to be inserted or deleted by simply replacing pointers.
 Drawback is the need to track pointer one by one from the top one if one wishes to access specific
data.
 Use a linked list instead of an array when:
o You have an unpredictable number of data elements.
o Your list needs to be sorted quickly.
 Application:
o Used in computing numeric values that is too large for the computer to computer.
o Creation of programs involving the storing of records (i.e., master list, telephone directory, etc.)

You might also like