Data Structure Using C
Data Structure Using C
BASIC TERMINOLOGIES
DATA : Data are simply values or set of values.
INFORMATION : Meaningful or processed data is called information.
DATA ITEM : Data Item refers to a single unit of values.
GROUP ITEMS : Data items that are divided into subitems are called
group items. eg,.name may be divided into three subitems, first,
middle and last name.
ENTITY: An entity is something that has certain properties or attributes
which may be assigned values. eg, employee is an entity. The
attributes and values of an entity are:
Attributes: Name Age Address
Values : RAM 28 XYZ street
ENTITY SET : Entities with similar attributes form an entity set.
DEFINITION OF DATA STRUCTURES
Data may be organized in many different ways.
The logical or mathematical model of a particular organization
of data is called data structure.
TREES GRAPHS
LINEAR DATA STRUCTURES: -A data structure whose elements
form a sequence, and every element in the structure has a unique
predecessor and unique successor. Eg. Arrays,linked lists, stacks
and queues.
NON-LINEAR DATA STRUCTURES: A data structure whose
elements do not form a sequence, and there is no unique
predecessor or unique successor. Eg. Trees and graphs.
G(n) Log N N N2 N3 2n
n n log n
5 3 5 15 25 125 32
10 4 10 40 100 103 103
100 7 100 700 104 106 1030
1000 10 103 104 106 109 10300
ARRAYS
• An array is a way to reference a series of memory locations
using the same name.
• Each memory location is represented by an array element.
• An array element is similar to one variable except it is identified by
an index value instead of a name. An index value is a number used
to identify an array element.
An array is declared as: type name[size]
The ‘type’ indicates the name of the array.
The ‘name’ indicates the name of the array.
The ‘size’ indicates the number of elements of ’type’ that ‘name’
contains.
• E.g int V[100];
• Which defines an array named V to hold 100 values of the primitives
type int.
• The variable v decays to the reference integral type in most
expression contexts; the value obtained from that decay points to
the memory address of the first element.
PV+1
PV+2
V:
• Loc(AAA[k]=base(AAA)+w(k-LB)
Loc(AAA[15])=300+4(15-5)=340
Loc(AAA[35])=300+4(35-5)=420
AAA[55] is not an element of AAA, since 5 exceed UB=50
Traversing Linear Array
• Here LA is a linear array with lower bound LB and upper
bound UB.This algorithm applies an operation process to
each element of LA
Algo1
step 1: [Initialize counter] set k:=LB
step 2: Repeat step 3 and 4 while k<=UB
step 3: [Visit element] Apply process to LA[k]
step 4: [Increase counter] Set k=k+1
[End of step 2 loop]
step 5: Exit
• Algo 2:
step 1: Repeat for k=LB to UB
Apply PROCESS to LA[k]
[End of loop]
step 2: Exit
Insertion into a linear array
• Insert(LA,N,K,ITEM)
Here LA is a linear array with N element and k is a positive
integer such that k<=N. This algorithm inserts an element ITEM
into Kth position in LA
step1: [Initialize counter] set J:=N
step2: Repeat step 3 and 4 while J>=k
step3:[Move Jth element downward] set LA[J+1]:=LA[J]
step4:[Decrease counter] set J=J-1
[End of step 2 loop]
step5:[Insert element] set LA[K]=ITEM
step6:[Reset N] set N=N+1
step7: Exit
Deletion from a Linear Array
• Delete(LA,N,K,ITEM)
Here LA is a Linear array with N element and K is a positive
integer such that K<=N.This algorithm deletes the Kth element
from LA
step1: set ITEM:=LA[K]
step2: Repeat for J=K to N-1
step3:[Move (J+1)th element upward] set LA[J]:=LA[J+1]
[End of loop]
step4:[Reset the number N of element in LA] set N:=N-1
step5: Exit
Searching:-Linear Search
• Linear(Data,N,Item,Loc)
Average case
f(n)= 1.p1 +2.p2+……….+n.pn+(n+1).q
In particular, suppose q is very small and Item appears with equal
probability in each element os Data. Then q=0 and each pi
=1/n.Accordingly,
f(n)=1.1/n+2.1/n+……..+n.1/n+(n+1).0
=(1+2+…….+n).1/n
=n(n+1).1/n= (n+1)/2
Binary Search
• Binary(Data,LB,UB,Item,Loc)
Here Data is a sorted array with lower bound LB and upper
bound UB and Item is a given item of information.The variable
MID denotes, middle location of a segment of element of
Data.This algorithm finds the location Loc of item in Data or set
Loc=Null
step1:[Initialize segment variable] set MID=INT((LB+UB)/2)
step2: Repeat step3 and step 4 while LB<=UB && Data[Mid]!=Item
step3: If Item<Data[MID], then:
set UB:=MID-1
else:
set LB:=MID+1
step4: set MID:= INT((LB+UB)/2)
[End of step 2 loop]
step5: If Data[MID]=Item,,then:
set Loc:=MID
else:
set Loc:=NULL
[End of If structure]
step6: Exit
• Example
Let Data be the following sorted 13 element array.
DATA: 11,22,30,33,40,44,55,60,66,77,80,88,99.Suppose Item=40,
find out the location by using Binary Search.
3 2 2
3 3 3
3 3
12 2 2 2
2 12 12 12 12
12
15 15 15
15 15 15
40 40 40 40 40 40
• Bubble(DATA,N))
Here DATA is an array with N elements.This algorithm sorts the
elements in DATA.
Sort
Pass 1 20 35 40 100 3 10 15
Loc=4 Small=4
Pass 5
3 10 15 20 100 35 40 Loc=5 Small=5
3 10 15 20 35 40 100
• MIN(A,K,N,LOC)
This algorithm finds the location of the smallest element among
A[K],A[k+1]…………….A[N]
1. Set MIN:=A[K] and LOC:=K
2. Repeat for J=K+1 to N:
If MIN > A[J],then: Set MIN:=A[J] and LOC:=J
[End of loop]
3. Return
a[1]<a[0]
Pass 1 35 20 40 100 3 10 15
a[2]>a[1]
Pass 2 20 35 40 10 3 10 15
20 35 40 100 3 10 15 a[3]>a[2]
Pass 3
20 35 40 100 3 10 15 a[4]<a[3]<a[2]<
Pass 4
a[1]<a[0]
Pass 5
3 20 35 40 100 10 15 a[5]<a[4]<a[3]<
a[2]<a[1]
Pass 6 3 10 15 20 35 40 100 a[6]<a[5]<a[4]<
a[3]<a[2]
3 10 15 20 35 40 100
Insertion(A,N)
This algorithm sorts the array A with N elements.
1. Repeat steps 2 to 4 for K=2 to N
2. Set TEMP:=A[K] and PTR:=K-1
3. Repeat while TEMP < A[PTR]:
(a) Set A[PTR+1]:=A[PTR]
(b) Set PTR:=PTR-1
[End of loop]
4. Set A[PTR+1]:=TEMP
[End of step 1 loop]
5. Exit.
fun(int a[],int n)
{ int i;
printf("enter the values");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
printf("%d",a[i]);
}
• Multidimensional Array:-
• A multidimensional array consists of two or more arrays defined by
set of array elements.
• Each set of array elements is an array.
• The first set of array elements is considered the primary array, and
the second and subsequent sets of array elements are considered
sub-arrays.
0 0
0 1 Sub array
Primary set
1 0
1 1
Array elements
• There are two arrays in the multidimensional array in the fig.
Linear arrays are used to represent all sorts of lists.They are used
to implement other data structures such as stacks,queues,heaps
etc.
0 0 1 2
0 0 0 5
1 0 2 0
2 0 3 0
• A common way of representing non-zero elements of a sparse matrix is the
3-type form.
• In this form each non-zero element is stored in a row, with first and second
element of this row containing the row and column in which the element is
present in the original matrix.
-3 7
8 9 2
• When the non-zero elements are present only on or above main
diagonal, the matrix is called upper triangular sparse matrix.
( A (i,j)=0 for i>j)
2 -4 0
6 2
-5 7 -3
3
4 4 -8 5
5 2 9
Sequential Representation of sparse matrix:-
3 0 0 2 8 6 4 1 8
7 4 2 9
8 4 3 4
9 4 4 7
11 5 1 3
12 5 4 2
13 5 5 8
pointers
Pointer as a
• Declaring and Initializing Pointers:-
variable
The syntax for declaring pointer variable is
data-type * ptrName;
int *int_ptr;
This statement declares a pointer variable int_ptr that can point to an integer type
variable.
• Initialization:-
{ int *ptr , x=10 , y ; y = * ptr
ptr = &x; y = * &x
y = *ptr; } y = *(&x)
* ptr
5002 10
H e l l o \0
strlen ( ):
Syntax : len = strlen ( ptr );
Int len ;
Char str[ ] = “ hello “;
len = strlen( str );
• Copying a string:-
void strCopy( char *dest, char *source) {
char *ptr1, *ptr2;
ptr1 = source;
Syntax : strcpy ( ptr2, ptr1 );
ptr2 = dest;
while(*ptr1) {
*ptr2 = *ptr1;
ptr1++;
ptr2++; }
*ptr2 = ‘ \ 0 ’ }
• Appending a string to another string:-
char *strConcatenate( char *dest, char *source) {
char *ptr1 , *ptr2;
ptr1 = source;
ptr2 = dest;
while(*ptr2++) ;
ptr2--; Syntax : strcat ( ptr2 , ptr1 ) ;
while(*ptr1) {
*ptr2 = *ptr1;
ptr1++;
ptr2++; }
*ptr2 = ‘ \0 ‘;
return dest;
}
• Comparing two Strings:-
• Void pop( )
• { int x;
• If (st.top == 0)
• { printf(“ underflow \n”);
• return; }
• x = st.s[st.top];
• st.top = st.top -1;
• printf(“%d” x);
• }
Applications of stack
1. P*Q^R+S
2. A && B || C || ! (E>F)
3. [A + (B – C)] * [(D – E) / (F – G + H)]
4. A+B*C–D/F
5. (A + B) / D ^ ( ( E – F) + G)
6. A + (B * C – (D / E – F * G) * H)
7. 5 * (6 + 2) – 12 / 4
• Transforming Infix Expressions into Postfix Expressions:-
Polish( Q, P) Suppose Q is an arithmeric expression written in infix
notation. This algorithm finds the equivalent postfix expression P.
1. Push “( “ onto stack, and add “ )“ to the end of Q.
2. Scan Q from left to right step3 to step6 for each element of Q unit
• ABCDE+*+-
• AB+CD*+E*
Evaluation of a post expression
• Algorithm:- This algoithm finds the value of an arithmetic
expression P written in postfix notation.
1. Add a right parenthesis “ )” at the end of P.
2. Scan P from left to right and repeat step 3 and 4 for each
element of P until “ )” is encountered.
3. If an operand is encountered, put it on stack.
4. If an operator X is encountered, then
a) Remove the top two elements of stack, where A is top
element of stack and B is the next to top element.
b) Evaluate B X A
c) Place the result of back on stack.
[End of if structure]
[End of step2 loop]
5. Set value equal to the top element on stack
6. Exit.
Recursion
3. Fibonacci Numbers
A very important sequence, Fibonacci sequence, usually denoted
by F0,F1,,,,,,,Fn, is as follows:
0,1,1,2,3,5,8,13,…..
A formal definition for Fibonacci number Fn is
n if n<=1
fib(n) =
fib(n-1) + fib(n-2) if n>1
Fib(5)
Fib(4) Fib(3)
Fib(1) Fib(0)
reverse(s,i+1,j-1);
}
}
Write a recursive function for the following
recursive definition:
A B C A B C
• Case 2: Given two disks
A B C A B C
(Start) (i)
A B C A B C
(ii) (finish)
• Case 3: Given three disks
• Move disk 1 from pin A to pin C
• Move disk 2 from pin A to pin B
• Move disk 1 from pin C to pin B
• Move disk 3 from pin A to pin C
• Move disk 1 from pin B to pin A
• Move disk 2 from pin B to pin C
• Finally move disk 1 from pin A to pin C
A B C A B C
A B C A B C
A B C A B C
Algorithm for towers of hanoi problem
Tower(N,BEG,END,AUX)
1. If N = 1,then:
(a) Write:BEG->END
(b) Return
[End of If structure]
2.[Move N-1 disks from peg BEG to peg AUX]
Call Tower(N-1,BEG,AUX,END)
3.Write:BEG->END
4. [Move N-1 disks from peg AUX to peg END]
Call Tower(N-1,AUX,END,BEG)
5. Return
Tail Recursion
• A recursive function is said to be tail recursive if there are no
pending operations to be performed on return from a recursive
call. Tail recursive functions are often said to "return the value of the
last recursive call as the value of the function." Tail recursion is very
desirable because the amount of information which must be stored
during the computation is independent of the number of recursive
calls. Some modern computing systems will actually compute tail-
recursive functions using an iterative process.
• The factorial function “fact” is usually written in a non-tail-recursive
manner:
int fact (int n) { /* n >= 0 */
if (n == 0)
return 1;
return n * fact(n - 1);
}
Notice that there is a "pending operation," namely multiplication, to
be performed on return from each recursive call. Whenever there is
a pending operation, the function is non-tail-recursive.
Information about each pending operation must be stored, so
the amount of information is dependent of the number of calls.
Converting Recursive Functions to be
Tail Recursive
• A non-tail recursive function can often be converted
to a tail-recursive function by means of an
"auxiliary" parameter. This parameter is used to
form the result.
• The idea is to attempt to incorporate the pending
operation into the auxiliary parameter in such a way that
the recursive call no longer has a pending operation. The
technique is usually used in conjunction with an
"auxiliary" function. This is simply to keep the syntax
clean and to hide the fact that auxiliary parameters
are needed.
• The factorial function can be written in a tail-recursive way:
int fact_aux(int n, int result)
{
if (n == 1) return result;
return fact_aux(n - 1, n * result)
}
int fact(n)
{
return fact_aux(n, 1);
}
The "auxiliary" function fact_aux is used to keep the syntax of fact(n)
the same as before. The recursive function is really fact_aux, not
fact. Note that fact_aux has no pending operations on return from
recursive calls. The value computed by the recursive call is simply
returned with no modification. The amount of information which must
be stored is constant (the value of n and the value of result),
independent of the number of recursive calls