Professional Documents
Culture Documents
Lecture 3-2022
Lecture 3-2022
Lecture 3-2022
return 0;
}
◼ It contains a serious bug and may die with an exception when run. The
problem is that the default member-wise copy constructor is being used to
create the "manager" object. But the member-wise copy copies the address
stored in the pointer _name in "programmer" to the pointer _name in
"manager".
◼ When you run the above program, programmer's destructor gets called first. It
works fine and frees the block in memory. When manager's destructor is
called, an exception is thrown when the delete is attempted because that
block of memory has already been released.
J.-J. Chen, EE NTUST
2022/3/21 Stack & Queue 5
Copy Constructor (example)
class Employee { Employee::~Employee() {
public: delete[ ] _name;
Employee(char *name, int id);
Employee(Employee &rhs); }
~Employee();
char *getName(){return _name;} Employee::Employee( Employee &rhs)
int getId() {return _id;}
//other accessor methods {
private: _id = rhs.getId();
int _id; _name = new char[strlen(rhs.getName()) + 1];
char *_name; strcpy(_name, rhs._name);
};
}
Employee::Employee( char *name, int id)
{
_id = id;
a2 a2
a1 a1
a0 a0
Previous frame fp
pointer
Return Pushress
a1
Local variables
Previous frame pointer fp
Return Pushress main Previous frame
pointer
Return Pushress main
• Used by a program at run time to process function calls
– The previous frame pointer points to the stack frame of the invoking function
– The return Pushress contains the location of the statement to be executed after the
function terminates
• When the invoked function terminates, its stack frame is removed and the invoking
function continues its processing
J.-J. Chen, EE NTUST
2022/3/21 Stack & Queue 13
Stack operations
E top
top D top
D D
C top C C
C
B top B B B
B
A top A A A A
A
public:
Boolean IsEmpty();
// if number of elements in the stack is 0, return TRUE(1) else return FALSE(0)
KeyType* Delete(KeyType& );
// if IsEmpty(), then StackEmpty() and return 0;
// else remove and return a pointer to the top element of the stack.
};
J.-J. Chen, EE NTUST
2022/3/21 Stack & Queue 15
Implementation of Stack by Array
an-1
a2
a1
a0
a0 a1 a2 an-1
Array index 0 1 2 3 n-1
a0 a1 a2 an-1
front rear
front rear
front rear
front rear
J.-J. Chen, EE NTUST
2022/3/21 Stack & Queue 18
The Queue ADT
• Queue: an ordered list
– All insertions take place at one end
– All deletions take pace at the opposite end
– First-In-First-Out, FIFO
• Example
– Given a queue Q={a0,a1,…,an-1}
– a0 is the front element, an-1 is the rear element
– ai is behind ai-1 for 1 i n
Boolean IsFull();
// if number of elements in the queue is equal to the maximum size of
// the queue, return TRUE(1); otherwise, return FALSE(0)
Boolean IsEmpty();
// if number of elements in the queue is equal to 0, return TRUE(1)
// else return FALSE(0)
KeyType* Delete(KeyType&);
// if IsEmpty(), then QueueEmpty() and return 0;
// else remove the item at the front of the queue and return a pointer to it
};
J.-J. Chen, EE NTUST
2022/3/21 Stack & Queue 20
Class Queue & Its Operations
template<KeyType>
class Queue{
public:…
private:
int front; // front: index of the first element to be retrieved
int rear; // rear:index of the last element
KeyType *queue; // KeyType array
int MaxSize;
}
A A B A B C A B C D A B C D E B C D E
f,r f r f r f r f r f r
template<class KeyType>
inline Boolean Queue <Keytype>::IsFull();
{
if (rear == MaxSize-1) return TRUE;
else return FALSE;
} template<KeyType>
inline Boolean Queue<KeyType>::IsEmpty()
{
if(front == rear) return TRUE;
else return FALSE;
}
J.-J. Chen, EE NTUST
2022/3/21 Stack & Queue 22
Member Function of Queue
template <class KeyType>
void Queue <Keytype> ::Push(const KeyType &x) // push x to the queue
{
if(IsFull()) QueueFull();
else queue[++rear]=x;
}
4 J4 4
J3 n-4 n-4
3 3 J1
J2
J1
n-3 J2 n-3
2 2 J4 J3
n-2 n-2
1 1
0 n-1 0 n-1
4 J4 4
J3 n-4 n-4
3 3 J1
J2
J1 n-3 J2 n-3
2 2 J4 J3
n-2 n-2
1 1
0 n-1 0 n-1
front = 0; rear = 4 front = n-4; rear = 0
template <KeyType>
void Queue <KeyType>::Push(const KeyType &x)
{
int new_rear=(rear+1)%MaxSize;
if (front == new_rear) QueueFull();
else queue[rear = new_rear]=x;
}
J.-J. Chen, EE NTUST
2022/3/21 Stack & Queue 29
Delete an Element from a Circular Q
4 J4 4
J3 n-4 n-4
3 3 J1
J2
J1 n-3 J2 n-3
2 2 J4 J3
n-2 n-2
1 1
0 n-1 0 n-1
front = 0; rear = 4 front = n-4; rear = 0
template <KeyType>
KeyType *Queue <KeyType> ::Delete(const KeyType &x)
// remove the front element from queue
{
if (front == rear) { QueueEmpty(); return(0); }
front = (front+1)%MaxSize;
x = queue[front]; return (&x);
}
2022/3/21 J.-J. Chen, EE NTUST Stack & Queue 30
DeQue
• Definition
– A Double-ended queue (Deque) is a linear list in which
push and delete operations may be made at either end
• Exercise
– Design a data representation that maps a deque into a
one-dimensional array
– Write algorithms to Push and delete elements from
either end of the queue
Stack::~Stack() { }
// Destructor for Bag is automatically called when Stack is destroyed.
// This ensures that array is deleted.
int* Stack::Delete(int& x)
{ if (IsEmpty()) {Empty(); return 0; }
x = array[top--];
return &x;
}
Example:
Stack s(3); int x
NW N NE
W [i][j-1] X [i][j+1] E
[i][j]
[i][j-1] X [i][j+1]
W [i][j] E
int x, y;
q move[q].x move[q].y
};
N -1 0
enum directions {
NE -1 1
N, NE, E, SE, S, SW, W, NW
}; E 0 1
SE 1 1
offset move[8];
→ the SW of (i, j) will be (g, h) where S 1 0
g= i + move[SW].x; // e.g., move[5].x SW 1 -1
h= j + move[SW].y; W 0 -1
NW -1 -1
J.-J. Chen, EE NTUST
2022/3/21 Stack & Queue 41
First Pass at Finding a Path
Through a Maze
Initialize the stack to the maze entrance coordinates and direction east;
while (stack is not empty)
{
(i, j, dir) = coordinates and direction deleted from top of stack;
while (there are more moves from (i, j) )
{
(g, h) = coordinates of next move; // visit the position clockwise
if ((g = = m) && (h = = p)) success; // output the path info. & exit
if ((!maze [g][h]) // legal move
&& (!mark [g][h])) // haven’t been here before
{
mark [g ][h] = 1;
dir = next direction to try; an item of the stack:
add (i, j, dir) to top of stack; struct items {
(i, j, dir) = (g, h, N) ; int x, y, dir;
} }
}
}
cout << "No path in maze." << endl;
J.-J. Chen, EE NTUST
2022/3/21 Stack & Queue 42
Example: A Maze Problem
Row4
(1,2) (2,1,SW) Push(1,2,SW)
(2,1) (3,2,SE) Push(2,1,SE)
C0 C1 C2 C3 C4
(3,2) (3,3,E) Pop out the entire stack
Stack success!
(2,1,SE)
(1,2,SW) Complete path:
(1,1,E) (3,3) (3,2,E) (2,1,SE) (1,2,SW) (1,1,E)
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 0 1 0 0 0 1 1 0 0 0 1 1 1 1 1 1
1 1 0 0 0 1 1 0 1 1 1 0 0 1 1 1 1
1 0 1 1 0 0 0 0 1 1 1 1 0 0 1 1 1
1 1 1 0 1 1 1 1 0 1 1 0 1 1 0 0 1
1 1 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1
1 0 0 1 1 0 1 1 1 0 1 0 0 1 0 1 1
1 0 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1
1 0 0 1 1 0 1 1 0 1 1 1 1 1 0 1 1
1 1 1 0 0 0 1 1 0 1 1 0 0 0 0 0 1
1 0 0 1 1 1 1 1 0 0 0 1 1 1 1 0 1
1 0 1 0 0 1 1 1 1 1 0 1 1 1 1 0 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Priority Operator
1 Unary minus, !
2 *,/,%
3 +,-
4 <,<=,>,>=
5 ==,!=
6 &&
7 ||
→AB/C-DE*+AC*-
m / n − 1
Stack A Stack B
– boundary[stack_no], top[stack_no]
0 m / n − 1 2 m / n − 1 m-1
b[i+1] t[i+1]
b[0] t[0] b[1] t[1] b[i] t[i] b[j] t[j] b[j+1] b[n]
X
meet
b=boundary, t=top
Find free space from right or left
(a) A B C
(b) − A + B − C + D
(c ) A * − B + C
(d ) ( A + B) D + E / ( F + A * D) + C
(e) A & & B || C ||!( E F ) (assuming C++ precedence)
( f ) !( A & &!(( B C ) || (C D))) || (C E )
(a ) ** ABC
(b) + − + uABCD (u represents the unary minus)
(c) + * AuBC
(d) + + * + ABD / E + F * ADC
(e) → &&AB || C || !>EF
→ || || && ABC ! > EF
(f) → || ! && A ! || < BC > CD < CE