0% found this document useful (0 votes)
2 views62 pages

BASICS ds

The document provides an overview of data structures, including definitions of data, data types, and derived data types. It discusses the importance of choosing appropriate data structures for efficient data organization and manipulation, highlighting static and dynamic data structures along with their advantages and disadvantages. Additionally, it covers pointers, their usage, and examples of passing pointers to functions in programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views62 pages

BASICS ds

The document provides an overview of data structures, including definitions of data, data types, and derived data types. It discusses the importance of choosing appropriate data structures for efficient data organization and manipulation, highlighting static and dynamic data structures along with their advantages and disadvantages. Additionally, it covers pointers, their usage, and examples of passing pointers to functions in programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Data Structures

Data, Data Types and Derived Data Types


Data
Data is facts and statistics collected together for reference or analysis.
Computer data is processed to derive information or stored by a computer.

In computing, data is information that has been translated into a form that is efficient for movement or
processing.
Data type
A data type, in programming, is a classification that specifies which type of value a variable has and what type
of mathematical, relational, or logical operations can be applied to it without causing an error.
For example, int, char, float, and double

Derived Data Type


A derived data type is basically an aggregation of the fundamental data type. Void, Float, Integer, and Character
are fundamental data types. Structures, Unions, Arrays, and Pointers are the derived data types.
Data Structures

• Data structure is the mechanism of organizing and storing the data

properly and establishing the relationship between data stored.

Whenever required these data can be searched, retrieved, sorted, or

perform various other operation without much effort.


Data Structures
• Data Structure allows us to store data in planned manner so that it
can be retrieved and again placed in proper and planned way.

• Similarly, writing effective program requires proper knowledge and


usage of appropriate data structure.

• Knowledge of Data Structure is also useful in storing, updating and


retrieving data and variables conveniently.
Introduction to Data Structures Course
Program = Algorithm + Data Structure

Choosing a proper data structure


• Data Structure should be simple and easy to use.
• Data Structure selected should be able to reflect the relationships
that exists between data.
• Data Structure should allow to access, store, sort, search data in
convenient manner.
Types of Data Structures
Static data structures are those that have a fixed size and structure at compile
time, meaning that they cannot be modified or resized during runtime.
Examples of static data structures include arrays, structs, and enums.
Advantages: Fast and easy to access due to their constant memory address and
index
Disadvantages: Rigid and inflexible when it comes to adapting to changing data
or requirements. Additionally, they may allocate more memory than needed or
leave unused space, making them wasteful and inefficient.

Dynamic data structures, such as linked lists, stacks, queues, trees, graphs, and
hash tables, are those that have a variable size and structure at runtime.
Advantages: They can be modified or resized as needed. Therefor, offers flexibility
and adaptability to accommodate changing data or requirements, as well as
efficiency and optimal use of memory.
Additionally, they are powerful and versatile enough to support complex or
dynamic operations.
Disadvantages: Slow and complex access that may require traversing pointers or
hashing functions.
What is this Course About?
Clever ways to organize information in order to enable efficient
computation

• What do we mean by clever?

• What do we mean by efficient?


Choosing Data Structures (Queues and Binary Trees)
The jobs will be printed in the order
in which they are received.
Most network print servers maintain
such a print queue.
Choosing Data Structures
A binary tree is a good data structure
to use for searching sorted data.
The middle item from the list is
stored in the root node, with lesser
items to the left and greater items to
the right. Example, telephone
directory
Data Structures (List and Arrays)

A list is an ordered set of data. It is often used to store objects


that are to be processed sequentially.

An array is a collection of elements (values or variables), of


same memory size, each identified by at least one array index
or key.

A list is a set of items.

An array is a set of variables (a[0], a[1]…) that each store an


item.
Arrays and Lists
You can see the difference between arrays and lists
when you delete items.
Asymptotic Complexity
Our notion of efficiency:
How the running time of an algorithm scales with
the size of its input
• several ways to further refine:
• worst case
• average case
• Best case
More details on the running time of an algorithm as
its scales will be taught in Design and Analysis of
Algorithms
Graphics
Theory AI

Applications
Systems

Used Everywhere!
Mastery of this
material separates
you from:

• Perhaps the most important course in your CS curriculum!


