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
data collection.
displayList(in aList:List)
/ Displays t
An Implementation of the ADT List Using Exceptions
#include <stdexcept>
#include <string>
using namespace std;
class ListIndexOutOfRangeException : public out_of_range
cfw_
public:
ListIndexOutOfRangeException(const string & message = ")
: out_of_range(me
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 backward.
* @pre The string s contains size characters,
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 in computer science and give you a way to formalize the
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 encapsulation. Inheritance and polymorphism allow you t
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 for implementing them. As you will see, queues
are commo
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 design that
permeates our approach to problem solving, i
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 science.
The recursive solutions to the problems you will see
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 also describes several variations of the basic linked li
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 and its data structures. During the design of a soluti
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
chapter assumes that you have had little or no previous
in
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 1 begins by describing the characteristics of a good so
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 *stringPtr;
/* Writes a string.
* @pre The string is represen
/ 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->data < newName)
cur = cur->next;
ListNode *temp = new L
/ 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 circular list to find insertion point
while (cur->data
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 = cur->next;
deletes from the list the node to which c
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 delete the last node (containing j) on the list result
Implementing insert() and remove() using a dummy head node:
void List:insert(int index, ListItemType newItem)
cfw_ int newLength = getLength() + 1;
if (index < 1) | (index > newLength)
throw ListIndexOutOfRangeException(
"ListOutOfRangeException: insert i
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 = newNode;
/ end insertNode
void deleteNode (Node * & h
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;
if (prev = NULL)
head = newNode;
else
prev->next = ne
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_
tail->next = newNode;
tail = tail->next;
/ end insertE
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 that precedes it in the list. Without a trailing pointe
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 data portion of the current node
Set the current pointer
/ 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 as newValue > the
/ current data item
/ Loop invariant:
/ 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 or dummy node is reached
while (cur->data != newName)
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 to a list of type node.
/ Postcondition: The largest ite
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 changing pointers.
2. Return the node to the system.
cu
/ 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(fileName);
int nextItem;
if (inFile > nextItem) / Is file e