Data Structure Notes

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Abstract data type

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 Abstract Data Type


Data Structure is a systematic way of organizing, storing and retrieving data and their relationship
with each other.
Classification of Data Structure:
There are two main types of Data Structure classification.
1. Primitive Data Structure - Int, char, float, pointer
2. Non-primitive Data Structure
 Linear - Array, Linked list, Stack, queue
 Non-Linear - Trees, Graphs
Primitive Data Structure: It is the basic Data structure which can be directly operated by the
machine instruction.
Non-Primitive Data Structure: It is the Data structure which emphasizes on structuring of a group of
homogenous or heterogeneous data items.
Linear Data Structures: It is a data structure which contains a linear arrangement of elements in the
memory.
Non-Linear Data Structure: It is a data structure which represents a hierarchical arrangement of
elements.

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.

Implementation of List: There are two methods


1. Array
2. Linked list
Array Implementation of list:
Array: A set of data elements of same data type is called array. Array is a static data structure i.e.,
the memory should be allocated in advance and the size is fixed. This will waste the memory space
when used space is less than the allocated space. An array implementation allows the following
operations.
The basic operations are:
a. Creation of a List.
b. Insertion of a data in the List
c. Deletion of a data from the List
d. Searching of a data in the list
Limitation of array implementation
 An array size is fixed at the start of execution and can store only the limited number of
elements.
 Insertion and deletion of array are expensive.
 Since insertion is performed by pushing the entire array one position down and deletion is
performed by shifting the entire array one position up.

2. Singly Linked List/Linked List


A linked list is the collection of nodes that are randomly stored. Each node consists of two
fields, i.e., data and link. Here, data is the value stored at that particular node, and the link is
the pointer that holds the address of the next node.

Use of Linked List

 It is used to implement stacks and queues


 To prevent the collision between the data in the hash map, we use a singly linked
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.

addNode() will add a new node to the list:

1. Create a new node.


2. It first checks, whether the head is equal to null which means the list is empty.
3. If the list is empty, both head and tail will point to the newly added node.
4. If the list is not empty, the new node will be added to end of the list such that
tail's next will point to the newly added node. This new node will become the
new tail of the list.

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

3.Doubly Linked List


 A doubly linked list is a linked data structure that consists of a set of sequentially
linked records called nodes. Each node contains three fields: two link fields
(references to the previous and to the next node in the sequence of nodes) and one
data field. 
• Data− Each node 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 Representation :

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.

 Stack is an ordered list of similar data type.


 Stack is a LIFO(Last in First out) structure .

Operation of stack

 push() function is used to insert new elements into the Stack


 pop() function is used to remove an element from the stack. Both insertion and
removal are allowed at only one end of Stack called Top.
 Stack is said to be in Overflow state when it is completely full and is said to be in
Underflow state if it is completely empty.

Types of Implementation in Stack


1, Static Implementation (Array implementation of Stack)
2, Dynamic Implementation (Lined List Implementation of Stack)

Algorithm for PUSH operation

1. Check if the stack is full or not.


2. If the stack is full, then print stack is full and exit the program.
3. If the stack is not full, then increment the top and add the element.

public void push(String item) // add an item on top of stack


{
if(top == a.length-1)
{ System.out.println("Stack is full");
return;
}
top++; // increment top
a[top] = item; // insert an item
}
Algorithm for POP operation

1. Check if the stack is empty or not.


2. If the stack is empty, then print stack is empty and exit the program.
3. If the stack is not empty, then print the element at the top and decrement the top.

public String pop() // remove an item from top of stack


{
if( isEmpty() )
{
System.out.println("Stack is empty");
return null;
}
String item = a[top]; // access top item
top--; // decrement top
return item;
}

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

1. Implementation using Array (Static Queue)

2. Implementation using Linked List (Dynamic Queue)


Operation on Queues
1. enqueue() Insert an element at the rear end of the queue.
2. dequeue() Deletes the front element and return the same.
3. empty() It returns true(1) if the queue is empty and return false(0) if the queue is not
empty.
4. full() It returns true(1) if the queue is full and return false(0) if the queue is not full.
Algorithm for enqueue operation

1. Check if the queue is full or not.


2. If the queue is full, then print queue is full and exit the program.
3. If the queueis not full, then increment the rear and add the element.

public void insert(Object item)


{
if( count == maxSize )
{
System.out.println("Queue is Full"); return;
}
else que[++rear] = item;
count++;
}
Algorithm for dequeue operation

1. Check if thequeue is empty or not.


2. If the queue is empty, then print queue is empty and exit the program.
3. If the queue is not empty, then print the element at the front and increment the front.
public void dequeue() // delete item from front of queue
{
if( isEmpty() )
{
System.out.println("Queue is Empty");
}
tmp = que[front];
front++;
}
Application of queue

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.

You might also like