Objectives
At the end of the class you should be able to answer:
Basic Operations
The four basic list operations are :
- Insertion
- Deletion
- Retrieval
- Traversal
Insertion
- Insertion is used to add a new element to the list.
- List insertion can be ordered or random.
- Ordered list are maintained in sequence according to the data or, when available, a key that identifies the data.
- In random lists there is no sequential relationship between two elements.Random lists are sometimes called chronological lists.
Deletion
- Deletion is used to remove an element from the list.
- Deletion from a list requires that the list be searched to locate the data being deleted
- Once located, the data are removed from the list.
Retrieval
- Retrieval is used to get the information related to an element without changing the structure of the list.
- List retrieval requires that data be located in a list and presented to the calling module without changing the contents of the list.
Traversal
- List traversal processes each element in a list in sequence
- It requires a looping algorithm rather than a search.
Implementation
- Several data structures can be used to implement a list; we use a linked list.
- Linked list is a good structure for a list because data are easily inserted and deleted at the beginning, in the middle, or at the end of the list.
Data Structure
To implement a list, we need two different structures, a head node and data node.
Algorithm
Create List:
Create list allocate the head structure and initializes the metadata for the list.
Figure 5-6 shows the header before and after it is initialized by create list.
Insert Node
Insert node adds data to a list. Given the predecessor, there are three steps to the insertion:
1. Allocate memory for the new node and move data to the node.
2. Point the new node to its successor.
3. Point the new node’s predecessor to the new node.
Insert at Beginning
- To insert a node at the beginning of the list, we simply point the new node to the first node of the list and then ser the head pointer to point to the new first node.
- Figure 5-8 shows inserting a node at the beginning
Insert in Middle
- To insert a node between two nodes, we point the new node to its successor and then point its predecessor to the new node.
- Figure 5-9 shows inserting a node in the middle.
Insert at End
- To add at the end of the list, we only need to point the predessor to the new node.
- The Figure5-10 insert the node at the end
Delete Node
- The delete node algorithm logically removes a node from the list by changing various link pointers and then physically delete the node from dynamic memory.
- Figure 5-11 shows Deleting the first node
Retrieve Node
Advance Linked List
- Circular Linked List
- Circular linked list is a type of linked list where the last node will point back to the first node
0 comments:
Post a Comment