Bca Unit 2 Data Structures
Bca Unit 2 Data Structures
Linear data structure is a Data Structure in which the data items or elements are
organised Linearly or Sequentially.
It is a list which shows the relationship of adjacency between the elements.
All the data items(elements) in a linear data structure can be traversed in single
run.
It’s implementation is easy.
It includes arrays, linked list, stack and queue data structures.
2. Linked list: Linked list is a collection of data elements. It consists of two parts:
data and Link.
3. Stack: Stack is a linear list of elements in which the element is inserted or deleted
at one end called Top. It is follows Last-in-First-out (LIFO) organisation.
2
4. Queue: Queue is a linear list of elements in which the elements are added at one
end called rear and deleted from another end called front. It follows first-in-First-
out (FIFO) organisation.
It is a data structure where the data items or elements are not organized
sequentially is called non-linear data structure.
The data elements of the non-linear data structure can be connected to more than
one element.
In Non-linear data structure the relationship of adjacency is not maintained
between the data elements.
All the data elements in non-linear data structure cannot be traversed in single
run.
It’s implementation is complex.
Examples of non-linear data structures are: Trees, Graphs
Tree:
A tree is collection of nodes where these nodes are arranged hierarchically and form a
parent child relationship.
3
Graph:
4
There are different types of arrays. They are
Datatype – specifies the type of array, it can be int, float, double , char etc
Arrayname is the userdefined name and size indicates no of elements in an array.
Example: int a[5];
5
Accessing elements of the Array:
The individual elements in an array are accessed by using array name followed by
index no.
Eg: a[3]=44;
Arrayname itself indicates the starting address or base address. If we know the base
address, we can calculate the address of any element using the following formula.
6
Operations of One-Dimensional Arrays:
Datatype – specifies the type of array, it can be int, float, double , char etc
Arrayname is the userdefined name
rowsize indicates no of rows and colsize indicates the no of cols.
7
Accessing elements of the Array:
8
To determine element address A[i,j]:
N – No of cols
Eg:
A[2,1]=2000+2* (3*2+1)=2000+14=2014
4. Transpose of a matrix
9
Declaration :
INSERTION:
Insertion operation refers to adding elements into array. We can add the elements from
beginning or last or at middle position.
1. Let I=N
2. IF I>= POS THEN
a) A[I+1]=A[I]
b) I=I-1
c) Goto step2
End IF
3. A[POS]=ITEM
4. SET N=N+1
10
DELETION:
Deletion operation refers to removing elements from array. We can delete the
elements at beginning, last or at middle.
1. Let ITEM=A[POS]
2. FOR I=POS TO N-1 REPEAT STEP 3
3. A[I]=A[I+1]
4. SET N=N-1
TRAVERSE:
1) Let K = LB
2) Repeat , if K<=UB
a. If A[K]=KEY THEN
i. Print “item found at “ K position
End IF
b) K++
c) Goto step 2
End
11
SORTING:
start
end
Record is a composite data type that groups together multiple related fields or
elements, each representing a specific attribute of a single entity.
Each field in a record has a unique name and usually a specific data type, such as
integer, string, or date.
Characteristics of a Record:
2. Multiple Fields: Each record consists of multiple fields, with each field
representing an attribute of the entity.
12
3. Fixed Structure: Records generally have a fixed number of fields, defined by the
structure.
5. Access by Field Name: Each field can be accessed independently using its name.
Example of a Record:
Example :
struct Employee {
int EmployeeID;
char Name[50];
char Position[30];
float Salary;
};
13
Q) Explain about Pointers in detail (OR) Overview of pointers
Uses:
14
This operator also called as “indirection operator” and is used as a prefix to
pointer variable.
Eg: int *p; float *b;
Working with pointers:
A pointer variable which stores the address of another pointer variable is known
as pointer to pointer.
int a=2;
int *b;
int **c;
b=&a;
c=&b;
Various arithmetic operations can be performed on pointers like addition,
subtraction, preincrement, postincrement, predecrement, postdecrement.
Eg:
b=b+1=1000 + 1 * sizeof(int)=1000+2=1002
b=b-3 = 1000 – 3 *sizeof(int)=1000-3*2=1000 – 6=994
15
swap(&a,&b);
printf(“%d%d”,a,b);
}
Eg: main()
{
int (*a)[2], i,j;
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
scanf(“%d”,*(a+i)+j);
}
}
17
Q) Difference between Array and Pointer: (*********5m**********)
ARRAY POINTER
1)Array is a group of consecutive 1) pointer is a variable that contains
memory locations that stores address of another variable
homogenous type of data
2)Array names cannot be used as lvalue 2)Pointers can be used as values
Eg: int a[]={1,2,3} Eg: int a=5;
int b; int b;
b=a; //error p1=&a;
b=p1;
3)Array can’t be assigned to another 3)Pointers can be assigned to another
array pointer
Eg: if a,b are arrays a =b produces error Eg: p2=p1
4)Array name gives the address of array 4)Pointer name doesn’t specify any
address
5)sizeof operator with array gives the 5)sizeof operator with pointer returns
size of array the no of bytes used for pointer
variable
18
Representing a sparse matrix
1. Array representation
2. Linked list representation
Using Arrays:
2D array is used to represent a sparse matrix in which there are three rows named as
In linked list, each node has four fields. These four fields are defined as:
19
Row: Index of row, where non-zero element is located
20