1 / 15

Lists

Lists. 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

lesa
Download Presentation

Lists

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 nodeat a time • Items are notcontiguous in memory

  5. first linked list (dynamic) • nodecontains >=1 data item and pointer to next node • The last node's "next" pointer is "empty" • A separate pointer accesses first node data next

  6. Accessing nodes • no direct access to individual nodes • nodes only accessed via pointers • access types • a list is a sequential accessdata 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 typedefComplxQueueElement; 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? structNode { intX; Node * next; };

  9. first addedNode 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

  10. addedNode temp 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

  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)

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

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

  14. addedNode temp 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

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

More Related