• Guaranteed non-obsolescence!
Advantages of Data Structures
• Efficient storage of data in the storage device.
• Provides convenience while retrieving the data from storage device.
• Provides effective and efficient processing of small as well as large
amount of data.
• Proper data structure - help programmer - save lots of time or
processing time - operations such as storage, retrieval or processing
of data.
• Manipulation of large amount of data can be carried out easily with
the use of good data structure approach.
Advantages of Data Structures
• Most of the well organized data structures like Array, stack, queues,
graph, tree, linked list has well built and pre-planned approach for
operations like storage, addition, retrieval, manipulation, deletion,
etc.
• While using them, programmer can be completely rely on these data
structures.
• Data structure usage can simply encourage reusability in long run as
well.
Pointers

25 February 2024 16
Introduction
• A pointer is a variable that represents the location (rather than the
value) of a data item.
• They have a number of useful applications.
• Enables us to access a variable that is defined outside the function.
• Can be used to pass information back and forth between a function and its
reference point.
• More efficient in handling data tables.
• Reduces the length and complexity of a program.
• Sometimes also increases the execution speed.

25 February 2024 Data Structure 17


Basic Concept
• Within the computer memory, every stored data
item occupies one or more contiguous memory
cells.
• The number of memory cells required to store a data
item depends on its type (char, int, double, etc.).
• Whenever we declare a variable, the system
allocates memory location(s) to hold the value of
the variable.
• Since every byte in memory has a unique address, this
location will also have its own (unique) address.

25 February 2024 Data Structure 18


Contd.
• Consider the statement
int xyz = 50;
• This statement instructs the compiler to allocate a location for the integer
variable xyz, and put the value 50 in that location.
• Suppose that the address location chosen is 1380.

xyz ➔ variable
50 ➔ value
1380 ➔ address

25 February 2024 Data Structure 19


Contd.
• During execution of the program, the system
always associates the name xyz with the address
1380.
• The value 50 can be accessed by using either the name
xyz or the address 1380.
• Since memory addresses are simply numbers, they
can be assigned to some variables which can be
stored in memory.
• Such variables that hold memory addresses are called
pointers.
• Since a pointer is a variable, its value is also stored in
some memory location.

25 February 2024 Data Structure 20


Contd.
• Suppose we assign the address of xyz to a variable p.
• p is said to point to the variable xyz.

Variable Value Address


xyz 50 1380 p = &xyz;
p 1380 2545

2545 1380 1380 50


p xyz
25 February 2024 Data Structure 21
Accessing the Address of a Variable
• The address of a variable can be determined using
the ‘&’ operator.
• The operator ‘&’ immediately preceding a variable
returns the address of the variable.
• Example:
p = &xyz;
• The address of xyz (1380) is assigned to p.
• The ‘&’ operator can be used only with a simple
variable or an array element.
&distance
&x[0]
&x[i-2]
25 February 2024 Data Structure 22
Contd.
• Following usages are illegal:
&235
• Pointing at constant.

int arr[20];
:
&arr;
• Pointing at array name.

&(a+b)
• Pointing at expression.

25 February 2024 Data Structure 23


Example
#include <stdio.h>
main()
{
int a;
float b, c;
double d;
char ch;

a = 10; b = 2.5; c = 12.36; d = 12345.66; ch = ‘A’;


printf (“%d is stored in location %u \n”, a, &a) ;
printf (“%f is stored in location %u \n”, b, &b) ;
printf (“%f is stored in location %u \n”, c, &c) ;
printf (“%ld is stored in location %u \n”, d, &d) ;
printf (“%c is stored in location %u \n”, ch, &ch) ;
}

25 February 2024 Data Structure 24


Output:

10 is stored in location 3221224908 a


2.500000 is stored in location 3221224904 b
12.360000 is stored in location 3221224900
12345.660000 is stored in location 3221224892 c
A is stored in location 3221224891 d
ch

Incidentally variables a,b,c,d and ch are allocated


to contiguous memory locations.

25 February 2024 Data Structure 25


Pointer Declarations
• Pointer variables must be declared before we
use them.
• General form:
data_type *pointer_name;
Three things are specified in the above
declaration:
1. The asterisk (*) tells that the variable pointer_name is a
pointer variable.
2. pointer_name needs a memory location.
3. pointer_name points to a variable of type data_type.

25 February 2024 Data Structure 26


Contd.
• Example:
int *count;
float *speed;
• Once a pointer variable has been declared, it can be made to point to
a variable using an assignment statement like:
int *p, xyz;
:
p = &xyz;
• This is called pointer initialization.

25 February 2024 Data Structure 27


