n The operation insert adds data to a data collection.
n The operation remove removes data from a data collection.
n The operations isEmpty, getLength, and retrieve ask questions about the data in a
d
An Implementation of the ADT List Using Exceptions
#include <stdexcept>
#include <string>
using namespace std;
class ListIndexOutOfRangeException : public out_of_range
cfw_
public:
ListIndexOutOfRange
writeBackward(in s:string)
if (the string is empty)
Do nothing - this is the base case
else
cfw_ Write the last character of s
writeBackward(s minus its last character)
/* Writes a character string b
Algorithm Efficiency and Sorting
his chapter will show you how to analyze the efficiency of algorithms. The basic
mathematical techniques for analyzing algorithms are central to more advanced
topics i
Advanced C+ Topics
+ classes provide a way to enforce the walls of data abstraction by encapsulating
an abstract data types data and operations. An object-oriented approach, however,
goes well beyond
Queues
hereas a stacks behavior is characterized as last in, first
out, a queues behavior is characterized as first in, first out.
This chapter defines the queues operations and discusses
strategies f
Problem Solving with Abstract
Data Types
art I of this book reviewed aspects of problem solving that are closely related to
programming issues, presented data abstraction as a technique for solution d
Recursion as a Problem-Solving Technique
hapter 2 presented the basic concepts of recursion, and now this chapter moves on
to some extremely useful and somewhat complex applications in computer scienc
Linked Lists
This chapter introduces you to C+ pointers and the data structure linked list. You
will see algorithms for fundamental linked list operations such as insertion and
deletion. The chapter a
Data Abstraction: The Walls
This chapter elaborates on data abstraction, which was introduced in Chapter 1 as a
technique for increasing the modularity of a programfor building walls between a
program
Recursion: The Mirrors
he goal of this chapter is to ensure that you have a basic
understanding of recursion, which is one of the most
powerful techniques available to the computer scientist. This
cha
Problem-Solving Techniques
he primary concern of the five chapters in Part I of this book is to develop a
repertoire of problem-solving techniques that form the basis of the rest of the
book. Chapter
Write the first character of the string
Write the string minus its first character
The following C+ function implements this strategy:
struct Node
cfw_ char item;
Node * next;
; / end Node
Node *strin
/ insertion of a node into a doubly linked list without a dummy head
/ node
ListNode *cur = listHead;
/ compare names in list to find insertion point
while (cur != NULL) & (cur->next != NULL) &
(cur->
/ insertion of a node into a doubly linked list with a dummy head node
ListNode *dummy = listHead; / assumes listHead points to dummy node
ListNode *cur = dummy->next;
/ compare names and position in
Dummy Head Nodes
Also, the insertion and deletion algorithms
initialize prev to point to the dummy head node rather than to NULL. Thus, for
example, in the deletion algorithm, the statement
prev->next
struct Node
cfw_
char ch;
Node *next;
;
Node *head, *prev, *cur, *p;
we have the following:
a F gets inserted into the middle as follows:
p = new Node;
p->next = cur;
prev->next = p;
p->ch = 'F';
b To
Insert and delete at the head of the linked list:
void insertNode (Node * & head, Node * newNode)
cfw_
if (head = NULL)
cfw_
head = newNode;
head->next = NULL;
else
cfw_
newNode->next = head;
head =
Insert and delete at the end of the linked list:
void insertEndNode (Node * & head, Node *newNode)
cfw_
Node *cur, *prev;
cur = head;
prev = NULL;
while (cur != NULL)
cfw_
prev = cur;
cur = cur->next;
Insert and delete at the end of the linked list with a tail pointer:
void insertEndNode (Node *& head, Node *& tail, Node *newNode)
cfw_
if (head = NULL)
cfw_
head = newNode;
tail = head;
else
cfw_
t
Suppose that you wanted to delete a particular node from a linked list. If you were
able to locate the node directly without a traversal, you would not have established
a trailing pointer to the node
double* oldArray = anArray; / copy pointer to array
anArray = new double[2*arraySize]; / double array size
for (int index = 0; index < arraySize; +index)
anArray[index] = oldArray[index]; / copy old a
Displaying the Contents of a Linked List
A high-level pseudocode solution is
Let a current pointer point to the first node in
the linked list
while (the current pointer is not NULL)
cfw_ Display the d
/ determine the point of insertion into a sorted
/ linked list
/ initialize prev and cur to start the traversal
/ from the beginning of the list
prev = NULL
cur = head
/ advance prev and cur as long a
/ deletion of a node from a doubly linked, circular linked list.
/ Assumes listHead points to dummy head node.
ListNode *dummy = listHead;
ListNode *cur = dummy->next;
/ advance until newName is found
typedef int itemType;
struct Node
cfw_ itemType data;
Node * next;
;
void deleteHighestItem(Node *& head)
/ -
/ Deletes the largest item from the list pointed to by head.
/ Precondition: head points t
Deleting a Specified Node from a Linked List
To summarize, the deletion process has three high-level steps:
1. Locate the node that you want to delete.
1. Disconnect this node from the linked list by
/ Creates a linked list from the data in a text
/ file. The pointer variables head and tail are
/ initially NULL. fileName is a string that names
/ an existing external text file.
ifstream inFile(file