1 / 20

Linked lists

Linked lists. ITK 169 Fall 2003. Memory. Arrays and vectors are stored in consecutive memory storage space. The array name or vector name is actually a pointer to the first element. Traversing to the other elements is done by using pointer arithmetic.

blithe
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 ITK 169 Fall 2003

  2. Memory • Arrays and vectors are stored in consecutive memory storage space. • The array name or vector name is actually a pointer to the first element. • Traversing to the other elements is done by using pointer arithmetic. • This allows access to any element at any time.

  3. Linked Lists • Linked Lists are not necessarily stored in consecutive memory. • Linked Lists are constructed by nodes. • Each node has a pointer that points to the next node in the list.

  4. http://ironbark.bendigo.latrobe.edu.au/~mal/bitdst/ds2000/session090/lect04f1.gifhttp://ironbark.bendigo.latrobe.edu.au/~mal/bitdst/ds2000/session090/lect04f1.gif

  5. Linked Lists • To access the 3rd node in a linked list, you must traverse the first 2 to it. 1st 2nd 3rd

  6. Nodes • Each node in a list is similar to an element in an array. • Nodes hold data and are generally represented as a struct or class. • Each node must contain at least one pointer to connect it to the next in the list (doubly linked lists have 2 pointers).

  7. Linked Lists Source: http://newdata.box.sk/bx/c/htm/ch11.htm

  8. typedef • What is the problem with this declaration? int* p1, p2;// 1 pointer, 1 int • This can be solved by using typedef • typedef can be used to create new variable types. typedef int* IntPtr; IntPtr p1, p2;// 2 int pointers

  9. Sample Node struct struct Node { string item; int count; Node * link; } typedef Node* NodePtr; item link count

  10. In the Beginning • Each linked list starts with a special pointer called a head. • The head does not hold any data. • It is simply a node pointer that points to the first node in the list. NodePtr head; head = new Node;

  11. Assigning Data to Nodes • There are 2 ways to assign data to the first node: dereferencing and using the dot operator (*head).item = “candle”; (*head).count = 14; or using the arrow operator (more common) head->item = “candle”; head->count = 14;

  12. NULL pointers • Having a pointer that points to nothing can cause strange results. • NULL is a special constant value that is assigned to pointers to set them to ‘0’ (meaning no address). • It is important to assign NULL to the pointer variable of a new node. head->link = NULL;

  13. Build an Inventory List using the Part class struct PartNode { Part item; PartNode* next; }; typedef PartNode* NodePtr; NodePtr head = new PartNode; Fill the first node using the special constructor // special constructor to enter data into all members Part(string num1, double cost1, int num2, int num3) head->item = Part(“123”, 12.95, 12, 0); head->next = NULL;

  14. First Node Fill the first node using the special constructor // special constructor to enter data into all members Part(string num1, double cost1, int num2, int num3) head->item = Part(“123”, 12.95, 12, 0); head->next = NULL;

  15. Adding to the Front • Declare a new node. • Fill the node with data. • Attach it to the list. • Move the head to the new node. • Take care not to move the head until you have attached the new node!

  16. Adding a new Part // declare a temp node pointer and a new node NodePtr temp = new PartNode; // fill the new node temp->item = Part("345", 11.50, 15, 10); // set this new node to point to the first node temp->next = head; // move head to point to new node head = temp;

  17. Functions with Lists • To pass a linked list to a function, you simply pass the list’s head as a parameter. void front_insert(NodePtr& head, Part& newPart) { // declare a temp node pointer and declare a new node NodePtr temp = new PartNode; // fill the new node temp->item = newPart; // set this new node to point to the first node temp->next = head; // move head to point to new node head = temp; }

  18. Searching a List • Suppose we have an entire list of Parts with head pointing to the first node. • Suppose we want to find a particular Part with partNum = 442 • How would we proceed?

  19. Search Function // requires != and == to be overloaded for any class object passed NodePtr findPart(NodePtr head, const Part& target) { // set up temp pointer that starts at the first node in the list NodePtr here = head; // always check for empty list if(here == NULL) return NULL; else { while(here->item != target) here = here->next; if(here->item == target) return here; else // target not found return NULL; } }

  20. Optional Functions • The book discusses functions to add to the middle of a list and delete a node from a list. • You will not be responsible for this functionality.

More Related