Linked List With Arrays (No Pseudocode)
Linked List With Arrays (No Pseudocode)
We can simulate a linked list using two arrays: one to store the data and another to
store the "links" (indices) to the next data element.
Arrays:
● dataArray: Stores the actual data values.
● nextPtrArray: Stores the index of the next element in dataArray. A special value
(e.g., -1) indicates the end of the list.
Variables:
● head: Stores the index of the first element in the linked list within dataArray.
● freePtr: Stores the index of the next available (unused) slot in dataArray.
Initialization:
1. Create the dataArray and nextPtrArray with a fixed size.
2. Initialize head to -1 (empty list).
3. Initialize freePtr to 0 (the first slot in the arrays).
4. Link all the slots in nextPtrArray to form a chain of available slots. The last slot in
nextPtrArray will contain -1.
Operations:
1. Add (Insert):
● Add at the Beginning:
1. Get the index of a free slot from freePtr.
2. Store the new data in dataArray at the obtained index.
3. Make the nextPtrArray value at the new data's index point to the current head.
4. Update head to point to the index of the new data.
● Add at the End:
1. Get the index of a free slot from freePtr.
2. Store the new data in dataArray at the obtained index.
3. Make the nextPtrArray value at the new data's index store -1.
4. If the list was initially empty, make head point to the new data's index.
5. Otherwise, traverse the list to find the last node and make its nextPtrArray
value point to the new data's index.
● Add at a Given Position:
1. Get the index of a free slot from freePtr.
2. Store the new data in dataArray at the obtained index.
3. If the position is 0, make the nextPtrArray value at the new data's index point
to the current head, and update head to the new data's index.
4. Otherwise, traverse the list to find the node at the given position.
5. Make the nextPtrArray value at the new data's index point to the node at the
given position.
6. Make the nextPtrArray value at the node before the given position point to the
new data's index.
2. Edit (Modify):
● To edit data, you need the position of the data you want to change.
1. If the list is empty, report an error.
2. Traverse the list to find the node at the given position.
3. If the position is invalid, report an error.
4. Change the value in dataArray at the found index.
3. Delete:
● Delete at the Beginning:
1. If the list is empty, report an error.
2. Store the index of the first node.
3. Update head to point to the next node using nextPtrArray.
4. Add the index of the deleted node to the freePtr chain.
● Delete at the End:
1. If the list is empty, report an error.
2. Traverse the list to find the last node. Keep track of the node before the last
node.
3. If there is only one node, update head to -1.
4. Otherwise, make the nextPtrArray value of the node before the last node -1.
5. Add the index of the deleted node to the freePtr chain.
● Delete at a Given Position:
1. If the list is empty, report an error.
2. If the position is 0:
■ Store the index of the first node.
■ Update head to point to the next node.
■ Add the deleted node's index to the freePtr chain.
3. Otherwise, traverse the list to find the node before the node to be deleted.
4. If the position is invalid, report an error.
5. Make the nextPtrArray value of the node before the deleted node point to the
node after the deleted node.
6. Add the deleted node's index to the freePtr chain.