Stacks: Basic Features of Stack
Stacks: Basic Features of Stack
Stacks: Basic Features of Stack
Stack is an abstract data type with a bounded(predefined) capacity. It is a simple data structure that
allows adding and removing elements in a particular order. Every time an element is added, it goes
on the top of the stack, the only element that can be removed is the element that was at the top of
the stack, just like a pile of objects.
Applications of Stack
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.
There are other uses also like : Parsing, Expression Conversion(Infix to Postfix, Postfix to Prefix
etc) and many more.
Implementation of Stack
Stack can be easily implemented using an Array or a Linked List. Arrays are quick, but are limited in
size and Linked List requires overhead to allocate, link, unlink, and deallocate, but is not limited in
size. Here we will implement Stack using array.
/*
Class Stack
{
int top;
public:
int a[10];
Stack()
{
top = -1;
}
};
void Stack::push(int x)
{
if( top >= 10)
{
*/
int Stack::pop()
{
if(top < 0)
{
cout << "Stack Underflow";
return 0;
}
else
{
int d = a[top--];
return d;
}
}
void Stack::isEmpty()
{
if(top < 0)
{
cout << "Stack is empty";
}
else
{
cout << "Stack is not empty";
}
}
Position of Top
Status of Stack
-1
Stack is Empty
N-1
Stack is Full
Analysis of Stacks
Below mentioned are the time complexities for various operations that can be performed on the
Stack data structure.
Applications of Queue
Queue, as the name suggests is used whenever we need to have any group of objects in an order in
which the first one coming in, also gets out first while the others wait for there turn, like in the
following scenarios :
1. Serving requests on a single shared resource, like a printer, CPU task scheduling etc.
2. In real life, Call Center phone systems will use Queues, to hold people calling them in an order,
until a service representative is free.
3. Handling of interrupts in real-time systems. The interrupts are handled in the same order as they
arrive, First come first served.
Implementation 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 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 move all the other elements on position forward. 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 whener we move head one
position ahead, after removal of first element, the size on Queue is reduced by one space each time.
/* Below program is wtitten in C++ language */
//same as tail
int front;
//same as head
public:
Queue()
{
rear = front = -1;
}
void enqueue(int x);
int dequeue();
void display();
}
To implement approach [A], you simply need to change the dequeue method, and include a for loop
which will shift all the remaining elements one position.
return a[0];
{
a[i]= a[i+1];
tail--;
}
Analysis of Queue
Enqueue : O(1)
Dequeue : O(1)
Size : O(1)
We know that, Stack is a data structure, in which data can be added using push() method
and data can be deleted using pop() method. To learn about Stack, follow the link : Stack
Data Structure
As our Queue has Stack for data storage in place of arrays, hence we will be adding data
to Stack, which can be done using the push() method, hence :
They are a dynamic in nature which allocates the memory when required.
Linked lists let you insert elements at the beginning and end of the list.
Singly Linked List : Singly linked lists contain nodes which have a data part as well as an
address part i.e. next, which points to the next node in sequence of nodes. The operations we
can perform on singly linked lists are insertion, deletion and traversal.
Doubly Linked List : In a doubly linked list, each node contains two links the first link points to
the previous node and the next link points to the next node in the sequence.
Circular Linked List : In the circular linked list the last node of the list contains the address of
the first node and forms a circular chain.
Before inserting the node in the list we will create a class Node. Like shown below :
class Node {
public:
int data;
//pointer to the next node
node* next;
node() {
data = 0;
next = NULL;
}
node(int x) {
data = x;
next = NULL;
}
}
We can also make the properties data and next as private, in that case we will need to add the
getter and setter methods to access them. You can add the getters and setter like this :
int getData() {
return data;
}
void setData(int x) {
this.data = x;
}
node* getNext() {
return next;
}
Node class basically creates a node for the data which you enter to be included into Linked List.
Once the node is created, we use various functions to fit in that node into the Linked List.
public:
node *head;
//declaring the functions
LinkedList() {
head = NULL;
}
}
//Iterating over the list till the node whose Next pointer points to null
//Return that node, because that will be the last node.
while(ptr->next!=NULL) {
//if Next is not Null, take the pointer one step forward
ptr = ptr->next;
}
return ptr;
}
If the Node to be deleted is the first node, then simply set the Next pointer of the Head to
point to the next element from the Node to be deleted.
If the Node is in the middle somewhere, then find the Node before it, and make the Node
before it point to the Node next to it.
Now you know a lot about how to handle List, how to traverse it, how to search an element. You
can yourself try to write new methods around the List.
If you are still figuring out, how to call all these methods, then below is how your main() method
will look like. As we have followed OOP standards, we will create the objects
of LinkedList class to initialize our List and then we will create objects of Node class whenever
we want to add any new node to the List.
int main() {
LinkedList L;
//We will ask value from user, read the value and add the value to our Node
int x;
Similarly you can call any of the functions of the LinkedList class, add as many Nodes you want
to your List.
The real life application where the circular linked list is used is our Personal Computers, where
multiple applications are running. All the running applications are kept in a circular linked list and
the OS gives a fixed time slot to all for running. The Operating System keeps on iterating over the
linked list until all the applications are completed.
Another example can be Multiplayer games. All the Players are kept in a Circular Linked List and the
pointer keeps on moving forward as a player's chance ends.
Circular Linked List can also be used to create Circular Queue. In a Queue we have to keep two
pointers, FRONT and REAR in memory all the time, where as in Circular Linked List, only one pointer
is required.
node() {
data = 0;
next = NULL;
}
node(int x) {
data = x;
next = NULL;
}
}
public:
node *head;
//declaring the functions
CircularLinkedList() {
head = NULL;
}
}
int i = 0;
/* If the list is empty */
if(head == NULL) {
n->next = head;
//making the new Node as Head
head = n;
i++;
}
else {
n->next = head;
//get the Last Node and make its next point to new Node
Node* last = getLastNode();
last->next = n;
//also make the head point to the new first Node
head = n;
i++;
}
//returning the position where Node is added
return i;
}
}
else {
//getting the last node
node *last = getLastNode();
last->next = n;
//making the next pointer of new node point to head
n->next = head;
}
}
If the Node to be deleted is the first node, then simply set the Next pointer of the Head to point to
the next element from the Node to be deleted. And update the next pointer of the Last Node as
well.
If the Node is in the middle somewhere, then find the Node before it, and make the Node before it
Link Each link of a linked list can store a data called an element.
Next Each link of a linked list contains a link to the next link called Next.
LinkedList A Linked List contains the connection link to the first link called
First.
Each link carries a data field(s) and a link field called next.
Each link is linked with its next link using its next link.
Last link carries a link as null to mark the end of the list.
Circular Linked List Last item contains link of the first element as next and
the first element has a link to the last element as previous.
Basic Operations
Following are the basic operations supported by a list.
Insertion Operation
Adding a new node in linked list is a more than one step activity. We shall
learn this with diagrams here. First, create a node using the same structure
and find the location where it has to be inserted.
Now, the next node at the left should point to the new node.
This will put the new node in the middle of the two. The new list should look
like this
Similar steps should be taken if the node is being inserted at the beginning
of the list. While inserting it at the end, the second last node of the list
should point to the new node and the new node will point to NULL.
Deletion Operation
Deletion is also a more than one step process. We shall learn with pictorial
representation. First, locate the target node to be removed, by using
searching algorithms.
The left (previous) node of the target node now should point to the next
node of the target node
LeftNode.next > TargetNode.next;
This will remove the link that was pointing to the target node. Now, using
the following code, we will remove what the target node is pointing at.
TargetNode.next > NULL;
We need to use the deleted node. We can keep that in memory otherwise
we can simply deallocate memory and wipe off the target node completely.
Reverse Operation
This operation is a thorough one. We need to make the last node to be
pointed by the head node and reverse the whole linked list.
First, we traverse to the end of the list. It should be pointing to NULL. Now,
we shall make it point to its previous node
We have to make sure that the last node is not the lost node. So we'll have
some temp node, which looks like the head node pointing to the last node.
Now, we shall make all left side nodes point to their previous nodes one by
one.
Except the node (first node) pointed by the head node, all nodes should
point to their predecessor, making them their new successor. The first node
will point to NULL.
We'll make the head node point to the new first node by using the temp
node.
Link Each link of a linked list can store a data called an element.
Next Each link of a linked list contains a link to the next link called Next.
Prev Each link of a linked list contains a link to the previous link called Prev.
LinkedList A Linked List contains the connection link to the first link called
First and to the last link called Last.
Doubly Linked List contains a link element called first and last.
Each link carries a data field(s) and two link fields called next and prev.
Each link is linked with its next link using its next link.
Each link is linked with its previous link using its previous link.
The last link carries a link as null to mark the end of the list.
Basic Operations
Following are the basic operations supported by a list.
I
Circular Linked List is a variation of Linked list in which the first element
points to the last element and the last element points to the first element.
Both Singly Linked List and Doubly Linked List can be made into a circular
linked list.
The last link's next points to the first link of the list in both cases of singly as
well as doubly linked list.
The first link's previous points to the last of the list in case of doubly linked list.
Basic Operations
Following are the important operations supported by a circular list.