Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an...
-
Upload
claude-stokes -
Category
Documents
-
view
214 -
download
0
Transcript of Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an...
![Page 1: Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.](https://reader036.fdocuments.us/reader036/viewer/2022082711/56649ee65503460f94bf617b/html5/thumbnails/1.jpg)
1
Lists
![Page 2: Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.](https://reader036.fdocuments.us/reader036/viewer/2022082711/56649ee65503460f94bf617b/html5/thumbnails/2.jpg)
2
Introduction
Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add an item at any position in the list Delete: Remove an item from the list at any position in the list Traverse: access and process elements
in order of occurrence
![Page 3: Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.](https://reader036.fdocuments.us/reader036/viewer/2022082711/56649ee65503460f94bf617b/html5/thumbnails/3.jpg)
3
Possible Implementations
• Array– capacity determined at compile-time– capacity determined at run-time– good choice if
• capacity known before list is constructed• insertions/deletions mainly at the end
• Linked List (dynamic structure)– capacity grows and shrinks as size changes– insertions/deletions do not require shifting – access to an item by index is not efficient
![Page 4: Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.](https://reader036.fdocuments.us/reader036/viewer/2022082711/56649ee65503460f94bf617b/html5/thumbnails/4.jpg)
Structural Concept
• Each element is a node• Space is dynamically allocated (and
returned) one node at a time• Items are not contiguous in memory
![Page 5: Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.](https://reader036.fdocuments.us/reader036/viewer/2022082711/56649ee65503460f94bf617b/html5/thumbnails/5.jpg)
linked list (dynamic)
• node contains >=1 data item and pointer to next node
• The last node's "next" pointer is "empty"• A separate pointer accesses first node
first
data next
![Page 6: Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.](https://reader036.fdocuments.us/reader036/viewer/2022082711/56649ee65503460f94bf617b/html5/thumbnails/6.jpg)
Accessing nodes
• no direct access to individual nodes• nodes only accessed via pointers• access types
– a list is a sequential access data structure• Because you cannot get directly to an item
– an array is a direct access data structure• Because a subscript gets you directly to an item
![Page 7: Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.](https://reader036.fdocuments.us/reader036/viewer/2022082711/56649ee65503460f94bf617b/html5/thumbnails/7.jpg)
7
Implementation
typedef Complx QueueElement;
struct QueueNode
{QueueNode * next;
QueueElement XX;
};
void enqueue (QueueNode * , Complx);
QueueNode * myFront; // Declare anchor
myFront= (QueueNode *) malloc (sizeof(QueueNode));
// above stmt creates list node 0, sets myFront
Complx X;
enqueue (myFront, X); // put X in the list
![Page 8: Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.](https://reader036.fdocuments.us/reader036/viewer/2022082711/56649ee65503460f94bf617b/html5/thumbnails/8.jpg)
8
Efficiency
• How do you insert?• How do you extract?• What about access in the "middle"?• Are some ways easier?
struct Node
{
int X;
Node * next;
};
![Page 9: Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.](https://reader036.fdocuments.us/reader036/viewer/2022082711/56649ee65503460f94bf617b/html5/thumbnails/9.jpg)
Inserting at position 0
// allocate space for a node & store item in itNode * aNode = new Node (item);
aNode -> next = first; // new node -> old head of list
first = aNode; //anchor -> new head of list
first
addedNode
![Page 10: Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.](https://reader036.fdocuments.us/reader036/viewer/2022082711/56649ee65503460f94bf617b/html5/thumbnails/10.jpg)
10
Inserting between the ends
• Locate position for insertion• Save "next"• Create new item• Change next -> new item• Store saved "next" in new item's "next"
first
addedNodetemp
![Page 11: Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.](https://reader036.fdocuments.us/reader036/viewer/2022082711/56649ee65503460f94bf617b/html5/thumbnails/11.jpg)
11
Removing a node
• Must be careful• Save the ptr to the node BEFORE the node
to be removed.• May have to "peek" at next item to decide if
you’re there yet• Save the "next" ptr of the node to remove• Put the saved "next" pointer into the "next"
of the previous node• Free old node (malloc/free or new/delete)
![Page 12: Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.](https://reader036.fdocuments.us/reader036/viewer/2022082711/56649ee65503460f94bf617b/html5/thumbnails/12.jpg)
12
DANGER!!!
• When removing a node– must save a ptr to it if re-use is possible– must delete everything the node points to
• free won't "chase down" additional storage
firsttemp
not freed
![Page 13: Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.](https://reader036.fdocuments.us/reader036/viewer/2022082711/56649ee65503460f94bf617b/html5/thumbnails/13.jpg)
13
Traversal
curr_ptr = first;
while (curr_ptr != NULL)
{compare data for correct node
curr_ptr=curr_ptr -> next; //advance
}
first
ptr to "current" node
![Page 14: Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.](https://reader036.fdocuments.us/reader036/viewer/2022082711/56649ee65503460f94bf617b/html5/thumbnails/14.jpg)
14
Two-way lists
struct Node
{ int X;
Node * next; // points forward in list
Node * prev; // points backward in list
};
• Insert & Delete functions more complex• Must have (or get) pointers to both sides
first
addedNodetemp
![Page 15: Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.](https://reader036.fdocuments.us/reader036/viewer/2022082711/56649ee65503460f94bf617b/html5/thumbnails/15.jpg)
15
Two-way lists-2
C=// the current node (maybe found with a search)
N=C->next; // save the tail_pointer
P=(Node*) malloc (Node); // get new node
P->next=N; // new node points to old tail of list (1)
P->prev=C; // cutoff point points to new node (2)
C->next=P; // old head points to new node (3)
N->prev=P; // old tail points to new node (4)
first
addedNode
CN
P 1
23
4