80 likes | 91 Views
Learn about destructors, pointers, linked lists, the difference between a stack and a queue, and recursion. Create and manipulate linked lists, explore storing different types of data, and understand templates.
E N D
Linked Lists and Templates But first: quiz results!
Quiz results • What is a destructor? • What is a pointer? • What is a linked list • What is the difference between a stack and a queue? • What is recursion?
Linked list reminder • What is a linked list? • What do we need to write to create a linked list?
Linked List Node • Let's go ahead and create a linked list node • Named LLNode • Members: • pointer to next node named 'node' • data to be stored of type double named 'data'
Linked List class • Let's make a linked list class named 'List': • constructor: initialize to an empty list • destructor: just ignore it for now • void add(double value): add an element to the front of the list • double front(): returns the first element in the list. You can assume the list has at least one element • member variable 'head' that points to the head of the list • And let's put all of the code in the header file
Example main List lst; lst.add(3.14); cout << lst.front(); lst.add(6.02); cout << lst.front();
Storing other types of data • Let's say we wanted to make a linked list of chars instead • What would we need to change?
Templates • Templates allow us to give the C++ compiler a pattern to follow to make classes or structs that work with many different types of objects. • We can make our linked list templated so that users will be able to choose what type of linked list they want • Let's see what we'd need to change...