0% found this document useful (0 votes)
3 views

Data Structures

The document covers fundamental concepts of data structures, including definitions of data items, group items, and various types of data structures like linear arrays, stacks, and queues. It also explains operations such as traversing, inserting, deleting, searching, and sorting, along with algorithms for linear search and binary search. Additionally, it discusses linked lists, records, and their differences from linear arrays.

Uploaded by

Jash Mehta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Data Structures

The document covers fundamental concepts of data structures, including definitions of data items, group items, and various types of data structures like linear arrays, stacks, and queues. It also explains operations such as traversing, inserting, deleting, searching, and sorting, along with algorithms for linear search and binary search. Additionally, it discusses linked lists, records, and their differences from linear arrays.

Uploaded by

Jash Mehta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Swami Vivekanand International School & Jr.

College
SYJC
Computer Science I
Chapter 2: Data Structure

Q1. Explain following terms w.r.t. data structure—


i. Data
a. Data are simple value or set of values.
ii. Data item
a. It refers to single unit or values of data.
iii. Group item
a. Data items which are divided into sub items are called as group items.
b. E.g. – Date may be divided into three sub items – day, month and year
iv. Elementary item
a. Data items which are not divided into sub items are called as elementary items.
b. E.g. – pin code number cannot be divided into sub item.
v. Entity
a. An entity is something that has certain attributes or properties which may be
assigned values.
vi. Entity set
a. The collection of all entities form an entity set.
vii. Field
a. Filed is a single elementary unit of information representing an attribute of an
entity.
viii. Record
a. Record is a collection of filed values of a given entity.
ix. File
a. File is a collection of field values of a given entity.
b. The variable is called as pointer variable, if it points to another variable i.e. it
contains the memory address of other variable.
x. Primary key
a. A record in a file may contain several fields.
b. There are certain fields having unique value.
c. Such field is called primary key.
xi. Pointer Array
a. An array is called pointer array, if each element of that array is a pointer.
xii. Data structure
a. Data can be organized in different ways.
b. The logical or mathematical model of a particular organization is called as Data
Structure.
xiii. Binary Tree
a. Binary tree is a finite set of elements called nodes such that is :
i. It may be empty or
ii. It is partitioned into three disjoint subsets-
1. There is a single distinguished element called the root of tree.
2. Other two subsets are themselves binary tree called left subtree and
right subtree of the original tree.
3. A left and right subtree can be empty.

Q2. Write a note on Data Structure operation.


Ans-
1. Traversing
a. It means accessing each record or element only once so that it can be processed.
2. Inserting
a. Adding a new record to the existing structure is called as inserting.
3. Deleting
a. Removing a record from the existing structure is called as deleting.
4. Searching
a. Finding the location of a record with given key values or finding all records that
satisfy the condition is called as searching.
5. Sorting
a. Arranging records in some logical order is called as sorting.
6. Merging
a. Merging means combining the records in two different files into a single file.

Q3. What is linear Array?


Ans-
1. A data structure is said to be linear if its elements from a sequence.
2. A linear array is the data structure which consists of finite, ordered set of
homogeneous data elements such that-
a. The elements of the array are referenced respectively by an index set consisting
of ‘n’ consecutive numbers.
b. The elements of the array are stored respectively in successive memory
locations.
c. The number ‘n’ of the elements is called length or size of array.
d. The length or size of the array can be obtained from the index set by the
formula—

Length = UB – LB + 1

3. E.g.

247 3
500 2
399 1
495 0

Q4. Write an algorithm for traversing linear array.


Ans-

Algorithm: Traversing a linear array.


Here LA is a linear array with lower bound LB and upper Bound UB. Following algorithm
applies PROCESS to each element of LA.

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. [ Increment counter ]


Set K: = K + 1
[ End of Step 2 loop ]

Step 5. Exit

Q5. Write an algorithm for inserting an item into linear array.


Ans-

Algorithm: Inserting element into linear array.


INSERT (LA, N, K, ITEM)
Here LA is a linear array with N elements and K is a positive integer, such that
K<=N. This algorithm inserts an element ITEM at Kth position in LA.

Step 1: [Initialize counter]


Set J: =N
Step 2: Repeat steps 3 and 4 while J >= K :

Step 3: [Move Jth element downward]


Set LA [J + 1]: = LA[J]

Step 4: [Decrement counter]


Set J:= J – 1
[End of step 2 loop]

Step 5: [Insert the element]


Set LA[K] := ITEM

Step 6: [Reset N]
Set N := N+1

Step 7: Exit

Q6. Write an algorithm for deleting an item into linear array.


Ans-

Algorithm: Deleting element from linear array.


INSERT (LA, N, K, ITEM)
Here LA is a linear array with N elements and K is a positive integer, such that
K<=N. This algorithm deletes the Kth element from LA.

Step 1: Set ITEM = LA[K]

Step 2: Repeat for J=K to N-1


Move J+1st element upward.
Set LA[J]=LA[J+1]
[End of loop]

Step 3: Reset N
N=N-1

Step 4: Exit

Q7. Explain bubble sort algorithm.


Ans-

Algorithm: Bubble Sort.


Sort (DATA, N)
Here DATA is a array with N elements. This algorithm sorts the elements in
DATA.
Step 1: Repeat steps 2 and 3 for K=1 to N-1

Step 2: Set PTR=1

Step 3: Repeat while PTR <=N-K


a. If DATA [PTR]>DATA [PTR+1] then
Interchange DATA [PTR] and DATA [PTR+1]
[End of IF structure]
b. Set PTR=PTR+1
[End of inner loop]

