SlideShare a Scribd company logo
 
Chapter 13 Pointers and Linked Lists Copyright © 2008 Pearson Addison-Wesley.  All rights reserved.
Overview 13.1  Nodes and Linked Lists  13.2  Stacks and Queues Slide 13-
13.1 Nodes and Linked Lists Copyright © 2008 Pearson Addison-Wesley.  All rights reserved.
Nodes and Linked Lists A linked list is a list that can grow and shrink while the program is running A linked list is constructed using pointers A linked list often consists of structs or classes that contain a pointer variable connecting them to other dynamic variables A linked list can be visualized as items, drawn as boxes, connected to other items by arrows Slide 13-  10 12 14 end head
The boxes in the previous drawing represent the  nodes of a linked list Nodes contain the data item(s) and a pointer that can point to another node of the same type The pointers point to the entire node, not an individual item that might be in the node The arrows in the drawing represent pointers  Nodes Slide 13-  Display 13.1
Nodes are implemented in C++ as structs or  classes Example:  A structure to store two data items and    a pointer to another node of the same type,   along with a type definition might be:   struct ListNode   {   string item;   int count;   ListNode *link;   };   typedef ListNode* ListNodePtr; Implementing Nodes Slide 13-  This circular definition  is allowed in C++
The head of a List The box labeled head, in display 13.1, is not a  node, but a pointer variable that points to a node Pointer variable head is declared as:   ListNodePtr head;  Slide 13-
Accessing Items in a Node Using the diagram of 13.1, this is one way to  change the number in the first node from  10 to 12:   (*head).count = 12; head is a pointer variable so *head is the node that head points to The parentheses are necessary because the dot operator . has higher precedence than the  dereference operator * Slide 13-
The arrow operator -> combines the actions of  the dereferencing  operator * and the dot operator to specify a member of a struct or object pointed to by a pointer (*head).count = 12;   can be written as   head->count = 12; The arrow operator is more commonly used The Arrow Operator Slide 13-  Display 13.2
NULL The defined constant NULL is used as… An end marker for a linked list A program can step through a list of nodes by following the pointers, but when it finds a node containing NULL, it knows it has come to the end of the list The value of a pointer that has nothing to point to The value of NULL is 0 Any pointer can be assigned the value NULL:   double* there = NULL; Slide 13-
To Use NULL A definition of NULL is found in several  libraries, including <iostream> and <cstddef> A using directive is not needed for NULL Slide 13-
Linked Lists The diagram in Display 13.2 depicts a linked list A linked list is a list of nodes in which each node  has a member variable that is a pointer that  points to the next node in the list The first node is called the head The pointer variable head, points to the first node The pointer named head is not the head of the list…it points to the head of the list The last node contains a pointer set to NULL Slide 13-
Building a Linked List: The node definition Let's begin with a simple node definition:     struct Node   {   int data;   Node *link;   };     typedef Node* NodePtr; Slide 13-
Building a Linked List: Declaring Pointer Variable head With the node defined and a type definition to  make or code easier to understand, we can  declare the pointer variable head:   NodePtr head; head is a pointer variable that will point to the  head node when the node is created  Slide 13-
Building a Linked List: Creating the First Node To create the first node, the operator new is used to create a new dynamic variable:   head = new Node; Now head points to the first, and only, node in the list Slide 13-
Building a Linked List: Initializing the Node Now that head points to a node, we need to  give values to the member variables of the node:   head->data = 3;   head->link = NULL; Since this node is the last node, the link is set to NULL Slide 13-
Function head_insert It would be better to create a function to insert nodes at the head of a list, such as: void head_insert(NodePtr& head, int the_number); The first parameter is a NodePtr parameter that points to the first node in the linked list The second parameter is the number to store in the list head_insert will create a new node for the number The number will be copied to the new node The new node will be inserted in the list as the new head node Slide 13-
Create a new dynamic variable pointed to by  temp_ptr Place the data in the new node called *temp_ptr Make temp_ptr's link variable point to the head node  Make the head pointer point to temp_ptr Pseudocode for head_insert Slide 13-  Display 13.3
The pseudocode for head_insert can be written in C++ using these lines in place of the lines of  pseudocode: NodePtr temp_ptr;   //create the temporary pointer  temp_ptr = new Node;  // create the new node temp_ptr->data = the_number;  //copy the number temp_ptr->link = head;  //new node points to first node head = temp_ptr;  // head points to new    // first node   Translating head_insert to C++ Slide 13-  Display 13.4
An Empty List A list with nothing in it is called an empty list An empty linked list has no head node The head pointer of an empty list is NULL   head = NULL; Any functions written to manipulate a linked list should check to see if it works on the empty list Slide 13-
You might be tempted to write head_insert using the head pointer to construct the new node:   head = new Node;   head->data = the_number; Now to attach the new node to the list The node that head used to point to is now lost! Losing Nodes Slide 13-  Display 13.5
Memory Leaks Nodes that are lost by assigning their pointers a  new address are not accessible any longer The program has no way to refer to the nodes and cannot delete them to return their memory to the freestore Programs that lose nodes have a memory leak Significant memory leaks can cause system crashes Slide 13-
Searching a Linked List To design a function that will locate a particular node in a linked list: We want the function to return a pointer to the node so we can use the data if we find it, else return NULL The linked list is one argument to the function The data we wish to find is the other argument This declaration will work:   NodePtr search(NodePtr head, int target); Slide 13-
Refining our function We will use a local pointer variable, named here, to move through the list checking for the target The only way to move around a linked list is to follow pointers We will start with here pointing to the first node and move the pointer from node to node following the pointer out of each node Function search Slide 13-  Display 13.6
Pseudocode for search Make pointer variable here point to the head node while(here does not point to a node containing target   AND here does not point to the last node)  {   make here point to the next node  } If (here points to a node containing the target)   return here;   else   return NULL; Slide 13-
Moving Through the List The pseudocode for search requires that pointer here step through the list How does here follow the pointers from node to node? When here points to a node, here->link is the  address of the next node To make here point to the next node, make the  assignment:   here = here->link; Slide 13-
A Refinement of search The search function can be refined in this way: here = head; while(here->data != target && here->link != NULL)   {   here = here->next;   }   if (here->data = = target)   return here;   else   return NULL; Slide 13-  Check for last node
Our search algorithm has a problem If the list is empty, here equals NULL before the while loop so… here->data is undefined here->link is undefined The empty list requires a special case in our  search function A refined search function that handles an empty list is shown in  Searching an Empty List Slide 13-  Display 13.7
Pointers as Iterators An iterator is a construct that allows you to  cycle through the data items in a data structure to perform an action on each item An iterator can be an object of an iterator class,  an array index, or simply a pointer A general outline using a pointer as an iterator:   Node_Type *iter;   for (iter = Head; iter != NULL; iter = iter->Link)   //perform the action on the node iter points to Head is a pointer to the head node of the list Slide 13-
Iterator Example Using the previous outline of an iterator we  can display the contents of a linked list in this way:   NodePtr iter;   for (iter = Head; iter != NULL; iter = iter->Link)   cout << (iter->data); Slide 13-
To insert a node after a specified node in the  linked list: Use another function to obtain a pointer to the node after which the new node will be inserted  Call the pointer after_me Use function insert, declared here to insert the node:   void insert(NodePtr after_me, int the_number); Inserting a Node Inside a List Slide 13-  Display 13.8
Inserting the New Node Function insert creates the new node just as  head_insert did We do not want our new node at the head of the  list however, so… We use the pointer after_me to insert the new node Slide 13-
Inserting the New Node This code will accomplish the insertion of the  new node, pointed to by temp_ptr, after the node pointed to by after_me:   temp_ptr->link = after_me->link;   after_me->link = temp_ptr; Slide 13-  head after_me temp_ptr 2 2 3 2 7 2 9 0 5 2
The order of pointer assignments is critical If we changed after_me->link to point to  temp_ptr first, we would loose the rest of the list! The complete insert function is shown  in  Caution! Slide 13-  Display 13.9
Function insert Again Notice that inserting into a linked list requires  that you only change two pointers This is true regardless of the length of the list Using an array for the list would involve copying as many as all of the array elements to new locations to make room for the new item Inserting into a linked list is often more efficient than inserting into an array Slide 13-
To remove a node from a linked list Position a pointer, before,  to point at the node prior to the node to remove Position a pointer, discard,  to point at the node to remove Perform:  before->link = discard->link; The node is removed from the list, but is still in memory Return *discard  to the freestore:  delete discard; Removing a Node Slide 13-  Display 13.10
Assignment With Pointers If head1 and head2 are pointer variables and  head1 points to the head node of a list:   head2 = head1; causes head2 and head1 to point to the same list There is only one list! If you want head2 to point to a separate copy, you must copy the list node by node or overload the assignment operator appropriately Slide 13-
Many other data structures can be constructed using nodes and pointers Doubly-Linked List Each node has two links, one to the next node and one to the previous node Allows easy traversal of the list in both directions struct Node { int data;   Node *forward_link;   Node *back_link; }; Variations on Linked Lists Slide 13-  Display 13.11
A tree is a data structure that looks like an upside-down tree with the root at the top No cycles In a binary tree each node has at most two links Binary Tree Slide 13-  struct TreeNode {   int data;   TreeNode *left_link;   TreeNode *right_link; }; Display 13.12
The preceding examples created linked lists of structs.  We can also create linked lists using classes. Logic to use a class is identical except the syntax of using and defining a class should be substituted in place of that for a struct Interface and Definition for a Node Class Linked List of Classes Slide 13-  Display 13.13 Display 13.14 (1-2)
Program using the Node class We can create a linked list of numbers using the Node class. Slide 13-  Display 13.15 (1-3)
Section 13.1 Conclusion Can you Write type definitions for the nodes and pointers in a linked list?  Call the node type NodeType and call the pointer type PointerType.  The linked lists will be lists of letters. Explain why inserting into an array can be less  efficient than inserting into a linked list? Slide 13-
13.2 Stacks and Queues Copyright © 2008 Pearson Addison-Wesley.  All rights reserved.
A stack is a data structure that retrieves data in the reverse order the data was stored If 'A', 'B', and then 'C' are placed in a stack, they will be removed in the order 'C', 'B', and then 'A' A stack is a last-in/first-out data structure like the stack of plates in a cafeteria; adding a plate pushes down the stack and the top plate is the first one removed A Linked List Application Slide 13-  Display 13.16
We will create a stack class to store characters Adding an item to a stack is pushing onto the stack Member function push will perform this task Removing an item from the stack is popping the the item off the stack Member function pop will perform this task contains the stack class interface  Program Example: A Stack Class Slide 13-  Display 13.17
demonstrates the use of the stack class  Using the stack Class Slide 13-  Display 13.18 (1-2)
Function push The push function adds an item to the stack It uses a parameter of the type stored in the stack   void push(char  the_symbol); Pushing an item onto the stack is precisely the same task accomplished by function head_insert of the linked list For a stack, a pointer named top is used instead of a pointer named head Slide 13-
Function pop The pop function returns the item that was at  the top of the stack   char pop( ); Before popping an item from a stack, pop checks  that the stack is not empty pop stores the top item in a local variable result,  and the  item is &quot;popped&quot; by:  top = top->link; A temporary pointer must point to the old top item  so it can be &quot;deleted&quot;  to prevent a memory leak pop then returns variable result Slide 13-
Empty Stack An empty stack is identified by setting the top pointer to NULL   top = NULL; Slide 13-
The Copy Constructor Because the stack class uses a pointer and creates new nodes using new, a copy constructor is needed The copy constructor (a self-test exercise) must make a copy of each item in the stack and store the copies in a new stack Items in the new stack must be in the same position in the stack as in the original Slide 13-
The stack destructor Because function pop calls delete each time an item is popped off the stack, ~stack only needs   to call pop until the stack is empty     char next;     while( ! empty ( ) )   {   next = pop( );   }  Slide 13-
The stack class implementation is  found in  stack Class Implementation Slide 13-  Display 13.19 (1) Display 13.19 (2)
Section 13.2 Conclusion Can you Give the definition of member function push? Create a definition for the stack class copy  constructor? Slide 13-
Chapter 13 -- End Slide 13-
Display 13.1  Slide 13-  Back Next
Display 13.2 Slide 13-  Back Next
Display 13.3 Slide 13-  Back Next
Display 13.4 Slide 13-  Back Next
Display 13.5 Slide 13-  Back Next
Display 13.6 Slide 13-  Back Next
Display 13.7 Slide 13-  Back Next
Display 13.8 Slide 13-  Next Back
Display 13.9 Slide 13-  Back Next
Display 13.10 Slide 13-  Next Back
Display 13.11 Slide 13-  Back Next
Display 13.12 Slide 13-  Back Next
Display 13.13 Slide 13-  Next Back
Display 13.14 (1/2) Slide 13-  Back Next
Display 13.14 (2/2) Slide 13-  Next Back
Display 13.15 (1/3) Slide 13-  Back Next
Display 13.15 (2/3)  Slide 13-  Next Back
Display 13.15 (3/3) Slide 13-  Back Next
Display 13.16 Slide 13-  Back Next
Display 13.17 Slide 13-  Back Next
Display 13.18 (1/2) Slide 13-  Back Next
Display 13.18 (2/2) Slide 13-  Back Next
Display 13.19 (1/2) Slide 13-  Back Next
Display 13.19 (2/2) Slide 13-  Back Next
Display 13.20 Slide 13-  Back Next
Display 13.21 (1/2) Slide 13-  Back Next
Display 13.21 (2/2) Slide 13-  Back Next
Display 13.22 (1/2) Slide 13-  Back Next
Display 13.22 (2/2) Slide 13-  Back Next

