Data Structure Notes
Data Structure Notes
Data Structure Notes
An abstract data type is defined as a mathematical model of the data objects that make up a data
type as well as the functions that operate on these objects. There are no standard conventions for
defining them. A broad division may be drawn between "imperative" and "functional" definition
styles.
List ADT:
A list is a dynamic data structure. The no. of nodes on a list may vary dramatically as elements are
inserted and removed. The dynamic nature of list is implemented using linked list and the static
nature of list is implemented using array.
In general the list is in the form of elements A1, A2, …, AN, where N is the size of the list associated
with a set of operations:
Insert: add an element e.g. Insert(X,5)-Insert the element X after the position 5.
Delete: remove an element e.g. Delete(X)-The element X is deleted.
Find: find the position of an element (search) e.g. Find(X)-Returns the position of X
MakeEmpty: Make the list as empty list.
Basic Operations :
Following are the basic operations supported by a singly linked list.
• Insertion − Adds an element at the beginning of the list.
• Deletion − Deletes an element at the beginning of the list.
• Insert at end − Adds an element at the end of the list.
• Delete at end − Deletes an element from the end of the list.
• Insert After − Adds an element after an item of the list.
Create a class Node which has two attributes: data and next. Next is a pointer to the next
node.Create another class which has two attributes: head and tail.
public class SinglyLinkedList
{
class Node
{
int data;
Node next;
public Node(int data)
{
this.data = data;
this.next = null;
}
}
public Node head = null;
public Node tail = null;
public void addNode(int data)
{
Node newNode = new Node(data);
if(head == null)
{
head = newNode;
tail = newNode;
}
else
{
tail.next = newNode;
tail = newNode;
}
}
public void deleteFromEnd() {
if(head == null) {
System.out.println("List is empty");
}
else {
if(head != tail ) {
Node current = head;
while(current.next != tail) {
current = current.next;
}
tail = current;
tail.next = null;
}
else {
head = tail = null;
} } }
Basic Operations :
Following are the basic operations supported by a doubly linked list.
• Insertion − Adds an element at the beginning of the list.
• Deletion − Deletes an element at the beginning of the list.
• Insert at end − Adds an element at the end of the list.
• Delete at end − Deletes an element from the end of the list.
• Insert After − Adds an element after an item of the list.
• Delete − Deletes an element from the list using the key.
• Display forward − Displays the complete list in a forward manner.
• Display backward − Displays the complete list in a backward manner.
Insertion Operation :
The following code demonstrates the insertion operation at the beginning of a doubly
linked list.
void insertFirst(int key, int data)
{
link->key = key;
link->data = data;
if(isEmpty())
last = link;
else
head->prev = link;
link->next = head;
head = link;
}
Deletion Operation :
Following code demonstrates the deletion operation at the beginning of a doubly
linked list.
deleteFirst()
{
*tempLink = head;
if(head->next == NULL)
last = NULL;
else
head->next->prev = NULL;
head = head->next
return tempLink; //return the deleted link
}
STACK
A Stack is data structure in which addition of new element or deletion of existing element
always takes place at a same end. This end is known as the top of the stack. That means that it
is possible to remove elements from a stack in reverse order from the insertion of elements
into the stack.
Operation of stack
Application of stack
1. Expression Evaluation.
2. Expression Conversion
o Infix to Postfix
o Infix to Prefix
o Postfix to Infix
o Prefix to Infix
3. Towers of Hanoi
4. Backtracking.
5. Memory Management.
6. Parsing
7. Recursive Function
8. Calling Function
Infix Notation
a - b + c, where operators are used in-between operands.
Prefix Notation
In this notation, The operator is written before the operands. For example, +ab. This is
equivalent to its infix notation a + b. Prefix notation is also known as Polish Notation.
Postfix Notation
This notation style is known as Reversed Polish Notation. The operator is written after the
operands. For example, ab+. This is equivalent to its infix notation a + b.
Queue
Queues is a kind of abstract data type where items are inserted one end (rear end) known
as enqueue operation and deleted from the other end(front end) known as dequeue
operation.This makes the queue a First-In-First-Out (FIFO) data structure.
Implementation of Queues
Circular queue
Circular Queue is a linear data structure in which the operations are performed based on
FIFO(First In First Out) principle and the last position is connected back to the first position
to make a circle. It is also called ‘Ring Buffer’.
Priority queue
A priority queue is an abstract data type that behaves similarly to the normal queue except
that each element has some priority, i.e., the element with the highest priority would come
first in a priority queue.