150 likes | 273 Views
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
E N D
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
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
Structural Concept • Each element is a node • Space is dynamically allocated (and returned) one nodeat a time • Items are notcontiguous in memory
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
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
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
Efficiency • How do you insert? • How do you extract? • What about access in the "middle"? • Are some ways easier? structNode { intX; Node * next; };
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
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
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)
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
first Traversal curr_ptr = first; while (curr_ptr != NULL) {compare data for correct node curr_ptr=curr_ptr -> next; //advance } ptr to "current" node
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
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