More Related Content

PPT
Savitch ch 13
Terry Yoast
 
PPT
Link List
umiekalsum
 
PPTX
Linked lists in Data Structure
Muhazzab Chouhadry
 
PPTX
Data Structures - Lecture 7 [Linked List]
Muhammad Hammad Waseem
 
PPT
Data Structure Lecture 5
Teksify
 
PPT
Data structure lecture 5
Kumar
 
PPTX
Linked lists 1
naymulhaque
 
Savitch ch 13
Terry Yoast
 
Link List
umiekalsum
 
Linked lists in Data Structure
Muhazzab Chouhadry
 
Data Structures - Lecture 7 [Linked List]
Muhammad Hammad Waseem
 
Data Structure Lecture 5
Teksify
 
Data structure lecture 5
Kumar
 
Linked lists 1
naymulhaque
 

What's hot (20)

PPT
header, circular and two way linked lists
student
 
PDF
Circular linked list
maamir farooq
 
PPT
linked list
Narendra Chauhan
 
PPT
Circular linked list
dchuynh
 
PPTX
CSE240 Doubly Linked Lists
Garrett Gutierrez
 
PPTX
Linked list
VONI
 
PPTX
Linked lists a
Khuram Shahzad
 
PPTX
Linklist
SHEETAL WAGHMARE
 