Things to Remember
• Pointer variables must always point to a data item of the same type.
float x;
int *p;
: ➔ will result in erroneous output
p = &x;
• Assigning an absolute address to a pointer variable is prohibited.
int *count;
:
count = 1268;

25 February 2024 Data Structure 28


Accessing a Variable Through its Pointer
• Once a pointer has been assigned the address of a variable, the value
of the variable can be accessed using the indirection operator (*).
int a, b;
int *p;
:
p = &a;
Equivalent to b=a
b = *p;

25 February 2024 Data Structure 29


Example 1
#include <stdio.h>
main()
{
int a, b;
int c = 5;
int *p; Equivalent
a = 4 * (c + 5) ;

p = &c;
b = 4 * (*p + 5) ;
printf (“a=%d b=%d \n”, a, b) ;
}

25 February 2024 Programming and Data Structure 30


Pointer Expressions
• Like other variables, pointer variables can be used in expressions.
• If p1 and p2 are two pointers, the following statements are valid:
sum = *p1 + *p2 ;
prod = *p1 * *p2 ;
prod = (*p1) * (*p2) ;
*p1 = *p1 + 2;
x = *p1 / *p2 + 5 ;

25 February 2024 Programming and Data Structure 33


Contd.
• What are allowed in C?
• Add an integer to a pointer.
• Subtract an integer from a pointer.
• Subtract one pointer from another (related).
• If p1 and p2 are both pointers to the same array, them p2–
p1 gives the number of elements between p1 and p2.
• What are not allowed?
• Add two pointers.
p1 = p1 + p2 ;
• Multiply / divide a pointer in an expression.
p1 = p2 / 5 ;
p1 = p1 – p2 * 10 ;

25 February 2024 Data Structure 34


Scale Factor
• We have seen that an integer value can be added
to or subtracted from a pointer variable.
int *p1, *p2 ;
int i, j;
:
p1 = p1 + 1 ;
p2 = p1 + j ;
p2++ ;
p2 = p2 – (i + j) ;
• In reality, it is not the integer value which is
added/subtracted, but rather the scale factor
times the value.

25 February 2024 Programming and Data Structure 35


Contd.
Data Type Scale Factor
char 1
int 4
float 4
double 8

• If p1 is an integer pointer, then


p1++
will increment the value of p1 by 4.

25 February 2024 Data Structure 36


Example:
Returns no. to findrequired
of bytes the for
scale factors
data type representation
#include <stdio.h>
main()
{
printf (“Number of bytes occupied by int is %d \n”, sizeof(int));
printf (“Number of bytes occupied by float is %d \n”, sizeof(float));
printf (“Number of bytes occupied by double is %d \n”, sizeof(double));
printf (“Number of bytes occupied by char is %d \n”, sizeof(char));
}

Output:

Number of bytes occupied by int is 4


Number of bytes occupied by float is 4
Number of bytes occupied by double is 8
Number of bytes occupied by char is 1

25 February 2024 Data Structure 37


Passing Pointers to a Function
• Pointers are often passed to a function as
arguments.
• Allows data items within the calling program to be
accessed by the function, altered, and then returned to
the calling program in altered form.
• Called call-by-reference (or by address or by location).
• Normally, arguments are passed to a function by
value.
• The data items are copied to the function.
• Changes are not reflected in the calling program.

25 February 2024 Data Structure 38


Example: passing arguments by value
#include <stdio.h>
main()
{ a and b
int a, b;
a = 5 ; b = 20 ;
do not
swap (a, b) ; swap
printf (“\n a = %d, b = %d”, a, b); Output
}
a = 5, b = 20
void swap (int x, int y)
{
int t ;
t=x;
x=y;
y=t;
} x and y swap

25 February 2024 Data Structure 39


Example: passing arguments by
reference
#include <stdio.h>
main()
{ *(&a) and *(&b)
int a, b;
a = 5 ; b = 20 ; swap
swap (&a, &b) ;
printf (“\n a = %d, b = %d”, a, b); Output
}
a = 20, b = 5
void swap (int *x, int *y)
{
int t ;
t = *x ;
*x = *y ;
*y = t ;
*x and *y
} swap

25 February 2024 Data Structure 40


scanf Revisited
int x, y ;
printf (“%d %d %d”, x, y, x+y) ;

• What about scanf ?


NO
scanf (“%d %d %d”, x, y, x+y) ;

