Sie sind auf Seite 1von 53

Introduction to Data Structures

SONAL PANDEY
NITTTR Chd.
Data Structure:

The logical or mathematical model of a particular organization


of data is called a data structure.

Data structure mainly specifies the following four things:

1. Organization of data

2. Accessing Methods

3. Degree of associativity

4. Processing alternatives for information


Types of Data Structure :

Data structure are normally divided into two categories

1. Primitive Data structure

2. Non-Primitive Data structure

Chart of Data structure is given on next slide.


Data Structure

Primitive DS Non-Primitive DS

int float pointer char Array Lists Files

Non Linear
Linear list
list

Stack Queues Graphs Trees


Different between them

A primitive data structure is generally a basic structure that is


usually built into the language, such as an integer, a float.

A non-primitive data structure is built out of primitive data


structures linked together in meaningful ways, such as a or a
linked-list, binary search tree, AVL Tree, graph etc.
Types of Data Structure


Array: is commonly used in computer
programming to mean a contiguous block of
memory locations, where each memory location
stores one fixed-length data item.
e.g. Array of Integers int a[10], Array of
Character char b[10]
Array of Integers Array of Character
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
5 6 4 3 7 8 9 2 1 2 5 6 4 3 7 8 9 2 1 2

zaid shabbir
Types of Data Structure ...
Stack: A stack is a data 1(TOP)
structure in which items can 4
be inserted only from one 8

end and get items back from


the same end. There , the last
item inserted into stack, is
the the first item to be taken
out from the stack. In short
its also called Last in First
out [LIFO].
1) PUSH 2) POP
zaid shabbir
Types of Data Structure ...

Queue is two ended data


structure in which item can
ended data
be insertedwhich
structure from one end
and
items taken
can out from the
other end. Therefore , the
first item inserted into
queue is the first item to
be taken out from the
queue. This property is
called First in First out
[FIFO]. zaid shabbir
1) REAR (ADD)
2) FRONT (REMOVE)

FRONT REAR

1 2 4 6 5 8
Types of Data Structure ...

Linked List: Could alternately used to store items. In


linked list space to store items is created as is needed
and destroyed when space no longer required to store
items. Hence linked list is a dynamic data structure
space acquire only when need.
Doubly link list
Types of Data Structure ...

Tree: is a non-linear Binary Tree: A binary


data structure which is tree is a tree such that
mainly used to represent every node has at most 2
data containing a child and each node is
hierarchical relationship labeled as either left of
between elements. right child.

zaid shabbir
Types of Data Structure ...
Types of Data Structure ...

Graph: It is a set of items connected by edges.


Each item is called a vertex or node. Trees are
just like a special kinds of graphs. Graphs are
usually represented by G = (V, E), where V is the
set vertices and E is the set of Edges.

zaid shabbir
Types of Data Structure ...

Undirected Graph: A
graph whose edges are
unordered pair of vertices.
That is each edge connects
two vertices. In an
undirected graph, direction
is not important, if the path
is available, it can be
traversed in any direction.
zaid shabbir
Types of Data Structure ...

Directed Graph: In directed graph a directional


edge connect two node/vertex. If there is one edge
from one vertex to other then only this path can
be followed.
Types of Data Structure ...

Weighted Graph: A graph having a weight, or


number associated with each edge

zaid shabbir
Data Structure Operations :

1. Traversing

2. Searching

3. Inserting

4. Deleting

5. Sorting

6. Merging
The basic operations that are performed on data structures are as
follows:

Insertion: Insertion means addition of a new data element in a


data structure.

Deletion:  Deletion means removal of a data element from a data


structure if it is found.

Searching: Searching involves searching for the specified data


element in a data structure.
Traversal: Traversal of a data structure means processing all the
data elements present in it.

Sorting: Arranging data elements of a data structure in a


specified order is called sorting.

Merging: Combining elements of two similar data structures to


form a new data structure of the same type, is called merging.
Data Type

data type is a data storage format that can contain a specific type
or range of values.

When computer programs store data in variables, each variable


must be assigned a specific data type.

Some common data types include integers, floating point numbers


, characters, strings, and arrays.
TYPES OF DATA TYPES

Data
Types

Primary Derived

Int Char Array Pointer

Float Type
Void

User Define

Enumerated Typedef
3
Structure
INT DATA TYPE

 Integersare whole numbers with a range of values, range of values are


machine dependent. Generally an integer occupies 2 bytes memory
space and its value range limited to -32768 to +32767.

4
CHAR DATA TYPE

 Charactertype variable can hold a single character. As there are singed and
unsigned int (either short or long), in the same way there are signed and
unsigned chars; both occupy 1 byte each, but having different ranges.
Unsigned characters have values between 0 and 255; signed characters
have values from –128 to 127.

