You are on page 1of 12

Project:

Subject: Data Structure & Algorithm


Topic: Project
Name: Muhammad Usman Azam
Roll no: 19373
Section: BSSE (E) 3rd semester
Submitted To: Hafiz Muhammad Adnan Asghar
What is Stack?
A stack is an abstract data structure that contains a collection of elements. Stack
implements the LIFO mechanism i.e. the element that is pushed at the end is popped
out first. Some of the principle operations in the stack are −
 Push - This adds a data value to the top of the stack.
 Pop - This removes the data value on top of the stack
 Peek - This returns the top data value of the stack

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:

template <class Type, class Container = deque<Type> > class 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.

Implementation of stack (Program):


1. #include <iostream>
2. using namespace std;
3. int stack[100], n=100, top=-1;
4. void push(int val) {
5. if(top>=n-1)
6. cout<<"Stack Overflow"<<endl;
7. else {
a. top++;
b. stack[top]=val;
8. }
9. }
10. void pop() {
11. if(top<=-1)
12. cout<<"Stack Underflow"<<endl;
13. else {
a. cout<<"The popped element is "<< stack[top] <<endl;
b. top--;
14. }
15. }
16. void display() {
17. if(top>=0) {
a. cout<<"Stack elements are:";
b. for(int i=top; i>=0; i--)
c. cout<<stack[i]<<" ";
d. cout<<endl;
18. } else
19. cout<<"Stack is empty";
20. }
21. int main() {
22. int ch, val;
23. cout<<"1) Push in stack"<<endl;
24. cout<<"2) Pop from stack"<<endl;
25. cout<<"3) Display stack"<<endl;
26. cout<<"4) Exit"<<endl;
27. do {
a. cout<<"Enter choice: "<<endl;
b. cin>>ch;
c. switch(ch) {
case 1: {
28. cout<<"Enter value to be pushed:"<<endl;
29. cin>>val;
30. push(val);
31. break;
32. }
33. case 2: {
34. pop();
35. break;
36. }
37. case 3: {
38. display();
39. break;
40. }
41. case 4: {
42. cout<<"Exit"<<endl;
43. break;
44. }
45. default: {
46. cout<<"Invalid Choice"<<endl;
47. }
a. }
48. }while(ch!=4);
49. return 0;
50. }

Output:
Graphical representation of Stack:

1. A stack can be represented by means of Array, Structure, Pointer, and Linked


List.
2. Stack can either be a fixed size one or it can be of dynamic resizing that means
the size of the stack can be changed dynamically.
3. Applications:
1. The simplest application of a stack is to reverse a word. You push a given
word to stack - letter by letter - and then pop letters from the stack.
2. Another real life application is a book set where a upper book can we
taken easily.

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.

Syntax: The syntax for C++ Queue is as below:

// syntax for queue in C++

Using namespace std;


Queue<data_type>queue_name;

To use above syntax for queue in C++, it is important to include #include<queue>

header file.

queue_name is the user-defined name of the queue.

data_type is the data type of all members of the queue.

Implementation of Queue (Program):

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.

DOUBLY & CIRCULAR LINKED LIST:

Doubly linked list:

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));

10. /* Assign data values */


11. one->data = 1;
12. two->data = 2;
13. three->data = 3;

14. /* Connect nodes */


15. one->next = two;
16. one->prev = NULL;

17. two->next = three;


18. two->prev = one;

19. three->next = NULL;


20. three->prev = two;

21. /* Save address of first node in head */


22. head = one;
23.

Output:

Circular Linked List

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:

A circular linked list can be either singly linked or doubly linked.

 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));

10. /* Assign data values */


11. one->data = 1;
12. two->data = 2;
13. three->data = 3;

14. /* Connect nodes */


15. one->next = two;
16. two->next = three;
17. three->next = one;

18. /* Save address of first node in head */


19. head = one;

You might also like