Application: Sparse Polynomials - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Application: Sparse Polynomials

Description:

insert an element at the rear of the queue // Postcondition: the queue has one more element ... until we arrive at the end of the list. while (curr != NULL) ... – PowerPoint PPT presentation

Number of Views:67
Avg rating:3.0/5.0
Slides: 17
Provided by: toshi155
Category:

less

Transcript and Presenter's Notes

Title: Application: Sparse Polynomials


1
Application Sparse Polynomials
Array representation
Good if
Not good for sparse polynomials which have only a
few nonzero terms.
If using an array of 100 elements, all but two
are zeros!
2
Linked Queue
Waste of memory to store all zero coefficients!
Store only the non-zero coefficients along with
the corresponding powers.
((5, 0), (7, 1), (-8, 3), (4, 5))
Use a linked list
3
Class Polynomial
ifndef Polynomial define Polynomial template
lttypename Tgt class Polynomial public //
function members // operation on
polynomials ... private class Term
public T coef
unsigned expo
class Node public Term data Node
next Node(T co0, int ex0, Node ptr0)
data.coef co
data.expo ex next ptr
// data members int myDegree
Node myTerms
endif
4
Polynomial Addition
template lttypename Tgt PolynomialltTgt
PolynomialltTgtoperator(const PolynomialltTgt
secondPoly) PolynomialltTgt polySum
PolynomialltTgtNode // pointers to run
through 1st poly, 2nd poly, sum poly
ptra myTerms -gt next, ptrb
secondPoly.myTerms-gtnext, ptrc
polySum.myTerms int degree 0
// more nodes in one of polys. copy from 1st
poly while (ptra ! NULL ptrb ! NULL)
if ((ptrb NULL) (ptra ! NULL
ptra -gt data.expo lt ptrb-gtdata.expo))
ptrc-gtnext new PolynomialltTgtNode(ptra-gtdata.co
ef, ptra-gtdata.expo)
degree ptra-gtdata.expo ptra ptra -gt
next ptrc ptrc -gt next

5
Overloading operator
else if ((ptra NULL) (ptrb ! NULL ptrb
-gt data.expo lt ptra-gtdata.expo)) // copy
from 2nd poly ptrc-gtnext new
PolynomialltTgtNode(ptrb-gtdata.coef,
ptrb-gtdata.expo) degree
ptrb-gtdata.expo ptrb ptrb -gt next
ptrc ptrc -gt next
else T sum ptra-gtdata.coef
ptrb-gtdata.coef if (sum ! 0)
// nonzero sum add to sum poly
ptrc-gtnext new PolynomialltTgtNode(sum,
ptra-gtdata.expo) degree ptra-gtdata.expo
ptrc ptrc-gtnext ptra
ptra-gtnext // move along in 1st and 2nd
lists. ptrb ptrb-gtnext
sumPoly.myDegree degree
return sumPoly
6
Linked Queue
To support dynamic memory management
7
Class linkedQueue
template lttypename Tgt class linkedQueue
public linkedQueue() // Constructor
creates an empty queue. // Postcondition
qsize 0, qfront qback NULL linkedQueue(co
nst linkedQueueltTgt obj) // copy constructor.
builds a copy of obj linkedQueue() //
destructor. clear the linked list linkedQueueltT
gt operator (const linkedQueueltTgt rhs) void
push(const T item) // insert an element at
the rear of the queue // Postcondition the
queue has one more element void pop() //
remove an element from the front of the
queue. // Precondition the queue is not
empty. if the queue // is empty, throw an
underflowError exception // Postcondition the
queue has one less element
8
linkedQueue (contd)
void pop() // remove an element from the
front of the queue. // Precondition the queue
is not empty. if the queue // is empty, throw
an underflowError exception // Postcondition
the queue has one less element T
front() // return a reference to the front of
the queue. // Precondition the queue is not
empty. if the queue // is empty, throw an
underflowError exception const T front()
const // constant version int size()
const // return the current size of the
queue bool empty() const // return true if
the queue is empty otherwise return false
9
linkedQueue (contd)
private nodeltTgt qfront, qback // the
linked list with qfront stores the
elements. // qback points to the rear of the
queue int qsize // number of elements in
the queue void copy(const linkedQueueltTgt
q) // copy q so the current list is identical
to q void clear() // clear the list. used
by the destructor and assignment nodeltTgt
getNode(const T item) // allocate a node
with value item and pointer value NULL // and
return a pointer to it. if memory allocation
fails, // throw the memoryAllocationError
exception
10
Function copy()
template lttypename Tgt void linkedQueueltTgtcopy(co
nst linkedQueueltTgt q) // qback moves through
the list we are building and // winds up at the
rear of our new list. p // moves through the
list we are copying nodeltTgt newNode, p
q.qfront // initially, the list is
empty qfront qback NULL // nothing to do
if p is NULL if (p ! NULL) // create the
first node in the queue and assign // its
addres to qback qfront qback
getNode(p-gtnodeValue)
11
copy (contd)
// move forward in the list we are copying p
p-gtnext // copy remaining items while(p
! NULL) // insert new node at the
back newNode getNode(p-gtnodeValue) qback-
gtnext newNode // qback is the new
node qback newNode // move to the next
node of the list we are copying p
p-gtnext // the size of the new list is
the size of q qsize q.qsize
12
Function clear()
template lttypename Tgt void linkedQueueltTgtclear()
nodeltTgt curr qfront, p // delete nodes
until we arrive at the end of the list while
(curr ! NULL) // save curr in p and move
curr forward to the next list node p
curr curr curr-gtnext // delete node
p delete p // specify an emtpy
list qfront qback NULL qsize 0
13
Constructors Destructor
// create the empty list qfront qback NULL
and qsize 0 template lttypename
Tgt linkedQueueltTgtlinkedQueue() qfront(NULL),
qback(NULL), qsize(0) template lttypename
Tgt linkedQueueltTgtlinkedQueue(const
linkedQueueltTgt obj) // call copy() and pass
the pointer to the front of // the linked list
in obj copy(obj) template lttypename
Tgt linkedQueueltTgtlinkedQueue() // call
clear() clear()
14
Overloaded Assignment
template lttypename Tgt linkedQueueltTgt
linkedQueueltTgtoperator (const linkedQueueltTgt
rhs) // delete the current list clear() //
make the current object a copy of
rhs copy(rhs) return this
15
Insertion
template lttypename Tgt void linkedQueueltTgtpush(co
nst T item) // allocate space for the new
node nodeltTgt newNode getNode(item) // if
the queue is empty, insert the new element at the
front of // the list and update both qfront and
qback if (qfront NULL) qfront
newNode qback newNode // in a non-empty
list, insert new element at back and update
qback else qback-gtnext newNode qback
newNode // increment the queue
size qsize
16
Deletion
template lttypename Tgt void linkedQueueltTgtpop()
// save the location of the front of the
queue nodeltTgt p qfront // if the queue is
empty, throw underflowError if (qsize
0) throw underflowError("queue pop() empty
queue") // move the front forward one
node qfront qfront-gtnext // if the queue is
now empty, set qback to NULL if (qfront
NULL) qback NULL // delete the
node delete p // decrement the queue
size qsize--
Write a Comment
User Comments (0)
About PowerShow.com