YES
scanf (“%d %d”, &x, &y) ;

25 February 2024 Programming and Data Structure 41


Pointers and Arrays
• When an array is declared,
• The compiler allocates a base address and sufficient amount of storage to
contain all the elements of the array in contiguous memory locations.
• The base address is the location of the first element (index 0) of the array.
• The compiler also defines the array name as a constant pointer to the first
element.

25 February 2024 Data Structure 46


Example
• Consider the declaration:
int x[5] = {1, 2, 3, 4, 5} ;
• Suppose that the base address of x is 2500, and each
integer requires 4 bytes.
Element Value Address
x[0] 1 2500
x[1] 2 2504
x[2] 3 2508
x[3] 4 2512
x[4] 5 2516

25 February 2024 Data Structure 47


Contd.
x  &x[0]  2500 ;

• p = x; and p = &x[0]; are equivalent.


• We can access successive values of x by using p++ or p- - to move from one
element to another.
• Relationship between p and x:
p = &x[0] = 2500
p+1 = &x[1] = 2504 *(p+i) gives the
p+2 = &x[2] = 2508
value of x[i]
p+3 = &x[3] = 2512
p+4 = &x[4] = 2516

25 February 2024 Data Structure 48


Accessing Arrays using Pointers
(arrayname + index) = address of the specified index of array.
*(arrayname + index) = value at the specified array index.

Accessing value Accessing value Value


using pointer using array
*(p+0) a[0] 1

*(p+1) a[1] 2

*(p+2) a[2] 3

*(p+3) a[3] 4

*(p+4) a[4] 5
Example: function to find average
int *array
#include <stdio.h> float avg (int array[ ],int size)
main() {
{ int *p, i , sum = 0;
int x[100], k, n ;
p = array ;
scanf (“%d”, &n) ; p[i]
for (i=0; i<size; i++)
for (k=0; k<n; k++) sum = sum + *(p+i);
scanf (“%d”, &x[k]) ;
return ((float) sum / size);
printf (“\nAverage is %f”, }
avg (x, n));
}

25 February 2024 Data Structure 50


NULL Pointer
• The NULL Pointer points to no objects or functions.
• By assigning null value.
• A null pointer always contains value 0.

#include <stdio.h>
int main()
{
int *p = NULL; //null pointer
printf(“p points to address: %x\n”,p);
return 0;
}
NULL Pointer
• What is a void pointer.
• What is a Wild pointer.
• What is a Dangling pointer.
Accessing Arrays using Pointers
#include<stdio.h>
int main(){
int a[5], i;
int *p;
p = a; //’p’ is pointing to the base address of ‘a’
printf(“\nEnter the values for the array…”);
for(i=0 ; i<5 ; i++)
scanf(“%d”, (p+i));
printf(“\nThe contents of the array are: “);
for(i=0; i<5; i++)
printf(“%d “, *(p+i));
return 0;
}
Dynamic Memory Allocation in C
• Used when the amount of memory to be allocated is not known.

• Functions – malloc, calloc, realloc and free.

• Header file – stdlib.h


Dynamic Memory Allocation – Using malloc

ptr = (cast-type*) malloc(byte-size) ;

Eg:
int *ptr1, *ptr2, *ptr3, n=4;
ptr1 = (int*)malloc(sizeof(int)); //allocates 4 bytes, to store one int.
ptr2 = (int*)malloc(5*sizeof(int)); //allocates 20 bytes, to store 5 int.
ptr3 = (int*)malloc(n*sizeof(int)); //allocates 16 bytes, to store 4 int
Example Code - malloc
#include<stdio.h>
#include<stdlib.h>
int main(){
int *ptr1;
ptr1 = (int*)malloc(sizeof(int));
printf(“Enter a value: “);
scanf(“%d”, ptr1);
printf(“The value entered is: %d”, *ptr1);
return 0;
}
Example Code - malloc
#include<stdio.h>
#include<stdlib.h>
int main(){
int *ptr1, n=5, i;
ptr1 = (int*)malloc(n*sizeof(int));
printf(“Enter 5 values…. : “);
for(i=0; i<n ; i++)
scanf(“%d”, (ptr1+i));
printf(“\nThe contents are: \n“);
for(i=0 ; i<n ; i++)
printf(“%d “, *(ptr1+i));
free(ptr1);
return 0;
}
Dynamic Memory Allocation – Using calloc

