Abstract Data Types (Adts) : Applications of Data Structures
Abstract Data Types (Adts) : Applications of Data Structures
Data structures
A data structure is a mathematical or logical way of organizing data in the memory that
consider not only the items stored but also the relationship to each other and also it is
characterized by accessing functions.
Operating System
Database Management system
Network analysis
Abstract Data type is defined by the set of operations that may be performed on it and by
mathematical constraints on the effects of those operations
Abstract Data type is an extension of modular design.
An abstract data type is a set of operations such as Union, Intersection,
Complement, Find etc.,
The basic idea of implementing ADT is that the operations are written once in
program and can be called by any part of the program.
List ADT
Array-based implementation
Implementation of List ADT
1. Array Implementation
2. Linked List Implementation
www.studentsfocus.com
1. Array Implementation of List
• Each node in a linked list contains one or more members that represent data.
• In addition to the data, each node contains a pointer, which can point to another
node.
www.studentsfocus.com
• A linked list is called "linked" because each node in the series has a pointer that
points to the next node in the list.
// Node class
class Node {
int data;
Node* next;
public:
www.studentsfocus.com
Node() {};
void SetData(int aData) { data = aData; };
void SetNext(Node* aNext) { next = aNext; };
int Data() { return data; };
Node* Next() { return next; };
};
Example: Insert the element ‘5’ in the beginning of the list temp.
Before insertion
After Insertion :
if ( tmp != NULL ) {
// Nodes already present in the list
// Parse to end of list
while ( tmp->Next() != NULL ) {
tmp = tmp->Next();
}
DELETION AT FIRST
Before deletion
www.studentsfocus.com
After deletion
/**
* Delete a node from the list
*/
void List::Delete(int data) {
// No nodes
if ( tmp == NULL )
return;
Polynomial Manipulation
Polynomial ADT
A set of values and a set of allowable operations on those values.
www.studentsfocus.com
1. Array (not recommended)
2. Linked List (preferred and recommended)
1. Array Implementation:
Stack
www.studentsfocus.com
Linear data structure
Follows LIFO concept
Addition of new items and the removal of existing items always takes place at the same
end. This end is commonly referred to as the “top.”
Basically three operations that can be performed on stacks They are
1) inserting an item into a stack (push).
2) deleting an item from the stack (pop).
3) displaying the contents of the stack.
Exceptional Conditions
OverFlow
Attempt to insert an element when the stack is full is said to be overflow.
UnderFlow
Attempt to delete an element, when the stack is empty is said to be underflow.
Implementation of stack
Array Implementation of Stack
Linked List Implementation of Stack
Array Implementation of Stack
In this implementation each stack is associated with a pop pointer, which is -1 for an
empty stack.
To push an element X onto the stack, Top Pointer is incremented and then set Stack [Top]
= X.
To pop an element, the stack [Top] value is returned and the top pointer is decremented.
www.studentsfocus.com
Push
void push(int element)
{
if(count == maxnum)
cout<<"stack is full";
else {
node *newTop = new node;
if(top == NULL)
{
newTop->data = element;
newTop->next = NULL;
top = newTop;
count++;
}
else
{
newTop->data = element;
newTop->next = top;
top = newTop;
count++;
}
}
}
Pop
void pop()
{
if(top == NULL)
cout<< "nothing to pop";
else
{
node * old = top;
top = top->next;
count--; delete(old);
}
}
Applications Of Stack
Some of the applications of stack are :
www.studentsfocus.com
Evaluating arithmetic expression
Balancing the symbols
Towers of Hannoi
Function Calls.
8 Queen Problem
Queue
Queue is also an abstract data type or a linear data structure, in which the first element is
inserted from one end called REAR(also called tail), and the deletion of exisiting element
takes place from the other end called as FRONT(also called head). This makes queue as FIFO
data structure, which means that element inserted first will also be removed first.
The process to add an element into queue is called Enqueue and the process of removal of an
element from queue is called Dequeue.
Implementation of queue
1. Array Implementation
Like a stack, an array is simple to use but has the limitation that the array may overflow.
Solution to reuse the array is to wrap around the end of the array.
www.studentsfocus.com
2. Implementing a queue as a linked list
The limitations of using an array for a stack are the same as using an array for
queue.For nodes we use the same LLObjectNode class we used for the linked implementation of
stacks.
www.studentsfocus.com
Dequeue operation
www.studentsfocus.com
a
ab
4) Read ‘*’, here ‘+’ has low priority than ‘*’, so ‘*’ is push into a stack.
ab
6) Read ‘+’, here ‘+’ has low priority than ‘*’, so pop ‘*’ & placed into output
abc*
7) In stack ‘+’ has equal priority, so pop ‘+’ and placed in to output.
abc*+
abc*+
abc*+d
abc*+d
www.studentsfocus.com
11) Read‘e’, place into an output.
abc*+de
12) Read ‘+’, here ‘+’ has low priority than ‘*’, so pop ‘*’ & placed into output
abc*+de*
14) Read ‘)’, pop all operators from ‘(‘ to ‘)’ and the popped operators are placed into an
output.
abc*+de*f+
15) Read ’*’, here ‘+’ has low priority than ‘*’, so ‘*’ is push into a stack.
abc*+de*f+
abc*+de*f+g
17) Now the input string is end, so we pop all the operators from stack and place into
output.
abc*+de*f+g*+
www.studentsfocus.com
Example: 6523+8*+3+*
3
TOP 2
2) Next ‘+’ is read, so 3 & 2 are popped & their sum 5 is pushed.
5
TOP 5
6
4) Next ‘*’ is read, so 8 & 5 are popped and 5*8=40 is pushed to stack
40
TOP 5
6
www.studentsfocus.com
and their sum 48 is pushed to stack.
www.studentsfocus.com
Sri vidya College of Engineering & Technology, Virudhunagar Course Material(Lecture
Notes)
EC6301 & Object oriented programming and Data Structures Unit III Page 16
www.studentsfocus.com