PPT
Notes fp201-pointer notes
Siti Nadirah
 
PPSX
Data Structure (Dynamic Array and Linked List)
Adam Mukharil Bachtiar
 
PPT
Unit ii(dsc++)
Durga Devi
 
PPTX
Fundamentals of Pointers in C
ShivanshuVerma11
 
PPTX
Computer Science Assignment Help
Programming Homework Help
 
PPTX
Linked list
akshat360
 
PPTX
Linear data structure concepts
Akila Krishnamoorthy
 
PPT
Linked list
eShikshak
 
PPTX
Function Pointer
Dr-Dipali Meher
 
PPSX
Data Structure (Double Linked List)
Adam Mukharil Bachtiar
 
PDF
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
header, circular and two way linked lists
student
 
Circular linked list
maamir farooq
 
linked list
Narendra Chauhan
 
Circular linked list
dchuynh
 
CSE240 Doubly Linked Lists
Garrett Gutierrez
 
Linked list
VONI
 
Linked lists a
Khuram Shahzad
 
Notes fp201-pointer notes
Siti Nadirah
 
Data Structure (Dynamic Array and Linked List)
Adam Mukharil Bachtiar
 
Unit ii(dsc++)
Durga Devi
 
Fundamentals of Pointers in C
ShivanshuVerma11
 
