{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

04LinkedLists - Chapter 4 Linked Lists Recall that linked...

Info icon This preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
 CS 240  Chapter 4 - Linked Lists 1Page 1 Static Array Chapter 4 Linked Lists Recall that linked lists enable us to dynamically adjust the size of a  list, eliminating the need to preallocate a specific amount of memory. ε δ χ β α Preallocation  Required! Wasteful if you  allocate too  much! Potentially fatal  if you allocate  too little! Dynamic Array ε δ χ β α Determine the size  of the list before   allocating memory! Not dynamic  enough – you can’t  change the size once  it’s been decided! Linked List Completely dynamic! No space is wasted due to  excess allocation! Additional space is  allocated as needed! α β χ δ ε
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
 CS 240  Chapter 4 - Linked Lists 2Page 2 Let’s review linked lists with a little example that also refreshes our memory  (so to speak) about recursion. In this example, we’re going to read text from an input file, keeping track of  how many times each word is used in the text. Each node in the linked list will require a string field (to hold the word), an  integer field (to hold the count), and a pointer field (to point to the next  node). string word int count ptr next
Image of page 2
 CS 240  Chapter 4 - Linked Lists 3Page 3 When a word is inserted in the list, the list is traversed to see if the  word’s already there... “yadda” 4 “wassup” 3 “groovy” 2 “groovy” “yadda” 4 “groovy” 3 “wassup” 3 “yadda” 4 “groovy” 3 “wassup” 3 “oops” 1 “oops” If the word is found, its node is removed, its count is incremented, and  the node is reinserted to preserve the count/alphabetical ordering... If the word isn’t found, a new node is created with a count of one, and  inserted appropriately...
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
 CS 240  Chapter 4 - Linked Lists 4Page 4 Desired linked list functionality: Default Constructor Copy Constructor INSERT member function RETRIEVE member function Destructor GETNODE member function Input (>>) friend operator Output (<<) friend operator Recursive Find function Recursive Insert function Recursive Output function Unpunctuate & upper-case function Make an empty list Make a deep copy list Delete all list nodes Insert a new(?) word Find a word’s count Create word/count node Read a text file Write word/count list Find a word Insert a word Write a word/count Edit a word
Image of page 4
 CS 240  Chapter 4 - Linked Lists 5Page 5 ///////////////////////////////////////// // Class definition file: linkedList.h // // // // Each node in a LinkedList will have // // two components: an elementType (a // // string representing a word and an // // integer counting that word's number // // of occurrences) and a pointer to // // the next node in the LinkedList. // ///////////////////////////////////////// #ifndef LINKED_LIST_H #include <string> using namespace std; struct elementType { string word; int count; }; struct node; typedef node *nodePtr; struct node { elementType item; nodePtr next; }; class LinkedList { public :
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern