Data Structures Test Prep Ch. 5 - Ch. 9

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Length of a linked list

The number of nodes

When a class depends on an underlying data type, the class can be implemented as a template class. True or false?

True

When implementing a stack, you need only keep track of one end of the list of entries, but when implementing a queue, you need to keep track of both ends of the list True or false?

True

Entries in a stack are "ordered". What is the meaning of this statement? A) There is a first entry, a second entry, and so on. B) Stack entries may be compared with the '<' operation. C) The entries must be stored in a linked list D) A collection of stacks can be sorted

A) There is a first entry, a second entry, and so on.

A doubly linked list has nodes with two pointers: one to the (blank) and one to the (blank)

next node; previous node

Template prefix

template <class Item>

Syntax for template class

template <class Item> class bag { public: typedef Item value_type; . . .

Two fields of nodes

Data and Next Data field contains data being stored in that specific node. It cannot just be a single variable. Next field contains the address of the next node. This is where the link between nodes is established.

Given the following C++ code, fill in the blanks that describe what the code does. struct node{ int data; node* next } Declare a ________ named _______ with an _________ and a _________

Declare a (struct) named (node) with an (integer data member) and a (pointer to next)

What if there is only one node created?

It is both the head and the tail

Bag class with a linked list

1) The items in the bag are stored in a linked list 2) The head pointer of the list is stored in the member variable head_ptr 3) The total number of items in the list is stored in the member variable many_nodes

Stack

A data structure of ordered entries such that entries can be inserted and removed at only one end (called the top)

Queue

A data structure of ordered entries such that entries can only be inserted at one end (called the rear) and removed at the other end (called the front). The entry at the front end of the queue is called the first entry

Deque

A double-ended queue that supports adding and removing items at both ends. (Pronounced like "deck")

Functions that manipulate linked lists

A function that manipulates linked lists has one or more parameters that are pointers to nodes in the list. If the function does not plan to change the list, then the parameter should be a const node*

Linked List

A linear data structure, much like an array, that consists of nodes, where each node contains data as well as a link to the next node, but that does not use contiguous memory.

Variant expression

A numeric quantity that is decreased by at least some fixed amount on each recursive call

Null pointer

A pointer that does not point to anything, usually represented by Ø or -1.

Tail pointer

A pointer to the last node in a linked list

FIFO

A queue is a first in/first out data structure. Entries are taken out in the order their insertion.

Infinite recursion

A recursion that doesn't have a base case, or never reaches it. Eventually, an infinite recursion causes a runtime error.

Script

A sample dialogue written at this point before we've written the function

Activation record

A special memory block that stores all of the important information that the function needs to work

LIFO

A stack is a Last-In/First-Out data structure. Entries are taken out of the stack in the reverse order of their insertion

Polish prefix notation

A way of writing arithmetic operations that places the operation in front of the two arguments Example: + 2 3 evaluates to 5

Polish postfix notation

A way of writing the arithmetic operations that places the operation after the two numbers being combined Example: (2 + 3) * 7 becomes 2 3 + 7 *

In the linked list implementation of the stack class, where does the push member function place the new entry on the linked list? A) At the head B) At the tail C) After all other entries that are greater than the new entry D) After all other entries that are smaller than the new entry

A) At the head

What kind of list is best to answer questions such as "What is the item at position n?" A) Lists implemented with an array B) Doubly-linked lists C) Singly-linked lists D) Doubly-linked or singly-linked lists are equally best

A) Lists implemented with an array

What is the value of the postfix expression: 6 3 2 4 + - *? A) Something between -15 and -100 B) Something between -5 and -15 C) Something between 5 and -5 D) Something between 5 and 15 E) Something between 15 and 100

A) Something between -15 and -100

What is the primary purpose of template functions? A) To allow a single function to be used with varying types of arguments B) To hide the name of the function from the linker (preventing duplicate symbols) C) To implement container classes D) To persist the use of the debugger without the -gstabs flag

A) To allow a single function to be used with varying types of arguments

Push operation

Adding an entry to a stack

Templates

An approach to writing code that is meant to be reused in a variety of settings

Queue overflow

An error received when a program attempts to add an entry to a queue that is already at its capacity

Queue underflow

An error received when a program attempts to remove an entry from an empty queue

Precedence

An ordering from high to low for the operations

Suppose cursor points to a node in a linked list (using the node definition with member functions called data and link). What boolean expression will be true when cursor points to the tail node of the list? A) cursor == NULL) B) (cursor -> link() == NULL) C) (cursor -> data() == NULL) D) (cursor -> data() == 0.0) E) None of the above

B) (cursor -> link() == NULL)

Which of the following statements writes this expression ((7 + 3) * 2) in POSTFIX notation? A) * + 7 3 2 B) 7 3 + 2 * C) ((7 + 3) * 2) D) 21 * 2

B) 7 3 + 2 *

In the linked list implementation of the queue class, where does the push member function place the entry on the linked list? A) At the head B) At the tail C) After all other entries that are greater than the new entry D) After all other entries that are smaller than the new entry

B) At the tail

Why does our node class have two versions of the link member function? A) One is to use with a const pointer, the other with a regular pointer B) One returns the forward link, the other returns the backward link C) One returns the data, the other returns a pointer to the next node

B) One returns the forward link, the other returns the backward link

Suppose top is called on a priority queue that has exactly two entries with equal priority. How is the return value of top selected? A) The implementation gets to choose either one B) The one which was inserted first C) The one which was inserted most recently D) This can never happen (violates the precondition)

B) The one which was inserted first

What is a major difference between the header file for a toolkit of template functions and the header file for a toolkit of ordinary functions? A) The ordinary function toolkit header file must have a #include for the implementation file, but the template version does not have this #include. B) The template function toolkit header file must have a #include for the implementation file, but the ordinary version does not have this #include. C) The ordinary function toolkit header file must have a macro guard, but the template version does not have this macro guard. D) The template function toolkit header file must have a macro guard, but the ordinary version does not have this macro guard.

B) The template function toolkit header file must have a #include for the implementation file, but the ordinary version does not have this #include.

When should a function be implemented as a template function? A) When the data types of the parameters all have copy constructors B) When the function depends on an underlying data type C) When the function is relatively short (usually just one line). D) When the function only takes one argument

B) When the function depends on an underlying data type

Suppose bag is a template class, what is the syntax for declaring a bag b of integers? A) bag = b; B) bag b; C) bag of int b; D) int bag b;

B) bag b;

Which of the following stack operations could result in a stack underflow? A) is_empty B) pop C) push D) Two or more of the above answers

B) pop

If data is a circular array of CAPACITY elements, and last is an index into that array, what is the formula for the index after last? A) (last % 1) + CAPACITY B) last % (1 + CAPACITY) C) (last + 1) % CAPACITY D) last + (1 % CAPACITY)

C) (last + 1) % CAPACITY

Which of the following statements writes this expression ((7 + 3) * 2) in PREFIX notation? A) ((7 + 3) * 2) B) 21 * 2 C) * + 7 3 2 D) 7 3 + 2 *

C) * + 7 3 2

Evaluate the postfix expression 2 3 - 43 + select the correct answer A) 45 B) 86 C) 42 D) 27

C) 42

I have implemented the queue with a linked list, keeping track of a front pointer and a rear pointer. Which of these pointers will change during an insertion into a NONEMPTY queue? A) Neither changes B) Only front_ptr changes C) Only rear_ptr changes D) Both change

C) Only rear_ptr changes

Which statement best describes the difference between a queue and a stack? A) Queues require dynamic memory, but stacks do not B) Stacks require dynamic memory, but queues do not C) Queues use two ends of the structure; stacks use only one D) Stacks use two ends of the structure, queues use only one

C) Queues use two ends of the structure; stacks use only one

Consider the following definition: template Item maximal (Item a, Item b){ if (a > b) return a; else return b; } What restrictions are placed on the Item data type for a program that uses the maximal function? A) The Item data type must either int, double, or float B) The Item data type must be one of the built-in C++ data types C) The Item data type must have a copy constructor and a > operator defined D) None of the above restrictions apply

C) The Item data type must have a copy constructor and a > operator defined

Suppose that a program wants to use both a bag of doubles and a bag of strings. Which version of the bag could you use? A) You can use the array version of the non-template bag B) You can use the linked list version of the non-template bag C) You can use the array version of the template bag D) Two of the above answers are right E) Answers A, B, and C are all right

C) You can use the array version of the template bag

I have implemented the queue with a circular array, keeping track of first, last, and count (the number of items in the array). Suppose first is zero, and last is CAPACITY - 1. What can you tell me about count? A) count must be zero B) count must be CAPACITY C) count could be zero or CAPACITY, but no other values could occur D) None of the above

C) count could be zero or CAPACITY, but no other values could occur

Suppose cursor points to a node in a linked list (using the node definition with member functions called data and link). What statement changes the cursor that it points to the next node? A) cursor += link(); B) cursor++ C) cursor = cursor -> link(); D) cursor = link();

C) cursor = cursor -> link();

The operation for removing an entry from a stack is traditionally called: A) delete B) peek C) pop D) remove

C) pop

toupper

Converts lowercase letters to uppercase

Here is an infix expression: 4 + 3 * (6 * 3 - 12) Suppose that we are using the usual stack algorithm to convert the expression from infix to postfix notation. What is the maximum number of symbols that will appear on the stack AT ONE TIME during the conversion of this expression? A) 1 B) 2 C) 3 D) 4 E) 5

D) 4

Which of the following applications may use a stack? A) A parentheses balancing program B) Keeping track of local variables at run time C) Syntax analyzer for a compiler D) All of the above

D) All of the above

i have implemented the queue with a linked list, keeping track of a front pointer and a rear pointer. Which of these pointers will change during an insertion into an EMPTY queue? A) Neither changes B) Only front_ptr changes C) Only rear_ptr changes D) Both change

D) Both change

If the characters 'D', 'C', 'B', 'A' are placed in a queue (in that order), and then removed one at a time, in what order will they be removed? A) ABCD B) ABDC C) DCAB D) DCBA

D) DCBA

Suppose that f is a function with a prototype like this: void f(_______ head_ptr); //Precondition: head_ptr is a head pointer for a linked list //Postcondition: The function f has done some computation with the linked list, but the list itself is unchanged What is the best data type for head_ptr in this function? A) node B) const node C) node* D) const node*

D) const node*

Suppose we have an array implementation of the queue class, with ten items in the stack stored at data[2] through data[11]. The CAPACITY is 42. Where does the push method place the new entry in the array? A) data [1] B) data [2] C) data [11] D) data[12]

D) data [12]

Suppose we have an array implementation of the stack class, with ten items in the stack stored at data[0] through data[9]. The CAPACITY is 42. Where does the push method place the new entry in the array? A) data[0] B) data[1] C) data[9] D) data[10]

D) data[10]

The operation for adding an entry to a stack is traditionally called: A) add B) append C) insert D) push

D) push

Pseudocode to use a stack to reverse a word

Declare a stack of characters; while (there are more characters of the word to read) Read a character, and push the character onto the stack. while(the stack is not empty) Write the top character to the screen, and pop it off the stack

What technique is used to provide the capability to step through the items of a container class? A) A copy constructor B) An overloaded assignment operator C) A destructor D) A default constructor E) An iterator

E) An iterator

Nodes

Elements of a linked list

void list_head_insert (node*& head_ptr, const node::value_type& entry);

Function that adds a new node at the head of a linked list

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 creates a special iterator that marks the position that is beyond the end of a linked list

The pointer field of the final node contains the

Null pointer

Instantiation

Process of creating an object, an instance of a class; creates space in memory for the new object and binds a name for the object with the object's data in memory.

Pop operation

Removing an entry from a stack

Types of Linked Lists

Singly-linked lists and doubly-linked lists In singly-linked lists, every element contains some data and a link to the next element. In doubly-linked lists, every node contains some data, a link to the next node, and a link to the next node

head_ptr = new node(entry, head_ptr);

Statement that will correctly add the first node to a list

One-level recursion

Suppose that every case is either a stopping case or it makes a recursive call tat is a stopping case. Then the deepest recursive call is only one level deep, and therefore no infinite recursion occurs

Threshold

The "small enough value" which guarantees a stopping case

Unification

The compiler's term for determining how to instantiate a template function

Stack overflow

The condition from resulting trying to push an item onto (add an item to) a full stack

Stack Underflow

The condition resulting from trying to access an item from an empty stack

Base case

The condition under which a recursive function returns without calling itself, thereby ending the recursive calls.

recursive thinking

The expectation to find simpler versions of a large problem during the design process

Induction

The usual method for showing correctness of a recursive function (Same as mathematical induction)

Induction Method to Show That a Recursive Function is Correct

To show that a recursive function meets its precondition/postcondition contract, first show that there is no infinite recursion (by showing the previous conditions 1 and 2) and then show that the following two conditions are also valid: 3) Whenever the function makes no recursive calls, then it meets the precondition/postcondition contract. (This is called the base step.) 4) Whenever the function is called and all the recursive calls it makes meet their precondition/postcondition contracts, then the original call will also meet its precondition/postcondition contract. (This is called the induction step)

A queue can be used to buffer data that is being sent from a fast computer component to a slower componend True or False?

True

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. True or false?

True

Attempting to push an entry onto a full stack is an error known as stack overflow True or false?

True

Koenig lookup

Use of arguments to determine which function to use

Head

Very first node

Tail

Very last node

short circuit evaluation

When a boolean expression is evaluated the evaluation starts at the left hand expression and proceeds to the right, stopping when it is no longer necessary to evaluate any further to determine the final outcome.

Recursion

When a function calls itself.

Parameters for linked lists

When a function needs access to a linked list, use a node* parameter and follow these guidelines: 1) Use a pointer to a constant node, const node*, when the function needs access to the linked list and the function will not make any changes to any of the list's nodes 2) Use a value parameter, node*, when the function may change the list in some way, but it does not need to make the pointer point to a new node 3) Use a reference parameter, node*&, when the function needs access to the linked list and the function may make the pointer to a new node

Empty list

When a linked list does not yet have any nodes

When to provide two versions for a function

When a nonmember function has a parameter that is a pointer to a node, and the return value is also a pointer to a node, you should often have two versions: one version where the parameter and return value are both node*, and a second version where the parameter and return values are both const node*

Buffering

When the program asks for input, the operating system provides characters from the front of its queue

Run-time stack

a data structure that keeps track of activation records during the execution of a program

Head pointer

initial pointer in a linked list


Kaugnay na mga set ng pag-aaral

Simple, Complete, and Compound Subjects and Predicates

View Set