Computer Science Assignment Help
Programming Homework Help
 
Linked list
akshat360
 
Linear data structure concepts
Akila Krishnamoorthy
 
Linked list
eShikshak
 
Function Pointer
Dr-Dipali Meher
 
Data Structure (Double Linked List)
Adam Mukharil Bachtiar
 
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
Ad

Viewers also liked (20)

PPT
Savitch Ch 17
Terry Yoast
 
PPT
Savitch Ch 02
Terry Yoast
 
PPT
Savitch Ch 07
Terry Yoast
 
PPT
Savitch Ch 12
Terry Yoast
 
PPT
Savitch Ch 10
Terry Yoast
 
PPT
Savitch c++ ppt figs ch1
Terry Yoast
 
PPT
Savitch ch 04
Terry Yoast
 
PPT
Savitch ch 022
Dr .Ahmed Tawwab
 
PPT
Savitch Ch 08
Terry Yoast
 
PPT
Savitch Ch 11
Terry Yoast
 
PPT
Savitch Ch 01
Terry Yoast
 
PPT
Savitch Ch 06
Terry Yoast
 
PPT
Savitch Ch 03
Terry Yoast
 
PPT
Savitch Ch 15
Terry Yoast
 
PPT
Savitch ch 01
Terry Yoast
 
PPT
Savitch Ch 18
Terry Yoast
 
PPT
Savitch ch 16
Terry Yoast
 
PPT
Savitch Ch 14
Terry Yoast
 
PPT
Savitch Ch 04
Terry Yoast
 
PPT
Savitch Ch 05
Terry Yoast
 
Savitch Ch 17
Terry Yoast
 
Savitch Ch 02
Terry Yoast
 
Savitch Ch 07
Terry Yoast
 
Savitch Ch 12
Terry Yoast
 
Savitch Ch 10
Terry Yoast
 
Savitch c++ ppt figs ch1
Terry Yoast
 
Savitch ch 04
Terry Yoast
 
Savitch ch 022
Dr .Ahmed Tawwab
 
Savitch Ch 08
Terry Yoast
 
Savitch Ch 11
Terry Yoast
 
Savitch Ch 01
Terry Yoast
 
Savitch Ch 06
Terry Yoast
 
Savitch Ch 03
Terry Yoast
 
Savitch Ch 15
Terry Yoast
 
Savitch ch 01
Terry Yoast
 
Savitch Ch 18
Terry Yoast
 
Savitch ch 16
Terry Yoast
 
Savitch Ch 14
Terry Yoast
 
Savitch Ch 04
Terry Yoast
 
Savitch Ch 05
Terry Yoast
 
Ad

Similar to Savitch Ch 13 (20)

PPT
Mi 103 linked list
Amit Vats
 
PPT
Link list part 1
Anaya Zafar
 
PPT
Algo>ADT list & linked list
Ain-ul-Moiz Khawaja
 
PPT
linked-list - Abstract data type (ADT) Linked Lists
Anil Yadav
 
PPT
Abstract data types
JAGDEEPKUMAR23
 
PPT
List
Amit Vats
 
PPT
Lecture 3 List of Data Structures & Algorithms
haseebanjum2611
 
PPT
Chapter 5 ds
Hanif Durad
 
