Data Structures Quiz 2
What is a vector? (Standard Template)
- It is a container that can store elements as an array, but its size can change dynamically - All elements will remain in the same order in which they are inserted
in a singly-linked list,
- each node stores a reference to an object that is an element of the sequence, as well as a reference to the next node of the list. - It does not store any pointer or reference to the previous node -Every node stores address or reference of the next node in the list and the last node has next address or reference as NULL
Template declaration and object creation
//declaration template <class T> class className { T var; public: T someOperation(T arg); }; //object creation className <dataType> classObject();
Reference Type as a Return Value:
1) The return value must be a variable or object that will still exist after the function returns. In particular, the return value must not be a local variable 2) The function returns this actual variable or object (not a copy of the object)
The node iterator has two constructors:
1) a constructor that attaches the iterator to a specified node in a linked list 2) a default constructor that cerates a special iterator that marks the position that is beyond the end of a linked list.
two uses of null pointers
1) used for the link field of the final node of a linked list 2) used for the head and tail pointers of a list that doesn't yet have any node
NULL is defined in the standard library (its not apart of the std library)
<cstdlib>
A Linked list is what type of structure?
A sequential data structure Collection of nodes that collectively form linear sequence
What is multiset? (Standard Template)
An Associative container and it can contain duplicate values
Whenever an insertion is not at the head, the insertion process requires a pointer to the node that is just _______ the intended location of the new node
BEFORE
Why a linked list?
Can use Array and Vector (resizable array)
Examples of Templates
Example 1: template <class Item, class SizeType> std::size_t index_of_max(const Item data[], SizeType n); cout << index_of_max(data, 12); //function call Example 2: template <class Item, class SizeType> void ordered_insert(Item data[], SizeType n, Item entry); ordered_insert(data, 3,15); //function call
Applications of Linked List
Implementation of stacks and queues Maintain a directory of names Doubly linked list: Web browser, image viewer, music player Previous and next items Circular linked list: OS timesharing environment
When does instantiation of a template parameter occur?
Instantiation of a template parameter occurs when an actual variable of the template class is declared
Stack is a L__O Data Structure
LIFO, Last in First Out, which means items are taken out in the reverse order of their insertion
When creating a linked list, head should =
NULL
Time complexity of deletion in a linked list
O(1) (Constant time complexity)
Time complexity of insertion in a Linked List
O(1) (Constant time complexity)
What is the typical worst-case time analysis for resizing operation on a container class that is implemented with a dynamic array?
O(n), where n is the size of the array prior to resizing
How to delete in a linked list
Set 'cur' to the node to be deleted Set the previous node 'next' pointer to the 'next' pointer of the node to be deleted
Types of linked lists
Singly Linked List Doubly Linked List Circular Linked List - Singly circular linked list - Doubly linked list
A template function is similar to an ordinary function with one important difference:
The definition of a template function can depend on an underlying data type. The underlying data type is given a name—such as Item—but Item is not pinned down to a specific type anywhere in the function's implementation.
Difference between Singly Linked List and Doubly Linked List:
The main difference is the ability to traverse. - In a single linked list, node only points towards next node, and there is no pointer to the previous node, which means you can not traverse back on a singly linked list -Doubly linked list maintains two pointers, towards next and previous node, which allows you to navigate in both directions in any linked list.
How does the node iterator's implementation differentiate between the prefix and postfix versions of the ++ operator?
The postfix version puts the keyword int in its parameter list, whereas the prefix version leaves its parameter list empty. This is an artificial use of int; there is no actual int parameter, and postfix makes a copy of the current item and returns it before incrementing to the next item
Which version of ++ is generally more efficient? Why?
The prefix version is more efficient because it doesn't have to make a copy of the iterator.
What is the template prefix?
The template prefix defines the list of underlying data types that a template function depends upon, for example: template <class Item> It warns the compiler that the following definition will be an unspecified data type
Time complexity of linked list vector
Time Complexity O(N) N - size of vector
Any functions that you write to manipulate a linked list MUST be able to handle a null head pointer and tail pointer (T/F)
True
Linked list can be implemented by using the list container (T/F)
True
Name two places in the bag template class where the name bag is not changed to bag<Item>
Within the class definition, bag is not changed to bag. Also, the name of the constructor is simply bag (not bag), and the name of the destructor is simply ~bag.
Linked lists re better at insertions/deletions at a cursor. Our sequence class maintains a cursor that points to
a current element
What is a Stack?
a data structure of ordered entries such that entries can be inserted and removed at only one end (called the top)
output iterator
a kind of iteratior that provides an alternartive way to insert new elements into a container One way to create an output iterator for a container class called c is with the expression: inserter(c, c.begin())
The links between nodes are implemented using pointers. Each pointer must be declaed as a pointer to
a particular type of data. In the case of a linked list, each ink is a pointer to a node.
Each node is a combination of two things:
a piece of data and a link to the next node
Each pointer to a node must be declared as
a pointer variable
A random access iterator looks a lot like an array. C++ will allow any pointer to an element in an arry to be used as if it were
a random access iterator
a) What is the disadvantage of the typedef approach of generalizing data compared to the template approach? b) Is there any advantage to the typedef approach?
a) A typedef approach allows only one data type to be defined for the data item within a program, whereas a template function allows several different usages in a single program because the compiler determines the data type when the template function is used. b) The typedef approach has a simpler syntax, which makes for easier debugging
In a doubly linked list,
there are two references associated with each node, one of the references points to the next node and the other to the previous node. The Advantage of this data structure is that we can traverse in both the directions and for deletion so we don't need to have explicit access to previous node.
Linked lists can be used to implement a class. Such a class has one or more private member variables that are pointers to nodes in a linked list. The member functions of the class use the linked-list functions to manipulate the linked list, which is accessed
through private member variables
The destructor is responsible for returning
all dynamic memory to the heap (list_clear)
If there is insufficient dynamic memory for a new node, then _______ is thrown before changing the list
bad_alloc
Doubly linked lists are better for a
two-way cursor doubly linked lists has two pointers: one pointing to the next node and one pointing to the previous node
The node class depends on an underlying data type -- the type of data in each node. To allow for easy changing of the item type, we generally use a ________ statement
typedef. To define the name 'value_type' to be a synonym for the type of data in each data in each node. The 'value_type' is then used within the node class.
The definition of the template function depends on the
user-defined data type, the compiler determines the data type of argument(member variable) during the function call( object creation)
If we need to change the type of items in the nodes then we will change only the
value_type
The Standard Template Library included three similar container classes:
vector, list and deque
Address is stored in
bytes
-> and :: is the member selection operator or
components election operator
the NULL pointer uses the standard library
cstdlib
Finding the Next Occurence of an Item (Make cursor point to the next occurence of target) (or NULL if there are no more occurences)
cursor = cursor->link(); cursor = list_search(cursor, target);
Input iterator
designged to provide a sequence of values. The current element of an input operator can be retrieved by using the deference * operator. The intention is that an algorithm that uses an input iterator will retrieve an element (with the dereferencing ) followed by an increment (++), over and over again
deque uses a
double ended queue, allowing inserting and removing from both ends uses dynamic array
list uses a
doubly linked list
vectors use a
dynamic array
Destructor is needed when implimentation uses
dynamic memory
An iterator allows a programmer to
easily step thorugh the items of a container class. The C++ Standard Library container classes are all provided with iterators We can also implement iterators for our own classes in a way that matches the STL iterators
Dynamic Arrays are better at random access. The term random access refers to
examining or chaning an arbitrary(random) element that is specified by its position in a list
the rand function
from cstdlib generates a non-negative pseudorandom integer
Bidirectional iterator
has all the abilities of a forward iterator, plus it can move backward with the -- operator, which returns iterator after it has moved backward, and it also returns a copy of the iterator before it moved
The most common access to a linked list is through the list's first node, which is called the
head of the list
A pointer to the first node is called the
head pointer
Typically a linked list is accessed through a
head pointer that points to the head node (the first node). Sometimes a linked list is accessed elsewhere, such as through the tail pointer that points to the last node
When a template function is used, the compiler examines the types of the arguments. At that point, the compiler determines the data type of Item. This is called the
instantiation of the template.
STD Standard template library - linkedlists
vector<Item> (#include <vector>) - dynamic array list<Item> (#include <list>) - doubly linked lists deque<Item> (#include <deque>) - double ended queue (The ends of the deque are called "front" and "back," . Insert and delete occurs at both end
Creating new node(in powerpoint)
void Node::create(){ Node *temp, *cur; string name; temp=new Node; cout<<"\nEnter the name:"; cin>>name; \ temp->info=name; \ temp->next=NULL; (new node creation) cur= head; / /
What is a linked list
is a sequence of items arranged one after another, with each item connected to the next by a link
where does the template prefix occur
it occurs before the template class definition and before each member function implementation. It also must appear before the prototype and implementation of any other template functions that manipulate the template class
If a class is frequntly adjusting its size, then a _______ ______ may be better than a dynamic array
linked list
A node pointer should be a value parameter when the function needs access to the linked list, and the function might change the linked list, but the function does not need to
make a the pointer point to a new node eg of value parameter (node * p, const node::value_type &entry)
template functions is the
methodology to use the C++ funcions for different data types
forward iterator
moves iterator forward, can be checkers for equality and inequality, can be is used in perfix or postfix notation, can access current item , has default constructpor, copy constructor and assignment operator
The Standard Template Libray (STL) contains a group of function in the <algorithm> facility that can manipulare contains including
multiset, set, map and multimap classes
Which of the const iterator categories is provided by a multiset?
multiset<T>::const_iterator is a bidirectional iterator
Common programming technique is to place each item together with the link to the next item, resulting in a simple component called a
node
Functions that manipulate a linked list follow basic patterns. Such functions are not
node member functions (so that they can handle empty lists with no nodes)
When a function needs access to a linked list, use a _______ parameter
node*
whenever a program needs to refer to the item type, we can use the expression
node::value_type
A linked list consists of
nodes
A function that manipulates lniked lists has ____ or more parmeters that are pointers to nodes in the list
one
A node pointer should be a reference parameter when the function needs to access the linked list and the function makes the pointer
point to a new node. This change to the pointer will make the actual argument point to a new node eg of reference parameter (node*& head_ptr, const node::value_type &entry)
Random access iterator
random access refers to the ability to quickly access any randomly selected location in a container. A random access iterator can always be used if it were any of the other types of iterators because it has all of the same operations.
What is the main purpose of a template function
serves to implement many functions that are identical except for a different underlying data type
invariant
set of assertions that must always hold true during the life of an object for the program to be valid. -It should hold true from the end of the constructor to the start of the destructor whenever the object is not currently executing a method that changes its state.
each node contains
some data and a pointer to the next node in the list. The pointer field of the final node contains the null pointer.
If a program attempts to push an item onto a full stack, the result is an error called
stack overlfow, which is the condition resulting from trying to push an item onto (add an item) a full stack
If a program attempts to pop an item off an empty stack, then an error will occur which is called
stack underflow, which is the condition resulting from trying to access an item from an empty stack
Linked list has a class node which has two private variables:
string info- which is the data and a Node pointer which points to the next node it often also has a node pointing to head
Sometimes we maintain a pointer to the last node in a linked list. The last node is the
tail of the list
bag operator +(const bag& b1, const bag& b2) as a template cass would be
template <class Item> bag<Item> operator+(const bag<Item>&b1, const bag<Item>& b2)
bag::size_type bag::count(const value_type& target) const as a template class:
template <class Item> typename bag<Item>::size type bag<Item>::count(const Item& target) const use Item instead of value_type outside a member function, when Item isn't specified
template prefix
template <class Item> OR tempate <typename Item>
When a class depends on an underlying data type, the class can be implemented as a
template class
Template parameter example
template<typename Item> Item max(Item a, Item b) template function call max(100,3000)
typename keyword tells the compiler
that the expression is the name of a data type
Unification is
the compiler's term for determining how to instantiate a template function through the template parameter. If a template parameter does not appear in the parameter list of a template function, the compiler will generate the message "Failed unification"
The template parameter must appear in
the parameter list of the template function E.g maximal(Item a, Item b) Without this rule, the compiler cannot figure out how to instantiate the template function when it is used Violating this rule will likely result in cryptic error messages such as "Failed unification"
The <algorithm> facility in the C++ Standard Library contains
the swap function, a max function and a min function
a pointer to the last node is called
the tail pointer
A circular linked list is
where all nodes are connected to form a circle. There is no NULL at the end. A circular linked list can be singly or or doubly. Advantage of this data structure is that any node can be made as a starting node, which is useful in implementation of a circular queue in a linked list.