1 / 25

Linked Lists

Linked Lists. list elements are stored, in memory, in an arbitrary order explicit information ( called a link) is used to go from one element to the next. a. b. c. d. e. c. a. e. d. b. Memory Layout. Layout of L = (a,b,c,d,e) using an array representation.

dleverett
Télécharger la présentation

Linked 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. Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next

  2. a b c d e c a e d b Memory Layout Layout of L = (a,b,c,d,e) using an array representation. A linked representation uses an arbitrary layout.

  3. first Linked Representation c a e d b pointer (or link) in e is NULL use a variable first to get to the first element a

  4. first NULL a b c d e Normal Way To Draw A Linked List link or pointer field of node data field of node

  5. Chain first NULL a b c d e A chain is a linked list in which each node represents one element. There is a link or pointer from one element to the next. The last node has a NULL (or 0) pointer.

  6. link data Node Representation typedef struct listNode *listPointer; typedef struct { char data; listPointer link; } listNode;

  7. first NULL a b c d e get(0) desiredNode = first; // gets you to first node return desiredNode->data;

  8. first NULL a b c d e get(1) desiredNode = first->link; // gets you to second node return desiredNode->data;

  9. first NULL a b c d e get(2) desiredNode = first->link->link; // gets you to third node return desiredNode->data;

  10. first NULL a b c d e get(5) desiredNode = first->link->link->link->link->link; // desiredNode = NULL return desiredNode->data; // NULL.element

  11. first NULL a b c d e Delete An Element delete(0) deleteNode = first; first = first->link; free(deleteNode);

  12. first b beforeNode NULL a b d e c c c delete(2) first get to node just before node to be removed beforeNode = first->link;

  13. delete(2) first null a b c d e save pointer to node that will be deleted beforeNode deleteNode = beforeNode->link;

  14. delete(2) first null a b c d e now change pointer in beforeNode beforeNode beforeNode->link = beforeNode->link->link; free(deleteNode);

  15. first NULL f a b c d e newNode insert(0,’f’) Step 1: get a node, set its data and link fields MALLOC( newNode, sizeof(*newNode)); newNode->data = ‘f’; newNode->link = NULL;

  16. first NULL f a b c d e newNode insert(0,’f’) Step 2: update first first = newNode;

  17. newNode f first NULL a b c d e c beforeNode insert(3,’f’) first find node whose index is 2 • next create a new node and set its data and link fields • finally link beforeNode to newNode

  18. newNode f first NULL a b c d e beforeNode c insert(3,’f’) beforeNode = first->link->link; MALLOC( newNode, sizeof(*newNode)); newNode->data = ‘f’; newNode->link = beforeNode->link; beforeNode->link = newNode;

  19. headerNode NULL a b c d e Chain With Header Node

  20. Empty Chain With Header Node headerNode NULL

  21. firstNode a b c d e Circular List

  22. firstNode lastNode NULL NULL a b c d e Doubly Linked List

  23. firstNode a b c d e Doubly Linked Circular List

  24. headerNode a b c d e Doubly Linked Circular List With Header Node

  25. Empty Doubly Linked Circular List With Header Node headerNode

More Related