5
FLOAT DATA TYPE

 The float data type is used to store fractional numbers (real numbers) with
6 digits of precision. Floating point numbers are denoted by the keyword
float. When the accuracy of the floating point number is insufficient, we
can use the double to define the number. The double is same as float but
with longer precision and takes double space (8 bytes) than float. To
extend the precision further we can use long double which occupies 10
bytes of memory space.

6
TYPE VOID DATA TYPE

 The void type has no values therefore we cannot declare it as variable as we


did in case of integer and float. The void data type is usually used with
function to specify its type. Like in our first C program we declared "main
()" as void type because it does not return any value.

7
DERIVED DATA TYPE

 Array: An array in C language is a collection of similar data-type, means


an array can hold value of a particular data type for which it has been
declared. Arrays can be created from any of the C data-types int.
 Pointer: CPointer is a special variable that can be used to store address
of another variable.

8
ENUMERATED DATA TYPE (ENUM)
Enumerated data type is a user defined data type having finite set of enumeration
constants. The keyword 'enum' is used to create enumerated data type.
Enumeration data type consists of named integer constants as a list.
It start with
Enumerated data0type(zero) by default
is a user and type
defined data valuehaving
is incremented
finite setby 1 for the
ofsequential
enumeration constants.
identifiers in theThe
list. keyword 'enum' is used to create
enumerated data type. Enumeration data type consists of named
integer
It start constants
with 0 as(zero)
a list. by default and value is
incremented by 1 for the
Syntax:
Enum [data_type] {const1, const2… constn}; Enum example
in C:
enum month { Jan, Feb, Mar }; or /* Jan, Feb and Mar variables will be assigned
to 0, 1 and 2 respectively by default */
enum month { Jan = 1, Feb, Mar }; /* Feb and Mar variables will be assigned to
2 and 3 respectively by default */
enum month { Jan = 20, Feb, Mar }; /* Jan is assigned to 20. Feb and Mar
9
variables will be assigned to 21 and 22 respectively by default */
Example:
#include <stdio.h>  
 enum weekdays{Sunday=1, Monday, Tuesday, Wednesday, Thurs
day, Friday, Saturday};  
 int main()  
{  
 enum weekdays w;  // variable declaration of weekdays type  
 w=Monday;  // assigning value of Monday to w.  
 printf("The value of w is %d",w);  
    return 0;  

 
Ouput:
The Value of w is 2
TYPEDEF DATA TYPE

It is used to create new data type. But it is commonly used to


change existing data type with another name.

Syntax:
typedef [data_type] new_data_type;
Example:
typedef int integer; integer roll_no;

1
0
STRUCTURE DATA TYPE
C Structure is a collection of different data types which are grouped

together and each element in a C structure is called member.


C Structure is a collection of different data types
which are grouped
Example:
struct student
{ int roll_no;
char name[20];
char city[20];
} 1
1
#include <stdio.h>
/* Created a structure here. The name of the structure is * StudentData. */
struct StudentData
{ Output:
char *stu_name; Student Name is: Steve
int stu_id; Student Id is: 1234
int stu_age; Student Age is: 30
};
int main()
{
/* student is the variable of structure StudentData*/
struct StudentData student;
/*Assigning the values of each struct member here*/
student.stu_name = "Steve";
student.stu_id = 1234;
student.stu_age = 30;
/* Displaying the values of struct members */
printf("Student Name is: %s", student.stu_name);
printf("\nStudent Id is: %d", student.stu_id);
printf("\nStudent Age is: %d", student.stu_age);
return 0;
}
Constant
As the name suggests the name constants is given to such variables
or values in C programming language which cannot be modified
once they are defined.
They are fixed values in a program. There can be any types of
constants like integer, float, octal, hexadecimal, character constants
etc. Every constant has some range. 

1. Using #define preprocessor directive: This directive is used to


declare an alias name for existing variable or any value. We can use
this to declare a constant as shown below:
#define identifierName value
identifierName: It is the name given to constant.
value: This refers to any value assigned to identifierName.

  
2. using a const keyword: Using const keyword to define
constants is as simple as defining variables, the difference is you
will have to precede the definition with a const keyword.

Const int var=5;


// Constants
#define val 10
#define floatVal 4.5 Integer Constant: 10
#define charVal 'G' Floating point Constant: 4.500000
   Character Constant: G

// Driver code
int main()
{
    printf("Integer Constant: %d\n", val);
    printf("Floating point Constant: %f\n", floatVal);
    printf("Character Constant: %c\n", charVal);
  
    return 0;
}
Variable
A variable in simple terms is a storage place which has some
memory allocated to it. Basically, a variable used to store some
form of data.
In programming, a variable is a container (storage area) to hold
data.
To indicate the storage area, each variable should be given a unique
name (identifier). Variable names are just the symbolic
representation of a memory location. For example:

int playerScore = 95;


Here, playerScore is a variable of int type. Here, the variable is
assigned an integer value 95.
Example:

#include<stdio.h>
void main()
{
/* c program to print value of a variable */
int age = 33;
printf("I am %d years old.\n", age); }

Output:
I am 33 years old.
Pointers :
A pointer is an entity that refers to the memory addresses. It
specifies the number that gives a location in a memory. Each
pointer variable can point to specific data type such as int, char,
float etc.
Pointers are used for…….
1. Accessing array elements.

2. Passing arguments to function by address when modification


of formal arguments are to be reflected on actual arguments.
3. Passing arrays and string to functions.

4. Creating data structure like Link List,Trees,Graphs etc.

5. Allocate a memory dynamically.


Pointer Defination
Syntax :
Datatype * ptrvar,………….
e.g
int * ptr;
char
*msg=“Hello”; Int
marks;
Ptr=&marks;
#include <stdio.h>
int main ()
{
int var = 20; /* actual variable declaration */
int *ip; /* pointer variable declaration */
ip = &var; /* store address of var in pointer variable*/
printf("Address of var variable: %x\n", &var );
/* address stored in pointer variable */
printf("Address stored in ip variable: %x\n", ip );
/* access the value using the pointer */
printf("Value of *ip variable: %d\n", *ip );
return 0;
}
Output:
Address of var variable: bffd8b3c
Address stored in ip variable: bffd8b3c
Value of *ip variable: 20
The Top-Down Approach
• In the top-down approach, a complex algorithm is broken down
into smaller fragments, better known as ‘modules.’
• These modules are then further broken down into more smaller
fragments until they can no longer be fragmented.
• This process is called ‘modularization.’ However, during the
modularization process, you must always maintain the integrity
and originality of the algorithm.
• By breaking a bigger problem into smaller fragments, the top-
down approach minimizes the complications usually incurred
while designing algorithms.
• Furthermore, in this approach, each function in a code is unique
and works independently of other functions.
• The top-down approach is heavily used in the C programming
language.
The Bottom-Up Approach

• Contrary to the top-down approach, the bottom-up approach


focuses on designing an algorithm by beginning at the very
basic level and building up as it goes.
• In this approach, the modules are designed individually and are
then integrated together to form a complete algorithmic design.
• So, in this method, each and every module is built and tested at
an individual level (unit testing) prior to integrating them to
build a concrete solution.
• The unit testing is performed by leveraging specific low-level
functions.
Differences Between The Top-Down And The
Bottom-Up Approaches
Top - Down Bottom - up
While the top-down approach The bottom-up approach first
focuses on breaking down a big focuses on solving the smaller
problem into smaller and problems at the fundamental level
understandable chunks and then integrating them into a
whole and complete solution.

The top-down approach is The bottom-up approach is


primarily used by structured preferred by OOP languages such
programming languages such as C, as C++, C#, Python, Java, and Perl.
COBOL, Fortran. 
Top - Down Bottom - up

In the top-down approach, each However, the bottom-up approach


module and submodule are relies on data encapsulation and
processed separately, and hence, data-hiding, thereby, minimizing
they might contain redundant redundancy.
information.
The top-down approach doesn’t Whereas, in the bottom-up
require the modules to have a well- approach, the modules must have
established line of communication a certain degree of interaction and
among them communication among them.

While the top-down approach can The bottom-up approach is


be used in module documentation, primarily used in testing.
debugging, and code
implementation
Structured Programming Approach
• Structured Programming Approach, as the word suggests, can
be defined as a programming approach in which the program is
made as a single structure.
• It means that the code will execute the instruction by instruction
one after the other.
• It doesn’t support the possibility of jumping from one instruction
to some other with the help of any statement like GOTO, etc.
• Therefore, the instructions in this approach will be executed in a
serial and structured manner.
• The languages that support Structured programming approach
are:
C
C++
Java
C#
Advantages of Structured Programming
Approach:

• Easier to read and understand


• User Friendly
• Easier to Maintain
• Mainly problem based instead of being machine based
• Development is easier as it requires less effort and time
• Easier to Debug
• Machine-Independent, mostly.
Disadvantages of Structured Programming
Approach:
• Since it is Machine-Independent, So it takes time to convert into
machine code.
• The converted machine code is not the same as for assembly
language.
• The program depends upon changeable factors like data-types.
Therefore it needs to be updated with the need on the go.
• Usually the development in this approach takes longer time as it
is language-dependent. Whereas in the case of assembly
language, the development takes lesser time as it is fixed for the
machine.
Thank you..

Das könnte Ihnen auch gefallen