Characteristics of Ds Linear: Circular Linked List
Characteristics of Ds Linear: Circular Linked List
A single Operations in Strings SINGLY LINKED LIST:-Linked List can be defined as Applications of Queues
unit of value is called a Data item. Data items are 1) Length: Find length of the string. [Number of collection of objects called nodes that are randomly Following applications requiring FIFO storage and
divided into subgroups called Group items. characters in a string is called its length.] stored in the memory.A node contains two fields are implemented using Queues.
Entity is something that has certain attributes Eg: s=”computer’ i.e. data stored at that particular address and the Queues:-are used in Scheduling processes like LRU
which may be assigned values. An entity with Length(s) = 8 pointer which contains the address of the next node (Least Recently Used)In asynchronous programming
similar attributes is called an Entity set 2) Substring: Accessing a substring from a given in the memory.The last node of the list contains *Manage resource sharing such as CPU scheduling,
Time complexity is a function describing the string. pointer to the disk scheduling..etc
amount of time an algorithm takes in terms of the 3) SUBSTRING(string, initial,length) TWO WAY LINKED LIST/DOUBLY LINKED LIST *When data is send and recieved betwwen two
amount of input to the algorithm. Eg: S=”TO BE OR NOT TO BE” , 3,4) =BE O A Doubly Linked List (DLL) contains an extra pointer, processos not necessivly at same rate.
Space complexity is a function describing the 3) Concatenation: let S1 and S2 be strings s1//s2 is typically called previous pointer, together with next Applications of Graphs
amount of memory (space) an algorithm takes in the string consisting of the characters of s1 followed pointer and data which are there in singly linked *Transportation networks that like the one used by
terms of the amount of input to the algorithm. Big by the characters of s2. list.A doubly linked list or a two-way linked list is a Google Map.
O notation is the language we use for talking about Eg: S1= ‘welcome” more complex type of linked list which contains a *Representation of molecular structure.
how long an algorithm takes to run. Order of S2=”home” pointer to the next as well as the previous node in *Finding shortest path.
magnitude is often called Big-O notation (for S1//s2=” welcomehome” sequence, Therefore, it contains three parts are *Airline network.
“order”) and written as O(f(n)). 4) Indexing (pattern matching): find the position data, a pointer to the next node, and a pointer to *Social networks.
Common Functions for Big-O where a string pattern P Firstappears in a given the previous node. This would enable us to traverse Expression Trees
F(n) Name string T. the list in the backward direction as well An expression tree is a representation of
1 Constant Eg: T=”welcome to ABC college” INDEX(T,”to”) =8 CIRCULAR LINKED LIST:Circular linked list is a linked expressions arranged in a tree-like data structure. In
logn Logarithmic Explain Pattern matching algorithm list where all nodes are connected to form a circle. other words, it is a tree with leaves as operands of
n Linear Ans: Pattern matching algorithm Pattern Searching There is no NULL at the end. A circular linked list the expression and nodes contain the operators.
nlog n Log Linear algorithms are used to find a pattern or substring can be a singly circular linked list or doubly circular Similar to other data structures, data interaction is
n2 Quadratic from another bigger string. There are different linked list.AdvantagesofCircularLinkedLists: also possible in an expression tree. Expression trees
n3 Cubic algorithms. The main goal to design these types of 1) Any node can be a starting point. We can are mainly used for analyzing, evaluating and
2 n Exponential algorithms to reduce the time traverse the whole list by starting from any point. modifying expressions, especially complex
Eg: complexity. The traditional approach may take lots 2)useful to implementation of queue expressions.
Linear search : O(n) of time to complete the pattern searching task for a CHARACTERISTICS OF DS BINARY SEARCH TREE(BST)
Binary search : O(log n) longer text. • Linear: A linear describes data characteristicsBinary Search Tree is a node-based binary tree data
Bubble sort : O(n2) Eg: Main String: “ABAAABCDBBABCDDEBCABC”, whether the data items are arranged instructure which has the following properties:
Merge sort : O(n log n) Pattern “ABC” sequential form like an array. • The left subtree of a node contains only nodes
Write different applications of data structure. Output:Pattern found at position: 4 • Non-Linear: A Non-Linear data structurewith keys lesser than the node’s key
1. Dynamic memory allocation Pattern found at position: describes the characteristics of data items that • The right subtree of a node contains only nodes
2. Maintaining directory of names 10 are not in sequential form like a tree, graph. with keys greater than the node’s key.
3. Performing arithmetic operations on long Pattern found at position: 18 • Static: It is a static data structure that • The left and right subtree each must also be a
integers Algorithm: describes the size and structures of a collection of binary search tree.
4. History of visited websites P and T are strings with length R and S. data items associated with a memory location at The binary search tree is considered as efficient
5. job scheduling 1. set k=1 and MAX= S – R +1 compile time that are fixed. Example – Array. data structure in compare to arrays and linked lists.
• Homogenous: It is a characteristic of data
Operations in data structure 2. 2. Repeat steps 3 to 5 while K<=MAX In searching process, it removes half sub-tree at
structures representing whether the data type of
: Operation means processing the data in the data 3. Repeat for L=1 to R every step. Searching for an element in a binary
all elements is the same. Example- Array.
structure. If P[L]≠T[K+L-1] then goto step 5 search tree takes o(log2n) time. In worst case, the
• Non-Homogenous: It is a characteristic of data
The following are some important operations. 4. Set INDEX= K and exit
structures representing whether the data type time it takes to search an element is 0(n).
a. Traversing 5. Set K=K+1 elements may or may not be the same Adding New Node BST:-
b. Searching 6. Set INDEX=0 • Dynamic: It is a dynamic data structure that •Create a new BST node and assign values to it.
c. Inserting 7.Exit defines the shrinking and expanding of data items •Insert(node, key)If root == NULL,
d. Deleting ARRAY VS LINKED LIST at the run time or the program’s execution. It is •Return the new node to the calling function.If
e. Sorting Array:-•Size of an array is fixed •Memory is also related to the utilization of memory location root=>data < key
f. Merging allocated from stack•It is necessary to specify the that can be changed at the program’s run time. •Call the insert function with root=>right and assign
Traversing number of elements during declaration (i.e., Example: Linked list the return value in root=>right.
To visit or process each data exactly once in the duringcompile time).• It occupies less memory than Applications of data structures • Root->right = insert(root=>right,key)
data structure a linked list for the same number of elements. Different types of data structures are used for • If root=>data > key
Searching •Inserting new elements at the front ispotentially different kinds of purposes. Some of them are •Call the insert function with root->left and assign
To search for a particular value in the data structure expensive because existing elements need to be highly specialised in specific tasks. Applications of the return value in root=>left.
for the given key value. shifted over to make room•Deleting an element data structures are listed below. •Root=>left = insert(root=>left,key)
Inserting from an array is not possible. Applications of Arrays •Finally, return the original root pointer to the
To add a new value to the data structure Linked List:-•Size of a list is not fixed•Memory is *To implement mathematical vectors and matrices. calling function.
Deleting allocated from heap•It is not necessary to specify *To model sets or collections in computer APPLICATIONS OF TREES
To remove a value from the data structure the Number of elements during declaration(i.e., programming. • Storing naturally hierarchical data: Trees are
Sorting memory is allocated during run Time).•It occupies *To implement other data structures such as lists, used to store the data in the hierarchical structure.
To arrange the values in the data structure in a more memory.•Deleting an element is possible queue, stack and heaps. For example, the file system. The file system stored
particular order. ARRAY:- *Sometimes used to emulate in-program dynamic on the disc drive, the file and folder are in the form
Merging An array is a collection of similar data elements memory allocation. of the naturally hierarchical data and stored in the
To join two same type of data structure values stored at contiguous memory locations. It is the Applications of Linked lists form of trees.
Categories of DS simplest data structure where each data element *To implement other data structures such as •Organize data: It is used to organize data for
Primitive can be accessed directly by only using its index queues,stacks,trees…etc efficient insertion, deletion and searching. For
Non-primitive number. … Rather, we can define an array which *Used for dynamic memory allocation. example, a binary tree has a logN time for searching
Primitive data types: will store the data elements at contiguous memory *For manipulating polynomials, representing sparse an element.
These are the data structures which are directly locations. matrices…etc •Trie: It is a special kind of tree that is used to store
supported by the machine.i.e,Any operation can be 2D ARRAYThe two-dimensional array can be *Doubly linked list can be used in navigation the dictionary. It is a fast and efficient way for
performed in these data items. The different defined as an array of arrays. The 2D array is systems. dynamic spell checking.
primitive data types are: Integer, Float, Double organized as matrices which can be represented as *Doubly linked list is also used by various •Heap: It is also a tree data structure implemented
Non -Primitive data types: the collection of rows and columns. However, 2D application to implement Undo and Redo using arrays. It is used to implement priority
These Data structures do not allow any specific arrays are created to implement a relational functionality. queues.
instructions to be performed on the Data items database lookalike data structure. It provides ease *Advanced data structures like Fibonnacci Heap are •B-Tree and B+Tree: B-Tree and B+Tree are the tree
directly. of holding the bulk of data at once which can be implemented using circular doubly linked lists. data structures used to implement indexing in
The different non primitive data types are: Arrays, passed to any number of functions wherever Applications of Trees databases.
Structures, Unions Non- primitive data types are required.Syntax: *To search an element in a set quickly, Binary •Routing table: The tree data structure is also used
again divided into 2 data_type array_name[rows][columns]; Search Trees(BSTs) are used. to store the data in routing tables in the routers.
1. Linear LINKED LIST:Linked lists and arrays are similar since *Heap sort is done by a kind of tree called Heap. Θ Notation: The theta notation bounds a function
2. Non – linear they both store collections of data. Array is the *Tries are modified version of trees used to store from above and below, so it defines exact
Linear Data structures: Most common data structure used to store routing informations in routers. asymptotic behavior. A simple way to get Theta
This Data Structures involve arranging the elements collections of elements. Arrays are Convenient to *Compiler use a syntax tree to parse program you notation of an expression is to drop low order terms
in linear fashion. declare and provide the easy syntax to access any write. and ignore leading constants
Eg: Stacks, Queue, Lists element by its index Number. Once the array is set *Shortest path trees and spanning Trees are used in • Big O Notation: The Big O notation defines an
Non- Linear Data structures: up, access to any element is convenient and fast bridges and routers. upper bound of an algorithm, it bounds a function
This Data structures involve representing the Advantages of linked lists: Applications of Stacks only from above. For example, consider the case of
elements in Hierarchical order. •Linked lists are dynamic data structures. i.e., they •Stacks are used in applications like:UNDO/REDO Insertion Sort. It takes linear time in best case and
Eg: Trees, Graphs can grow or shrink during The execution of a LIFO property of Stack can be useful in the following quadratic time in worst case. We can safely say that
Define string? Explain different string operations. program. applications. the time complexity of Insertion sort is O(n^2). Note
Ans: Strings are defined as an array of characters. •Linked lists have efficient memory utilization. *Evaluating Expressions. that O(n^2) also covers linear time.
The difference between a Here, memory is not pre-Allocated. Memory is *Converting between expressions. • Ω Notation: Just as Big O notation provides an
character array and a string is the string is allocated whenever it is required and it is de- *Back tracking. asymptotic upper bound on a function, Ω notation
terminated with a special character ‘\0’. allocated when it is no longer needed. •Insertion *Parsing context-free languages. provides an asymptotic lower bound. Ω Notation
Recursion: and Deletions are easier and efficient. *Recursion removal. can be useful when we have lower bound on time
A function is recursive if a statement in the body of •Linked lists provide flexibility•In inserting a data *Tree and graph traversal. complexity of an algorithm
the function calls itself. Recursion is The process of item at a specified position and deletion of the data
defining something in terms of itself. For a item From the given position.
computer language to be Recursive, a function must •Many complex applications can be easily carried
be able to call itself. out with linked lists.
CONVERT INFIX TO POSTFIX Stack using Linked List What is priority queue? Depth First Search Algorithm(Graph)
•Push “(“ onto stack, and add “)” to the end of X. In linked list implementation of stack, the nodes are Ans: It is a collection of elements such that each Depth-first search or depth-first traversal is a
•Scan X from left to right and repeat Steps 3 to 6 for maintained non-contiguously in the memory. Each element has been assigned a priority and such that recursive algorithm used to visit all the vertices in a
each element of X until the STACK is empty : node contains a pointer to its immediate successor the order in which elements are deleted and graph.In the DFS algorithm, the traversal starts from
•If an operand is encountered, add it to Y. node in the stack. Stack is said to be overflown if processed comes from the following rules: An a node and then follows its outgoing nodes. The
•If a left parenthesis is encountered, push it onto the space left in the memory heap is not enough to element of higher priority is processed before any search goes deeper and deeper until the goal node
STACK. create a node element of lower priority. Two elements with the is found, or until a node that has no children is
•if an operator is encountered, then(a) Repeatedly Push an element to stack using Linked List same priority are processed according to the order found.
pop from STACK and add to Y each operator (on the Step 1 - Create a newNode with given value. in which they were added to the queue. DFS implementation puts each vertex in the graph
top of STACK) which has the same precedence as or Step 2 - Check whether stack Define forest. . into the following lists:
higher precedence than operator.(b) Add operator is Empty (top == NULL) Forest is a collection of disjoint trees. In other *Visited. *Not Visited (Stack).
to STACK./*End of If structure */ Step 3 - If it is Empty, then set newNode→ words, we can also say that forest is a collection of This marking is used to avoid cycles while
•if a right parenthesis is encountered, then : next = NULL. an acyclic graph which is not connected. Here is a traversing. The DFS algorithm performs the
(a) Repeatedly pop from STACK and add to Y each Step 4 - If it is Not Empty, then set newNode→ pictorial representation of a forest. following operations:
operator (on the top of STACK) until a left next = top. What are the applications of tree? 1.Start from any vertex. PUSH it onto the stack.
parenthesis is encountered. Step 5 - Finally, set top = newNode 1. Store hierarchical data, like folder structure, 2.POP the top item from the stack and add to the
(b) Remove the left parenthesis. [Do not add the What are the different types of notations? organization structure, XML/HTML data. list of visisted nodes.
left parenthesis to Y]./* end of If structure *//* end Infix : 2. Binary Search Tree :is a tree that allows fast 3.Select a adjacent vertex that is not visisted and
of Step 2 loop */ An expression is called the Infix expression if the search, insert, delete on a sorted data. It also PUSH it onto the stack.
•exit. operator appears in between the operands in the allows finding closest item 4.Repeat steps 2 and 3 until stack is empty.
MEMORY ALLOCATION IN TWO DIMENSIONAL expression. 3. Heap: is a tree data structure which is The formal algorithm is as follows:
ARRAY Example : A+B implemented using arrays and used to implement 1: SET STATUS=1 (Not Visted) for each node in G
1)using a single pointer:- A simple way is to allocate (A+B) * (C-D) priority queues. 2: Push the starting node A on the stack and set its
memory block of size r*c and access elements using Prefix : 4. B-Tree and B+ Tree : They are used to implement STATUS=2 (waiting state)
simple pointer arithmetic. An expression is called the prefix expression if the indexing in databases. 3: Repeat Steps 4 and 5 until STACK is empty
2)using an array of pointers:- We can create an operator appears in the expression before the 5. Syntax Tree: Used in Compilers. 4: Pop the top node N. Process it and set its
array of pointers of size r. Note that from C99, C operands. 6. K-D Tree: A space partitioning tree used to STATUS=3 (Visited)
language allows variable sized arrays. After creating Prefix notation is also known as Polish Notation. organize points in K dimensional space. 5: Push on the stack all the neighbours of N that are
an array of pointers, we can dynamically allocate Example : 7. Trie : Used to implement dictionaries with prefix not visisted (whose STATUS=1) and set their
memory for every row. infix prefix lookup. STATUS=2 (waiting state)
3)using pointer to a pointer:- We can create an A+B +AB 8. Suffix Tree : For quick pattern searching in a fixed 6: EXIT
array of pointers also dynamically using a double (A+B) * (C-D) *+AB-CD text. Depth-first search or depth-first traversal is a
pointer. Once we have an array pointers allocated Postfix : 9. Spanning Trees and shortest path trees are used recursive algorithm used to visit all the vertices in a
dynamically, we can dynamically allocate memory An expression is called the postfix expression if the in routers and bridges respectively in computer graph.
and for every row like method 2. 4) Using double operator appears in the expression after the networks Breadth First Search Algorithm(graph)
pointer and one malloc call; operands. 10.As a workflow for compositing digital images for In the BFS algorithm, the traversal starts from a
GRAPH Postfix notation is also known as Reverse Polish visual effects. node and then carries onto its adjacent nodes. It
A graph can be defined as group of vertices and Notation or Suffix. QuickSort traverses all the sibling nodes within a level and
edges that are used to connect these vertices. A Example : QuickSort is a sorting algorithm that works based on then moves to the next level. The search continues
graph can be seen as a cyclic tree, where the Infix postfix the divide-and-conquer strategy. It works as until all the vertices are visited.
vertices (Nodes) maintain any complex relationship A+B AB+ follows: BFS implementation puts each vertex in the graph
among them instead of having parent child (A+B)*(C-D) AB+CD-* 1.Choose an element as pivot from the array. into the following lists:
relationship 2.Position the pivot element in such a way that all *Visited. *Not Visited (Queue).
WEiGHTED GRAPH ?write down stack operations algorithms. elements less than the pivot appear before it and all This marking is used to avoid cycles while
A WG is a graph with the property that its edges Two basic operations: elements greater than the pivot appear after it traversing. The BFS algorithm performs the
have a number associated with it.the number is a) Push : Insert an element into a stack. (equal values can go either way). following operations:
called weight of the edge.The WG can be b) Pop : Delete an element from a stack. Sort the two sub-arrays recursively. 1.Put the starting node to the back of the Queue.
undirected or directed Algorithm:PUSH The pivot element can be selected in any of the 2.Repeat Steps 3 and 4 until Queue is empty.
DIRECTED GRAPH PUSH(STACK,TOP,MAXSTK,ITEM) following ways: 3.Remove the front node N of Queue and add it to
A DG is a graph with the property that it edges have 1: if TOP = MAXSTK then print: OVERFLOW and *Pick first element as pivot (Explained Here). the Visited List.
directions.They are also called digraph return. *Pick last element as pivot. 4.Add all unvisited neighbours of N to the rear of
INSERTION INTO A QUEUE 2: set TOP = TOP + 1 *Pick a random element as pivot. the Queue
Step 1: IF REAR = MAX-1 Write OVERFLOW Goto 3: set STACK[TOP] = ITEM *Pick median as pivot Recursion Using Stack:A function that calls itself is
step 4 [END OF IF] Step 2: IF FRONT=-1 and REAR=-1 4: return The Complexity of QuickSort Algorithm called a recursive function and this technique is
SET FRONT = REAR = 0 ELSE SET REAR = REAR+1 Algorithm:POP *Best case – O(n log n) called recursion. A recursive function will call itself
[END OF IF] POP(STACK,TOP,ITEM) *Worst-case – O(n2) until a final call that does not require a call to itself
Step 3: SET QUEUE[REAR] = NUM 1: if TOP = 0 then print: UNDERFLOW and return *Average case – O(n log n) is made. It takes advantage of the system stack to
Step 4: Exit 2: set ITEM = STACK[TOP] Pros and Cons of QuickSort temporarily store the calling function’s return
Array operations 3: set TOP = TOP - 1 Quicksort is faster than other algorithms such as address and local variables.
Traversal 4: return bubble sort, selection sort, and insertion sort. It can There are two major cases in any recursive solution.
Insertion Write an algorithm to perform insertion operation be used to sort arrays of various sizes. On the other They are
Deletion in a queue? hand, quicksort is complicated and massively *Base case in which the problem is simple enough
Traversal Ans: Insertion :Algorithm recursive. to be solved directly without the need for any more
It prints all the array elements one after another. QINSERT( Queue, N, front, rear, item) .Hashing is a technique of mapping a large set of calls to the same function
Algorithm Step 1 : Check if the queue is full. data into tabular indices *Recursive case
LA is a linear array with lower bound LB and upper Step 2 : If the queue is full, produce overflow error The importance of Hashing:We know that the time *The problem at hand is initially subdivided into
bound UB. and exit. complexity of search algorithms depends upon the simpler sub-parts.
step 1: set k=LB [ rear = N ] number of elements in the list. linear *The function calls itself again, but this time with
step 2: Repeat steps 3 and 4 while k<=UB step 3: Step 3 : If the queue is not full, search and binary search algorithms have a time sub-parts of the problem obtained in the first step.
Apply visit to LA[k] (a) If rear=0 then set rear=front=1 otherwise complexity of O(n) and O(log n) respectively. *The final result is obtained by combining the
step 4: set k=k+1 (b) increment rear pointer to point the next empty Hashing is a search technique which is independent solutions of simpler sub-parts.
step 5: exit space. of the number of elements in the list. It uniquely A recursive function is said to be well-defined if it
Insertion [rear = rear + 1] identify a specific item from a group of similar has the following two properties:
It adds an element at given index. Step 4 : Add data element to the queue location, items. Therefore, hashing allows searching, *There must be a base criteria in which the function
Algorithm where the rear is pointing. updating and retrieval of data in a constant time, doesn’t call itself.
INSERT(LA,N,K,ITEM) [ Queue[rear] = item ] that is O(1). A hash table :-is a data structure that *Every time the function is called itself, it must be
LA is a linear array with N elements and K is a Step 5 : return success. stores data as key-value pairs. Each key is matched closer to the base criteria.
positive integer K<=N. This algorithm inserts an Deletion : Algorithm to a value in the hash table.
element ITEM into QDELETE( Queue, N, front, rear, item)
the Kth position in LA. Step 1 : Check if the queue is EMPTY. .
Step 1: set J=N Step 2 : If the queue is EMPTY, produce underflow Key and Value in Hash table
step 2: Repeat steps 3 and 4 while J>=K error and exit. *Keys are used to index the data.
step 3: set LA[J+1]=LA[J] [ front=0 ] *Value specifies the data associated with the keys.
step 4: set J=J-1 Step 3 : If the queue is not empty, then set item = Hash Function
step 5: set LA[K]=ITEM Queue[front] A hash function is a mathematical formula, used for .
step 6: set N=N+1 and perform (a) or(b) mapping keys into table indices. This process of
step 7: exit (a) If front=N, then set rear=front=0 otherwise mapping the keys to corresponding indices in a hash
Deletion (b) increment front pointer to point the next empty table is called hashing.
Operation of removing one of the elements from LA. space. Assume k is a key and h(x) is a hash function. In a
Algorithm [ front = front + 1] hash table, an element linked with key k will be
DELETE(LA,N,K,ITEM) Step 5 : return success stored at the index h(k)
LA is a linear array with N elements and K is a Characteristics of good hash function
positive integer ,K<=N. This algorithm deletes the CIRCULAR QUEUE *It should be very simple and quick to compute.
Kth element Circular Queue is a linear data structure in which The cost of execution must be low.
from LA. the operations are performed based on FIFO (First It should distribute the hash addresses as evenly as
step 1: set ITEM=LA[K] In First Out) principle and the last position is possible within L, so there are fewer collisions.
step 2: Repeat for J=K to N-1step 3: set LA[J]=LA[J+1] connected back to the first position to make a *The same hash value must be generated for a
step 4: set N=N-1 step 5: exit circle.It is also called 'Ring Buffer' given input value.