Module 1
Module 1
DATA STRUCTURES
BASIC TERMINOLOGY
DATA: Values or set of values
DATA ITEM:
• Single unit of values
• That are divided into sub items called
GROUP items
• Collection of data are frequently organized
into hierarchy of fields ,record and files
Entity ,Attribute and entity set
• Entity is something that has certain properties
(attributes) which may be assigned values.
Ex: Employee entity
name age emp_id attributes
Rohit 34 e567
• Entities with similar attributes form an entity
set
Data Structures
• Logical or mathematical model of a particular
organization of data
• The choice of a particular model depends on 2
consideration
1. Rich enough in structure to mirror the actual
relationships of data in the real world
2. Simple enough that one can effectively
process the data when necessary
Classification of Data Structure
1.Primitive Data Structures: These are basic
structures that directly operate on machine-
level instructions.
Examples: Integer, Float, Char, Pointer
2. Non-Primitive Data Structures: These are
more complex structures built using primitive
data types
Arrays
• Linear array is a list of finite number n similar
data elements referenced respectively by a
set of n consecutive numbers.
STUDENT,SALES
Linked List
• A linked list is a linear data structure that
includes a series of connected nodes.
• each node stores the data and the address of
the next node.
• STACK: A Stack, also called a last-in-first-out ( LIFO )
system. It is a linear list in which insertions and
deletions cam take place only at one end called the top.
• QUEUE : A queue, also called a first-in-first out
(FIFO)system, is a linear list in which deletions
can take place only at one end of the list. The
“front” of the list, and insertions can take
place only at the other end of the list, the
“rear” of the tag.
TREES
• Reflects the hierarchical relationship between
various elements
Ex:Employee(e_id,name,address,age,sal)
• GRAPH : Data sometimes contain a
relationship between pairs of elements which
is not necessary hierarchical in nature.
DATA STRUCTURE OPERATIONS
• TRAVERSING : Accessing each record exactly once so
that certain items in the record may be processed.
• .
malloc() Allocates a single block
of requested memory.
calloc() Allocates multiple
blocks of requested
memory
realloc() Reallocates the memory
occupied by malloc() or
calloc() functions
free() Frees the dynamically
allocated memory
malloc()
• used to dynamically allocate a single large block of memory
with the specified size.
• It returns a pointer of type void which can be cast into a
pointer of any form
Syntax :
ptr = (cast-type*) malloc(byte-size)
• doesn't initialize memory at execution time, so it has garbage
value initially.
• It returns NULL if memory is not sufficient.
• ptr holds the address of the first byte in the allocated memory
Example:
ptr = (int*) malloc(3 * sizeof(int));
calloc()
struct structure_name
{
data_type member1;
data_type member2;
.
.
data_type memeberN;
};
Example:
struct person
{
char name[20];
int age;
float salary
};
Declaring structure variable
struct structure_name
{
data_type member1;
data_type member2;
.
.
data_type memeberN;
};
struct structure_name var1,var2,…….,varN;
Accessing members of the structure
a) Tree
b) Array
c) Linked list
d) Queue
Ans: a
Queue data structure works on the
principle of ____________
Ans:c
If malloc() and calloc() are not type casted, the default return
type is _____
(a)void*
(b)void**
(c)int*
(d)char*
Ans: void*
Which of the following functions allocates multiple blocks of memory, each block of
the same size?
(a)malloc()
(b)realloc()
(c)calloc()
(d)free()
Ans: c
What is functionality of realloc() function
then:
• A(x) + B(x) = Σ (ai + bi) xi
Polynomial Representation
}
starta++;
startb++;
break;
case -1:
attach(terms[startb].coef, terms[startb].expon);
startb++;
break;
case 1:
attach(terms[starta].coef, terms[starta].expon);
starta++;
break;
}
}
for (; starta <= finisha; starta++)
{
attach(terms[starta].coef, terms[starta].expon);
}
for (; startb <= finishb; startb++)
{
attach(terms[startb].coef, terms[startb].expon);
}
*finishd = avail - 1;
}
void attach(float coefficient, int exponent)
{
if (avail >=MAX_TERMS)
{
printf( "Too many terms in the polynomial\n");
}
terms[avail].coef = coefficient;
terms[avail++].expon = exponent;
}
Sparse Matrix
• a matrix that contains a few non-zero
elements
Sparse matrix representation
• we can characterize uniquely any element
within a matrix by using the triple
<row,col,value>
• An array of triples is used to represent a
sparse matrix
• Sparsematrix create(maxrows,maxcol)::=
typdef struct
{
int row,
int col,
int value
}term;
term a[max_terms];
Transposing a Matrix
• each element in a[i][j] in the original matrix
becomes element b[j][i] the transpose matrix
• algorithm for transposing a matrix:
Algorithm:
for each row i
take element <i,j,value>and store it as element
<j,i,value>of the transpose;
• if we process the original matrix by the row
indices we will not know exactly where to place
element in the transpose matrix until we have
processed all the elements that precede it
Algorithm
for all elements in column j
take element <i,j,value>and store it as element
<j,i,value> of the transpose
void transpose(term a[], term b[]) {
int n, i, j, currentb;
Output :
Hello, world
2.char *strncat(char *dest, char *src, int
n):concatenate dest and n characters from src; return
result in dest
7 4 3 * 1 5 + / *
A=14
6 2 / 3 - 4 2 * +
A=8
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <string.h>
#define SIZE 50 /* Size of Stack */
case '-':
push(op1 - op2);
break;
• case '*':
push(op1 * op2);
break;
case '/':
push(op1 / op2);
break;
case '%':
push(op1 % op2);
break;
case '^':
push(pow(op1, op2));
break;
default:
printf("Invalid operator\n");
}
}
void push(int elem)
{
s[++top] = elem;
}
int pop()
{
return s[top--];
}
THE QUEUE
• A queue is an ordered list in which all
insertions take place at one end and all
deletions take place at the opposite end.
Given a queue Q = (a0,a1……,an-1)
a0 is FRONT element
an-1 is REAR element
INSERTION –REAR END
DELETION- FRONT END
Types of Queues
1. Simple Queue
2. Circular Queue
3. Priority Queue
4. Dequeue (Double Ended Queue)
circular queue
• A circular queue exhibits similar characteristics
to that of a simple queue, with the additional
property of joining the front end to the rear
end.
• It is also known as the ring buffer.
Double Ended Queue
}
return queue[++ rear]
}
Dequeue (deletion from queue)
element deleteq(int {int front, int rear)
{
if (front == rear)
return queue_empty();
return queue[front++];
}
CUSTOMER SALES PERSON
1 ADAMS SMITH
2 BROWN RAY
3 CLARK JONES
4 DREW RAY
5 JOHN SMITH
6 RAMYA JONES
7 ANU RAY
8 SANDEEP SMITH
9 POOJA RAY
• Separate array for sales people and an entry
in the customer file which gives the location of
each customer’s salesperson
CUSTOMER POINTERS
1 ADAMS 3
2 BROWN 2 SALESPER
SON
3 CLARK 1 JONES 1
RAY 2
4 DREW 2
SMITH 3
5 JOHN 3
6 RAMYA 1
7 ANU 2
8 SANDEEP 3
9 POOJA 2
• Suppose the firm wants the list of customers
for a given sales person
CUSTOMER POINTER
1 ADAMS 3
2 BROWN 2 SALESPER
SON
3 CLARK 1 JONES 1
RAY 2
4 DREW 2
SMITH 3
5 JOHN 3
6 RAMYA 1
7 ANU 2
8 SANDEEP 3
9 POOJA 2
CUSTOMER POINTER
1 ADAMS 3
6 RAMYA 1
7 ANU 2
8 SANDEEP 3
9 POOJA 2
CUSTOMER POINTER
1 ADAMS 5
6 RAMYA 0
7 ANU 9
8 SANDEEP 0
9 POOJA 0