PPT
Array linked list.ppt
Waf1231
 
PPTX
Linked lists
Eleonora Ciceri
 
PPT
linkedlistwith animations.ppt
MuhammadShafi89
 
PPT
dynamicList.ppt
ssuser0be977
 
PDF
computer notes - Linked list inside computer memory
ecomputernotes
 
PPT
Ch17
Abbott
 
PDF
Lec-4_Linked-List (1).pdf
KylaMaeGarcia1
 
PPT
linked_lists.ppt linked_lists linked_lists
AmsaAzeem
 
PPTX
Linked List.pptx
PoonamPatil120
 
PPTX
DS_LinkedList.pptx
msohail37
 
PPT
linked-list.ppt
DikkySuryadiSKomMKom
 
PPTX
3.linked list
Chandan Singh
 
Mi 103 linked list
Amit Vats
 
Link list part 1
Anaya Zafar
 
Algo>ADT list & linked list
Ain-ul-Moiz Khawaja
 
linked-list - Abstract data type (ADT) Linked Lists
Anil Yadav
 
Abstract data types
JAGDEEPKUMAR23
 
List
Amit Vats
 
Lecture 3 List of Data Structures & Algorithms
haseebanjum2611
 
Chapter 5 ds
Hanif Durad
 
Array linked list.ppt
Waf1231
 
Linked lists
Eleonora Ciceri
 
linkedlistwith animations.ppt
MuhammadShafi89
 
dynamicList.ppt
ssuser0be977
 
computer notes - Linked list inside computer memory
ecomputernotes
 
Ch17
Abbott
 
Lec-4_Linked-List (1).pdf
KylaMaeGarcia1
 
linked_lists.ppt linked_lists linked_lists
AmsaAzeem
 
Linked List.pptx
PoonamPatil120
 
DS_LinkedList.pptx
msohail37
 
linked-list.ppt
DikkySuryadiSKomMKom
 
3.linked list
Chandan Singh
 

More from Terry Yoast (20)

PPT
9781305078444 ppt ch12
Terry Yoast
 
PPT
9781305078444 ppt ch11
Terry Yoast
 
PPT
9781305078444 ppt ch10
Terry Yoast
 
PPT
9781305078444 ppt ch09
Terry Yoast
 
PPT
9781305078444 ppt ch08
Terry Yoast
 
PPT
9781305078444 ppt ch07
Terry Yoast
 
PPT
9781305078444 ppt ch06
Terry Yoast
 
PPT
9781305078444 ppt ch05
Terry Yoast
 
PPT
9781305078444 ppt ch04
Terry Yoast
 
PPT
9781305078444 ppt ch03
Terry Yoast
 
PPT
9781305078444 ppt ch02
Terry Yoast
 
PPT
9781305078444 ppt ch01
Terry Yoast
 
PPTX
9781337102087 ppt ch13
Terry Yoast
 
PPTX
9781337102087 ppt ch18
Terry Yoast
 
PPTX
9781337102087 ppt ch17
Terry Yoast
 
PPTX
9781337102087 ppt ch16
Terry Yoast
 
PPTX
9781337102087 ppt ch15
Terry Yoast
 
PPTX
9781337102087 ppt ch14
Terry Yoast
 
PPTX
9781337102087 ppt ch12
Terry Yoast
 
PPTX
9781337102087 ppt ch11
Terry Yoast
 
9781305078444 ppt ch12
Terry Yoast
 
9781305078444 ppt ch11
Terry Yoast
 
9781305078444 ppt ch10
Terry Yoast
 
9781305078444 ppt ch09
Terry Yoast
 
9781305078444 ppt ch08
Terry Yoast
 
9781305078444 ppt ch07
Terry Yoast
 
9781305078444 ppt ch06
Terry Yoast
 
9781305078444 ppt ch05
Terry Yoast
 
9781305078444 ppt ch04
Terry Yoast
 
9781305078444 ppt ch03
Terry Yoast
 
9781305078444 ppt ch02
Terry Yoast
 
9781305078444 ppt ch01
Terry Yoast
 
9781337102087 ppt ch13
Terry Yoast
 
9781337102087 ppt ch18
Terry Yoast
 
9781337102087 ppt ch17
Terry Yoast
 
9781337102087 ppt ch16
Terry Yoast
 
9781337102087 ppt ch15
Terry Yoast
 
9781337102087 ppt ch14
Terry Yoast
 
9781337102087 ppt ch12
Terry Yoast
 
9781337102087 ppt ch11
Terry Yoast
 

Recently uploaded (20)

PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PPTX
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PDF
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
PDF
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
PPTX
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
PDF
5.EXPLORING-FORCES-Detailed-Notes.pdf/8TH CLASS SCIENCE CURIOSITY
Sandeep Swamy
 
PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
PPTX
IMMUNIZATION PROGRAMME pptx
AneetaSharma15
 
PPTX
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
PPTX
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
PPTX
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
PPTX
Strengthening open access through collaboration: building connections with OP...
Jisc
 
