0% found this document useful (0 votes)
53 views30 pages

Data Structures

The document discusses different types of data structures including linear and non-linear data structures. Linear data structures like arrays, stacks and queues arrange data sequentially while non-linear structures like trees and graphs arrange data in a non-sequential manner. The document then focuses on arrays, describing them as fixed size collections of elements of the same type accessed via indexes. It covers array memory layout, common array operations like traversal, insertion, deletion and searching, and algorithms to implement these operations.

Uploaded by

Divyansh Gautam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views30 pages

Data Structures

The document discusses different types of data structures including linear and non-linear data structures. Linear data structures like arrays, stacks and queues arrange data sequentially while non-linear structures like trees and graphs arrange data in a non-sequential manner. The document then focuses on arrays, describing them as fixed size collections of elements of the same type accessed via indexes. It covers array memory layout, common array operations like traversal, insertion, deletion and searching, and algorithms to implement these operations.

Uploaded by

Divyansh Gautam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Data Structures

• Data structure is representation of the logical


relationship existing between individual elements of
data.
• A data structure is a way of organizing all data items
that considers not only the elements stored but also
their relationship to each other.

Some of the more commonly used data structures


include lists, arrays, stacks, queues, heaps, trees, and
graphs
Classification of Data Structure

Data structure

Primitive DS Non-Primitive DS

Integer Float Character Pointer


Classification of Data Structure

Non-Primitive DS

Linear Non-Linear

Array Queue Graph Trees

Link List Stack


Data Structures
• LINEAR DATA STRUCTURE
• Data structure where data elements are
arranged sequentially or linearly, where the
elements are attached to its previous and next
adjacent is called a linear data structure.
• In linear data structure, single level is involved.
Therefore, we can traverse all the elements in
single run only. Linear data structures are easy
to implement because computer memory is
arranged in a linear way. Its examples
are array, stack, queue, linked list, etc.
Data Structures
• NON-LINEAR DATA STRUCTURES
• Data structures where data elements are not
arranged sequentially or linearly are called non-
linear data structures. In a non-linear data
structure, single level is not involved. Therefore, we
can’t traverse all the elements in single run only.
Non-linear data structures are not easy to
implement in comparison to linear data structure.
It utilizes computer memory efficiently in
comparison to a linear data structure. Its examples
are trees and graphs.
Data Structures
• The way in which the data is organized affects
the performance of a program for different
tasks.
• Computer programmers decide which data
structures to use based on the nature of the
data and the processes that need to be
performed on that data
Arrays
An array is a sequenced collection of elements,
normally of the same data type. We can refer to
the elements in the array as the first element, the
second element and so forth, until last element.
Arrays
• Array is a container which can hold a fix number of items and these items should be
of the same type. Most of the data structures make use of arrays to implement their
algorithms. Following are the important terms to understand the concept of Array.
• In an array we have two types of identifiers
• Element − Each item stored in an array is called an element.
• Index − Each location of an element in an array has a numerical index, which is used
to identify the element.
• Array Representation
• Arrays can be declared in various ways in different languages.

• Eg. Scores[10]
• the name of the array is scores and name of each element is the name of the array
followed by the index, for example, scores[1], scores[2], and so on. In this chapter,
we mostly need the names of the elements, but in some languages, such as C, we also
need to use the name of the array.
Memory layout
• The indexes in a one-dimensional array directly define the
relative positions of the element in actual memory. Figure
shows a two-dimensional array and how it is stored in
memory using row-major or column-major storage. Row-
major storage is more common.
Memory layout
 The number of elements that can be stored in an array, that is the
size of array or its length is given by the following equation:

(Upperbound-lowerbound)+1

• Loc (X[i]) = Loc(X[LB]) + w(i-1)


• where w is length of memory location required.
• A[10]
• A[i]=base address + w(i-1)
• BA=address of X[lb]
• W=word size
Memory layout
• The arrays discussed so far are known as one-dimensional arrays
because the data is organized linearly in only one direction. Many
applications require that data be stored in more than one dimension.
Figure shows a table, which is commonly called a two-dimensional
array.
100 103 106

Memory105layout
101
102
104 107

• Row major System:

• Address of A[i][j]=B+W*[N*(i-Lr)+(j-Lc)]
• Column Major System :

• Address of A[i][j] =B+W*[(i-Lr)+M*(j-Lc)]


• B= base address
i= row Subscript of element whose address to be found
W= Storage size of an element
Lr= lower limit of RoW / start row index of matrix, if not given then assume zero
(0)
Lc= lower limit of Column / start column index of matrix, if not given then
assume 0(Zero)
M= number of rows of the given matrix
N= number of column of the given matrix
• M=3,N=3,LR=1,LC=1,W=1

