Download presentation
Presentation is loading. Please wait.
Published byClaude Stokes Modified over 9 years ago
1
Lists 1
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 2
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 3
4
Structural Concept Each element is a node Space is dynamically allocated (and returned) one node at a time Items are not contiguous in memory
5
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
6
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
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 7
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; }; 8
9
Inserting at position 0 // allocate space for a node & store item in it Node * aNode = new Node (item); aNode -> next = first; // new node -> old head of list first = aNode; //anchor -> new head of list first addedNode
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" 10 first addedNode temp
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) 11
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 12 first temp not freed
13
Traversal curr_ptr = first; while (curr_ptr != NULL) {compare data for correct node curr_ptr=curr_ptr -> next; //advance } 13 first ptr to "current" node
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 14 first addedNode temp
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) 15 first addedNode C N P1 2 3 4
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.