Step 4: Exit

Q8. Explain linear search algorithm with example.


Ans-

Algorithm: Linear Search.


LINEAR (DATA, N, ITEM, LOC)
Here DATA is a linear array with N elements and ITEM is a given element.
This algorithm finds the location LOC of ITEM in DATA or sets LOC=0, if
search us unsuccessful.

Step 1: [Insert ITEM at the end of DATA]


Set DATA [N+1] = ITEM

Step 2: [Initialize counter]


Set LOC =1

Step 3: [Search for item]


Repeat while DATA [LOC] ≠ ITEM
Set LOC = LOC + 1
[End of loop]

Step 4: If LOC = N + 1 then


Set LOC = 0

Step 5: Exit

Example:
Given DATA array with following 5 elements
11 22 33 44 55
Suppose ITEM = 33

Step 1: set DATA [6] = 33, so the list becomes


11 22 33 44 55 33

Step 2: LOC = 1

Step 3: Since DATA [1] = 11 ≠ 33, So LOC=2


Since DATA [2] = 22 ≠33, So LOC=3
Here DATA [3] = 33 = 33 = ITEM

Step 4: Hence ITEM = 33 found at position, LOC = 3.

Q9. Explain binary search algorithm with example.


Ans-

Algorithm: Binary Search.


BINARY (DATA, LB, UB, ITEM, LOC)
Here DATA is a linear array with lower bound LB and upper bound UB. ITEM
is a given element. BEG denotes beginning, MID denotes middle and END
denotes end location of DATA. This algorithm finds the location LOC of ITEM
in DATA or sets LOC=NULL, if search us unsuccessful.

Step 1: [Initialize Variables]


Set BEG = LB, END = UB and MID = INT((BEG+END)/2)

Step 2: Repeat steps 3 and 4


While BEG = END and DATA [MID] ≠ ITEM

Step 3: If ITEM < DATA [MID], then:


Set END = MID – 1
Else
Set BEG = MID + 1
[End of If structure]

Step 4: Set MID = INT ((BEG + END) / 2)


[End of step 2 loop]

Step 5: If DATA [MID] = ITEM, then:


Set LOC = MID
Else
LOC = NULL
[End of If structure]

Step 6: Exit

Example:
Given DATA be the following sorted 13 element array:
11 22 30 33 40 44 55 60 66 77 80 88 99
Suppose ITEM = 40

Step 1: Initially BEG = 1 and END = 13


Hence MID = INT [(1+ 13)/2] = 7
And so DATA [MID] = DATA [7] = 55

Step 2: Since 40 < 55, END has its value changed by


END = MID – 1 = 7 – 1 = 6
Hence MID = INT [(1+6)/2] = 3
And so DATA [MID] = DATA [3] = 30

Step 3: Since 40 > 30, BEG has its value changed by


BEG = MID + 1 = 3 + 1 = 4
Hence MID = INT [(4+6)/2] = 5
And so DATA [MID] = DATA [5] = 40
Hence, Found ITEM in location LOC = MID = 5

Q10. Drawing Binary tree—


a. E=(a+b)/[(c*d)-e]

b. E=(2x+y)(a-7b)3
c. E=[(a+b)*c]/[a*((b-c)+a)]

d. E=(x+2y)3-(p+q)
Q11. Explain stacks and Queues.
Ans-
a. Stacks
1. LIFO system is last-in-first-out system.
2. In this system, the element which is inserted at last, will be deleted first.
3. Stack is an example of LIFO system.
4. It is a linear system in which insertion and deletion takes place only at one end
i.e. top of the list.
5. The insertion operation is referred to as push and deletion operation as pop.
6. Example – stack of books.
b. Queue
1. FIFO system is first-in-first-out system.
2. In this system, the element which is inserted first in the list will also be deleted
first.
3. Queue is an FIFO system.
4. A queue is a linear list, in which insertion takes place only at one end of the list
known as ‘rear’ of list and deletion takes place at the other end, called as ‘front’
of the list.
5. Example – A queue for tickets in cinema hall.

Q12. What is linked list? How it is presented in memory.


Ans-
1. A linked list is a linear collection of data elements, called nodes, where the linear
order is maintained with the help of pointers.
2. Linked list is also called as one-way list.
3. Each node in he linked list is divided into two parts.
4. First part is called as INFO which contains the information about the element or actual
element.
5. Second part is called as LINK which is next pointers field.
6. Example

a. The above figure shows linked list having six nodes.


b. The left part of the node is the INFO part, which contains information of the
element, while the right part is Link part, which is next pointers field.
c. An arrow is drawn from Link part of one node to the next node to indicate link.
d. The address of the list is stored is Start or Name of list.
e. The Link part of last node is NULL pointer i.e. it contains nothing.
f. To trace the linked list, we just require the address of Start or Name.
Q13. What is record? How it differs from linear array?
Ans-
1. A record is a collection of field or attributes i.e. relative data items.
2. Collection of data is frequently organized into hierarchy of fields i.e. records.
3. A file is nothing but collection of records.
4. Difference between records and linear arrays—
a. A record is a collection of fields, while an array is list of homogeneous data
elements.
b. A record may contain non-homogeneous data. An array always contains
homogeneous data.
c. In record, natural ordering of elements is not possible. Array elements can be
naturally ordered.
d. Elements of record are referenced by level number, while those of array can be
referenced by an index set consisting of n consecutive numbers.

You might also like