Eg:
int *ptr1, *ptr2, *ptr3, n=4;
ptr1 = (int*)calloc(1, sizeof(int)); //allocates 4 bytes, to store one int.
ptr2 = (int*)calloc(5, sizeof(int)); //allocates 20 bytes, to store 5 int.
ptr3 = (int*)calloc(n, sizeof(int)); //allocates 16 bytes, to store 4 int
Example Code - calloc
#include<stdio.h>
#include<stdlib.h>
int main(){
int *ptr1, n=5, i;
ptr1 = (int*)calloc(n, sizeof(int));
printf(“Enter 5 values…. : “);
for(i=0; i<n ; i++)
scanf(“%d”, (ptr1+i));
printf(“\nThe contents are: \n“);
for(i=0 ; i<n ; i++)
printf(“%d “, *(ptr1+i));
free(ptr1);
return 0;
}
2D Arrays in C using pointer to pointer
#include<stdio.h>
#include<stdlib.h>
int main(){
int **arr, row=3, col=5, i, j, count=0;
arr = (int**)malloc(row*sizeof(int*));
for(i=0 ; i<row ; i++)
*(arr+i) = (int*)malloc(col*sizeof(int));
for(i=0 ; i<row ; i++)
for(j=0 ; j<col ; j++)
*(*(arr+i)+j) = ++count;
for(i=0 ; i<row ; i++){
for(j=0 ; j<col ; j++)
printf(“%d “, *(*(arr+i)+j));
printf(“\n”);
}
return 0;
}
2D Arrays in C using pointer to pointer – Dynamic Memory
Allocation
Int **mArray, rows, cols;

mArray = (int **)malloc(rows*sizeof(int *));

for( i=0 ; i<rows ; i++)


*(mArray+i) = (int*)malloc(cols*sizeof(int));

for(i=0 ; i<rows ; i++)


for(j=0 ; i<cols ; i++)
scanf(“%d”, (*(mArray+i)+j));

for(i=0 ; i<rows ; i++)


for(j=0 ; j<cols ; j++)
printf(“%d”, *(*(mArray+i)+j));
Abstract Data Type (ADT)
• Datatype – Collection of Objects and set of operations acts on it.

• ADT – Organized – Specification - Object - Operation – Separated –


Representation of Objects and Implementation of Operation.

• How specification differs from implementation?

• Specification – function description, function name, types of


arguments and type of result.

• ADT – Implementation Independent.


Abstract Data Type (ADT) - Functions
• Creator / Constructor – Create a new instance of the designated type.

• Transformers – Create an instance by using other instances.

• Observers / Reporters - Provide info about an instance of the type.


ADT – Natural Number
ADT structure Natural_Number is
objects: an ordered subrange of the integers starting at zero and ending
at the maximum integer (INT_MAX) on the computer
functions:
for all x, y  Nat_Number; TRUE, FALSE  Boolean
and where +, -, <, and == are the usual integer operations.
Nat_No Zero ( ) ::= 0
Boolean Is_Zero(x) ::= if (x) return FALSE
else return TRUE
Nat_No Add(x, y) ::= if ((x+y) <= INT_MAX) return x+y
else return INT_MAX
Boolean Equal(x,y) ::= if (x== y) return TRUE
else return FALSE
Nat_No Successor(x) ::= if (x == INT_MAX) return x
else return x+1
Nat_No Subtract(x,y) ::= if (x<y) return 0
else return x-y
end Natural_Number
Home Work
1. Add the following operations on NaturalNumber ADT: Predecessor,
IsGreater, Multiply and Divide.

2. Create an ADT set, include the operations: Create, Insert, Remove,


IsIn, Union, Intersection, Difference.

3. Create an ADT Bag (similar to set, can have duplicates). Include the
operations Create, Insert, Remove, IsIn.

4. Create an ADT Boolean. Include the operations And, Or, Not and
Xor.
Array ADT
• Array: a set of index and value

• data structure
• For each index, there is a value associated with that index.

• representation (possible)
• implemented by using consecutive memory.
Array ADT
• Structure Array is
objects: A set of pairs <index, value>
• Functions:
for all A  Array, i  index, x  item, j, size  integer
Array Create(j, list) ::= return an array of j dimensions.

Item Retrieve(A, i) ::= if (i  index) return the item associated with


index value i in array A
else return error

Array Store(A, i, x) ::= if (i in index)


return an array that is identical to array
A except the new pair <i, x> has been
inserted else return error
end array

You might also like