Sei sulla pagina 1di 39

What is Program

• A Set of Instructions
• Data Structures + Algorithms
• Data Structure = A Container stores Data
• Algoirthm = Logic + Control
Data Structures
• What is a data structure?
• “a scheme for organizing related pieces of information.“
-http://www.webopedia.com/TERM/D/data_structure.html

• Example “pieces of information”…

• Typically describes the operations which can be


performed on the data and/or how data are
organized to support those operations
C++ STACKS
18.1 Introduction to the Stack ADT

• A stack is a data structure that stores and


retrieves items in a last-in-first-out (LIFO)
manner.
Applications of Stacks
• Reverse array of characters.
• Some calculators use stacks for performing
mathematical operations.
Stack Operations
• push(paramter)
• Add a value to be stored in the top stack.
• pop()
• Removes a value from the top stack.
• top()
• Return element on top of stack.
• Bool empty()
• Return 1 if stack is empty and return 0 if stack
in not empty
The Push Operation
• Suppose we have an empty integer stack that is capable
of holding a maximum of three values. With that stack we
execute the following push operations.

push(5);
push(10);
push(15);
The Push Operation

The state of the stack after each of the push operations:


The Pop Operation
• Now, suppose we execute three consecutive
pop operations on the same stack:
Program 18-1
#include <iostream.h>
#include “stack“
cout << "Popping...\n";
void main() cout <<myStack.top()<< endl;
{
stack<int> myStack; myStack.pop();
cout << "Pushing 5\n"; cout <<myStack.top()<< endl;
myStack.push(5); myStack.pop();
cout << "Pushing 10\n";
myStack.push(10); cout <<myStack.top()<< endl;
cout << "Pushing 15\n"; myStack.pop();
myStack.push(15);
cout << "Pushing 20\n"; cout <<myStack.top()<< endl;
myStack.push(20); myStack.pop();
cout << "Pushing 25\n";
myStack.push(25); cout <<myStack.top()<< endl;
myStack.pop();}
Program 18-1 (continued)

Program Output
Pushing 5
Pushing 10
Pushing 15
Pushing 20
Pushing 25
Popping...
25
20
15
10
5
About Program 18-1

• In the program, the constructor is called with the


argument 5. This sets up the member variables as
shown in Figure 18-4. Since top is set to –1, the stack
is empty
About Program 18-1

• Figure 18-5 shows the state of the member variables


after the push function is called the first time (with 5 as
its argument). The top of the stack is now at element
0.
About Program 18-1

• Figure 18-6 shows the state of the member variables


after all five calls to the push function. Now the top of
the stack is at element 4, and the stack is full.
A balancing act
• ([]({()}[()])) is balanced; ([]({()}[())]) is not
• Simple counting is not enough to check balance
• You can do it with a stack: going left to right,
• If you see a (, [, or {, push it on the stack
• If you see a ), ], or }, pop the stack and check whether you got
the corresponding (, [, or {
• When you reach the end, check that the stack is empty
Example: 1+2*3+4
• 1 : push 1 on number stack
• + : push + on op stack
• 2 : push 2 on number stack
• * : because * has higher precedence than +, push * onto op stack
• 3 : push 3 onto number stack
• + : because + has lower precedence than *:
• pop 3, 2, and *
• compute 2*3=6, and push 6 onto number stack
• push + onto op stack
• 4 : push 4 onto number stack
• end : pop 4, 6 and +, compute 6+4=10, push 10; pop 10, 1, and +,
compute 1+10=11, push 11
• 11 (at the top of the stack) is the answer
Stack Task
• Write a code to get maximum element in stack.
C++ QUEUES
Introduction to the Queue ADT
• A queue represents a waiting list
• A queue can be viewed as a special type of list, where
the elements are inserted into the end (tail) of the
queue, and are accessed and deleted from the
beginning (head) of the queue
Queue Applications
• Real Time applications:
• Buy a movie ticket
• Check out at a bookstore
• Bank / ATM
• Call an airline
• Cashier lines in any store
• Computer Science Applications:
• OS task scheduling
• Print lines of a document
• Printer shared between computers
• Convert digit strings to decimal
• Shared resource usage (CPU, memory access, …)
Queue Operations
• Push(parameter)
• causes a value to be stored in the queue
• Pop()
• Remove a value from the queue
• front()
• retrieves a from front of queue
• back()
• retrieves a from front of queue
queue FIFO(First In First Out)
• ordered list
• all insertions are made at one end called
“end” -> insertion means push
• all deletions are made at the other end
called “front” delete mean pop
Queue Illustration

Push(D)

Push(A)

Pop(D)
Push(T)

Push(U)

Push(M)
Empty( )

size( )
Using Queue in code blocks C++
Queue Task
Write a program to display the content of a queue in
reverse direction.
C++ LISTS
Motivation:Why Lists?

• Arrays can be used to store linear data of similar types, but


arrays have following limitations:

• The size of the arrays is fixed: So we must know the max no.
of elements in advance.

• Inserting a new element in an array of elements is expensive,


because room has to be created for the new elements and to
create room existing elements have to shifted.
Why Lists? (Cont.)
• For example, in a system if we maintain a sorted list of IDs in
an array id[].
• id[] = [10,20,30,40,50].

And if we want to insert a new ID 15, then to maintain the


sorted order, we have to move all the elements after 10
(excluding 10).

• Deletion is also expensive with arrays until unless some special


techniques are used. For example, to delete 20 in id[],
everything after 20 has to be moved.
Advantages of Lists over arrays

• Dynamic size
• Ease of insertion/deletion
Linked list (Types)

2 list
Front back

Data Data Data


Insertion Methods in list
• The following methods are defined in the list
• ■ push_back() insert at end
• ■ insert() insert after a given position.
• Additionally, the following method is available in the list
and deque classes
• ■ push_front() insert at beginning.
• pop_front() remove from beginning.

Potrebbero piacerti anche