2. Stack
A data structure where insertion can only
be done in the end part and deletion can
only be done in the end part as well
Last-in first-out data structure (LIFO)
Supports the following operations
push – inserts an item at the end
pop – deletes the end item
peek – returns the last element
3. Stack
Study the code below
Stack s;
for(int i=10; i<25; i+=3)
s.push(i);
s.display();
s.pop();
s.display();
s.push(100);
s.display();
cout<<s.peek()<<endl;
4. Array Implementation of Stack
Just like the array implementation of the List, we
also need the array of items where we are going
to store the elements of the Stack
Aside from this, we also need an object that
keeps track of where the last element is located
From this point on, we are going to call it the top
top is simply an integer (very much like the head in
the cursor implementation of the List)
5. Array Implementation of Stack
Our Stack class should look very much like this:
const MAX = 100;
class Stack{
private:
int top, items[MAX];
public:
Stack();
bool push(int);
bool pop();
int peek(); //int top();
bool isEmpty();
bool isFull();
void display();
};
6. Array Implementation of Stack
The constructor The push
Stack::Stack(){ bool Stack::push(int x){
top = -1; if(isFull())
}
return false;
The full check
items[++top] = x;
bool Stack::isFull(){
if(top+1==MAX) return true;
return true; }
return false; The pop
} bool Stack::pop(){
The empty check if(isEmpty())
bool Stack::isEmpty(){
return false;
if(top==-1)
return true; top--;
return false; return true;
} }
9. Array Implementation of Stack
The peek
int Stack::peek(){
return items[top];
}
The display
void Stack::display(){
for(int i=top; i>=0; i--)
cout<<items[i]<<endl;
}
10.
11. Linked-list Implementation of Stack
This implementation is the linked-list
implementation of the list except for the
following operations
General insert and append
General delete
15. Linked-list Implementation of Stack
The class Stack can be declared as below
class Stack{
private:
node *head, *top;
public:
Stack();
bool push(int);
bool pop();
int peek(); //int top();
bool isEmpty();
void display();
~Stack();
};
16. Linked-list Implementation of Stack
The constructor
Stack::Stack(){
head = top = NULL;
}
The empty check
bool Stack::isEmpty(){
if(top==NULL)
return true;
return false;
}
17. Linked-list Implementation of Stack
The push
bool Stack::push(int x){
node *n = new node(x);
if(n==NULL)
return false;
if(isEmpty())
head = top = n;
else{
top->next = n;
top = n;
}
return true;
}
18. Linked-list Implementation of Stack
The pop
bool Stack::pop(){
if(isEmpty())
return false;
node *tmp = head;
node *del;
if(tmp == top){
del = top;
delete del;
head = top = NULL;
}
else{
while(tmp->next!=top)
tmp = tmp->next;
del = tmp->next;
tmp->next = NULL;
top = tmp;
delete del;
}
return true;
}
19. Doubly-linked List Implementation
of Stack
Let us review the pop of the singly-linked list
implementation of Stack
Let’s us change the definition of a node
Why not include a pointer to the previous node as well?
class node{
public:
int item;
node *next, *prev;
node(int);
node();
};
22. Doubly-linked List Implementation
of Stack
The push
bool Stack::push(int x){
node *n = new node(x);
if(n==NULL)
return false;
if(isEmpty())
top = n;
else{
top->next = n;
n->prev = top;
top = n;
}
return true;
}
23. Doubly-linked List Implementation
of Stack
The pop
bool Stack::pop(){
if(isEmpty())
return false;
node *del = top;
top = top->prev;
if(top!=NULL)
top->next = NULL;
del->prev = NULL;
return true;
}
24. Queue
The Queue is like the List but with “limited”
insertion and deletion.
Insertion can be done only at the end or rear
Deletion can only be done in the front
FIFO – first-in-first-out data structure
Operations
enqueue
dequeue
26. Array Implementation of Queue
Just like the array implementation of the List, we
also need the array of items where we are going
to store the elements of the Queue
Aside from this, we also need an object that
keeps track of where the first and last elements
are located
Size will do
27. Array Implementation of Queue
Our Queue class should look very much like this:
const MAX = 100;
template <class type>
class Queue{
private:
int size, items[MAX];
public:
Queue();
bool enqueue(type);
bool dequeue();
type front();
type rear();
bool isEmpty();
bool isFull();
void display();
};
34. Array Implementation of Queue
Our Queue class should look very much like this:
const MAX = 100;
template <class type>
class Queue{
private:
int front, rear, items[MAX];
public:
Queue();
bool enqueue(type);
bool dequeue();
type front();
type rear();
bool isEmpty();
bool isFull();
void display();
};