• A[2][3]=100+1(3(2-1)+(3-1))=100+5=105

• A[2[[3]=100+(2-1)+3(3-1)=107
Memory layout
• We have stored the two-dimensional array students in
memory. The array is 100 × 4 (100 rows and 4 columns).
Show the address of the element students[5][3] assuming
that the element student[1][1] is stored in the memory
location with address 1000 and each element occupies only
one memory location. The computer uses row-major
storage.

• If the first element occupies the location 1000, the target


element occupies the location 1018.
Operations on arrays
• Basic Operations
• Following are the basic operations supported by an array.
• Traverse − print all the array elements one by one.
• Insertion − Adds an element at the given index.
• Deletion − Deletes an element at the given index.
• Search − Searches an element using the given index or by the
value.
• Update − Updates an element at the given index.

Although searching, retrieval and traversal of an array is an easy job,


insertion and deletion is time consuming. The elements need to be
shifted down before insertion and shifted up after deletion.
Traversing Algorithm
• Traversing operation means visit every element once.e.g.
to print, etc.
• Algorithm:
• 1.K=LB
• 2. Repeat step 2.1 and 2.2 while K <= UB
2.1 do PROCESS on A[K]
2.2 K=K+1
• 3. end
• 4. exit
Conditions

• Overflow

• Underflow
Insertion at the end
Algorithm:
Insend (A is the array, LB is the lower bound ,Ub
is the upper bound, n is the no of elements
present, item is the Value we want to insert )
1 . If n=UB write overflow and exit.
2. n=n+1
3. A[n]=item
4. exit
Insertion at the beginning
Algorithm:
Insbeg(A,LB,UB,n,item)
1 . If n=UB write overflow and exit.
2. For k=n to 1 (-1)
a) A[k+1]=A[k]
(End of for)
3. A[1]=item
4. n=n+1 item 10 20 30

5. exit
example
Deletion from the end
10 20 10
Algorithm:
Delend(A,LB,UB,n,item)
1 . If n=0 write underflow and exit.
2. Item=A[n]
3. n=n-1
4. exit
Deletion from the beginning
20 30 40
Algorithm:
Delbeg (A,LB,UB,n,item)
1 . If n=0 write underflow and exit.
2. Item=A[1]
3. For j=1to n-1 do (+1)
a) A[J] = A[J + 1]
b) J = J+1
6. N = N-1
7. Exit
• Start
• 2. Set J = 0
• 3. Repeat steps 4 and 5 while J < N
• 4. IF LA[J] is equal ITEM THEN GOTO STEP 6
• 5. Set J = J +1
• 6. PRINT J, ITEM
• 7. Stop
Insertion at Specific Location
Algorithm INSSPEC(A,K,N,UB,ITEM)
A is the array , K is the location, N is the no of elements already present.

1 . If n=UB write overflow and exit.


2. If k>n+1 then
a) Write not enough elements so insert at the end of array
b) A[n+1]=item
c) n-=n+1
d) return
3. For i=n to k (-1)
a) A[i+1]=A[i]
(End of for)
4. A[k]=item
5. n=n+1
6. exit
1 12 11 1 1
Deletion from Specific Location
Algorithm DELSPEC(A,K,N,UB,ITEM)
A is the array , K is the location, N is the no of elements
already present.

1 . If n=0 write underflow and exit.


2. If k>n then Write not enough elements to delete and exit
3. Item=A[k]
4. For i=k+1 to n(+1)
a) A[i-1]=A[i]
(End of for)
5. n=n-1
6. exit
Arrays:
int a[10], n, ub=9,lb=0;
Main()
{
Int choice;
n=-1;
Printf(“enter 1 for insertion at beginning”);
Printf(“enter 2 for insertion at end”);
Printf(“enter 3 for insertion at spec location”);
Printf(“enter 4 for deletion from beginning”);
Printf(“enter 5 for deletion from end”);
Printf(“enter 6 for insertion from spec loc”);
Printf(“enter 7 for display”);
Do{
Printf(“enter your choice”);
Scanf(“%d”,&choice);
Switch(choice)
Case 1: insbeg();
Break;
Case 2: insend();
Break;
Case 3: insspec();
Break;
Case 4: delbeg();
Break;
Case 5; delend();
Break;
Case 6: delspec();
Break;
Case 7: display();
Break;
}
void insend()
{
If(n==ub)
{
printf(“overflow”);
return;
}
scanf(“%d”, &a[n+1]);
n++;
return;
}
void delend()
{
If(n==-1)
[
Printf(“underflow”);
Return;
}
Printf(“element deleted is %d”, a[n]);
N--;
}
Void display()
{
For(i=lb;i<=n; i++)
{
Printf(“%d”,a[i]);
}
Return;
}

You might also like