Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lists.

Similar presentations


Presentation on theme: "Lists."— Presentation transcript:

1 Lists

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

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

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 data next anchor

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

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; };

9 Inserting at position 0 Node * anchor ;
anchor=NULL; // no list exists yet // allocate space for a node & store item in it Node * aNode = malloc (sizeof(Node)); aNode -> next = anchor; // new node -> old head of list anchor = aNode; //anchor -> new head of list aNode anchor

10 Inserting at the end Node * last ;
// allocate space for a node & store item in it Node * aNode = malloc (sizeof(Node)); aNode -> next = anchor; // new node -> old head of list last=aNode; // point to new last element anchor aNode last

11 Inserting between any 2 nodes
Locate position for insertion (after “last”) Node * aNode=malloc(sizeof(aNode)); last->next = aNode; // point to new node aNode->next" =tail; aNode tail anchor last

12 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)

13 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 first temp not freed

14 Traversal curr_ptr = first; while (curr_ptr != NULL) {compare data for correct node curr_ptr=curr_ptr -> next; //advance } first ptr to "current" node

15 Two-way lists Insert & Delete functions more complex
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 addedNode temp first

16 Two-way lists-2 addedNode
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) addedNode P 1 4 2 first 3 C N


Download ppt "Lists."

Similar presentations


Ads by Google