Project DSA
Project DSA
Stack Syntax:
For creating a stack, we must include the <stack> header file in our code. We then
use this syntax to define the std::stack:
Type – is the Type of element contained in the std::stack. It can be any valid C++
type or even a user-defined type.
Container – is the Type of underlying container object.
Member Types:
value_type- The first template parameter, T. It denotes the element types.
container_type- The second template parameter, Container. It denotes the underlying
container type.
size_type- Unsigned integral type.
Output:
Graphical representation of Stack:
Graph:
QUEUE:
What is Queue?
A Queue is a linear structure which follows a particular order in which the operations are
performed. The order is First In First Out (FIFO). A good example of a queue is any
queue of consumers for a resource where the consumer that came first is served first.
header file.
1. #include <iostream>
2. #include <queue> // header file to use queue
functionalities in C++
3. using namespace std;
4. int main()
5. {
6. // declaration of queue named queue_sample
7. queue<int> queue_sample;
8. // inserting element in the queue container
9. queue_sample.push(1);
10. queue_sample.push(2);
11. queue_sample.push(3);
12. queue_sample.push(4);
13. queue_sample.push(5);
14. // Removing/Extracting the content from the queue
container
15. while (!queue_sample.empty()) {
16. cout << ' ' << queue_sample.front();
17. queue_sample.pop();
18. }
19. return 0;
20. }
Output:
Graphical Representation of Queue:
Queue can be implemented using an Array, Stack or Linked List. The easiest way of
implementing a queue is by using an Array.
Initially the head(FRONT) and the tail(REAR) of the queue points at the first index of the
array (starting the index of array from 0). As we add elements to the queue,
the tail keeps on moving ahead, always pointing to the position where the next element
will be inserted, while the head remains at the first index.
When we remove an element from Queue, we can follow two possible approaches
(mentioned [A] and [B] in above diagram). In [A] approach, we remove the element
at head position, and then one by one shift all the other elements in forward position.
In approach [B] we remove the element from head position and then move head to the
next position.
In approach [A] there is an overhead of shifting the elements one position forward every
time we remove the first element.
In approach [B] there is no such overhead, but whenever we move head one position
ahead, after removal of first element, the size on Queue is reduced by one space each
time.
We add a pointer to the previous node in a doubly-linked list. Thus, we can go in either
direction: forward or backward.
Graph:
Implementation (program):
1. /* Initialize nodes */
2. struct node *head;
3. struct node *one = NULL;
4. struct node *two = NULL;
5. struct node *three = NULL;
6. /* Allocate memory */
7. one = malloc(sizeof(struct node));
8. two = malloc(sizeof(struct node));
9. three = malloc(sizeof(struct node));
Output:
A circular linked list is a variation of a linked list in which the last element is linked to the
first element. This forms a circular loop.
Graph:
for singly linked list, next pointer of last item points to the first item
In the doubly linked list, prev pointer of the first item points to the last item as well.
A three-member circular singly linked list can be created as:
Implementation (Program):
1. /* Initialize nodes */
2. struct node *head;
3. struct node *one = NULL;
4. struct node *two = NULL;
5. struct node *three = NULL;
6. /* Allocate memory */
7. one = malloc(sizeof(struct node));
8. two = malloc(sizeof(struct node));
9. three = malloc(sizeof(struct node));