200 likes | 269 Views
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.
E N D
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. • This allows access to any element at any time.
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.
http://ironbark.bendigo.latrobe.edu.au/~mal/bitdst/ds2000/session090/lect04f1.gifhttp://ironbark.bendigo.latrobe.edu.au/~mal/bitdst/ds2000/session090/lect04f1.gif
Linked Lists • To access the 3rd node in a linked list, you must traverse the first 2 to it. 1st 2nd 3rd
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).
Linked Lists Source: http://newdata.box.sk/bx/c/htm/ch11.htm
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
Sample Node struct struct Node { string item; int count; Node * link; } typedef Node* NodePtr; item link count
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;
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;
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;
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;
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;
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!
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;
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; }
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?
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; } }
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.