Data structure
Data Structure is a way of collecting and
organizing data in such a way that we can
perform operations on these data in an effective
way. They are the building blocks of a program.
CLASSIFICATION OF DATA
STRUCTURES
• Primitive and
• Non-primitive Data Structures
Primitive data structures are the fundamental data types which are
supported by a programming Language.(integer, real, character,
and boolean)
Non-primitive data structures are those data structures which are
created using primitive data structures.(linked lists, stacks, trees,
and graphs)
Non-primitive data structures can further be classified
into two categories:
linear and non-linear data structures.
• If the elements of a data structure are stored in a
linear or sequential order, then it is a linear data
structure.(arrays, linked lists, stacks, and queues)
• if the elements of a data structure are not stored in a
sequential order, then it is a non-linear data
structure.(trees and graphs)
Arrays
• An array is a collection of similar data elements.
• These data elements have the same data type.
• elements of the array are stored in consecutive memory locations
and are referenced by an index (also known as the subscript).
• In C, arrays are declared using the following syntax:
• type name[size];
• For example, int marks[10];
Linked Lists
• A linked list is a very flexible, dynamic data structure in which
elements (called nodes) form a sequential list.
• In a linked list, each node is allocated space as it is added to the list.
Every node in the list points to the next node in the list.
• in a linked list, every node contains the following two types of data:
– The value of the node or any other data that corresponds to that
node
– A pointer or link to the next node in the list
• The last node in the list contains a NULL pointer to indicate that it is
the end or tail of the list.
Linked Lists
• Advantage: Easier to insert or delete data elements
• Disadvantage: Slow search operation and requires more memory
space
• Since the memory for a node is dynamically allocated when it is
added to the list, the total number of nodes that may be added to a
list is limited only by the amount of memory available.
Stacks
• A stack is a linear data structure in which insertion and deletion of
elements are done at only one end, which is known as the top of the
stack.
• LIFO
• can be implemented using arrays or linked lists.
• Every stack has a variable top associated with it.
• It is this position from where the element will be added or deleted
• variable MAX, is used to store the maximum number of elements
that the stack can store.
Stacks
• If top = NULL, then it indicates that the stack is empty
• if top = MAX–1, then the stack is full
• A stack supports three basic operations:
– push, pop, and peek
• Before inserting/deleting check overflow/underflow conditions
Queues
• A queue is FIFO data structure
• The elements in a queue are added at one end called the rear
and removed from the other end called the front
• queues can be implemented by using either arrays or linked
lists.
Queue after insertion of a new element
Queue after deletion of an element
Trees
• is a non-linear data structure which consists of a collection of
nodes arranged in a hierarchical order.
• One of the nodes is designated as the root node and the
remaining nodes can be partitioned into disjoint sets such that
each set is a sub-tree of the root.
• The simplest form of a tree is a binary tree.
Advantage: Provides quick search, insert operations
Disadvantage: Complicated deletion algorithm
Graphs
• is a non-linear data structure which is a collection of vertices (also called nodes)
and edges that connect these vertices.
• When two nodes are connected via an edge, the two nodes are known as
neighbours.
• instead of parent-to-child relationship between tree nodes, any kind of complex
relationships between the nodes can exist.
• In a tree structure, nodes can have any number of children but only one parent, a
graph on the other hand relaxes all such kinds of restrictions.
• Advantage: Best models real-world situations
• Disadvantage: Some algorithms are slow and very complex
OPERATIONS ON DATA STRUCTURES
• Traversing-to access each data item exactly once
so that it can be processed
• Searching-It is used to find the location of one or
more data items that satisfy the given constraint.
• Inserting
• Deleting
• Sorting
• Merging