PPTX
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
PDF
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
PDF
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
PPTX
How to Manage Global Discount in Odoo 18 POS
Celine George
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
5.EXPLORING-FORCES-Detailed-Notes.pdf/8TH CLASS SCIENCE CURIOSITY
Sandeep Swamy
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
IMMUNIZATION PROGRAMME pptx
AneetaSharma15
 
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
Strengthening open access through collaboration: building connections with OP...
Jisc
 
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
How to Manage Global Discount in Odoo 18 POS
Celine George
 

Savitch Ch 13

  • 1.  
  • 2. Chapter 13 Pointers and Linked Lists Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
  • 3. Overview 13.1 Nodes and Linked Lists 13.2 Stacks and Queues Slide 13-
  • 4. 13.1 Nodes and Linked Lists Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
  • 5. Nodes and Linked Lists A linked list is a list that can grow and shrink while the program is running A linked list is constructed using pointers A linked list often consists of structs or classes that contain a pointer variable connecting them to other dynamic variables A linked list can be visualized as items, drawn as boxes, connected to other items by arrows Slide 13- 10 12 14 end head
  • 6. The boxes in the previous drawing represent the nodes of a linked list Nodes contain the data item(s) and a pointer that can point to another node of the same type The pointers point to the entire node, not an individual item that might be in the node The arrows in the drawing represent pointers Nodes Slide 13- Display 13.1
  • 7. Nodes are implemented in C++ as structs or classes Example: A structure to store two data items and a pointer to another node of the same type, along with a type definition might be: struct ListNode { string item; int count; ListNode *link; }; typedef ListNode* ListNodePtr; Implementing Nodes Slide 13- This circular definition is allowed in C++
  • 8. The head of a List The box labeled head, in display 13.1, is not a node, but a pointer variable that points to a node Pointer variable head is declared as: ListNodePtr head; Slide 13-
  • 9. Accessing Items in a Node Using the diagram of 13.1, this is one way to change the number in the first node from 10 to 12: (*head).count = 12; head is a pointer variable so *head is the node that head points to The parentheses are necessary because the dot operator . has higher precedence than the dereference operator * Slide 13-
  • 10. The arrow operator -> combines the actions of the dereferencing operator * and the dot operator to specify a member of a struct or object pointed to by a pointer (*head).count = 12; can be written as head->count = 12; The arrow operator is more commonly used The Arrow Operator Slide 13- Display 13.2
  • 11. NULL The defined constant NULL is used as… An end marker for a linked list A program can step through a list of nodes by following the pointers, but when it finds a node containing NULL, it knows it has come to the end of the list The value of a pointer that has nothing to point to The value of NULL is 0 Any pointer can be assigned the value NULL: double* there = NULL; Slide 13-
  • 12. To Use NULL A definition of NULL is found in several libraries, including <iostream> and <cstddef> A using directive is not needed for NULL Slide 13-
  • 13. Linked Lists The diagram in Display 13.2 depicts a linked list A linked list is a list of nodes in which each node has a member variable that is a pointer that points to the next node in the list The first node is called the head The pointer variable head, points to the first node The pointer named head is not the head of the list…it points to the head of the list The last node contains a pointer set to NULL Slide 13-
  • 14. Building a Linked List: The node definition Let's begin with a simple node definition: struct Node { int data; Node *link; }; typedef Node* NodePtr; Slide 13-
  • 15. Building a Linked List: Declaring Pointer Variable head With the node defined and a type definition to make or code easier to understand, we can declare the pointer variable head: NodePtr head; head is a pointer variable that will point to the head node when the node is created Slide 13-
  • 16. Building a Linked List: Creating the First Node To create the first node, the operator new is used to create a new dynamic variable: head = new Node; Now head points to the first, and only, node in the list Slide 13-
  • 17. Building a Linked List: Initializing the Node Now that head points to a node, we need to give values to the member variables of the node: head->data = 3; head->link = NULL; Since this node is the last node, the link is set to NULL Slide 13-
  • 18. Function head_insert It would be better to create a function to insert nodes at the head of a list, such as: void head_insert(NodePtr& head, int the_number); The first parameter is a NodePtr parameter that points to the first node in the linked list The second parameter is the number to store in the list head_insert will create a new node for the number The number will be copied to the new node The new node will be inserted in the list as the new head node Slide 13-
  • 19. Create a new dynamic variable pointed to by temp_ptr Place the data in the new node called *temp_ptr Make temp_ptr's link variable point to the head node Make the head pointer point to temp_ptr Pseudocode for head_insert Slide 13- Display 13.3
  • 20. The pseudocode for head_insert can be written in C++ using these lines in place of the lines of pseudocode: NodePtr temp_ptr; //create the temporary pointer temp_ptr = new Node; // create the new node temp_ptr->data = the_number; //copy the number temp_ptr->link = head; //new node points to first node head = temp_ptr; // head points to new // first node Translating head_insert to C++ Slide 13- Display 13.4
  • 21. An Empty List A list with nothing in it is called an empty list An empty linked list has no head node The head pointer of an empty list is NULL head = NULL; Any functions written to manipulate a linked list should check to see if it works on the empty list Slide 13-
  • 22. You might be tempted to write head_insert using the head pointer to construct the new node: head = new Node; head->data = the_number; Now to attach the new node to the list The node that head used to point to is now lost! Losing Nodes Slide 13- Display 13.5
  • 23. Memory Leaks Nodes that are lost by assigning their pointers a new address are not accessible any longer The program has no way to refer to the nodes and cannot delete them to return their memory to the freestore Programs that lose nodes have a memory leak Significant memory leaks can cause system crashes Slide 13-
  • 24. Searching a Linked List To design a function that will locate a particular node in a linked list: We want the function to return a pointer to the node so we can use the data if we find it, else return NULL The linked list is one argument to the function The data we wish to find is the other argument This declaration will work: NodePtr search(NodePtr head, int target); Slide 13-
  • 25. Refining our function We will use a local pointer variable, named here, to move through the list checking for the target The only way to move around a linked list is to follow pointers We will start with here pointing to the first node and move the pointer from node to node following the pointer out of each node Function search Slide 13- Display 13.6
  • 26. Pseudocode for search Make pointer variable here point to the head node while(here does not point to a node containing target AND here does not point to the last node) { make here point to the next node } If (here points to a node containing the target) return here; else return NULL; Slide 13-
  • 27. Moving Through the List The pseudocode for search requires that pointer here step through the list How does here follow the pointers from node to node? When here points to a node, here->link is the address of the next node To make here point to the next node, make the assignment: here = here->link; Slide 13-
  • 28. A Refinement of search The search function can be refined in this way: here = head; while(here->data != target && here->link != NULL) { here = here->next; } if (here->data = = target) return here; else return NULL; Slide 13- Check for last node
  • 29. Our search algorithm has a problem If the list is empty, here equals NULL before the while loop so… here->data is undefined here->link is undefined The empty list requires a special case in our search function A refined search function that handles an empty list is shown in Searching an Empty List Slide 13- Display 13.7
  • 30. Pointers as Iterators An iterator is a construct that allows you to cycle through the data items in a data structure to perform an action on each item An iterator can be an object of an iterator class, an array index, or simply a pointer A general outline using a pointer as an iterator: Node_Type *iter; for (iter = Head; iter != NULL; iter = iter->Link) //perform the action on the node iter points to Head is a pointer to the head node of the list Slide 13-
  • 31. Iterator Example Using the previous outline of an iterator we can display the contents of a linked list in this way: NodePtr iter; for (iter = Head; iter != NULL; iter = iter->Link) cout << (iter->data); Slide 13-
  • 32. To insert a node after a specified node in the linked list: Use another function to obtain a pointer to the node after which the new node will be inserted Call the pointer after_me Use function insert, declared here to insert the node: void insert(NodePtr after_me, int the_number); Inserting a Node Inside a List Slide 13- Display 13.8
  • 33. Inserting the New Node Function insert creates the new node just as head_insert did We do not want our new node at the head of the list however, so… We use the pointer after_me to insert the new node Slide 13-
  • 34. Inserting the New Node This code will accomplish the insertion of the new node, pointed to by temp_ptr, after the node pointed to by after_me: temp_ptr->link = after_me->link; after_me->link = temp_ptr; Slide 13- head after_me temp_ptr 2 2 3 2 7 2 9 0 5 2
  • 35. The order of pointer assignments is critical If we changed after_me->link to point to temp_ptr first, we would loose the rest of the list! The complete insert function is shown in Caution! Slide 13- Display 13.9
  • 36. Function insert Again Notice that inserting into a linked list requires that you only change two pointers This is true regardless of the length of the list Using an array for the list would involve copying as many as all of the array elements to new locations to make room for the new item Inserting into a linked list is often more efficient than inserting into an array Slide 13-
  • 37. To remove a node from a linked list Position a pointer, before, to point at the node prior to the node to remove Position a pointer, discard, to point at the node to remove Perform: before->link = discard->link; The node is removed from the list, but is still in memory Return *discard to the freestore: delete discard; Removing a Node Slide 13- Display 13.10
  • 38. Assignment With Pointers If head1 and head2 are pointer variables and head1 points to the head node of a list: head2 = head1; causes head2 and head1 to point to the same list There is only one list! If you want head2 to point to a separate copy, you must copy the list node by node or overload the assignment operator appropriately Slide 13-
  • 39. Many other data structures can be constructed using nodes and pointers Doubly-Linked List Each node has two links, one to the next node and one to the previous node Allows easy traversal of the list in both directions struct Node { int data; Node *forward_link; Node *back_link; }; Variations on Linked Lists Slide 13- Display 13.11
  • 40. A tree is a data structure that looks like an upside-down tree with the root at the top No cycles In a binary tree each node has at most two links Binary Tree Slide 13- struct TreeNode { int data; TreeNode *left_link; TreeNode *right_link; }; Display 13.12
  • 41. The preceding examples created linked lists of structs. We can also create linked lists using classes. Logic to use a class is identical except the syntax of using and defining a class should be substituted in place of that for a struct Interface and Definition for a Node Class Linked List of Classes Slide 13- Display 13.13 Display 13.14 (1-2)
  • 42. Program using the Node class We can create a linked list of numbers using the Node class. Slide 13- Display 13.15 (1-3)
  • 43. Section 13.1 Conclusion Can you Write type definitions for the nodes and pointers in a linked list? Call the node type NodeType and call the pointer type PointerType. The linked lists will be lists of letters. Explain why inserting into an array can be less efficient than inserting into a linked list? Slide 13-
  • 44. 13.2 Stacks and Queues Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
  • 45. A stack is a data structure that retrieves data in the reverse order the data was stored If 'A', 'B', and then 'C' are placed in a stack, they will be removed in the order 'C', 'B', and then 'A' A stack is a last-in/first-out data structure like the stack of plates in a cafeteria; adding a plate pushes down the stack and the top plate is the first one removed A Linked List Application Slide 13- Display 13.16
  • 46. We will create a stack class to store characters Adding an item to a stack is pushing onto the stack Member function push will perform this task Removing an item from the stack is popping the the item off the stack Member function pop will perform this task contains the stack class interface Program Example: A Stack Class Slide 13- Display 13.17
  • 47. demonstrates the use of the stack class Using the stack Class Slide 13- Display 13.18 (1-2)
  • 48. Function push The push function adds an item to the stack It uses a parameter of the type stored in the stack void push(char the_symbol); Pushing an item onto the stack is precisely the same task accomplished by function head_insert of the linked list For a stack, a pointer named top is used instead of a pointer named head Slide 13-
  • 49. Function pop The pop function returns the item that was at the top of the stack char pop( ); Before popping an item from a stack, pop checks that the stack is not empty pop stores the top item in a local variable result, and the item is &quot;popped&quot; by: top = top->link; A temporary pointer must point to the old top item so it can be &quot;deleted&quot; to prevent a memory leak pop then returns variable result Slide 13-
  • 50. Empty Stack An empty stack is identified by setting the top pointer to NULL top = NULL; Slide 13-
  • 51. The Copy Constructor Because the stack class uses a pointer and creates new nodes using new, a copy constructor is needed The copy constructor (a self-test exercise) must make a copy of each item in the stack and store the copies in a new stack Items in the new stack must be in the same position in the stack as in the original Slide 13-
  • 52. The stack destructor Because function pop calls delete each time an item is popped off the stack, ~stack only needs to call pop until the stack is empty char next; while( ! empty ( ) ) { next = pop( ); } Slide 13-
  • 53. The stack class implementation is found in stack Class Implementation Slide 13- Display 13.19 (1) Display 13.19 (2)
  • 54. Section 13.2 Conclusion Can you Give the definition of member function push? Create a definition for the stack class copy constructor? Slide 13-
  • 55. Chapter 13 -- End Slide 13-
  • 56. Display 13.1 Slide 13- Back Next
  • 57. Display 13.2 Slide 13- Back Next
  • 58. Display 13.3 Slide 13- Back Next
  • 59. Display 13.4 Slide 13- Back Next
  • 60. Display 13.5 Slide 13- Back Next
  • 61. Display 13.6 Slide 13- Back Next
  • 62. Display 13.7 Slide 13- Back Next
  • 63. Display 13.8 Slide 13- Next Back
  • 64. Display 13.9 Slide 13- Back Next
  • 65. Display 13.10 Slide 13- Next Back
  • 66. Display 13.11 Slide 13- Back Next
  • 67. Display 13.12 Slide 13- Back Next
  • 68. Display 13.13 Slide 13- Next Back
  • 69. Display 13.14 (1/2) Slide 13- Back Next
  • 70. Display 13.14 (2/2) Slide 13- Next Back
  • 71. Display 13.15 (1/3) Slide 13- Back Next
  • 72. Display 13.15 (2/3) Slide 13- Next Back
  • 73. Display 13.15 (3/3) Slide 13- Back Next
  • 74. Display 13.16 Slide 13- Back Next
  • 75. Display 13.17 Slide 13- Back Next
  • 76. Display 13.18 (1/2) Slide 13- Back Next
  • 77. Display 13.18 (2/2) Slide 13- Back Next
  • 78. Display 13.19 (1/2) Slide 13- Back Next
  • 79. Display 13.19 (2/2) Slide 13- Back Next
  • 80. Display 13.20 Slide 13- Back Next
  • 81. Display 13.21 (1/2) Slide 13- Back Next
  • 82. Display 13.21 (2/2) Slide 13- Back Next
  • 83. Display 13.22 (1/2) Slide 13- Back Next
  • 84. Display 13.22 (2/2) Slide 13- Back Next